1 /*
2  * Copyright (C) 2021 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 package com.android.launcher3.taskbar;
17 
18 import static com.android.launcher3.util.FlagDebugUtils.appendFlag;
19 
20 import androidx.annotation.IntDef;
21 
22 import com.android.quickstep.SystemUiProxy;
23 
24 import java.io.PrintWriter;
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 import java.util.StringJoiner;
28 
29 /**
30  * Normally Taskbar will auto-hide when entering immersive (fullscreen) apps. This controller allows
31  * us to suspend that behavior in certain cases (e.g. opening a Folder or dragging an icon).
32  */
33 public class TaskbarAutohideSuspendController implements
34         TaskbarControllers.LoggableTaskbarController {
35 
36     // Taskbar window is fullscreen.
37     public static final int FLAG_AUTOHIDE_SUSPEND_FULLSCREEN = 1 << 0;
38     // User is dragging item.
39     public static final int FLAG_AUTOHIDE_SUSPEND_DRAGGING = 1 << 1;
40     // User has touched down but has not lifted finger.
41     public static final int FLAG_AUTOHIDE_SUSPEND_TOUCHING = 1 << 2;
42     // Taskbar EDU overlay is open above the Taskbar. */
43     public static final int FLAG_AUTOHIDE_SUSPEND_EDU_OPEN = 1 << 3;
44     // Taskbar is in immersive mode in overview
45     public static final int FLAG_AUTOHIDE_SUSPEND_IN_LAUNCHER = 1 << 4;
46     // Transient Taskbar is temporarily unstashed (pending a timeout).
47     public static final int FLAG_AUTOHIDE_SUSPEND_TRANSIENT_TASKBAR = 1 << 5;
48     // User has hovered the taskbar.
49     public static final int FLAG_AUTOHIDE_SUSPEND_HOVERING_ICONS = 1 << 6;
50 
51     @IntDef(flag = true, value = {
52             FLAG_AUTOHIDE_SUSPEND_FULLSCREEN,
53             FLAG_AUTOHIDE_SUSPEND_DRAGGING,
54             FLAG_AUTOHIDE_SUSPEND_TOUCHING,
55             FLAG_AUTOHIDE_SUSPEND_EDU_OPEN,
56             FLAG_AUTOHIDE_SUSPEND_IN_LAUNCHER,
57             FLAG_AUTOHIDE_SUSPEND_TRANSIENT_TASKBAR,
58             FLAG_AUTOHIDE_SUSPEND_HOVERING_ICONS,
59     })
60     @Retention(RetentionPolicy.SOURCE)
61     public @interface AutohideSuspendFlag {}
62 
63     private final TaskbarActivityContext mActivity;
64     private final SystemUiProxy mSystemUiProxy;
65 
66     private @AutohideSuspendFlag int mAutohideSuspendFlags = 0;
67 
TaskbarAutohideSuspendController(TaskbarActivityContext activity)68     public TaskbarAutohideSuspendController(TaskbarActivityContext activity) {
69         mActivity = activity;
70         mSystemUiProxy = SystemUiProxy.INSTANCE.get(activity);
71     }
72 
onDestroy()73     public void onDestroy() {
74         mSystemUiProxy.notifyTaskbarAutohideSuspend(false);
75     }
76 
77     /**
78      * Adds or removes the given flag, then notifies system UI proxy whether to suspend auto-hide.
79      */
updateFlag(@utohideSuspendFlag int flag, boolean enabled)80     public void updateFlag(@AutohideSuspendFlag int flag, boolean enabled) {
81         int flagsBefore = mAutohideSuspendFlags;
82         if (enabled) {
83             mAutohideSuspendFlags |= flag;
84         } else {
85             mAutohideSuspendFlags &= ~flag;
86         }
87         if (flagsBefore == mAutohideSuspendFlags) {
88             // Nothing has changed, no need to notify.
89             return;
90         }
91 
92         boolean isSuspended = isSuspended();
93         mSystemUiProxy.notifyTaskbarAutohideSuspend(isSuspended);
94         mActivity.onTransientAutohideSuspendFlagChanged(isTransientTaskbarStashingSuspended());
95     }
96 
97     /**
98      * Returns true iff taskbar autohide is currently suspended.
99      */
isSuspended()100     private boolean isSuspended() {
101         return mAutohideSuspendFlags != 0;
102     }
103 
104     /**
105      * Returns whether Transient Taskbar should avoid auto-stashing in Launcher(Overview).
106      */
isSuspendedForTransientTaskbarInLauncher()107     public boolean isSuspendedForTransientTaskbarInLauncher() {
108         return (mAutohideSuspendFlags & FLAG_AUTOHIDE_SUSPEND_IN_LAUNCHER) != 0;
109     }
110 
111     /**
112      * Returns whether Transient Taskbar should avoid auto-stashing.
113      */
isTransientTaskbarStashingSuspended()114     public boolean isTransientTaskbarStashingSuspended() {
115         return (mAutohideSuspendFlags & ~FLAG_AUTOHIDE_SUSPEND_TRANSIENT_TASKBAR) != 0;
116     }
117 
118     @Override
dumpLogs(String prefix, PrintWriter pw)119     public void dumpLogs(String prefix, PrintWriter pw) {
120         pw.println(prefix + "TaskbarAutohideSuspendController:");
121 
122         pw.println(prefix + "\tmAutohideSuspendFlags=" + getStateString(mAutohideSuspendFlags));
123     }
124 
getStateString(int flags)125     private static String getStateString(int flags) {
126         StringJoiner str = new StringJoiner("|");
127         appendFlag(str, flags, FLAG_AUTOHIDE_SUSPEND_FULLSCREEN,
128                 "FLAG_AUTOHIDE_SUSPEND_FULLSCREEN");
129         appendFlag(str, flags, FLAG_AUTOHIDE_SUSPEND_DRAGGING, "FLAG_AUTOHIDE_SUSPEND_DRAGGING");
130         appendFlag(str, flags, FLAG_AUTOHIDE_SUSPEND_TOUCHING, "FLAG_AUTOHIDE_SUSPEND_TOUCHING");
131         appendFlag(str, flags, FLAG_AUTOHIDE_SUSPEND_EDU_OPEN, "FLAG_AUTOHIDE_SUSPEND_EDU_OPEN");
132         appendFlag(str, flags, FLAG_AUTOHIDE_SUSPEND_IN_LAUNCHER,
133                 "FLAG_AUTOHIDE_SUSPEND_IN_LAUNCHER");
134         appendFlag(str, flags, FLAG_AUTOHIDE_SUSPEND_TRANSIENT_TASKBAR,
135                 "FLAG_AUTOHIDE_SUSPEND_TRANSIENT_TASKBAR");
136         return str.toString();
137     }
138 }
139