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.accessibility.AccessibilityManager;
23import android.widget.Toast;
24
25import com.android.internal.R;
26
27/**
28 *  Helper to manage showing/hiding a image to notify them that they are entering
29 *  or exiting lock-to-app mode.
30 */
31public class LockTaskNotify {
32    private static final String TAG = "LockTaskNotify";
33
34    private final Context mContext;
35    private final H mHandler;
36    private AccessibilityManager mAccessibilityManager;
37    private Toast mLastToast;
38
39    public LockTaskNotify(Context context) {
40        mContext = context;
41        mAccessibilityManager = (AccessibilityManager)
42                mContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
43        mHandler = new H();
44    }
45
46    public void showToast(boolean isLocked) {
47        mHandler.obtainMessage(H.SHOW_TOAST, isLocked ? 1 : 0, 0 /* Not used */).sendToTarget();
48    }
49
50    public void handleShowToast(boolean isLocked) {
51        String text = mContext.getString(isLocked
52                ? R.string.lock_to_app_toast_locked : R.string.lock_to_app_toast);
53        if (!isLocked && mAccessibilityManager.isEnabled()) {
54            text = mContext.getString(R.string.lock_to_app_toast_accessible);
55        }
56        if (mLastToast != null) {
57            mLastToast.cancel();
58        }
59        mLastToast = Toast.makeText(mContext, text, Toast.LENGTH_LONG);
60        mLastToast.show();
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        Toast.makeText(mContext, mContext.getString(showString), Toast.LENGTH_LONG).show();
69    }
70
71    private final class H extends Handler {
72        private static final int SHOW_TOAST = 3;
73
74        @Override
75        public void handleMessage(Message msg) {
76            switch(msg.what) {
77                case SHOW_TOAST:
78                    handleShowToast(msg.arg1 != 0);
79                    break;
80            }
81        }
82    }
83}
84