12ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner 22ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautnerpackage com.google.android.example.locktasktests; 32ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner 42ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautnerimport android.app.Activity; 52ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautnerimport android.app.ActivityManager; 62ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautnerimport android.content.Context; 72ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautnerimport android.content.Intent; 82ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautnerimport android.os.Bundle; 961e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautnerimport android.os.Handler; 1061e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautnerimport android.os.Looper; 112ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautnerimport android.view.View; 122ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner 132ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautnerpublic class MainActivity extends Activity { 142ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner 152ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner private final static String TAG = "LockTaskTests"; 1661e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner Runnable mBackgroundPolling; 1761e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner boolean mRunning; 1861e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner Handler mHandler; 192ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner 202ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner @Override 212ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner protected void onCreate(Bundle savedInstanceState) { 222ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner super.onCreate(savedInstanceState); 232ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner setContentView(R.layout.activity_main); 2461e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner mBackgroundPolling = new Runnable() { 2561e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner // Poll lock task state and set background pink if locked, otherwise white. 2661e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner @Override 2761e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner public void run() { 2861e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner if (!mRunning) { 2961e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner return; 3061e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner } 3161e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner ActivityManager activityManager = 3261e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 3361e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner final int color = activityManager.getLockTaskModeState() != 3461e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner ActivityManager.LOCK_TASK_MODE_NONE ? 0xFFFFC0C0 : 0xFFFFFFFF; 3561e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner findViewById(R.id.root_launch).setBackgroundColor(color); 3661e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner mHandler.postDelayed(this, 500); 3761e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner } 3861e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner }; 3961e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner mHandler = new Handler(Looper.getMainLooper()); 402ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner } 412ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner 422ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner @Override 432ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner public void onResume() { 442ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner super.onResume(); 4561e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner mRunning = true; 4661e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner mBackgroundPolling.run(); 4761e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner } 4861e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner 4961e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner @Override 5061e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner public void onPause() { 5161e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner super.onPause(); 5261e1e09a2b6471defa1032a44a20090618c21d3fCraig Mautner mRunning = false; 532ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner } 542ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner 552ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner public void onButtonPressed(View v) { 562ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner Class activity = null; 572ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner switch (v.getId()) { 582ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner case R.id.button_default: 592ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner activity = LockDefaultActivity.class; 602ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner break; 612ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner case R.id.button_never: 622ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner activity = LockTaskNeverActivity.class; 632ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner break; 642ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner case R.id.button_whitelist: 652ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner activity = LockWhitelistedActivity.class; 662ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner break; 672ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner case R.id.button_always: 682ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner activity = LockAtLaunchActivity.class; 692ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner break; 702ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner } 712ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner Intent intent = new Intent(this, activity); 722ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 732ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner startActivity(intent); 742ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner } 752ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner 76c21ae9ed221912b61306e075d3f22726ac04013cCraig Mautner public void onToast(View v) { 77c21ae9ed221912b61306e075d3f22726ac04013cCraig Mautner showLockTaskEscapeMessage(); 782ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner } 792ad0bb46a42476f91b63baa95f0bcf97a337a4fcCraig Mautner} 80