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