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