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.content.Context;
20import android.os.Handler;
21import android.os.Message;
22import android.view.WindowManager;
23import android.view.accessibility.AccessibilityManager;
24import android.widget.Toast;
25
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
35    private final Context mContext;
36    private final H mHandler;
37    private AccessibilityManager mAccessibilityManager;
38    private Toast mLastToast;
39
40    public LockTaskNotify(Context context) {
41        mContext = context;
42        mAccessibilityManager = (AccessibilityManager)
43                mContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
44        mHandler = new H();
45    }
46
47    public void showToast(boolean isLocked) {
48        mHandler.obtainMessage(H.SHOW_TOAST, isLocked ? 1 : 0, 0 /* Not used */).sendToTarget();
49    }
50
51    public void handleShowToast(boolean isLocked) {
52        String text = mContext.getString(isLocked
53                ? R.string.lock_to_app_toast_locked : R.string.lock_to_app_toast);
54        if (!isLocked && mAccessibilityManager.isEnabled()) {
55            text = mContext.getString(R.string.lock_to_app_toast_accessible);
56        }
57        if (mLastToast != null) {
58            mLastToast.cancel();
59        }
60        mLastToast = makeAllUserToastAndShow(text);
61    }
62
63    public void show(boolean starting) {
64        int showString = R.string.lock_to_app_exit;
65        if (starting) {
66            showString = R.string.lock_to_app_start;
67        }
68        makeAllUserToastAndShow(mContext.getString(showString));
69    }
70
71    private Toast makeAllUserToastAndShow(String text) {
72        Toast toast = Toast.makeText(mContext, text, Toast.LENGTH_LONG);
73        toast.getWindowParams().privateFlags |=
74                WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
75        toast.show();
76        return toast;
77    }
78
79    private final class H extends Handler {
80        private static final int SHOW_TOAST = 3;
81
82        @Override
83        public void handleMessage(Message msg) {
84            switch(msg.what) {
85                case SHOW_TOAST:
86                    handleShowToast(msg.arg1 != 0);
87                    break;
88            }
89        }
90    }
91}
92