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