1 
2 package com.google.android.example.locktasktests;
3 
4 import android.app.Activity;
5 import android.app.ActivityManager;
6 import android.content.Context;
7 import android.content.Intent;
8 import android.os.Bundle;
9 import android.os.Handler;
10 import android.os.Looper;
11 import android.view.View;
12 
13 public class MainActivity extends Activity {
14 
15     private final static String TAG = "LockTaskTests";
16     Runnable mBackgroundPolling;
17     boolean mRunning;
18     Handler mHandler;
19 
20     @Override
onCreate(Bundle savedInstanceState)21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24         mBackgroundPolling = new Runnable() {
25             // Poll lock task state and set background pink if locked, otherwise white.
26             @Override
27             public void run() {
28                 if (!mRunning) {
29                     return;
30                 }
31                 ActivityManager activityManager =
32                         (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
33                 final int color = activityManager.getLockTaskModeState() !=
34                         ActivityManager.LOCK_TASK_MODE_NONE ? 0xFFFFC0C0 : 0xFFFFFFFF;
35                 findViewById(R.id.root_launch).setBackgroundColor(color);
36                 mHandler.postDelayed(this, 500);
37             }
38         };
39         mHandler = new Handler(Looper.getMainLooper());
40     }
41 
42     @Override
onResume()43     public void onResume() {
44         super.onResume();
45         mRunning = true;
46         mBackgroundPolling.run();
47     }
48 
49     @Override
onPause()50     public void onPause() {
51         super.onPause();
52         mRunning = false;
53     }
54 
onButtonPressed(View v)55     public void onButtonPressed(View v) {
56         Class activity = null;
57         switch (v.getId()) {
58             case R.id.button_default:
59                 activity = LockDefaultActivity.class;
60                 break;
61             case R.id.button_never:
62                 activity = LockTaskNeverActivity.class;
63                 break;
64             case R.id.button_whitelist:
65                 activity = LockWhitelistedActivity.class;
66                 break;
67             case R.id.button_always:
68                 activity = LockAtLaunchActivity.class;
69                 break;
70         }
71         Intent intent = new Intent(this, activity);
72         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
73         startActivity(intent);
74     }
75 
onToast(View v)76     public void onToast(View v) {
77         showLockTaskEscapeMessage();
78     }
79 }
80