1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.server.am; 18 19import android.app.ActivityManager; 20import android.content.Context; 21import android.os.Handler; 22import android.os.Message; 23import android.os.SystemClock; 24import android.util.Slog; 25import android.view.WindowManager; 26import android.widget.Toast; 27 28import com.android.internal.R; 29 30/** 31 * Helper to manage showing/hiding a image to notify them that they are entering 32 * or exiting lock-to-app mode. 33 */ 34public class LockTaskNotify { 35 private static final String TAG = "LockTaskNotify"; 36 private static final long SHOW_TOAST_MINIMUM_INTERVAL = 1000; 37 38 private final Context mContext; 39 private final H mHandler; 40 private Toast mLastToast; 41 private long mLastShowToastTime; 42 43 public LockTaskNotify(Context context) { 44 mContext = context; 45 mHandler = new H(); 46 } 47 48 public void showToast(int lockTaskModeState) { 49 mHandler.obtainMessage(H.SHOW_TOAST, lockTaskModeState, 0 /* Not used */).sendToTarget(); 50 } 51 52 public void handleShowToast(int lockTaskModeState) { 53 String text = null; 54 if (lockTaskModeState == ActivityManager.LOCK_TASK_MODE_LOCKED) { 55 text = mContext.getString(R.string.lock_to_app_toast_locked); 56 } else if (lockTaskModeState == ActivityManager.LOCK_TASK_MODE_PINNED) { 57 text = mContext.getString(R.string.lock_to_app_toast); 58 } 59 if (text == null) { 60 return; 61 } 62 long showToastTime = SystemClock.elapsedRealtime(); 63 if ((showToastTime - mLastShowToastTime) < SHOW_TOAST_MINIMUM_INTERVAL) { 64 Slog.i(TAG, "Ignore toast since it is requested in very short interval."); 65 return; 66 } 67 if (mLastToast != null) { 68 mLastToast.cancel(); 69 } 70 mLastToast = makeAllUserToastAndShow(text); 71 mLastShowToastTime = showToastTime; 72 } 73 74 public void show(boolean starting) { 75 int showString = R.string.lock_to_app_exit; 76 if (starting) { 77 showString = R.string.lock_to_app_start; 78 } 79 makeAllUserToastAndShow(mContext.getString(showString)); 80 } 81 82 private Toast makeAllUserToastAndShow(String text) { 83 Toast toast = Toast.makeText(mContext, text, Toast.LENGTH_LONG); 84 toast.getWindowParams().privateFlags |= 85 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS; 86 toast.show(); 87 return toast; 88 } 89 90 private final class H extends Handler { 91 private static final int SHOW_TOAST = 3; 92 93 @Override 94 public void handleMessage(Message msg) { 95 switch(msg.what) { 96 case SHOW_TOAST: 97 handleShowToast(msg.arg1); 98 break; 99 } 100 } 101 } 102} 103