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 
17 package com.android.server.am;
18 
19 import android.app.ActivityManager;
20 import android.content.Context;
21 import android.os.Handler;
22 import android.os.Message;
23 import android.view.WindowManager;
24 import android.widget.Toast;
25 
26 import 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  */
32 public class LockTaskNotify {
33     private static final String TAG = "LockTaskNotify";
34 
35     private final Context mContext;
36     private final H mHandler;
37     private Toast mLastToast;
38 
LockTaskNotify(Context context)39     public LockTaskNotify(Context context) {
40         mContext = context;
41         mHandler = new H();
42     }
43 
showToast(int lockTaskModeState)44     public void showToast(int lockTaskModeState) {
45         mHandler.obtainMessage(H.SHOW_TOAST, lockTaskModeState, 0 /* Not used */).sendToTarget();
46     }
47 
handleShowToast(int lockTaskModeState)48     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) {
53             text = mContext.getString(R.string.lock_to_app_toast);
54         }
55         if (text == null) {
56             return;
57         }
58         if (mLastToast != null) {
59             mLastToast.cancel();
60         }
61         mLastToast = makeAllUserToastAndShow(text);
62     }
63 
show(boolean starting)64     public void show(boolean starting) {
65         int showString = R.string.lock_to_app_exit;
66         if (starting) {
67             showString = R.string.lock_to_app_start;
68         }
69         makeAllUserToastAndShow(mContext.getString(showString));
70     }
71 
makeAllUserToastAndShow(String text)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;
78     }
79 
80     private final class H extends Handler {
81         private static final int SHOW_TOAST = 3;
82 
83         @Override
handleMessage(Message msg)84         public void handleMessage(Message msg) {
85             switch(msg.what) {
86                 case SHOW_TOAST:
87                     handleShowToast(msg.arg1);
88                     break;
89             }
90         }
91     }
92 }
93