blob: 0412db5e49c922643863d0944f6f67ab7c120125 [file] [log] [blame]
Jason Monk62515be2014-05-21 16:06:19 -04001/*
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
Benjamin Franz43261142015-02-11 15:59:44 +000019import android.app.ActivityManager;
Jason Monk62515be2014-05-21 16:06:19 -040020import android.content.Context;
Jason Monk62515be2014-05-21 16:06:19 -040021import android.os.Handler;
22import android.os.Message;
Koji Fukuid8c16032016-10-06 23:00:00 +090023import android.os.SystemClock;
24import android.util.Slog;
Jason Monka5852682014-10-23 11:48:36 -040025import android.view.WindowManager;
Jason Monka8f569c2014-07-07 12:15:21 -040026import android.widget.Toast;
Jason Monk62515be2014-05-21 16:06:19 -040027
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";
Koji Fukuid8c16032016-10-06 23:00:00 +090036 private static final long SHOW_TOAST_MINIMUM_INTERVAL = 1000;
Jason Monk62515be2014-05-21 16:06:19 -040037
Jason Monk62515be2014-05-21 16:06:19 -040038 private final Context mContext;
39 private final H mHandler;
Jason Monk07f82b72014-08-19 15:20:01 -040040 private Toast mLastToast;
Koji Fukuid8c16032016-10-06 23:00:00 +090041 private long mLastShowToastTime;
Jason Monk62515be2014-05-21 16:06:19 -040042
Jason Monk62515be2014-05-21 16:06:19 -040043 public LockTaskNotify(Context context) {
44 mContext = context;
45 mHandler = new H();
Jason Monk62515be2014-05-21 16:06:19 -040046 }
47
Benjamin Franz43261142015-02-11 15:59:44 +000048 public void showToast(int lockTaskModeState) {
49 mHandler.obtainMessage(H.SHOW_TOAST, lockTaskModeState, 0 /* Not used */).sendToTarget();
Jason Monka8f569c2014-07-07 12:15:21 -040050 }
51
Benjamin Franz43261142015-02-11 15:59:44 +000052 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) {
Jorim Jaggicf984c52016-05-25 14:14:46 -070057 text = mContext.getString(R.string.lock_to_app_toast);
Benjamin Franz43261142015-02-11 15:59:44 +000058 }
59 if (text == null) {
60 return;
Jason Monk815e0572014-08-12 17:26:36 -040061 }
Koji Fukuid8c16032016-10-06 23:00:00 +090062 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 }
Jason Monk07f82b72014-08-19 15:20:01 -040067 if (mLastToast != null) {
68 mLastToast.cancel();
69 }
Jason Monka5852682014-10-23 11:48:36 -040070 mLastToast = makeAllUserToastAndShow(text);
Koji Fukuid8c16032016-10-06 23:00:00 +090071 mLastShowToastTime = showToastTime;
Jason Monka8f569c2014-07-07 12:15:21 -040072 }
73
Jason Monk62515be2014-05-21 16:06:19 -040074 public void show(boolean starting) {
Jason Monk7779bf12014-07-14 10:20:21 -040075 int showString = R.string.lock_to_app_exit;
76 if (starting) {
77 showString = R.string.lock_to_app_start;
Jason Monk62515be2014-05-21 16:06:19 -040078 }
Jason Monka5852682014-10-23 11:48:36 -040079 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;
Jason Monk62515be2014-05-21 16:06:19 -040088 }
89
90 private final class H extends Handler {
Jason Monka8f569c2014-07-07 12:15:21 -040091 private static final int SHOW_TOAST = 3;
Jason Monk62515be2014-05-21 16:06:19 -040092
93 @Override
94 public void handleMessage(Message msg) {
95 switch(msg.what) {
Jason Monka8f569c2014-07-07 12:15:21 -040096 case SHOW_TOAST:
Benjamin Franz43261142015-02-11 15:59:44 +000097 handleShowToast(msg.arg1);
Jason Monka8f569c2014-07-07 12:15:21 -040098 break;
Jason Monk62515be2014-05-21 16:06:19 -040099 }
100 }
101 }
102}