• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.animation.ValueAnimator.areAnimatorsEnabled;
20 import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED;
21 import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
22 
23 import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
24 import static com.android.launcher3.compat.AccessibilityManagerCompat.sendCustomAccessibilityEvent;
25 
26 import android.annotation.SuppressLint;
27 import android.annotation.TargetApi;
28 import android.content.Context;
29 import android.os.Build;
30 import android.util.AttributeSet;
31 import android.util.Pair;
32 import android.view.MotionEvent;
33 import android.view.View;
34 import android.view.accessibility.AccessibilityNodeInfo;
35 import android.view.animation.Interpolator;
36 import android.widget.LinearLayout;
37 import android.window.OnBackAnimationCallback;
38 
39 import androidx.annotation.IntDef;
40 
41 import com.android.launcher3.anim.PendingAnimation;
42 import com.android.launcher3.util.TouchController;
43 import com.android.launcher3.views.ActivityContext;
44 import com.android.launcher3.views.BaseDragLayer;
45 
46 import java.lang.annotation.Retention;
47 import java.lang.annotation.RetentionPolicy;
48 
49 /**
50  * Base class for a View which shows a floating UI on top of the launcher UI.
51  */
52 @TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
53 public abstract class AbstractFloatingView extends LinearLayout implements TouchController,
54         OnBackAnimationCallback {
55 
56     @IntDef(flag = true, value = {
57             TYPE_FOLDER,
58             TYPE_ACTION_POPUP,
59             TYPE_WIDGETS_BOTTOM_SHEET,
60             TYPE_WIDGET_RESIZE_FRAME,
61             TYPE_WIDGETS_FULL_SHEET,
62             TYPE_ON_BOARD_POPUP,
63             TYPE_DISCOVERY_BOUNCE,
64             TYPE_SNACKBAR,
65             TYPE_LISTENER,
66             TYPE_ALL_APPS_EDU,
67             TYPE_DRAG_DROP_POPUP,
68             TYPE_TASK_MENU,
69             TYPE_OPTIONS_POPUP,
70             TYPE_ICON_SURFACE,
71             TYPE_OPTIONS_POPUP_DIALOG,
72             TYPE_PIN_WIDGET_FROM_EXTERNAL_POPUP,
73             TYPE_TASKBAR_EDUCATION_DIALOG,
74             TYPE_TASKBAR_ALL_APPS,
75             TYPE_ADD_TO_HOME_CONFIRMATION,
76             TYPE_TASKBAR_OVERLAY_PROXY,
77             TYPE_TASKBAR_PINNING_POPUP,
78             TYPE_PIN_IME_POPUP,
79             TYPE_ONE_GRID_MIGRATION_EDU,
80     })
81     @Retention(RetentionPolicy.SOURCE)
82     public @interface FloatingViewType {}
83     public static final int TYPE_FOLDER = 1 << 0;
84     public static final int TYPE_ACTION_POPUP = 1 << 1;
85     public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2;
86     public static final int TYPE_WIDGET_RESIZE_FRAME = 1 << 3;
87     public static final int TYPE_WIDGETS_FULL_SHEET = 1 << 4;
88     public static final int TYPE_ON_BOARD_POPUP = 1 << 5;
89     public static final int TYPE_DISCOVERY_BOUNCE = 1 << 6;
90     public static final int TYPE_SNACKBAR = 1 << 7;
91     public static final int TYPE_LISTENER = 1 << 8;
92     public static final int TYPE_ALL_APPS_EDU = 1 << 9;
93     public static final int TYPE_DRAG_DROP_POPUP = 1 << 10;
94 
95     // Popups related to quickstep UI
96     public static final int TYPE_TASK_MENU = 1 << 11;
97     public static final int TYPE_OPTIONS_POPUP = 1 << 12;
98     public static final int TYPE_ICON_SURFACE = 1 << 13;
99     public static final int TYPE_OPTIONS_POPUP_DIALOG = 1 << 14;
100 
101     public static final int TYPE_PIN_WIDGET_FROM_EXTERNAL_POPUP = 1 << 15;
102     public static final int TYPE_TASKBAR_EDUCATION_DIALOG = 1 << 17;
103     public static final int TYPE_TASKBAR_ALL_APPS = 1 << 18;
104     public static final int TYPE_ADD_TO_HOME_CONFIRMATION = 1 << 19;
105     public static final int TYPE_TASKBAR_OVERLAY_PROXY = 1 << 20;
106     public static final int TYPE_TASKBAR_PINNING_POPUP = 1 << 21;
107     public static final int TYPE_PIN_IME_POPUP = 1 << 22;
108     public static final int TYPE_ONE_GRID_MIGRATION_EDU = 1 << 23;
109 
110     public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP
111             | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET
112             | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU
113             | TYPE_OPTIONS_POPUP | TYPE_SNACKBAR | TYPE_LISTENER | TYPE_ALL_APPS_EDU
114             | TYPE_ICON_SURFACE | TYPE_DRAG_DROP_POPUP | TYPE_PIN_WIDGET_FROM_EXTERNAL_POPUP
115             | TYPE_TASKBAR_EDUCATION_DIALOG | TYPE_TASKBAR_ALL_APPS | TYPE_OPTIONS_POPUP_DIALOG
116             | TYPE_ADD_TO_HOME_CONFIRMATION | TYPE_TASKBAR_OVERLAY_PROXY
117             | TYPE_TASKBAR_PINNING_POPUP | TYPE_PIN_IME_POPUP | TYPE_ONE_GRID_MIGRATION_EDU;
118 
119     // Type of popups which should be kept open during launcher rebind
120     public static final int TYPE_REBIND_SAFE = TYPE_WIDGETS_FULL_SHEET
121             | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE
122             | TYPE_ALL_APPS_EDU | TYPE_ICON_SURFACE | TYPE_TASKBAR_EDUCATION_DIALOG
123             | TYPE_TASKBAR_ALL_APPS | TYPE_OPTIONS_POPUP_DIALOG | TYPE_TASKBAR_OVERLAY_PROXY
124             | TYPE_PIN_IME_POPUP | TYPE_ONE_GRID_MIGRATION_EDU;
125 
126     /** Type of popups that should get exclusive accessibility focus. */
127     public static final int TYPE_ACCESSIBLE = TYPE_ALL & ~TYPE_DISCOVERY_BOUNCE & ~TYPE_LISTENER
128             & ~TYPE_ALL_APPS_EDU & ~TYPE_TASKBAR_ALL_APPS & ~TYPE_PIN_IME_POPUP
129             & ~TYPE_WIDGET_RESIZE_FRAME & ~TYPE_ONE_GRID_MIGRATION_EDU & ~TYPE_ON_BOARD_POPUP
130             & ~TYPE_TASKBAR_OVERLAY_PROXY;
131 
132     // These view all have particular operation associated with swipe down interaction.
133     public static final int TYPE_STATUS_BAR_SWIPE_DOWN_DISALLOW = TYPE_WIDGETS_BOTTOM_SHEET |
134             TYPE_WIDGETS_FULL_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_ON_BOARD_POPUP |
135             TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU | TYPE_DRAG_DROP_POPUP;
136 
137     // Floating views that are exclusive to the taskbar overlay window.
138     public static final int TYPE_TASKBAR_OVERLAYS =
139             TYPE_TASKBAR_ALL_APPS | TYPE_TASKBAR_EDUCATION_DIALOG;
140 
141     // Floating views that a TouchController should not try to intercept touches from.
142     public static final int TYPE_TOUCH_CONTROLLER_NO_INTERCEPT = TYPE_ALL & ~TYPE_DISCOVERY_BOUNCE
143             & ~TYPE_LISTENER & ~TYPE_TASKBAR_OVERLAYS;
144 
145     protected boolean mIsOpen;
146 
AbstractFloatingView(Context context, AttributeSet attrs)147     public AbstractFloatingView(Context context, AttributeSet attrs) {
148         super(context, attrs);
149     }
150 
AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr)151     public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
152         super(context, attrs, defStyleAttr);
153     }
154 
155     /**
156      * We need to handle touch events to prevent them from falling through to the workspace below.
157      */
158     @SuppressLint("ClickableViewAccessibility")
159     @Override
onTouchEvent(MotionEvent ev)160     public boolean onTouchEvent(MotionEvent ev) {
161         return true;
162     }
163 
close(boolean animate)164     public final void close(boolean animate) {
165         animate &= areAnimatorsEnabled();
166         if (mIsOpen) {
167             // Add to WW logging
168         }
169         handleClose(animate);
170         mIsOpen = false;
171     }
172 
handleClose(boolean animate)173     protected abstract void handleClose(boolean animate);
174 
175     /**
176      * Creates a user-controlled animation to hint that the view will be closed if completed.
177      * @param distanceToMove The max distance that elements should move from their starting point.
178      */
addHintCloseAnim( float distanceToMove, Interpolator interpolator, PendingAnimation target)179     public void addHintCloseAnim(
180             float distanceToMove, Interpolator interpolator, PendingAnimation target) { }
181 
isOpen()182     public final boolean isOpen() {
183         return mIsOpen;
184     }
185 
isOfType(@loatingViewType int type)186     protected abstract boolean isOfType(@FloatingViewType int type);
187 
188     /** Return true if this view can consume back press. */
canHandleBack()189     public boolean canHandleBack() {
190         return true;
191     }
192 
193     @Override
onBackInvoked()194     public void onBackInvoked() {
195         close(true);
196     }
197 
198     @Override
onControllerTouchEvent(MotionEvent ev)199     public boolean onControllerTouchEvent(MotionEvent ev) {
200         return false;
201     }
202 
announceAccessibilityChanges()203     protected void announceAccessibilityChanges() {
204         Pair<View, String> targetInfo = getAccessibilityTarget();
205         if (targetInfo == null || !isAccessibilityEnabled(getContext())) {
206             return;
207         }
208         sendCustomAccessibilityEvent(
209                 targetInfo.first, TYPE_WINDOW_STATE_CHANGED, targetInfo.second);
210 
211         if (mIsOpen) {
212             getAccessibilityInitialFocusView().performAccessibilityAction(
213                     AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS, null);
214         }
215         ActivityContext.lookupContext(getContext()).getDragLayer()
216                 .sendAccessibilityEvent(TYPE_WINDOW_CONTENT_CHANGED);
217     }
218 
getAccessibilityTarget()219     protected Pair<View, String> getAccessibilityTarget() {
220         return null;
221     }
222 
223     /** Returns the View that Accessibility services should focus on first. */
getAccessibilityInitialFocusView()224     protected View getAccessibilityInitialFocusView() {
225         return this;
226     }
227 
228     /**
229      * Returns a view matching FloatingViewType and {@link #isOpen()} == true.
230      */
getOpenView( ActivityContext activity, @FloatingViewType int type)231     public static <T extends AbstractFloatingView> T getOpenView(
232             ActivityContext activity, @FloatingViewType int type) {
233         return getView(activity, type, true /* mustBeOpen */);
234     }
235 
236     /**
237      * Returns whether there is at least one view of the given type where {@link #isOpen()} == true.
238      */
hasOpenView(ActivityContext activity, @FloatingViewType int type)239     public static boolean hasOpenView(ActivityContext activity, @FloatingViewType int type) {
240         return getOpenView(activity, type) != null;
241     }
242 
243     /**
244      * Returns a view matching FloatingViewType, and {@link #isOpen()} may be false (if animating
245      * closed).
246      */
getAnyView( ActivityContext activity, @FloatingViewType int type)247     public static <T extends AbstractFloatingView> T getAnyView(
248             ActivityContext activity, @FloatingViewType int type) {
249         return getView(activity, type, false /* mustBeOpen */);
250     }
251 
getView( ActivityContext activity, @FloatingViewType int type, boolean mustBeOpen)252     private static <T extends AbstractFloatingView> T getView(
253             ActivityContext activity, @FloatingViewType int type, boolean mustBeOpen) {
254         BaseDragLayer dragLayer = activity.getDragLayer();
255         if (dragLayer == null) return null;
256         // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
257         // and will be one of the last views.
258         for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
259             View child = dragLayer.getChildAt(i);
260             if (child instanceof AbstractFloatingView) {
261                 AbstractFloatingView view = (AbstractFloatingView) child;
262                 if (view.isOfType(type) && (!mustBeOpen || view.isOpen())) {
263                     return (T) view;
264                 }
265             }
266         }
267         return null;
268     }
269 
closeOpenContainer(ActivityContext activity, @FloatingViewType int type)270     public static void closeOpenContainer(ActivityContext activity,
271             @FloatingViewType int type) {
272         AbstractFloatingView view = getOpenView(activity, type);
273         if (view != null) {
274             view.close(true);
275         }
276     }
277 
closeOpenViews(ActivityContext activity, boolean animate, @FloatingViewType int type)278     public static void closeOpenViews(ActivityContext activity, boolean animate,
279             @FloatingViewType int type) {
280         new AbstractFloatingViewHelper().closeOpenViews(activity, animate, type);
281     }
282 
closeAllOpenViews(ActivityContext activity, boolean animate)283     public static void closeAllOpenViews(ActivityContext activity, boolean animate) {
284         closeOpenViews(activity, animate, TYPE_ALL);
285         activity.finishAutoCancelActionMode();
286     }
287 
closeAllOpenViews(ActivityContext activity)288     public static void closeAllOpenViews(ActivityContext activity) {
289         closeAllOpenViews(activity, true);
290     }
291 
closeAllOpenViewsExcept(ActivityContext activity, boolean animate, @FloatingViewType int type)292     public static void closeAllOpenViewsExcept(ActivityContext activity, boolean animate,
293                                                @FloatingViewType int type) {
294         closeOpenViews(activity, animate, TYPE_ALL & ~type);
295         activity.finishAutoCancelActionMode();
296     }
297 
closeAllOpenViewsExcept(ActivityContext activity, @FloatingViewType int type)298     public static void closeAllOpenViewsExcept(ActivityContext activity,
299                                                @FloatingViewType int type) {
300         closeAllOpenViewsExcept(activity, true, type);
301     }
302 
getTopOpenView(ActivityContext activity)303     public static AbstractFloatingView getTopOpenView(ActivityContext activity) {
304         return getTopOpenViewWithType(activity, TYPE_ALL);
305     }
306 
getTopOpenViewWithType(ActivityContext activity, @FloatingViewType int type)307     public static AbstractFloatingView getTopOpenViewWithType(ActivityContext activity,
308             @FloatingViewType int type) {
309         return getOpenView(activity, type);
310     }
311 
canInterceptEventsInSystemGestureRegion()312     public boolean canInterceptEventsInSystemGestureRegion() {
313         return false;
314     }
315 }
316