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