162515beee67307d8859beec521b7baedfa54b2b5Jason Monk/* 262515beee67307d8859beec521b7baedfa54b2b5Jason Monk * Copyright (C) 2013 The Android Open Source Project 362515beee67307d8859beec521b7baedfa54b2b5Jason Monk * 462515beee67307d8859beec521b7baedfa54b2b5Jason Monk * Licensed under the Apache License, Version 2.0 (the "License"); 562515beee67307d8859beec521b7baedfa54b2b5Jason Monk * you may not use this file except in compliance with the License. 662515beee67307d8859beec521b7baedfa54b2b5Jason Monk * You may obtain a copy of the License at 762515beee67307d8859beec521b7baedfa54b2b5Jason Monk * 862515beee67307d8859beec521b7baedfa54b2b5Jason Monk * http://www.apache.org/licenses/LICENSE-2.0 962515beee67307d8859beec521b7baedfa54b2b5Jason Monk * 1062515beee67307d8859beec521b7baedfa54b2b5Jason Monk * Unless required by applicable law or agreed to in writing, software 1162515beee67307d8859beec521b7baedfa54b2b5Jason Monk * distributed under the License is distributed on an "AS IS" BASIS, 1262515beee67307d8859beec521b7baedfa54b2b5Jason Monk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1362515beee67307d8859beec521b7baedfa54b2b5Jason Monk * See the License for the specific language governing permissions and 1462515beee67307d8859beec521b7baedfa54b2b5Jason Monk * limitations under the License. 1562515beee67307d8859beec521b7baedfa54b2b5Jason Monk */ 1662515beee67307d8859beec521b7baedfa54b2b5Jason Monk 1762515beee67307d8859beec521b7baedfa54b2b5Jason Monkpackage com.android.server.am; 1862515beee67307d8859beec521b7baedfa54b2b5Jason Monk 1962515beee67307d8859beec521b7baedfa54b2b5Jason Monkimport android.content.Context; 2062515beee67307d8859beec521b7baedfa54b2b5Jason Monkimport android.os.Handler; 2162515beee67307d8859beec521b7baedfa54b2b5Jason Monkimport android.os.Message; 22a585268aff94b620b2c22e9ba3ae1163bb5dc8a8Jason Monkimport android.view.WindowManager; 23815e057b9bb19acd77bf01ecb690b6e9fa85902eJason Monkimport android.view.accessibility.AccessibilityManager; 24a8f569c10a423ced5be7e019c3df2bca11b052f5Jason Monkimport android.widget.Toast; 2562515beee67307d8859beec521b7baedfa54b2b5Jason Monk 2662515beee67307d8859beec521b7baedfa54b2b5Jason Monkimport com.android.internal.R; 2762515beee67307d8859beec521b7baedfa54b2b5Jason Monk 2862515beee67307d8859beec521b7baedfa54b2b5Jason Monk/** 2962515beee67307d8859beec521b7baedfa54b2b5Jason Monk * Helper to manage showing/hiding a image to notify them that they are entering 3062515beee67307d8859beec521b7baedfa54b2b5Jason Monk * or exiting lock-to-app mode. 3162515beee67307d8859beec521b7baedfa54b2b5Jason Monk */ 3262515beee67307d8859beec521b7baedfa54b2b5Jason Monkpublic class LockTaskNotify { 3362515beee67307d8859beec521b7baedfa54b2b5Jason Monk private static final String TAG = "LockTaskNotify"; 3462515beee67307d8859beec521b7baedfa54b2b5Jason Monk 3562515beee67307d8859beec521b7baedfa54b2b5Jason Monk private final Context mContext; 3662515beee67307d8859beec521b7baedfa54b2b5Jason Monk private final H mHandler; 37815e057b9bb19acd77bf01ecb690b6e9fa85902eJason Monk private AccessibilityManager mAccessibilityManager; 3807f82b79422c4c1e4bc789775c72d3ef04fbb041Jason Monk private Toast mLastToast; 3962515beee67307d8859beec521b7baedfa54b2b5Jason Monk 4062515beee67307d8859beec521b7baedfa54b2b5Jason Monk public LockTaskNotify(Context context) { 4162515beee67307d8859beec521b7baedfa54b2b5Jason Monk mContext = context; 42815e057b9bb19acd77bf01ecb690b6e9fa85902eJason Monk mAccessibilityManager = (AccessibilityManager) 43815e057b9bb19acd77bf01ecb690b6e9fa85902eJason Monk mContext.getSystemService(Context.ACCESSIBILITY_SERVICE); 4462515beee67307d8859beec521b7baedfa54b2b5Jason Monk mHandler = new H(); 4562515beee67307d8859beec521b7baedfa54b2b5Jason Monk } 4662515beee67307d8859beec521b7baedfa54b2b5Jason Monk 47a8f569c10a423ced5be7e019c3df2bca11b052f5Jason Monk public void showToast(boolean isLocked) { 48a8f569c10a423ced5be7e019c3df2bca11b052f5Jason Monk mHandler.obtainMessage(H.SHOW_TOAST, isLocked ? 1 : 0, 0 /* Not used */).sendToTarget(); 49a8f569c10a423ced5be7e019c3df2bca11b052f5Jason Monk } 50a8f569c10a423ced5be7e019c3df2bca11b052f5Jason Monk 51a8f569c10a423ced5be7e019c3df2bca11b052f5Jason Monk public void handleShowToast(boolean isLocked) { 52a8f569c10a423ced5be7e019c3df2bca11b052f5Jason Monk String text = mContext.getString(isLocked 53a8f569c10a423ced5be7e019c3df2bca11b052f5Jason Monk ? R.string.lock_to_app_toast_locked : R.string.lock_to_app_toast); 54815e057b9bb19acd77bf01ecb690b6e9fa85902eJason Monk if (!isLocked && mAccessibilityManager.isEnabled()) { 55815e057b9bb19acd77bf01ecb690b6e9fa85902eJason Monk text = mContext.getString(R.string.lock_to_app_toast_accessible); 56815e057b9bb19acd77bf01ecb690b6e9fa85902eJason Monk } 5707f82b79422c4c1e4bc789775c72d3ef04fbb041Jason Monk if (mLastToast != null) { 5807f82b79422c4c1e4bc789775c72d3ef04fbb041Jason Monk mLastToast.cancel(); 5907f82b79422c4c1e4bc789775c72d3ef04fbb041Jason Monk } 60a585268aff94b620b2c22e9ba3ae1163bb5dc8a8Jason Monk mLastToast = makeAllUserToastAndShow(text); 61a8f569c10a423ced5be7e019c3df2bca11b052f5Jason Monk } 62a8f569c10a423ced5be7e019c3df2bca11b052f5Jason Monk 6362515beee67307d8859beec521b7baedfa54b2b5Jason Monk public void show(boolean starting) { 647779bf1ce8243d9f856c05db3cc5a37a8227a970Jason Monk int showString = R.string.lock_to_app_exit; 657779bf1ce8243d9f856c05db3cc5a37a8227a970Jason Monk if (starting) { 667779bf1ce8243d9f856c05db3cc5a37a8227a970Jason Monk showString = R.string.lock_to_app_start; 6762515beee67307d8859beec521b7baedfa54b2b5Jason Monk } 68a585268aff94b620b2c22e9ba3ae1163bb5dc8a8Jason Monk makeAllUserToastAndShow(mContext.getString(showString)); 69a585268aff94b620b2c22e9ba3ae1163bb5dc8a8Jason Monk } 70a585268aff94b620b2c22e9ba3ae1163bb5dc8a8Jason Monk 71a585268aff94b620b2c22e9ba3ae1163bb5dc8a8Jason Monk private Toast makeAllUserToastAndShow(String text) { 72a585268aff94b620b2c22e9ba3ae1163bb5dc8a8Jason Monk Toast toast = Toast.makeText(mContext, text, Toast.LENGTH_LONG); 73a585268aff94b620b2c22e9ba3ae1163bb5dc8a8Jason Monk toast.getWindowParams().privateFlags |= 74a585268aff94b620b2c22e9ba3ae1163bb5dc8a8Jason Monk WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS; 75a585268aff94b620b2c22e9ba3ae1163bb5dc8a8Jason Monk toast.show(); 76a585268aff94b620b2c22e9ba3ae1163bb5dc8a8Jason Monk return toast; 7762515beee67307d8859beec521b7baedfa54b2b5Jason Monk } 7862515beee67307d8859beec521b7baedfa54b2b5Jason Monk 7962515beee67307d8859beec521b7baedfa54b2b5Jason Monk private final class H extends Handler { 80a8f569c10a423ced5be7e019c3df2bca11b052f5Jason Monk private static final int SHOW_TOAST = 3; 8162515beee67307d8859beec521b7baedfa54b2b5Jason Monk 8262515beee67307d8859beec521b7baedfa54b2b5Jason Monk @Override 8362515beee67307d8859beec521b7baedfa54b2b5Jason Monk public void handleMessage(Message msg) { 8462515beee67307d8859beec521b7baedfa54b2b5Jason Monk switch(msg.what) { 85a8f569c10a423ced5be7e019c3df2bca11b052f5Jason Monk case SHOW_TOAST: 86a8f569c10a423ced5be7e019c3df2bca11b052f5Jason Monk handleShowToast(msg.arg1 != 0); 87a8f569c10a423ced5be7e019c3df2bca11b052f5Jason Monk break; 8862515beee67307d8859beec521b7baedfa54b2b5Jason Monk } 8962515beee67307d8859beec521b7baedfa54b2b5Jason Monk } 9062515beee67307d8859beec521b7baedfa54b2b5Jason Monk } 9162515beee67307d8859beec521b7baedfa54b2b5Jason Monk} 92