blob: ceb38d0e779b5b41072121b9688168335cec85fc [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 */
Sunny Goyal37920962017-09-28 13:43:24 -070049public abstract class AbstractFloatingView extends LinearLayout implements TouchController {
Sunny Goyal740ac7f2016-09-28 16:47:32 -070050
Tony Wickham50e51652017-03-20 17:12:24 -070051 @IntDef(flag = true, value = {
52 TYPE_FOLDER,
Sunny Goyal10a1bd02017-10-09 14:56:21 -070053 TYPE_ACTION_POPUP,
Sunny Goyal37920962017-09-28 13:43:24 -070054 TYPE_WIDGETS_BOTTOM_SHEET,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070055 TYPE_WIDGET_RESIZE_FRAME,
Sunny Goyalf8088ee2017-11-10 14:52:00 -080056 TYPE_WIDGETS_FULL_SHEET,
Tony Wickham2fae2a02017-12-14 18:38:25 -080057 TYPE_ON_BOARD_POPUP,
Vadim Tryshev17839d52018-05-23 14:45:56 -070058 TYPE_DISCOVERY_BOUNCE,
Tony Wickham6a71a5b2018-08-21 11:40:23 -070059 TYPE_SNACKBAR,
Jon Mirandade0093d2019-04-16 20:53:24 -070060 TYPE_LISTENER,
Jon Miranda517cec52020-05-19 14:04:59 -070061 TYPE_ALL_APPS_EDU,
Sunny Goyal384b5782021-02-09 22:50:02 -080062 TYPE_DRAG_DROP_POPUP,
Sunny Goyal02424b22018-01-19 11:24:32 -080063 TYPE_TASK_MENU,
Sunny Goyal30ac97d2020-06-24 23:47:46 -070064 TYPE_OPTIONS_POPUP,
Alina Zaidid80cec62021-05-13 14:13:18 +010065 TYPE_ICON_SURFACE,
Alina Zaidif2c79de2021-05-27 14:55:21 +010066 TYPE_PIN_WIDGET_FROM_EXTERNAL_POPUP,
Andy Wickham13f3ffe2021-07-30 16:00:20 -100067 TYPE_WIDGETS_EDUCATION_DIALOG,
Brian Isganitis9e80fb32022-01-26 19:20:13 -050068 TYPE_TASKBAR_EDUCATION_DIALOG,
69 TYPE_TASKBAR_ALL_APPS
Tony Wickham50e51652017-03-20 17:12:24 -070070 })
Sunny Goyal740ac7f2016-09-28 16:47:32 -070071 @Retention(RetentionPolicy.SOURCE)
72 public @interface FloatingViewType {}
73 public static final int TYPE_FOLDER = 1 << 0;
Sunny Goyal10a1bd02017-10-09 14:56:21 -070074 public static final int TYPE_ACTION_POPUP = 1 << 1;
Tony Wickham343a77e2017-04-12 18:31:09 -070075 public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2;
Sunny Goyal37920962017-09-28 13:43:24 -070076 public static final int TYPE_WIDGET_RESIZE_FRAME = 1 << 3;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070077 public static final int TYPE_WIDGETS_FULL_SHEET = 1 << 4;
Sunny Goyal02424b22018-01-19 11:24:32 -080078 public static final int TYPE_ON_BOARD_POPUP = 1 << 5;
Vadim Tryshev17839d52018-05-23 14:45:56 -070079 public static final int TYPE_DISCOVERY_BOUNCE = 1 << 6;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070080 public static final int TYPE_SNACKBAR = 1 << 7;
Jon Mirandade0093d2019-04-16 20:53:24 -070081 public static final int TYPE_LISTENER = 1 << 8;
Jon Miranda517cec52020-05-19 14:04:59 -070082 public static final int TYPE_ALL_APPS_EDU = 1 << 9;
Sunny Goyala4647b62021-02-02 13:45:34 -080083 public static final int TYPE_DRAG_DROP_POPUP = 1 << 10;
Sunny Goyal02424b22018-01-19 11:24:32 -080084
85 // Popups related to quickstep UI
Sunny Goyala4647b62021-02-02 13:45:34 -080086 public static final int TYPE_TASK_MENU = 1 << 11;
87 public static final int TYPE_OPTIONS_POPUP = 1 << 12;
88 public static final int TYPE_ICON_SURFACE = 1 << 13;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070089
Alina Zaidid80cec62021-05-13 14:13:18 +010090 public static final int TYPE_PIN_WIDGET_FROM_EXTERNAL_POPUP = 1 << 14;
Alina Zaidif2c79de2021-05-27 14:55:21 +010091 public static final int TYPE_WIDGETS_EDUCATION_DIALOG = 1 << 15;
Andy Wickham13f3ffe2021-07-30 16:00:20 -100092 public static final int TYPE_TASKBAR_EDUCATION_DIALOG = 1 << 16;
Brian Isganitis9e80fb32022-01-26 19:20:13 -050093 public static final int TYPE_TASKBAR_ALL_APPS = 1 << 17;
Alina Zaidid80cec62021-05-13 14:13:18 +010094
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070095 public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP
Sunny Goyalf8088ee2017-11-10 14:52:00 -080096 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET
Sunny Goyal462551b2019-02-18 14:42:47 -080097 | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU
Sunny Goyal30ac97d2020-06-24 23:47:46 -070098 | TYPE_OPTIONS_POPUP | TYPE_SNACKBAR | TYPE_LISTENER | TYPE_ALL_APPS_EDU
Alina Zaidif2c79de2021-05-27 14:55:21 +010099 | TYPE_ICON_SURFACE | TYPE_DRAG_DROP_POPUP | TYPE_PIN_WIDGET_FROM_EXTERNAL_POPUP
Brian Isganitis9e80fb32022-01-26 19:20:13 -0500100 | TYPE_WIDGETS_EDUCATION_DIALOG | TYPE_TASKBAR_EDUCATION_DIALOG | TYPE_TASKBAR_ALL_APPS;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700101
Sunny Goyal7ede6112017-12-05 15:11:21 -0800102 // Type of popups which should be kept open during launcher rebind
103 public static final int TYPE_REBIND_SAFE = TYPE_WIDGETS_FULL_SHEET
Jon Miranda517cec52020-05-19 14:04:59 -0700104 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE
Andy Wickham13f3ffe2021-07-30 16:00:20 -1000105 | TYPE_ALL_APPS_EDU | TYPE_ICON_SURFACE | TYPE_WIDGETS_EDUCATION_DIALOG
Brian Isganitis9e80fb32022-01-26 19:20:13 -0500106 | TYPE_TASKBAR_EDUCATION_DIALOG | TYPE_TASKBAR_ALL_APPS;
Sunny Goyal7ede6112017-12-05 15:11:21 -0800107
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700108 // Usually we show the back button when a floating view is open. Instead, hide for these types.
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700109 public static final int TYPE_HIDE_BACK_BUTTON = TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE
Tonyf941f842019-05-13 11:15:54 -0500110 | TYPE_SNACKBAR | TYPE_WIDGET_RESIZE_FRAME | TYPE_LISTENER;
Vadim Tryshev17839d52018-05-23 14:45:56 -0700111
Jon Miranda517cec52020-05-19 14:04:59 -0700112 public static final int TYPE_ACCESSIBLE = TYPE_ALL & ~TYPE_DISCOVERY_BOUNCE & ~TYPE_LISTENER
113 & ~TYPE_ALL_APPS_EDU;
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700114
Hyunyoung Songf58cf5e2018-09-19 16:06:16 -0700115 // These view all have particular operation associated with swipe down interaction.
116 public static final int TYPE_STATUS_BAR_SWIPE_DOWN_DISALLOW = TYPE_WIDGETS_BOTTOM_SHEET |
117 TYPE_WIDGETS_FULL_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_ON_BOARD_POPUP |
Sunny Goyala4647b62021-02-02 13:45:34 -0800118 TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU | TYPE_DRAG_DROP_POPUP;
Hyunyoung Songf58cf5e2018-09-19 16:06:16 -0700119
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700120 protected boolean mIsOpen;
121
122 public AbstractFloatingView(Context context, AttributeSet attrs) {
123 super(context, attrs);
124 }
125
126 public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
127 super(context, attrs, defStyleAttr);
128 }
129
Tony Wickham50e51652017-03-20 17:12:24 -0700130 /**
131 * We need to handle touch events to prevent them from falling through to the workspace below.
132 */
133 @SuppressLint("ClickableViewAccessibility")
134 @Override
135 public boolean onTouchEvent(MotionEvent ev) {
136 return true;
137 }
138
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700139 public final void close(boolean animate) {
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700140 animate &= areAnimatorsEnabled();
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700141 if (mIsOpen) {
Hyunyoung Song6b670d62020-08-22 23:24:43 -0700142 // Add to WW logging
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700143 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700144 handleClose(animate);
Jon Miranda83337f92018-04-24 12:21:28 -0700145 mIsOpen = false;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700146 }
147
148 protected abstract void handleClose(boolean animate);
149
Tony Wickham9791bd12019-04-05 13:52:35 -0700150 /**
151 * Creates a user-controlled animation to hint that the view will be closed if completed.
152 * @param distanceToMove The max distance that elements should move from their starting point.
153 */
Sunny Goyalf3ac7032020-03-13 13:01:33 -0700154 public void addHintCloseAnim(
155 float distanceToMove, Interpolator interpolator, PendingAnimation target) { }
Tony Wickham9791bd12019-04-05 13:52:35 -0700156
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700157 public final boolean isOpen() {
158 return mIsOpen;
159 }
160
161 protected abstract boolean isOfType(@FloatingViewType int type);
162
Tony Wickham52c1b662018-05-21 13:13:58 -0700163 /** @return Whether the back is consumed. If false, Launcher will handle the back as well. */
164 public boolean onBackPressed() {
Sunny Goyal37920962017-09-28 13:43:24 -0700165 close(true);
Tony Wickham52c1b662018-05-21 13:13:58 -0700166 return true;
Sunny Goyal37920962017-09-28 13:43:24 -0700167 }
168
169 @Override
170 public boolean onControllerTouchEvent(MotionEvent ev) {
171 return false;
172 }
173
Sunny Goyalde753212018-05-15 13:55:57 -0700174 protected void announceAccessibilityChanges() {
175 Pair<View, String> targetInfo = getAccessibilityTarget();
176 if (targetInfo == null || !isAccessibilityEnabled(getContext())) {
177 return;
178 }
179 sendCustomAccessibilityEvent(
180 targetInfo.first, TYPE_WINDOW_STATE_CHANGED, targetInfo.second);
181
182 if (mIsOpen) {
Andy Wickhambc83edb2020-05-27 12:58:40 -0700183 getAccessibilityInitialFocusView().performAccessibilityAction(
184 AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS, null);
Sunny Goyalde753212018-05-15 13:55:57 -0700185 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700186 ActivityContext.lookupContext(getContext()).getDragLayer()
Sunny Goyalde753212018-05-15 13:55:57 -0700187 .sendAccessibilityEvent(TYPE_WINDOW_CONTENT_CHANGED);
188 }
189
190 protected Pair<View, String> getAccessibilityTarget() {
191 return null;
192 }
193
Andy Wickhambc83edb2020-05-27 12:58:40 -0700194 /** Returns the View that Accessibility services should focus on first. */
195 protected View getAccessibilityInitialFocusView() {
196 return this;
197 }
198
Samuel Fufa82bbdac2020-03-09 18:24:47 -0700199 /**
Tony Wickhamd5e116a2021-09-14 17:48:14 -0700200 * Returns a view matching FloatingViewType and {@link #isOpen()} == true.
Samuel Fufa82bbdac2020-03-09 18:24:47 -0700201 */
202 public static <T extends AbstractFloatingView> T getOpenView(
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700203 ActivityContext activity, @FloatingViewType int type) {
Tony Wickhamd5e116a2021-09-14 17:48:14 -0700204 return getView(activity, type, true /* mustBeOpen */);
205 }
206
207 /**
208 * Returns a view matching FloatingViewType, and {@link #isOpen()} may be false (if animating
209 * closed).
210 */
211 public static <T extends AbstractFloatingView> T getAnyView(
212 ActivityContext activity, @FloatingViewType int type) {
213 return getView(activity, type, false /* mustBeOpen */);
214 }
215
216 private static <T extends AbstractFloatingView> T getView(
217 ActivityContext activity, @FloatingViewType int type, boolean mustBeOpen) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700218 BaseDragLayer dragLayer = activity.getDragLayer();
vadimt5a22ef72019-04-17 18:39:00 -0700219 if (dragLayer == null) return null;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700220 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
221 // and will be one of the last views.
222 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
223 View child = dragLayer.getChildAt(i);
224 if (child instanceof AbstractFloatingView) {
225 AbstractFloatingView view = (AbstractFloatingView) child;
Tony Wickhamd5e116a2021-09-14 17:48:14 -0700226 if (view.isOfType(type) && (!mustBeOpen || view.isOpen())) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700227 return (T) view;
228 }
229 }
230 }
231 return null;
232 }
233
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700234 public static void closeOpenContainer(ActivityContext activity,
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700235 @FloatingViewType int type) {
236 AbstractFloatingView view = getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700237 if (view != null) {
238 view.close(true);
239 }
240 }
241
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700242 public static void closeOpenViews(ActivityContext activity, boolean animate,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700243 @FloatingViewType int type) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700244 BaseDragLayer dragLayer = activity.getDragLayer();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700245 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
246 // and will be one of the last views.
247 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
248 View child = dragLayer.getChildAt(i);
249 if (child instanceof AbstractFloatingView) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700250 AbstractFloatingView abs = (AbstractFloatingView) child;
251 if (abs.isOfType(type)) {
252 abs.close(animate);
253 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700254 }
255 }
256 }
257
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700258 public static void closeAllOpenViews(ActivityContext activity, boolean animate) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700259 closeOpenViews(activity, animate, TYPE_ALL);
260 activity.finishAutoCancelActionMode();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700261 }
262
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700263 public static void closeAllOpenViews(ActivityContext activity) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700264 closeAllOpenViews(activity, true);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700265 }
266
Tracy Zhoud43e7c22018-10-16 14:49:55 -0700267 public static void closeAllOpenViewsExcept(ActivityContext activity, boolean animate,
268 @FloatingViewType int type) {
269 closeOpenViews(activity, animate, TYPE_ALL & ~type);
270 activity.finishAutoCancelActionMode();
271 }
272
273 public static void closeAllOpenViewsExcept(ActivityContext activity,
274 @FloatingViewType int type) {
275 closeAllOpenViewsExcept(activity, true, type);
276 }
277
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700278 public static AbstractFloatingView getTopOpenView(ActivityContext activity) {
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700279 return getTopOpenViewWithType(activity, TYPE_ALL);
280 }
281
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700282 public static AbstractFloatingView getTopOpenViewWithType(ActivityContext activity,
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700283 @FloatingViewType int type) {
284 return getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700285 }
vadimt34cc5f42020-06-04 13:38:17 -0700286
287 public boolean canInterceptEventsInSystemGestureRegion() {
288 return false;
289 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700290}