LockTaskNotify.java revision 815e057b9bb19acd77bf01ecb690b6e9fa85902e
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
38    public LockTaskNotify(Context context) {
39        mContext = context;
40        mAccessibilityManager = (AccessibilityManager)
41                mContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
42        mHandler = new H();
43    }
44
45    public void showToast(boolean isLocked) {
46        mHandler.obtainMessage(H.SHOW_TOAST, isLocked ? 1 : 0, 0 /* Not used */).sendToTarget();
47    }
48
49    public void handleShowToast(boolean isLocked) {
50        String text = mContext.getString(isLocked
51                ? R.string.lock_to_app_toast_locked : R.string.lock_to_app_toast);
52        if (!isLocked && mAccessibilityManager.isEnabled()) {
53            text = mContext.getString(R.string.lock_to_app_toast_accessible);
54        }
55        Toast.makeText(mContext, text, Toast.LENGTH_LONG).show();
56    }
57
58    public void show(boolean starting) {
59        int showString = R.string.lock_to_app_exit;
60        if (starting) {
61            showString = R.string.lock_to_app_start;
62        }
63        Toast.makeText(mContext, mContext.getString(showString), Toast.LENGTH_LONG).show();
64    }
65
66    private final class H extends Handler {
67        private static final int SHOW_TOAST = 3;
68
69        @Override
70        public void handleMessage(Message msg) {
71            switch(msg.what) {
72                case SHOW_TOAST:
73                    handleShowToast(msg.arg1 != 0);
74                    break;
75            }
76        }
77    }
78}
79