blob: 31f9bfea4adfd49101f44ee0b609e594ac98aea0 [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;
Fengjiang Lib7860bd2023-03-15 10:50:50 -070027import android.annotation.TargetApi;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070028import android.content.Context;
Fengjiang Lib7860bd2023-03-15 10:50:50 -070029import android.os.Build;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070030import android.util.AttributeSet;
Sunny Goyalde753212018-05-15 13:55:57 -070031import android.util.Pair;
Tony Wickham50e51652017-03-20 17:12:24 -070032import android.view.MotionEvent;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070033import android.view.View;
Pinyao Ting52d02522019-09-27 16:29:32 -070034import android.view.accessibility.AccessibilityNodeInfo;
Sunny Goyalf3ac7032020-03-13 13:01:33 -070035import android.view.animation.Interpolator;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070036import android.widget.LinearLayout;
Fengjiang Lib7860bd2023-03-15 10:50:50 -070037import android.window.OnBackAnimationCallback;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070038
Tony Wickham9791bd12019-04-05 13:52:35 -070039import androidx.annotation.IntDef;
Tony Wickham9791bd12019-04-05 13:52:35 -070040
Sunny Goyalf3ac7032020-03-13 13:01:33 -070041import com.android.launcher3.anim.PendingAnimation;
Sunny Goyal37920962017-09-28 13:43:24 -070042import com.android.launcher3.util.TouchController;
Sunny Goyal87b5eb62018-07-03 15:53:39 -070043import com.android.launcher3.views.ActivityContext;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070044import com.android.launcher3.views.BaseDragLayer;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070045
46import java.lang.annotation.Retention;
47import java.lang.annotation.RetentionPolicy;
48
49/**
50 * Base class for a View which shows a floating UI on top of the launcher UI.
51 */
Fengjiang Lib7860bd2023-03-15 10:50:50 -070052@TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
Fengjiang Li6bb8d792023-01-12 16:20:58 -080053public abstract class AbstractFloatingView extends LinearLayout implements TouchController,
Fengjiang Lib7860bd2023-03-15 10:50:50 -070054 OnBackAnimationCallback {
Sunny Goyal740ac7f2016-09-28 16:47:32 -070055
Tony Wickham50e51652017-03-20 17:12:24 -070056 @IntDef(flag = true, value = {
57 TYPE_FOLDER,
Sunny Goyal10a1bd02017-10-09 14:56:21 -070058 TYPE_ACTION_POPUP,
Sunny Goyal37920962017-09-28 13:43:24 -070059 TYPE_WIDGETS_BOTTOM_SHEET,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070060 TYPE_WIDGET_RESIZE_FRAME,
Sunny Goyalf8088ee2017-11-10 14:52:00 -080061 TYPE_WIDGETS_FULL_SHEET,
Tony Wickham2fae2a02017-12-14 18:38:25 -080062 TYPE_ON_BOARD_POPUP,
Vadim Tryshev17839d52018-05-23 14:45:56 -070063 TYPE_DISCOVERY_BOUNCE,
Tony Wickham6a71a5b2018-08-21 11:40:23 -070064 TYPE_SNACKBAR,
Jon Mirandade0093d2019-04-16 20:53:24 -070065 TYPE_LISTENER,
Jon Miranda517cec52020-05-19 14:04:59 -070066 TYPE_ALL_APPS_EDU,
Sunny Goyal384b5782021-02-09 22:50:02 -080067 TYPE_DRAG_DROP_POPUP,
Sunny Goyal02424b22018-01-19 11:24:32 -080068 TYPE_TASK_MENU,
Sunny Goyal30ac97d2020-06-24 23:47:46 -070069 TYPE_OPTIONS_POPUP,
Alina Zaidid80cec62021-05-13 14:13:18 +010070 TYPE_ICON_SURFACE,
Sunny Goyala992ac92023-01-10 17:50:32 -080071 TYPE_OPTIONS_POPUP_DIALOG,
Alina Zaidif2c79de2021-05-27 14:55:21 +010072 TYPE_PIN_WIDGET_FROM_EXTERNAL_POPUP,
Andy Wickham13f3ffe2021-07-30 16:00:20 -100073 TYPE_WIDGETS_EDUCATION_DIALOG,
Brian Isganitis9e80fb32022-01-26 19:20:13 -050074 TYPE_TASKBAR_EDUCATION_DIALOG,
Brian Isganitis7ef4fe42022-04-07 16:29:44 -070075 TYPE_TASKBAR_ALL_APPS,
Sunny Goyala992ac92023-01-10 17:50:32 -080076 TYPE_ADD_TO_HOME_CONFIRMATION,
Jagrut Desaic6d625b2023-04-03 16:57:32 -070077 TYPE_TASKBAR_OVERLAY_PROXY,
78 TYPE_TASKBAR_PINNING_POPUP
Tony Wickham50e51652017-03-20 17:12:24 -070079 })
Sunny Goyal740ac7f2016-09-28 16:47:32 -070080 @Retention(RetentionPolicy.SOURCE)
81 public @interface FloatingViewType {}
82 public static final int TYPE_FOLDER = 1 << 0;
Sunny Goyal10a1bd02017-10-09 14:56:21 -070083 public static final int TYPE_ACTION_POPUP = 1 << 1;
Tony Wickham343a77e2017-04-12 18:31:09 -070084 public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2;
Sunny Goyal37920962017-09-28 13:43:24 -070085 public static final int TYPE_WIDGET_RESIZE_FRAME = 1 << 3;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070086 public static final int TYPE_WIDGETS_FULL_SHEET = 1 << 4;
Sunny Goyal02424b22018-01-19 11:24:32 -080087 public static final int TYPE_ON_BOARD_POPUP = 1 << 5;
Vadim Tryshev17839d52018-05-23 14:45:56 -070088 public static final int TYPE_DISCOVERY_BOUNCE = 1 << 6;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070089 public static final int TYPE_SNACKBAR = 1 << 7;
Jon Mirandade0093d2019-04-16 20:53:24 -070090 public static final int TYPE_LISTENER = 1 << 8;
Jon Miranda517cec52020-05-19 14:04:59 -070091 public static final int TYPE_ALL_APPS_EDU = 1 << 9;
Sunny Goyala4647b62021-02-02 13:45:34 -080092 public static final int TYPE_DRAG_DROP_POPUP = 1 << 10;
Sunny Goyal02424b22018-01-19 11:24:32 -080093
94 // Popups related to quickstep UI
Sunny Goyala4647b62021-02-02 13:45:34 -080095 public static final int TYPE_TASK_MENU = 1 << 11;
96 public static final int TYPE_OPTIONS_POPUP = 1 << 12;
97 public static final int TYPE_ICON_SURFACE = 1 << 13;
Sunny Goyala992ac92023-01-10 17:50:32 -080098 public static final int TYPE_OPTIONS_POPUP_DIALOG = 1 << 14;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070099
Sunny Goyala992ac92023-01-10 17:50:32 -0800100 public static final int TYPE_PIN_WIDGET_FROM_EXTERNAL_POPUP = 1 << 15;
101 public static final int TYPE_WIDGETS_EDUCATION_DIALOG = 1 << 16;
102 public static final int TYPE_TASKBAR_EDUCATION_DIALOG = 1 << 17;
103 public static final int TYPE_TASKBAR_ALL_APPS = 1 << 18;
104 public static final int TYPE_ADD_TO_HOME_CONFIRMATION = 1 << 19;
105 public static final int TYPE_TASKBAR_OVERLAY_PROXY = 1 << 20;
Jagrut Desaic6d625b2023-04-03 16:57:32 -0700106 public static final int TYPE_TASKBAR_PINNING_POPUP = 1 << 21;
Alina Zaidid80cec62021-05-13 14:13:18 +0100107
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700108 public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP
Sunny Goyalf8088ee2017-11-10 14:52:00 -0800109 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET
Sunny Goyal462551b2019-02-18 14:42:47 -0800110 | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700111 | TYPE_OPTIONS_POPUP | TYPE_SNACKBAR | TYPE_LISTENER | TYPE_ALL_APPS_EDU
Alina Zaidif2c79de2021-05-27 14:55:21 +0100112 | TYPE_ICON_SURFACE | TYPE_DRAG_DROP_POPUP | TYPE_PIN_WIDGET_FROM_EXTERNAL_POPUP
Brian Isganitis7ef4fe42022-04-07 16:29:44 -0700113 | TYPE_WIDGETS_EDUCATION_DIALOG | TYPE_TASKBAR_EDUCATION_DIALOG | TYPE_TASKBAR_ALL_APPS
Brian Isganitis589c8d32022-11-17 20:08:05 +0000114 | TYPE_OPTIONS_POPUP_DIALOG | TYPE_ADD_TO_HOME_CONFIRMATION
Jagrut Desaic6d625b2023-04-03 16:57:32 -0700115 | TYPE_TASKBAR_OVERLAY_PROXY | TYPE_TASKBAR_PINNING_POPUP;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700116
Sunny Goyal7ede6112017-12-05 15:11:21 -0800117 // Type of popups which should be kept open during launcher rebind
118 public static final int TYPE_REBIND_SAFE = TYPE_WIDGETS_FULL_SHEET
Jon Miranda517cec52020-05-19 14:04:59 -0700119 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE
Andy Wickham13f3ffe2021-07-30 16:00:20 -1000120 | TYPE_ALL_APPS_EDU | TYPE_ICON_SURFACE | TYPE_WIDGETS_EDUCATION_DIALOG
Brian Isganitis589c8d32022-11-17 20:08:05 +0000121 | TYPE_TASKBAR_EDUCATION_DIALOG | TYPE_TASKBAR_ALL_APPS | TYPE_OPTIONS_POPUP_DIALOG
122 | TYPE_TASKBAR_OVERLAY_PROXY;
Sunny Goyal7ede6112017-12-05 15:11:21 -0800123
Jon Miranda517cec52020-05-19 14:04:59 -0700124 public static final int TYPE_ACCESSIBLE = TYPE_ALL & ~TYPE_DISCOVERY_BOUNCE & ~TYPE_LISTENER
125 & ~TYPE_ALL_APPS_EDU;
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700126
Hyunyoung Songf58cf5e2018-09-19 16:06:16 -0700127 // These view all have particular operation associated with swipe down interaction.
128 public static final int TYPE_STATUS_BAR_SWIPE_DOWN_DISALLOW = TYPE_WIDGETS_BOTTOM_SHEET |
129 TYPE_WIDGETS_FULL_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_ON_BOARD_POPUP |
Sunny Goyala4647b62021-02-02 13:45:34 -0800130 TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU | TYPE_DRAG_DROP_POPUP;
Hyunyoung Songf58cf5e2018-09-19 16:06:16 -0700131
Brian Isganitise41faf52023-04-12 14:16:25 -0400132 // Floating views that are exclusive to the taskbar overlay window.
133 public static final int TYPE_TASKBAR_OVERLAYS =
134 TYPE_TASKBAR_ALL_APPS | TYPE_TASKBAR_EDUCATION_DIALOG;
135
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700136 protected boolean mIsOpen;
137
138 public AbstractFloatingView(Context context, AttributeSet attrs) {
139 super(context, attrs);
140 }
141
142 public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
143 super(context, attrs, defStyleAttr);
144 }
145
Tony Wickham50e51652017-03-20 17:12:24 -0700146 /**
147 * We need to handle touch events to prevent them from falling through to the workspace below.
148 */
149 @SuppressLint("ClickableViewAccessibility")
150 @Override
151 public boolean onTouchEvent(MotionEvent ev) {
152 return true;
153 }
154
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700155 public final void close(boolean animate) {
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700156 animate &= areAnimatorsEnabled();
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700157 if (mIsOpen) {
Hyunyoung Song6b670d62020-08-22 23:24:43 -0700158 // Add to WW logging
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700159 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700160 handleClose(animate);
Jon Miranda83337f92018-04-24 12:21:28 -0700161 mIsOpen = false;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700162 }
163
164 protected abstract void handleClose(boolean animate);
165
Tony Wickham9791bd12019-04-05 13:52:35 -0700166 /**
167 * Creates a user-controlled animation to hint that the view will be closed if completed.
168 * @param distanceToMove The max distance that elements should move from their starting point.
169 */
Sunny Goyalf3ac7032020-03-13 13:01:33 -0700170 public void addHintCloseAnim(
171 float distanceToMove, Interpolator interpolator, PendingAnimation target) { }
Tony Wickham9791bd12019-04-05 13:52:35 -0700172
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700173 public final boolean isOpen() {
174 return mIsOpen;
175 }
176
177 protected abstract boolean isOfType(@FloatingViewType int type);
178
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800179 /** Return true if this view can consume back press. */
180 public boolean canHandleBack() {
Tony Wickham52c1b662018-05-21 13:13:58 -0700181 return true;
Sunny Goyal37920962017-09-28 13:43:24 -0700182 }
183
184 @Override
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800185 public void onBackInvoked() {
186 close(true);
187 }
188
189 @Override
Sunny Goyal37920962017-09-28 13:43:24 -0700190 public boolean onControllerTouchEvent(MotionEvent ev) {
191 return false;
192 }
193
Sunny Goyalde753212018-05-15 13:55:57 -0700194 protected void announceAccessibilityChanges() {
195 Pair<View, String> targetInfo = getAccessibilityTarget();
196 if (targetInfo == null || !isAccessibilityEnabled(getContext())) {
197 return;
198 }
199 sendCustomAccessibilityEvent(
200 targetInfo.first, TYPE_WINDOW_STATE_CHANGED, targetInfo.second);
201
202 if (mIsOpen) {
Andy Wickhambc83edb2020-05-27 12:58:40 -0700203 getAccessibilityInitialFocusView().performAccessibilityAction(
204 AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS, null);
Sunny Goyalde753212018-05-15 13:55:57 -0700205 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700206 ActivityContext.lookupContext(getContext()).getDragLayer()
Sunny Goyalde753212018-05-15 13:55:57 -0700207 .sendAccessibilityEvent(TYPE_WINDOW_CONTENT_CHANGED);
208 }
209
210 protected Pair<View, String> getAccessibilityTarget() {
211 return null;
212 }
213
Andy Wickhambc83edb2020-05-27 12:58:40 -0700214 /** Returns the View that Accessibility services should focus on first. */
215 protected View getAccessibilityInitialFocusView() {
216 return this;
217 }
218
Samuel Fufa82bbdac2020-03-09 18:24:47 -0700219 /**
Tony Wickhamd5e116a2021-09-14 17:48:14 -0700220 * Returns a view matching FloatingViewType and {@link #isOpen()} == true.
Samuel Fufa82bbdac2020-03-09 18:24:47 -0700221 */
222 public static <T extends AbstractFloatingView> T getOpenView(
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700223 ActivityContext activity, @FloatingViewType int type) {
Tony Wickhamd5e116a2021-09-14 17:48:14 -0700224 return getView(activity, type, true /* mustBeOpen */);
225 }
226
227 /**
Tony Wickhamf286f9c2022-04-20 14:43:39 -0700228 * Returns whether there is at least one view of the given type where {@link #isOpen()} == true.
229 */
230 public static boolean hasOpenView(ActivityContext activity, @FloatingViewType int type) {
231 return getOpenView(activity, type) != null;
232 }
233
234 /**
Tony Wickhamd5e116a2021-09-14 17:48:14 -0700235 * Returns a view matching FloatingViewType, and {@link #isOpen()} may be false (if animating
236 * closed).
237 */
238 public static <T extends AbstractFloatingView> T getAnyView(
239 ActivityContext activity, @FloatingViewType int type) {
240 return getView(activity, type, false /* mustBeOpen */);
241 }
242
243 private static <T extends AbstractFloatingView> T getView(
244 ActivityContext activity, @FloatingViewType int type, boolean mustBeOpen) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700245 BaseDragLayer dragLayer = activity.getDragLayer();
vadimt5a22ef72019-04-17 18:39:00 -0700246 if (dragLayer == null) return null;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700247 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
248 // and will be one of the last views.
249 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
250 View child = dragLayer.getChildAt(i);
251 if (child instanceof AbstractFloatingView) {
252 AbstractFloatingView view = (AbstractFloatingView) child;
Tony Wickhamd5e116a2021-09-14 17:48:14 -0700253 if (view.isOfType(type) && (!mustBeOpen || view.isOpen())) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700254 return (T) view;
255 }
256 }
257 }
258 return null;
259 }
260
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700261 public static void closeOpenContainer(ActivityContext activity,
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700262 @FloatingViewType int type) {
263 AbstractFloatingView view = getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700264 if (view != null) {
265 view.close(true);
266 }
267 }
268
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700269 public static void closeOpenViews(ActivityContext activity, boolean animate,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700270 @FloatingViewType int type) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700271 BaseDragLayer dragLayer = activity.getDragLayer();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700272 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
273 // and will be one of the last views.
274 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
275 View child = dragLayer.getChildAt(i);
276 if (child instanceof AbstractFloatingView) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700277 AbstractFloatingView abs = (AbstractFloatingView) child;
278 if (abs.isOfType(type)) {
279 abs.close(animate);
280 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700281 }
282 }
283 }
284
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700285 public static void closeAllOpenViews(ActivityContext activity, boolean animate) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700286 closeOpenViews(activity, animate, TYPE_ALL);
287 activity.finishAutoCancelActionMode();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700288 }
289
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700290 public static void closeAllOpenViews(ActivityContext activity) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700291 closeAllOpenViews(activity, true);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700292 }
293
Tracy Zhoud43e7c22018-10-16 14:49:55 -0700294 public static void closeAllOpenViewsExcept(ActivityContext activity, boolean animate,
295 @FloatingViewType int type) {
296 closeOpenViews(activity, animate, TYPE_ALL & ~type);
297 activity.finishAutoCancelActionMode();
298 }
299
300 public static void closeAllOpenViewsExcept(ActivityContext activity,
301 @FloatingViewType int type) {
302 closeAllOpenViewsExcept(activity, true, type);
303 }
304
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700305 public static AbstractFloatingView getTopOpenView(ActivityContext activity) {
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700306 return getTopOpenViewWithType(activity, TYPE_ALL);
307 }
308
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700309 public static AbstractFloatingView getTopOpenViewWithType(ActivityContext activity,
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700310 @FloatingViewType int type) {
311 return getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700312 }
vadimt34cc5f42020-06-04 13:38:17 -0700313
314 public boolean canInterceptEventsInSystemGestureRegion() {
315 return false;
316 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700317}