blob: 8c18c462e956ac57b16c5e6c8d6f4a6ba1614362 [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;
Jason Monka5852682014-10-23 11:48:36 -040023import android.view.WindowManager;
Jason Monka8f569c2014-07-07 12:15:21 -040024import android.widget.Toast;
Jason Monk62515be2014-05-21 16:06:19 -040025
26import com.android.internal.R;
27
28/**
29 * Helper to manage showing/hiding a image to notify them that they are entering
30 * or exiting lock-to-app mode.
31 */
32public class LockTaskNotify {
33 private static final String TAG = "LockTaskNotify";
34
Jason Monk62515be2014-05-21 16:06:19 -040035 private final Context mContext;
36 private final H mHandler;
Jason Monk07f82b72014-08-19 15:20:01 -040037 private Toast mLastToast;
Jason Monk62515be2014-05-21 16:06:19 -040038
Jason Monk62515be2014-05-21 16:06:19 -040039 public LockTaskNotify(Context context) {
40 mContext = context;
41 mHandler = new H();
Jason Monk62515be2014-05-21 16:06:19 -040042 }
43
Benjamin Franz43261142015-02-11 15:59:44 +000044 public void showToast(int lockTaskModeState) {
45 mHandler.obtainMessage(H.SHOW_TOAST, lockTaskModeState, 0 /* Not used */).sendToTarget();
Jason Monka8f569c2014-07-07 12:15:21 -040046 }
47
Benjamin Franz43261142015-02-11 15:59:44 +000048 public void handleShowToast(int lockTaskModeState) {
49 String text = null;
50 if (lockTaskModeState == ActivityManager.LOCK_TASK_MODE_LOCKED) {
51 text = mContext.getString(R.string.lock_to_app_toast_locked);
52 } else if (lockTaskModeState == ActivityManager.LOCK_TASK_MODE_PINNED) {
Jorim Jaggicf984c52016-05-25 14:14:46 -070053 text = mContext.getString(R.string.lock_to_app_toast);
Benjamin Franz43261142015-02-11 15:59:44 +000054 }
55 if (text == null) {
56 return;
Jason Monk815e0572014-08-12 17:26:36 -040057 }
Jason Monk07f82b72014-08-19 15:20:01 -040058 if (mLastToast != null) {
59 mLastToast.cancel();
60 }
Jason Monka5852682014-10-23 11:48:36 -040061 mLastToast = makeAllUserToastAndShow(text);
Jason Monka8f569c2014-07-07 12:15:21 -040062 }
63
Jason Monk62515be2014-05-21 16:06:19 -040064 public void show(boolean starting) {
Jason Monk7779bf12014-07-14 10:20:21 -040065 int showString = R.string.lock_to_app_exit;
66 if (starting) {
67 showString = R.string.lock_to_app_start;
Jason Monk62515be2014-05-21 16:06:19 -040068 }
Jason Monka5852682014-10-23 11:48:36 -040069 makeAllUserToastAndShow(mContext.getString(showString));
70 }
71
72 private Toast makeAllUserToastAndShow(String text) {
73 Toast toast = Toast.makeText(mContext, text, Toast.LENGTH_LONG);
74 toast.getWindowParams().privateFlags |=
75 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
76 toast.show();
77 return toast;
Jason Monk62515be2014-05-21 16:06:19 -040078 }
79
80 private final class H extends Handler {
Jason Monka8f569c2014-07-07 12:15:21 -040081 private static final int SHOW_TOAST = 3;
Jason Monk62515be2014-05-21 16:06:19 -040082
83 @Override
84 public void handleMessage(Message msg) {
85 switch(msg.what) {
Jason Monka8f569c2014-07-07 12:15:21 -040086 case SHOW_TOAST:
Benjamin Franz43261142015-02-11 15:59:44 +000087 handleShowToast(msg.arg1);
Jason Monka8f569c2014-07-07 12:15:21 -040088 break;
Jason Monk62515be2014-05-21 16:06:19 -040089 }
90 }
91 }
92}