1 /*
2  * Copyright (C) 2018 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.systemui.statusbar.phone;
18 
19 import android.content.Context;
20 import android.os.SystemClock;
21 import android.util.Slog;
22 import android.widget.Toast;
23 
24 import com.android.systemui.R;
25 import com.android.systemui.SysUIToast;
26 
27 /**
28  *  Helper to manage showing/hiding a image to notify them that they are entering or exiting screen
29  *  pinning mode. All exposed methods should be called from a handler thread.
30  */
31 public class ScreenPinningNotify {
32     private static final String TAG = "ScreenPinningNotify";
33     private static final long SHOW_TOAST_MINIMUM_INTERVAL = 1000;
34 
35     private final Context mContext;
36     private Toast mLastToast;
37     private long mLastShowToastTime;
38 
ScreenPinningNotify(Context context)39     public ScreenPinningNotify(Context context) {
40         mContext = context;
41     }
42 
43     /** Show "Screen pinned" toast. */
showPinningStartToast()44     public void showPinningStartToast() {
45         makeAllUserToastAndShow(R.string.screen_pinning_start);
46     }
47 
48     /** Show "Screen unpinned" toast. */
showPinningExitToast()49     public void showPinningExitToast() {
50         makeAllUserToastAndShow(R.string.screen_pinning_exit);
51     }
52 
53     /** Show a toast that describes the gesture the user should use to escape pinned mode. */
showEscapeToast(boolean isGestureNavEnabled, boolean isRecentsButtonVisible)54     public void showEscapeToast(boolean isGestureNavEnabled, boolean isRecentsButtonVisible) {
55         long showToastTime = SystemClock.elapsedRealtime();
56         if ((showToastTime - mLastShowToastTime) < SHOW_TOAST_MINIMUM_INTERVAL) {
57             Slog.i(TAG, "Ignore toast since it is requested in very short interval.");
58             return;
59         }
60         if (mLastToast != null) {
61             mLastToast.cancel();
62         }
63         mLastToast = makeAllUserToastAndShow(isGestureNavEnabled
64                 ? R.string.screen_pinning_toast_gesture_nav
65                 : isRecentsButtonVisible
66                         ? R.string.screen_pinning_toast
67                         : R.string.screen_pinning_toast_recents_invisible);
68         mLastShowToastTime = showToastTime;
69     }
70 
makeAllUserToastAndShow(int resId)71     private Toast makeAllUserToastAndShow(int resId) {
72         Toast toast = SysUIToast.makeText(mContext, resId, Toast.LENGTH_LONG);
73         toast.show();
74         return toast;
75     }
76 }
77