Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 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 | |
Benjamin Franz | 4326114 | 2015-02-11 15:59:44 +0000 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 20 | import android.content.Context; |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 21 | import android.os.Handler; |
| 22 | import android.os.Message; |
Koji Fukui | d8c1603 | 2016-10-06 23:00:00 +0900 | [diff] [blame] | 23 | import android.os.SystemClock; |
| 24 | import android.util.Slog; |
Jason Monk | a585268 | 2014-10-23 11:48:36 -0400 | [diff] [blame] | 25 | import android.view.WindowManager; |
Jason Monk | a8f569c | 2014-07-07 12:15:21 -0400 | [diff] [blame] | 26 | import android.widget.Toast; |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 27 | |
| 28 | import com.android.internal.R; |
| 29 | |
| 30 | /** |
| 31 | * Helper to manage showing/hiding a image to notify them that they are entering |
| 32 | * or exiting lock-to-app mode. |
| 33 | */ |
| 34 | public class LockTaskNotify { |
| 35 | private static final String TAG = "LockTaskNotify"; |
Koji Fukui | d8c1603 | 2016-10-06 23:00:00 +0900 | [diff] [blame] | 36 | private static final long SHOW_TOAST_MINIMUM_INTERVAL = 1000; |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 37 | |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 38 | private final Context mContext; |
| 39 | private final H mHandler; |
Jason Monk | 07f82b7 | 2014-08-19 15:20:01 -0400 | [diff] [blame] | 40 | private Toast mLastToast; |
Koji Fukui | d8c1603 | 2016-10-06 23:00:00 +0900 | [diff] [blame] | 41 | private long mLastShowToastTime; |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 42 | |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 43 | public LockTaskNotify(Context context) { |
| 44 | mContext = context; |
| 45 | mHandler = new H(); |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 46 | } |
| 47 | |
Benjamin Franz | 4326114 | 2015-02-11 15:59:44 +0000 | [diff] [blame] | 48 | public void showToast(int lockTaskModeState) { |
| 49 | mHandler.obtainMessage(H.SHOW_TOAST, lockTaskModeState, 0 /* Not used */).sendToTarget(); |
Jason Monk | a8f569c | 2014-07-07 12:15:21 -0400 | [diff] [blame] | 50 | } |
| 51 | |
Benjamin Franz | 4326114 | 2015-02-11 15:59:44 +0000 | [diff] [blame] | 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) { |
Jorim Jaggi | cf984c5 | 2016-05-25 14:14:46 -0700 | [diff] [blame] | 57 | text = mContext.getString(R.string.lock_to_app_toast); |
Benjamin Franz | 4326114 | 2015-02-11 15:59:44 +0000 | [diff] [blame] | 58 | } |
| 59 | if (text == null) { |
| 60 | return; |
Jason Monk | 815e057 | 2014-08-12 17:26:36 -0400 | [diff] [blame] | 61 | } |
Koji Fukui | d8c1603 | 2016-10-06 23:00:00 +0900 | [diff] [blame] | 62 | long showToastTime = SystemClock.elapsedRealtime(); |
| 63 | if ((showToastTime - mLastShowToastTime) < SHOW_TOAST_MINIMUM_INTERVAL) { |
| 64 | Slog.i(TAG, "Ignore toast since it is requested in very short interval."); |
| 65 | return; |
| 66 | } |
Jason Monk | 07f82b7 | 2014-08-19 15:20:01 -0400 | [diff] [blame] | 67 | if (mLastToast != null) { |
| 68 | mLastToast.cancel(); |
| 69 | } |
Jason Monk | a585268 | 2014-10-23 11:48:36 -0400 | [diff] [blame] | 70 | mLastToast = makeAllUserToastAndShow(text); |
Koji Fukui | d8c1603 | 2016-10-06 23:00:00 +0900 | [diff] [blame] | 71 | mLastShowToastTime = showToastTime; |
Jason Monk | a8f569c | 2014-07-07 12:15:21 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 74 | public void show(boolean starting) { |
Jason Monk | 7779bf1 | 2014-07-14 10:20:21 -0400 | [diff] [blame] | 75 | int showString = R.string.lock_to_app_exit; |
| 76 | if (starting) { |
| 77 | showString = R.string.lock_to_app_start; |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 78 | } |
Jason Monk | a585268 | 2014-10-23 11:48:36 -0400 | [diff] [blame] | 79 | makeAllUserToastAndShow(mContext.getString(showString)); |
| 80 | } |
| 81 | |
| 82 | private Toast makeAllUserToastAndShow(String text) { |
| 83 | Toast toast = Toast.makeText(mContext, text, Toast.LENGTH_LONG); |
| 84 | toast.getWindowParams().privateFlags |= |
| 85 | WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS; |
| 86 | toast.show(); |
| 87 | return toast; |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | private final class H extends Handler { |
Jason Monk | a8f569c | 2014-07-07 12:15:21 -0400 | [diff] [blame] | 91 | private static final int SHOW_TOAST = 3; |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 92 | |
| 93 | @Override |
| 94 | public void handleMessage(Message msg) { |
| 95 | switch(msg.what) { |
Jason Monk | a8f569c | 2014-07-07 12:15:21 -0400 | [diff] [blame] | 96 | case SHOW_TOAST: |
Benjamin Franz | 4326114 | 2015-02-11 15:59:44 +0000 | [diff] [blame] | 97 | handleShowToast(msg.arg1); |
Jason Monk | a8f569c | 2014-07-07 12:15:21 -0400 | [diff] [blame] | 98 | break; |
Jason Monk | 62515be | 2014-05-21 16:06:19 -0400 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |