blob: b3952ca1bc29b70e44db5bcc55763cce38dfe51c [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;
Jon Miranda0b6fa712021-05-05 15:36:27 -070042import com.android.launcher3.widget.LocalColorExtractor;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070043
44import java.lang.annotation.Retention;
45import java.lang.annotation.RetentionPolicy;
46
47/**
48 * Base class for a View which shows a floating UI on top of the launcher UI.
49 */
Sunny Goyal37920962017-09-28 13:43:24 -070050public abstract class AbstractFloatingView extends LinearLayout implements TouchController {
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,
67 TYPE_PIN_WIDGET_FROM_EXTERNAL_POPUP
Tony Wickham50e51652017-03-20 17:12:24 -070068 })
Sunny Goyal740ac7f2016-09-28 16:47:32 -070069 @Retention(RetentionPolicy.SOURCE)
70 public @interface FloatingViewType {}
71 public static final int TYPE_FOLDER = 1 << 0;
Sunny Goyal10a1bd02017-10-09 14:56:21 -070072 public static final int TYPE_ACTION_POPUP = 1 << 1;
Tony Wickham343a77e2017-04-12 18:31:09 -070073 public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2;
Sunny Goyal37920962017-09-28 13:43:24 -070074 public static final int TYPE_WIDGET_RESIZE_FRAME = 1 << 3;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070075 public static final int TYPE_WIDGETS_FULL_SHEET = 1 << 4;
Sunny Goyal02424b22018-01-19 11:24:32 -080076 public static final int TYPE_ON_BOARD_POPUP = 1 << 5;
Vadim Tryshev17839d52018-05-23 14:45:56 -070077 public static final int TYPE_DISCOVERY_BOUNCE = 1 << 6;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070078 public static final int TYPE_SNACKBAR = 1 << 7;
Jon Mirandade0093d2019-04-16 20:53:24 -070079 public static final int TYPE_LISTENER = 1 << 8;
Jon Miranda517cec52020-05-19 14:04:59 -070080 public static final int TYPE_ALL_APPS_EDU = 1 << 9;
Sunny Goyala4647b62021-02-02 13:45:34 -080081 public static final int TYPE_DRAG_DROP_POPUP = 1 << 10;
Sunny Goyal02424b22018-01-19 11:24:32 -080082
83 // Popups related to quickstep UI
Sunny Goyala4647b62021-02-02 13:45:34 -080084 public static final int TYPE_TASK_MENU = 1 << 11;
85 public static final int TYPE_OPTIONS_POPUP = 1 << 12;
86 public static final int TYPE_ICON_SURFACE = 1 << 13;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070087
Alina Zaidid80cec62021-05-13 14:13:18 +010088 public static final int TYPE_PIN_WIDGET_FROM_EXTERNAL_POPUP = 1 << 14;
89
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070090 public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP
Sunny Goyalf8088ee2017-11-10 14:52:00 -080091 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET
Sunny Goyal462551b2019-02-18 14:42:47 -080092 | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU
Sunny Goyal30ac97d2020-06-24 23:47:46 -070093 | TYPE_OPTIONS_POPUP | TYPE_SNACKBAR | TYPE_LISTENER | TYPE_ALL_APPS_EDU
Alina Zaidid80cec62021-05-13 14:13:18 +010094 | TYPE_ICON_SURFACE | TYPE_DRAG_DROP_POPUP | TYPE_PIN_WIDGET_FROM_EXTERNAL_POPUP;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070095
Sunny Goyal7ede6112017-12-05 15:11:21 -080096 // Type of popups which should be kept open during launcher rebind
97 public static final int TYPE_REBIND_SAFE = TYPE_WIDGETS_FULL_SHEET
Jon Miranda517cec52020-05-19 14:04:59 -070098 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE
Sunny Goyal30ac97d2020-06-24 23:47:46 -070099 | TYPE_ALL_APPS_EDU | TYPE_ICON_SURFACE;
Sunny Goyal7ede6112017-12-05 15:11:21 -0800100
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700101 // Usually we show the back button when a floating view is open. Instead, hide for these types.
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700102 public static final int TYPE_HIDE_BACK_BUTTON = TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE
Tonyf941f842019-05-13 11:15:54 -0500103 | TYPE_SNACKBAR | TYPE_WIDGET_RESIZE_FRAME | TYPE_LISTENER;
Vadim Tryshev17839d52018-05-23 14:45:56 -0700104
Jon Miranda517cec52020-05-19 14:04:59 -0700105 public static final int TYPE_ACCESSIBLE = TYPE_ALL & ~TYPE_DISCOVERY_BOUNCE & ~TYPE_LISTENER
106 & ~TYPE_ALL_APPS_EDU;
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700107
Hyunyoung Songf58cf5e2018-09-19 16:06:16 -0700108 // These view all have particular operation associated with swipe down interaction.
109 public static final int TYPE_STATUS_BAR_SWIPE_DOWN_DISALLOW = TYPE_WIDGETS_BOTTOM_SHEET |
110 TYPE_WIDGETS_FULL_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_ON_BOARD_POPUP |
Sunny Goyala4647b62021-02-02 13:45:34 -0800111 TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU | TYPE_DRAG_DROP_POPUP;
Hyunyoung Songf58cf5e2018-09-19 16:06:16 -0700112
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700113 protected boolean mIsOpen;
114
Jon Miranda0b6fa712021-05-05 15:36:27 -0700115 // Index used to get background color when using local wallpaper color extraction.
116 protected int mColorExtractionIndex;
117
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700118 public AbstractFloatingView(Context context, AttributeSet attrs) {
119 super(context, attrs);
Jon Miranda0b6fa712021-05-05 15:36:27 -0700120 init(context);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700121 }
122
123 public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
124 super(context, attrs, defStyleAttr);
Jon Miranda0b6fa712021-05-05 15:36:27 -0700125 init(context);
126 }
127
128 private void init(Context context) {
129 mColorExtractionIndex = LocalColorExtractor.getColorIndex(context);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700130 }
131
Tony Wickham50e51652017-03-20 17:12:24 -0700132 /**
133 * We need to handle touch events to prevent them from falling through to the workspace below.
134 */
135 @SuppressLint("ClickableViewAccessibility")
136 @Override
137 public boolean onTouchEvent(MotionEvent ev) {
138 return true;
139 }
140
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700141 public final void close(boolean animate) {
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700142 animate &= areAnimatorsEnabled();
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700143 if (mIsOpen) {
Hyunyoung Song6b670d62020-08-22 23:24:43 -0700144 // Add to WW logging
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700145 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700146 handleClose(animate);
Jon Miranda83337f92018-04-24 12:21:28 -0700147 mIsOpen = false;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700148 }
149
150 protected abstract void handleClose(boolean animate);
151
Tony Wickham9791bd12019-04-05 13:52:35 -0700152 /**
153 * Creates a user-controlled animation to hint that the view will be closed if completed.
154 * @param distanceToMove The max distance that elements should move from their starting point.
155 */
Sunny Goyalf3ac7032020-03-13 13:01:33 -0700156 public void addHintCloseAnim(
157 float distanceToMove, Interpolator interpolator, PendingAnimation target) { }
Tony Wickham9791bd12019-04-05 13:52:35 -0700158
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700159 public final boolean isOpen() {
160 return mIsOpen;
161 }
162
163 protected abstract boolean isOfType(@FloatingViewType int type);
164
Tony Wickham52c1b662018-05-21 13:13:58 -0700165 /** @return Whether the back is consumed. If false, Launcher will handle the back as well. */
166 public boolean onBackPressed() {
Sunny Goyal37920962017-09-28 13:43:24 -0700167 close(true);
Tony Wickham52c1b662018-05-21 13:13:58 -0700168 return true;
Sunny Goyal37920962017-09-28 13:43:24 -0700169 }
170
171 @Override
172 public boolean onControllerTouchEvent(MotionEvent ev) {
173 return false;
174 }
175
Sunny Goyalde753212018-05-15 13:55:57 -0700176 protected void announceAccessibilityChanges() {
177 Pair<View, String> targetInfo = getAccessibilityTarget();
178 if (targetInfo == null || !isAccessibilityEnabled(getContext())) {
179 return;
180 }
181 sendCustomAccessibilityEvent(
182 targetInfo.first, TYPE_WINDOW_STATE_CHANGED, targetInfo.second);
183
184 if (mIsOpen) {
Andy Wickhambc83edb2020-05-27 12:58:40 -0700185 getAccessibilityInitialFocusView().performAccessibilityAction(
186 AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS, null);
Sunny Goyalde753212018-05-15 13:55:57 -0700187 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700188 ActivityContext.lookupContext(getContext()).getDragLayer()
Sunny Goyalde753212018-05-15 13:55:57 -0700189 .sendAccessibilityEvent(TYPE_WINDOW_CONTENT_CHANGED);
190 }
191
192 protected Pair<View, String> getAccessibilityTarget() {
193 return null;
194 }
195
Andy Wickhambc83edb2020-05-27 12:58:40 -0700196 /** Returns the View that Accessibility services should focus on first. */
197 protected View getAccessibilityInitialFocusView() {
198 return this;
199 }
200
Samuel Fufa82bbdac2020-03-09 18:24:47 -0700201 /**
202 * Returns a view matching FloatingViewType
203 */
204 public static <T extends AbstractFloatingView> T getOpenView(
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700205 ActivityContext activity, @FloatingViewType int type) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700206 BaseDragLayer dragLayer = activity.getDragLayer();
vadimt5a22ef72019-04-17 18:39:00 -0700207 if (dragLayer == null) return null;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700208 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
209 // and will be one of the last views.
210 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
211 View child = dragLayer.getChildAt(i);
212 if (child instanceof AbstractFloatingView) {
213 AbstractFloatingView view = (AbstractFloatingView) child;
214 if (view.isOfType(type) && view.isOpen()) {
215 return (T) view;
216 }
217 }
218 }
219 return null;
220 }
221
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700222 public static void closeOpenContainer(ActivityContext activity,
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700223 @FloatingViewType int type) {
224 AbstractFloatingView view = getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700225 if (view != null) {
226 view.close(true);
227 }
228 }
229
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700230 public static void closeOpenViews(ActivityContext activity, boolean animate,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700231 @FloatingViewType int type) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700232 BaseDragLayer dragLayer = activity.getDragLayer();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700233 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
234 // and will be one of the last views.
235 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
236 View child = dragLayer.getChildAt(i);
237 if (child instanceof AbstractFloatingView) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700238 AbstractFloatingView abs = (AbstractFloatingView) child;
239 if (abs.isOfType(type)) {
240 abs.close(animate);
241 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700242 }
243 }
244 }
245
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700246 public static void closeAllOpenViews(ActivityContext activity, boolean animate) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700247 closeOpenViews(activity, animate, TYPE_ALL);
248 activity.finishAutoCancelActionMode();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700249 }
250
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700251 public static void closeAllOpenViews(ActivityContext activity) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700252 closeAllOpenViews(activity, true);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700253 }
254
Tracy Zhoud43e7c22018-10-16 14:49:55 -0700255 public static void closeAllOpenViewsExcept(ActivityContext activity, boolean animate,
256 @FloatingViewType int type) {
257 closeOpenViews(activity, animate, TYPE_ALL & ~type);
258 activity.finishAutoCancelActionMode();
259 }
260
261 public static void closeAllOpenViewsExcept(ActivityContext activity,
262 @FloatingViewType int type) {
263 closeAllOpenViewsExcept(activity, true, type);
264 }
265
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700266 public static AbstractFloatingView getTopOpenView(ActivityContext activity) {
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700267 return getTopOpenViewWithType(activity, TYPE_ALL);
268 }
269
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700270 public static AbstractFloatingView getTopOpenViewWithType(ActivityContext activity,
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700271 @FloatingViewType int type) {
272 return getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700273 }
vadimt34cc5f42020-06-04 13:38:17 -0700274
275 public boolean canInterceptEventsInSystemGestureRegion() {
276 return false;
277 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700278}