1 /*
2  * Copyright (C) 2016 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.launcher3;
18 
19 import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED;
20 import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
21 
22 import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
23 import static com.android.launcher3.compat.AccessibilityManagerCompat.sendCustomAccessibilityEvent;
24 
25 import android.annotation.SuppressLint;
26 import android.content.Context;
27 import android.util.AttributeSet;
28 import android.util.Pair;
29 import android.view.MotionEvent;
30 import android.view.View;
31 import android.view.accessibility.AccessibilityNodeInfo;
32 import android.view.animation.Interpolator;
33 import android.widget.LinearLayout;
34 
35 import androidx.annotation.IntDef;
36 
37 import com.android.launcher3.anim.PendingAnimation;
38 import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
39 import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
40 import com.android.launcher3.util.TouchController;
41 import com.android.launcher3.views.ActivityContext;
42 import com.android.launcher3.views.BaseDragLayer;
43 
44 import java.lang.annotation.Retention;
45 import java.lang.annotation.RetentionPolicy;
46 
47 /**
48  * Base class for a View which shows a floating UI on top of the launcher UI.
49  */
50 public abstract class AbstractFloatingView extends LinearLayout implements TouchController {
51 
52     @IntDef(flag = true, value = {
53             TYPE_FOLDER,
54             TYPE_ACTION_POPUP,
55             TYPE_WIDGETS_BOTTOM_SHEET,
56             TYPE_WIDGET_RESIZE_FRAME,
57             TYPE_WIDGETS_FULL_SHEET,
58             TYPE_ON_BOARD_POPUP,
59             TYPE_DISCOVERY_BOUNCE,
60             TYPE_SNACKBAR,
61             TYPE_LISTENER,
62             TYPE_ALL_APPS_EDU,
63 
64             TYPE_TASK_MENU,
65             TYPE_OPTIONS_POPUP
66     })
67     @Retention(RetentionPolicy.SOURCE)
68     public @interface FloatingViewType {}
69     public static final int TYPE_FOLDER = 1 << 0;
70     public static final int TYPE_ACTION_POPUP = 1 << 1;
71     public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2;
72     public static final int TYPE_WIDGET_RESIZE_FRAME = 1 << 3;
73     public static final int TYPE_WIDGETS_FULL_SHEET = 1 << 4;
74     public static final int TYPE_ON_BOARD_POPUP = 1 << 5;
75     public static final int TYPE_DISCOVERY_BOUNCE = 1 << 6;
76     public static final int TYPE_SNACKBAR = 1 << 7;
77     public static final int TYPE_LISTENER = 1 << 8;
78     public static final int TYPE_ALL_APPS_EDU = 1 << 9;
79 
80     // Popups related to quickstep UI
81     public static final int TYPE_TASK_MENU = 1 << 10;
82     public static final int TYPE_OPTIONS_POPUP = 1 << 11;
83 
84     public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP
85             | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET
86             | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU
87             | TYPE_OPTIONS_POPUP | TYPE_SNACKBAR | TYPE_LISTENER | TYPE_ALL_APPS_EDU;
88 
89     // Type of popups which should be kept open during launcher rebind
90     public static final int TYPE_REBIND_SAFE = TYPE_WIDGETS_FULL_SHEET
91             | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE
92             | TYPE_ALL_APPS_EDU;
93 
94     // Usually we show the back button when a floating view is open. Instead, hide for these types.
95     public static final int TYPE_HIDE_BACK_BUTTON = TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE
96             | TYPE_SNACKBAR | TYPE_WIDGET_RESIZE_FRAME | TYPE_LISTENER;
97 
98     public static final int TYPE_ACCESSIBLE = TYPE_ALL & ~TYPE_DISCOVERY_BOUNCE & ~TYPE_LISTENER
99             & ~TYPE_ALL_APPS_EDU;
100 
101     // These view all have particular operation associated with swipe down interaction.
102     public static final int TYPE_STATUS_BAR_SWIPE_DOWN_DISALLOW = TYPE_WIDGETS_BOTTOM_SHEET |
103             TYPE_WIDGETS_FULL_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_ON_BOARD_POPUP |
104             TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU ;
105 
106     protected boolean mIsOpen;
107 
AbstractFloatingView(Context context, AttributeSet attrs)108     public AbstractFloatingView(Context context, AttributeSet attrs) {
109         super(context, attrs);
110     }
111 
AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr)112     public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
113         super(context, attrs, defStyleAttr);
114     }
115 
116     /**
117      * We need to handle touch events to prevent them from falling through to the workspace below.
118      */
119     @SuppressLint("ClickableViewAccessibility")
120     @Override
onTouchEvent(MotionEvent ev)121     public boolean onTouchEvent(MotionEvent ev) {
122         return true;
123     }
124 
close(boolean animate)125     public final void close(boolean animate) {
126         animate &= Utilities.areAnimationsEnabled(getContext());
127         if (mIsOpen) {
128             BaseActivity.fromContext(getContext()).getUserEventDispatcher()
129                     .resetElapsedContainerMillis("container closed");
130         }
131         handleClose(animate);
132         mIsOpen = false;
133     }
134 
handleClose(boolean animate)135     protected abstract void handleClose(boolean animate);
136 
137     /**
138      * Creates a user-controlled animation to hint that the view will be closed if completed.
139      * @param distanceToMove The max distance that elements should move from their starting point.
140      */
addHintCloseAnim( float distanceToMove, Interpolator interpolator, PendingAnimation target)141     public void addHintCloseAnim(
142             float distanceToMove, Interpolator interpolator, PendingAnimation target) { }
143 
logActionCommand(int command)144     public abstract void logActionCommand(int command);
145 
getLogContainerType()146     public int getLogContainerType() {
147         return ContainerType.DEFAULT_CONTAINERTYPE;
148     }
149 
isOpen()150     public final boolean isOpen() {
151         return mIsOpen;
152     }
153 
isOfType(@loatingViewType int type)154     protected abstract boolean isOfType(@FloatingViewType int type);
155 
156     /** @return Whether the back is consumed. If false, Launcher will handle the back as well. */
onBackPressed()157     public boolean onBackPressed() {
158         logActionCommand(Action.Command.BACK);
159         close(true);
160         return true;
161     }
162 
163     @Override
onControllerTouchEvent(MotionEvent ev)164     public boolean onControllerTouchEvent(MotionEvent ev) {
165         return false;
166     }
167 
announceAccessibilityChanges()168     protected void announceAccessibilityChanges() {
169         Pair<View, String> targetInfo = getAccessibilityTarget();
170         if (targetInfo == null || !isAccessibilityEnabled(getContext())) {
171             return;
172         }
173         sendCustomAccessibilityEvent(
174                 targetInfo.first, TYPE_WINDOW_STATE_CHANGED, targetInfo.second);
175 
176         if (mIsOpen) {
177             getAccessibilityInitialFocusView().performAccessibilityAction(
178                     AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS, null);
179         }
180         ActivityContext.lookupContext(getContext()).getDragLayer()
181                 .sendAccessibilityEvent(TYPE_WINDOW_CONTENT_CHANGED);
182     }
183 
getAccessibilityTarget()184     protected Pair<View, String> getAccessibilityTarget() {
185         return null;
186     }
187 
188     /** Returns the View that Accessibility services should focus on first. */
getAccessibilityInitialFocusView()189     protected View getAccessibilityInitialFocusView() {
190         return this;
191     }
192 
193     /**
194      * Returns a view matching FloatingViewType
195      */
getOpenView( ActivityContext activity, @FloatingViewType int type)196     public static <T extends AbstractFloatingView> T getOpenView(
197             ActivityContext activity, @FloatingViewType int type) {
198         BaseDragLayer dragLayer = activity.getDragLayer();
199         if (dragLayer == null) return null;
200         // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
201         // and will be one of the last views.
202         for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
203             View child = dragLayer.getChildAt(i);
204             if (child instanceof AbstractFloatingView) {
205                 AbstractFloatingView view = (AbstractFloatingView) child;
206                 if (view.isOfType(type) && view.isOpen()) {
207                     return (T) view;
208                 }
209             }
210         }
211         return null;
212     }
213 
closeOpenContainer(ActivityContext activity, @FloatingViewType int type)214     public static void closeOpenContainer(ActivityContext activity,
215             @FloatingViewType int type) {
216         AbstractFloatingView view = getOpenView(activity, type);
217         if (view != null) {
218             view.close(true);
219         }
220     }
221 
closeOpenViews(ActivityContext activity, boolean animate, @FloatingViewType int type)222     public static void closeOpenViews(ActivityContext activity, boolean animate,
223             @FloatingViewType int type) {
224         BaseDragLayer dragLayer = activity.getDragLayer();
225         // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
226         // and will be one of the last views.
227         for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
228             View child = dragLayer.getChildAt(i);
229             if (child instanceof AbstractFloatingView) {
230                 AbstractFloatingView abs = (AbstractFloatingView) child;
231                 if (abs.isOfType(type)) {
232                     abs.close(animate);
233                 }
234             }
235         }
236     }
237 
closeAllOpenViews(ActivityContext activity, boolean animate)238     public static void closeAllOpenViews(ActivityContext activity, boolean animate) {
239         closeOpenViews(activity, animate, TYPE_ALL);
240         activity.finishAutoCancelActionMode();
241     }
242 
closeAllOpenViews(ActivityContext activity)243     public static void closeAllOpenViews(ActivityContext activity) {
244         closeAllOpenViews(activity, true);
245     }
246 
closeAllOpenViewsExcept(ActivityContext activity, boolean animate, @FloatingViewType int type)247     public static void closeAllOpenViewsExcept(ActivityContext activity, boolean animate,
248                                                @FloatingViewType int type) {
249         closeOpenViews(activity, animate, TYPE_ALL & ~type);
250         activity.finishAutoCancelActionMode();
251     }
252 
closeAllOpenViewsExcept(ActivityContext activity, @FloatingViewType int type)253     public static void closeAllOpenViewsExcept(ActivityContext activity,
254                                                @FloatingViewType int type) {
255         closeAllOpenViewsExcept(activity, true, type);
256     }
257 
getTopOpenView(ActivityContext activity)258     public static AbstractFloatingView getTopOpenView(ActivityContext activity) {
259         return getTopOpenViewWithType(activity, TYPE_ALL);
260     }
261 
getTopOpenViewWithType(ActivityContext activity, @FloatingViewType int type)262     public static AbstractFloatingView getTopOpenViewWithType(ActivityContext activity,
263             @FloatingViewType int type) {
264         return getOpenView(activity, type);
265     }
266 
canInterceptEventsInSystemGestureRegion()267     public boolean canInterceptEventsInSystemGestureRegion() {
268         return false;
269     }
270 }
271