blob: 796fa80b71be360e4e131af9d18b9f1d3f9dee68 [file] [log] [blame]
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001/*
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
17package com.android.launcher3;
18
Sunny Goyaleaf7a952020-07-29 16:54:20 -070019import static android.animation.ValueAnimator.areAnimatorsEnabled;
Sunny Goyalde753212018-05-15 13:55:57 -070020import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED;
21import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
Tony Wickham9791bd12019-04-05 13:52:35 -070022
Sunny Goyalde753212018-05-15 13:55:57 -070023import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
24import static com.android.launcher3.compat.AccessibilityManagerCompat.sendCustomAccessibilityEvent;
25
Tony Wickham50e51652017-03-20 17:12:24 -070026import android.annotation.SuppressLint;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070027import android.content.Context;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070028import android.util.AttributeSet;
Sunny Goyalde753212018-05-15 13:55:57 -070029import android.util.Pair;
Tony Wickham50e51652017-03-20 17:12:24 -070030import android.view.MotionEvent;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070031import android.view.View;
Pinyao Ting52d02522019-09-27 16:29:32 -070032import android.view.accessibility.AccessibilityNodeInfo;
Sunny Goyalf3ac7032020-03-13 13:01:33 -070033import android.view.animation.Interpolator;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070034import android.widget.LinearLayout;
35
Tony Wickham9791bd12019-04-05 13:52:35 -070036import androidx.annotation.IntDef;
Tony Wickham9791bd12019-04-05 13:52:35 -070037
Sunny Goyalf3ac7032020-03-13 13:01:33 -070038import com.android.launcher3.anim.PendingAnimation;
Sunny Goyal37920962017-09-28 13:43:24 -070039import com.android.launcher3.util.TouchController;
Sunny Goyal87b5eb62018-07-03 15:53:39 -070040import com.android.launcher3.views.ActivityContext;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070041import com.android.launcher3.views.BaseDragLayer;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070042
43import java.lang.annotation.Retention;
44import java.lang.annotation.RetentionPolicy;
45
46/**
47 * Base class for a View which shows a floating UI on top of the launcher UI.
48 */
Fengjiang Li6bb8d792023-01-12 16:20:58 -080049public abstract class AbstractFloatingView extends LinearLayout implements TouchController,
50 OnBackPressedHandler {
Sunny Goyal740ac7f2016-09-28 16:47:32 -070051
Tony Wickham50e51652017-03-20 17:12:24 -070052 @IntDef(flag = true, value = {
53 TYPE_FOLDER,
Sunny Goyal10a1bd02017-10-09 14:56:21 -070054 TYPE_ACTION_POPUP,
Sunny Goyal37920962017-09-28 13:43:24 -070055 TYPE_WIDGETS_BOTTOM_SHEET,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070056 TYPE_WIDGET_RESIZE_FRAME,
Sunny Goyalf8088ee2017-11-10 14:52:00 -080057 TYPE_WIDGETS_FULL_SHEET,
Tony Wickham2fae2a02017-12-14 18:38:25 -080058 TYPE_ON_BOARD_POPUP,
Vadim Tryshev17839d52018-05-23 14:45:56 -070059 TYPE_DISCOVERY_BOUNCE,
Tony Wickham6a71a5b2018-08-21 11:40:23 -070060 TYPE_SNACKBAR,
Jon Mirandade0093d2019-04-16 20:53:24 -070061 TYPE_LISTENER,
Jon Miranda517cec52020-05-19 14:04:59 -070062 TYPE_ALL_APPS_EDU,
Sunny Goyal384b5782021-02-09 22:50:02 -080063 TYPE_DRAG_DROP_POPUP,
Sunny Goyal02424b22018-01-19 11:24:32 -080064 TYPE_TASK_MENU,
Sunny Goyal30ac97d2020-06-24 23:47:46 -070065 TYPE_OPTIONS_POPUP,
Alina Zaidid80cec62021-05-13 14:13:18 +010066 TYPE_ICON_SURFACE,
Sunny Goyala992ac92023-01-10 17:50:32 -080067 TYPE_OPTIONS_POPUP_DIALOG,
Alina Zaidif2c79de2021-05-27 14:55:21 +010068 TYPE_PIN_WIDGET_FROM_EXTERNAL_POPUP,
Andy Wickham13f3ffe2021-07-30 16:00:20 -100069 TYPE_WIDGETS_EDUCATION_DIALOG,
Brian Isganitis9e80fb32022-01-26 19:20:13 -050070 TYPE_TASKBAR_EDUCATION_DIALOG,
Brian Isganitis7ef4fe42022-04-07 16:29:44 -070071 TYPE_TASKBAR_ALL_APPS,
Sunny Goyala992ac92023-01-10 17:50:32 -080072 TYPE_ADD_TO_HOME_CONFIRMATION,
73 TYPE_TASKBAR_OVERLAY_PROXY
Tony Wickham50e51652017-03-20 17:12:24 -070074 })
Sunny Goyal740ac7f2016-09-28 16:47:32 -070075 @Retention(RetentionPolicy.SOURCE)
76 public @interface FloatingViewType {}
77 public static final int TYPE_FOLDER = 1 << 0;
Sunny Goyal10a1bd02017-10-09 14:56:21 -070078 public static final int TYPE_ACTION_POPUP = 1 << 1;
Tony Wickham343a77e2017-04-12 18:31:09 -070079 public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2;
Sunny Goyal37920962017-09-28 13:43:24 -070080 public static final int TYPE_WIDGET_RESIZE_FRAME = 1 << 3;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070081 public static final int TYPE_WIDGETS_FULL_SHEET = 1 << 4;
Sunny Goyal02424b22018-01-19 11:24:32 -080082 public static final int TYPE_ON_BOARD_POPUP = 1 << 5;
Vadim Tryshev17839d52018-05-23 14:45:56 -070083 public static final int TYPE_DISCOVERY_BOUNCE = 1 << 6;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070084 public static final int TYPE_SNACKBAR = 1 << 7;
Jon Mirandade0093d2019-04-16 20:53:24 -070085 public static final int TYPE_LISTENER = 1 << 8;
Jon Miranda517cec52020-05-19 14:04:59 -070086 public static final int TYPE_ALL_APPS_EDU = 1 << 9;
Sunny Goyala4647b62021-02-02 13:45:34 -080087 public static final int TYPE_DRAG_DROP_POPUP = 1 << 10;
Sunny Goyal02424b22018-01-19 11:24:32 -080088
89 // Popups related to quickstep UI
Sunny Goyala4647b62021-02-02 13:45:34 -080090 public static final int TYPE_TASK_MENU = 1 << 11;
91 public static final int TYPE_OPTIONS_POPUP = 1 << 12;
92 public static final int TYPE_ICON_SURFACE = 1 << 13;
Sunny Goyala992ac92023-01-10 17:50:32 -080093 public static final int TYPE_OPTIONS_POPUP_DIALOG = 1 << 14;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070094
Sunny Goyala992ac92023-01-10 17:50:32 -080095 public static final int TYPE_PIN_WIDGET_FROM_EXTERNAL_POPUP = 1 << 15;
96 public static final int TYPE_WIDGETS_EDUCATION_DIALOG = 1 << 16;
97 public static final int TYPE_TASKBAR_EDUCATION_DIALOG = 1 << 17;
98 public static final int TYPE_TASKBAR_ALL_APPS = 1 << 18;
99 public static final int TYPE_ADD_TO_HOME_CONFIRMATION = 1 << 19;
100 public static final int TYPE_TASKBAR_OVERLAY_PROXY = 1 << 20;
Alina Zaidid80cec62021-05-13 14:13:18 +0100101
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700102 public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800103 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET
Sunny Goyal462551b2019-02-18 14:42:47 -0800104 | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700105 | TYPE_OPTIONS_POPUP | TYPE_SNACKBAR | TYPE_LISTENER | TYPE_ALL_APPS_EDU
Alina Zaidif2c79de2021-05-27 14:55:21 +0100106 | TYPE_ICON_SURFACE | TYPE_DRAG_DROP_POPUP | TYPE_PIN_WIDGET_FROM_EXTERNAL_POPUP
Brian Isganitis7ef4fe42022-04-07 16:29:44 -0700107 | TYPE_WIDGETS_EDUCATION_DIALOG | TYPE_TASKBAR_EDUCATION_DIALOG | TYPE_TASKBAR_ALL_APPS
Brian Isganitis589c8d32022-11-17 20:08:05 +0000108 | TYPE_OPTIONS_POPUP_DIALOG | TYPE_ADD_TO_HOME_CONFIRMATION
109 | TYPE_TASKBAR_OVERLAY_PROXY;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700110
Sunny Goyal7ede6112017-12-05 15:11:21 -0800111 // Type of popups which should be kept open during launcher rebind
112 public static final int TYPE_REBIND_SAFE = TYPE_WIDGETS_FULL_SHEET
Jon Miranda517cec52020-05-19 14:04:59 -0700113 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE
Andy Wickham13f3ffe2021-07-30 16:00:20 -1000114 | TYPE_ALL_APPS_EDU | TYPE_ICON_SURFACE | TYPE_WIDGETS_EDUCATION_DIALOG
Brian Isganitis589c8d32022-11-17 20:08:05 +0000115 | TYPE_TASKBAR_EDUCATION_DIALOG | TYPE_TASKBAR_ALL_APPS | TYPE_OPTIONS_POPUP_DIALOG
116 | TYPE_TASKBAR_OVERLAY_PROXY;
Sunny Goyal7ede6112017-12-05 15:11:21 -0800117
Jon Miranda517cec52020-05-19 14:04:59 -0700118 public static final int TYPE_ACCESSIBLE = TYPE_ALL & ~TYPE_DISCOVERY_BOUNCE & ~TYPE_LISTENER
119 & ~TYPE_ALL_APPS_EDU;
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700120
Hyunyoung Songf58cf5e2018-09-19 16:06:16 -0700121 // These view all have particular operation associated with swipe down interaction.
122 public static final int TYPE_STATUS_BAR_SWIPE_DOWN_DISALLOW = TYPE_WIDGETS_BOTTOM_SHEET |
123 TYPE_WIDGETS_FULL_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_ON_BOARD_POPUP |
Sunny Goyala4647b62021-02-02 13:45:34 -0800124 TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU | TYPE_DRAG_DROP_POPUP;
Hyunyoung Songf58cf5e2018-09-19 16:06:16 -0700125
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700126 protected boolean mIsOpen;
127
128 public AbstractFloatingView(Context context, AttributeSet attrs) {
129 super(context, attrs);
130 }
131
132 public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
133 super(context, attrs, defStyleAttr);
134 }
135
Tony Wickham50e51652017-03-20 17:12:24 -0700136 /**
137 * We need to handle touch events to prevent them from falling through to the workspace below.
138 */
139 @SuppressLint("ClickableViewAccessibility")
140 @Override
141 public boolean onTouchEvent(MotionEvent ev) {
142 return true;
143 }
144
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700145 public final void close(boolean animate) {
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700146 animate &= areAnimatorsEnabled();
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700147 if (mIsOpen) {
Hyunyoung Song6b670d62020-08-22 23:24:43 -0700148 // Add to WW logging
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700149 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700150 handleClose(animate);
Jon Miranda83337f92018-04-24 12:21:28 -0700151 mIsOpen = false;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700152 }
153
154 protected abstract void handleClose(boolean animate);
155
Tony Wickham9791bd12019-04-05 13:52:35 -0700156 /**
157 * Creates a user-controlled animation to hint that the view will be closed if completed.
158 * @param distanceToMove The max distance that elements should move from their starting point.
159 */
Sunny Goyalf3ac7032020-03-13 13:01:33 -0700160 public void addHintCloseAnim(
161 float distanceToMove, Interpolator interpolator, PendingAnimation target) { }
Tony Wickham9791bd12019-04-05 13:52:35 -0700162
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700163 public final boolean isOpen() {
164 return mIsOpen;
165 }
166
167 protected abstract boolean isOfType(@FloatingViewType int type);
168
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800169 /** Return true if this view can consume back press. */
170 public boolean canHandleBack() {
Tony Wickham52c1b662018-05-21 13:13:58 -0700171 return true;
Sunny Goyal37920962017-09-28 13:43:24 -0700172 }
173
174 @Override
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800175 public void onBackInvoked() {
176 close(true);
177 }
178
179 @Override
Sunny Goyal37920962017-09-28 13:43:24 -0700180 public boolean onControllerTouchEvent(MotionEvent ev) {
181 return false;
182 }
183
Sunny Goyalde753212018-05-15 13:55:57 -0700184 protected void announceAccessibilityChanges() {
185 Pair<View, String> targetInfo = getAccessibilityTarget();
186 if (targetInfo == null || !isAccessibilityEnabled(getContext())) {
187 return;
188 }
189 sendCustomAccessibilityEvent(
190 targetInfo.first, TYPE_WINDOW_STATE_CHANGED, targetInfo.second);
191
192 if (mIsOpen) {
Andy Wickhambc83edb2020-05-27 12:58:40 -0700193 getAccessibilityInitialFocusView().performAccessibilityAction(
194 AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS, null);
Sunny Goyalde753212018-05-15 13:55:57 -0700195 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700196 ActivityContext.lookupContext(getContext()).getDragLayer()
Sunny Goyalde753212018-05-15 13:55:57 -0700197 .sendAccessibilityEvent(TYPE_WINDOW_CONTENT_CHANGED);
198 }
199
200 protected Pair<View, String> getAccessibilityTarget() {
201 return null;
202 }
203
Andy Wickhambc83edb2020-05-27 12:58:40 -0700204 /** Returns the View that Accessibility services should focus on first. */
205 protected View getAccessibilityInitialFocusView() {
206 return this;
207 }
208
Samuel Fufa82bbdac2020-03-09 18:24:47 -0700209 /**
Tony Wickhamd5e116a2021-09-14 17:48:14 -0700210 * Returns a view matching FloatingViewType and {@link #isOpen()} == true.
Samuel Fufa82bbdac2020-03-09 18:24:47 -0700211 */
212 public static <T extends AbstractFloatingView> T getOpenView(
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700213 ActivityContext activity, @FloatingViewType int type) {
Tony Wickhamd5e116a2021-09-14 17:48:14 -0700214 return getView(activity, type, true /* mustBeOpen */);
215 }
216
217 /**
Tony Wickhamf286f9c2022-04-20 14:43:39 -0700218 * Returns whether there is at least one view of the given type where {@link #isOpen()} == true.
219 */
220 public static boolean hasOpenView(ActivityContext activity, @FloatingViewType int type) {
221 return getOpenView(activity, type) != null;
222 }
223
224 /**
Tony Wickhamd5e116a2021-09-14 17:48:14 -0700225 * Returns a view matching FloatingViewType, and {@link #isOpen()} may be false (if animating
226 * closed).
227 */
228 public static <T extends AbstractFloatingView> T getAnyView(
229 ActivityContext activity, @FloatingViewType int type) {
230 return getView(activity, type, false /* mustBeOpen */);
231 }
232
233 private static <T extends AbstractFloatingView> T getView(
234 ActivityContext activity, @FloatingViewType int type, boolean mustBeOpen) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700235 BaseDragLayer dragLayer = activity.getDragLayer();
vadimt5a22ef72019-04-17 18:39:00 -0700236 if (dragLayer == null) return null;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700237 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
238 // and will be one of the last views.
239 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
240 View child = dragLayer.getChildAt(i);
241 if (child instanceof AbstractFloatingView) {
242 AbstractFloatingView view = (AbstractFloatingView) child;
Tony Wickhamd5e116a2021-09-14 17:48:14 -0700243 if (view.isOfType(type) && (!mustBeOpen || view.isOpen())) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700244 return (T) view;
245 }
246 }
247 }
248 return null;
249 }
250
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700251 public static void closeOpenContainer(ActivityContext activity,
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700252 @FloatingViewType int type) {
253 AbstractFloatingView view = getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700254 if (view != null) {
255 view.close(true);
256 }
257 }
258
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700259 public static void closeOpenViews(ActivityContext activity, boolean animate,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700260 @FloatingViewType int type) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700261 BaseDragLayer dragLayer = activity.getDragLayer();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700262 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
263 // and will be one of the last views.
264 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
265 View child = dragLayer.getChildAt(i);
266 if (child instanceof AbstractFloatingView) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700267 AbstractFloatingView abs = (AbstractFloatingView) child;
268 if (abs.isOfType(type)) {
269 abs.close(animate);
270 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700271 }
272 }
273 }
274
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700275 public static void closeAllOpenViews(ActivityContext activity, boolean animate) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700276 closeOpenViews(activity, animate, TYPE_ALL);
277 activity.finishAutoCancelActionMode();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700278 }
279
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700280 public static void closeAllOpenViews(ActivityContext activity) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700281 closeAllOpenViews(activity, true);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700282 }
283
Tracy Zhoud43e7c22018-10-16 14:49:55 -0700284 public static void closeAllOpenViewsExcept(ActivityContext activity, boolean animate,
285 @FloatingViewType int type) {
286 closeOpenViews(activity, animate, TYPE_ALL & ~type);
287 activity.finishAutoCancelActionMode();
288 }
289
290 public static void closeAllOpenViewsExcept(ActivityContext activity,
291 @FloatingViewType int type) {
292 closeAllOpenViewsExcept(activity, true, type);
293 }
294
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700295 public static AbstractFloatingView getTopOpenView(ActivityContext activity) {
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700296 return getTopOpenViewWithType(activity, TYPE_ALL);
297 }
298
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700299 public static AbstractFloatingView getTopOpenViewWithType(ActivityContext activity,
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700300 @FloatingViewType int type) {
301 return getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700302 }
vadimt34cc5f42020-06-04 13:38:17 -0700303
304 public boolean canInterceptEventsInSystemGestureRegion() {
305 return false;
306 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700307}