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.view.WindowManager;
24import android.view.accessibility.AccessibilityManager;
25import android.widget.Toast;
26
27import com.android.internal.R;
28
29/**
30 *  Helper to manage showing/hiding a image to notify them that they are entering
31 *  or exiting lock-to-app mode.
32 */
33public class LockTaskNotify {
34    private static final String TAG = "LockTaskNotify";
35
36    private final Context mContext;
37    private final H mHandler;
38    private AccessibilityManager mAccessibilityManager;
39    private Toast mLastToast;
40
41    public LockTaskNotify(Context context) {
42        mContext = context;
43        mAccessibilityManager = (AccessibilityManager)
44                mContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
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(mAccessibilityManager.isEnabled()
58                    ? R.string.lock_to_app_toast_accessible : R.string.lock_to_app_toast);
59        }
60        if (text == null) {
61            return;
62        }
63        if (mLastToast != null) {
64            mLastToast.cancel();
65        }
66        mLastToast = makeAllUserToastAndShow(text);
67    }
68
69    public void show(boolean starting) {
70        int showString = R.string.lock_to_app_exit;
71        if (starting) {
72            showString = R.string.lock_to_app_start;
73        }
74        makeAllUserToastAndShow(mContext.getString(showString));
75    }
76
77    private Toast makeAllUserToastAndShow(String text) {
78        Toast toast = Toast.makeText(mContext, text, Toast.LENGTH_LONG);
79        toast.getWindowParams().privateFlags |=
80                WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
81        toast.show();
82        return toast;
83    }
84
85    private final class H extends Handler {
86        private static final int SHOW_TOAST = 3;
87
88        @Override
89        public void handleMessage(Message msg) {
90            switch(msg.what) {
91                case SHOW_TOAST:
92                    handleShowToast(msg.arg1);
93                    break;
94            }
95        }
96    }
97}
98