blob: 1aa31446d52cd70f2a40645fc1abb86f13958609 [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 Goyalde753212018-05-15 13:55:57 -070019import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED;
20import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
Tony Wickham9791bd12019-04-05 13:52:35 -070021
Sunny Goyalde753212018-05-15 13:55:57 -070022import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
23import static com.android.launcher3.compat.AccessibilityManagerCompat.sendCustomAccessibilityEvent;
24
Tony Wickham50e51652017-03-20 17:12:24 -070025import android.annotation.SuppressLint;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070026import android.content.Context;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070027import android.util.AttributeSet;
Sunny Goyalde753212018-05-15 13:55:57 -070028import android.util.Pair;
Tony Wickham50e51652017-03-20 17:12:24 -070029import android.view.MotionEvent;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070030import android.view.View;
Pinyao Ting52d02522019-09-27 16:29:32 -070031import android.view.accessibility.AccessibilityNodeInfo;
Sunny Goyalf3ac7032020-03-13 13:01:33 -070032import android.view.animation.Interpolator;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070033import android.widget.LinearLayout;
34
Tony Wickham9791bd12019-04-05 13:52:35 -070035import androidx.annotation.IntDef;
Tony Wickham9791bd12019-04-05 13:52:35 -070036
Sunny Goyalf3ac7032020-03-13 13:01:33 -070037import com.android.launcher3.anim.PendingAnimation;
Sunny Goyal37920962017-09-28 13:43:24 -070038import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
Tony Wickham9791bd12019-04-05 13:52:35 -070039import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyal37920962017-09-28 13:43:24 -070040import com.android.launcher3.util.TouchController;
Sunny Goyal87b5eb62018-07-03 15:53:39 -070041import com.android.launcher3.views.ActivityContext;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070042import com.android.launcher3.views.BaseDragLayer;
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 Goyal02424b22018-01-19 11:24:32 -080063
Sunny Goyal02424b22018-01-19 11:24:32 -080064 TYPE_TASK_MENU,
65 TYPE_OPTIONS_POPUP
Tony Wickham50e51652017-03-20 17:12:24 -070066 })
Sunny Goyal740ac7f2016-09-28 16:47:32 -070067 @Retention(RetentionPolicy.SOURCE)
68 public @interface FloatingViewType {}
69 public static final int TYPE_FOLDER = 1 << 0;
Sunny Goyal10a1bd02017-10-09 14:56:21 -070070 public static final int TYPE_ACTION_POPUP = 1 << 1;
Tony Wickham343a77e2017-04-12 18:31:09 -070071 public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2;
Sunny Goyal37920962017-09-28 13:43:24 -070072 public static final int TYPE_WIDGET_RESIZE_FRAME = 1 << 3;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070073 public static final int TYPE_WIDGETS_FULL_SHEET = 1 << 4;
Sunny Goyal02424b22018-01-19 11:24:32 -080074 public static final int TYPE_ON_BOARD_POPUP = 1 << 5;
Vadim Tryshev17839d52018-05-23 14:45:56 -070075 public static final int TYPE_DISCOVERY_BOUNCE = 1 << 6;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070076 public static final int TYPE_SNACKBAR = 1 << 7;
Jon Mirandade0093d2019-04-16 20:53:24 -070077 public static final int TYPE_LISTENER = 1 << 8;
Jon Miranda517cec52020-05-19 14:04:59 -070078 public static final int TYPE_ALL_APPS_EDU = 1 << 9;
Sunny Goyal02424b22018-01-19 11:24:32 -080079
80 // Popups related to quickstep UI
Jon Miranda517cec52020-05-19 14:04:59 -070081 public static final int TYPE_TASK_MENU = 1 << 10;
82 public static final int TYPE_OPTIONS_POPUP = 1 << 11;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070083
84 public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP
Sunny Goyalf8088ee2017-11-10 14:52:00 -080085 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET
Sunny Goyal462551b2019-02-18 14:42:47 -080086 | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU
Jon Miranda517cec52020-05-19 14:04:59 -070087 | TYPE_OPTIONS_POPUP | TYPE_SNACKBAR | TYPE_LISTENER | TYPE_ALL_APPS_EDU;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070088
Sunny Goyal7ede6112017-12-05 15:11:21 -080089 // Type of popups which should be kept open during launcher rebind
90 public static final int TYPE_REBIND_SAFE = TYPE_WIDGETS_FULL_SHEET
Jon Miranda517cec52020-05-19 14:04:59 -070091 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE
92 | TYPE_ALL_APPS_EDU;
Sunny Goyal7ede6112017-12-05 15:11:21 -080093
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -070094 // Usually we show the back button when a floating view is open. Instead, hide for these types.
Tony Wickham6a71a5b2018-08-21 11:40:23 -070095 public static final int TYPE_HIDE_BACK_BUTTON = TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE
Tonyf941f842019-05-13 11:15:54 -050096 | TYPE_SNACKBAR | TYPE_WIDGET_RESIZE_FRAME | TYPE_LISTENER;
Vadim Tryshev17839d52018-05-23 14:45:56 -070097
Jon Miranda517cec52020-05-19 14:04:59 -070098 public static final int TYPE_ACCESSIBLE = TYPE_ALL & ~TYPE_DISCOVERY_BOUNCE & ~TYPE_LISTENER
99 & ~TYPE_ALL_APPS_EDU;
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700100
Hyunyoung Songf58cf5e2018-09-19 16:06:16 -0700101 // These view all have particular operation associated with swipe down interaction.
102 public static final int TYPE_STATUS_BAR_SWIPE_DOWN_DISALLOW = TYPE_WIDGETS_BOTTOM_SHEET |
103 TYPE_WIDGETS_FULL_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_ON_BOARD_POPUP |
104 TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU ;
105
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700106 protected boolean mIsOpen;
107
108 public AbstractFloatingView(Context context, AttributeSet attrs) {
109 super(context, attrs);
110 }
111
112 public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
113 super(context, attrs, defStyleAttr);
114 }
115
Tony Wickham50e51652017-03-20 17:12:24 -0700116 /**
117 * We need to handle touch events to prevent them from falling through to the workspace below.
118 */
119 @SuppressLint("ClickableViewAccessibility")
120 @Override
121 public boolean onTouchEvent(MotionEvent ev) {
122 return true;
123 }
124
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700125 public final void close(boolean animate) {
Sunny Goyal7368fa42019-04-22 09:58:14 -0700126 animate &= Utilities.areAnimationsEnabled(getContext());
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700127 if (mIsOpen) {
128 BaseActivity.fromContext(getContext()).getUserEventDispatcher()
129 .resetElapsedContainerMillis("container closed");
130 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700131 handleClose(animate);
Jon Miranda83337f92018-04-24 12:21:28 -0700132 mIsOpen = false;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700133 }
134
135 protected abstract void handleClose(boolean animate);
136
Tony Wickham9791bd12019-04-05 13:52:35 -0700137 /**
138 * Creates a user-controlled animation to hint that the view will be closed if completed.
139 * @param distanceToMove The max distance that elements should move from their starting point.
140 */
Sunny Goyalf3ac7032020-03-13 13:01:33 -0700141 public void addHintCloseAnim(
142 float distanceToMove, Interpolator interpolator, PendingAnimation target) { }
Tony Wickham9791bd12019-04-05 13:52:35 -0700143
Sunny Goyal37920962017-09-28 13:43:24 -0700144 public abstract void logActionCommand(int command);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700145
Tony Wickham9791bd12019-04-05 13:52:35 -0700146 public int getLogContainerType() {
147 return ContainerType.DEFAULT_CONTAINERTYPE;
148 }
149
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700150 public final boolean isOpen() {
151 return mIsOpen;
152 }
153
154 protected abstract boolean isOfType(@FloatingViewType int type);
155
Tony Wickham52c1b662018-05-21 13:13:58 -0700156 /** @return Whether the back is consumed. If false, Launcher will handle the back as well. */
157 public boolean onBackPressed() {
Sunny Goyal37920962017-09-28 13:43:24 -0700158 logActionCommand(Action.Command.BACK);
159 close(true);
Tony Wickham52c1b662018-05-21 13:13:58 -0700160 return true;
Sunny Goyal37920962017-09-28 13:43:24 -0700161 }
162
163 @Override
164 public boolean onControllerTouchEvent(MotionEvent ev) {
165 return false;
166 }
167
Sunny Goyalde753212018-05-15 13:55:57 -0700168 protected void announceAccessibilityChanges() {
169 Pair<View, String> targetInfo = getAccessibilityTarget();
170 if (targetInfo == null || !isAccessibilityEnabled(getContext())) {
171 return;
172 }
173 sendCustomAccessibilityEvent(
174 targetInfo.first, TYPE_WINDOW_STATE_CHANGED, targetInfo.second);
175
176 if (mIsOpen) {
Pinyao Ting52d02522019-09-27 16:29:32 -0700177 performAccessibilityAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS, null);
Sunny Goyalde753212018-05-15 13:55:57 -0700178 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700179 ActivityContext.lookupContext(getContext()).getDragLayer()
Sunny Goyalde753212018-05-15 13:55:57 -0700180 .sendAccessibilityEvent(TYPE_WINDOW_CONTENT_CHANGED);
181 }
182
183 protected Pair<View, String> getAccessibilityTarget() {
184 return null;
185 }
186
Samuel Fufa82bbdac2020-03-09 18:24:47 -0700187 /**
188 * Returns a view matching FloatingViewType
189 */
190 public static <T extends AbstractFloatingView> T getOpenView(
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700191 ActivityContext activity, @FloatingViewType int type) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700192 BaseDragLayer dragLayer = activity.getDragLayer();
vadimt5a22ef72019-04-17 18:39:00 -0700193 if (dragLayer == null) return null;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700194 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
195 // and will be one of the last views.
196 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
197 View child = dragLayer.getChildAt(i);
198 if (child instanceof AbstractFloatingView) {
199 AbstractFloatingView view = (AbstractFloatingView) child;
200 if (view.isOfType(type) && view.isOpen()) {
201 return (T) view;
202 }
203 }
204 }
205 return null;
206 }
207
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700208 public static void closeOpenContainer(ActivityContext activity,
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700209 @FloatingViewType int type) {
210 AbstractFloatingView view = getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700211 if (view != null) {
212 view.close(true);
213 }
214 }
215
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700216 public static void closeOpenViews(ActivityContext activity, boolean animate,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700217 @FloatingViewType int type) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700218 BaseDragLayer dragLayer = activity.getDragLayer();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700219 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
220 // and will be one of the last views.
221 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
222 View child = dragLayer.getChildAt(i);
223 if (child instanceof AbstractFloatingView) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700224 AbstractFloatingView abs = (AbstractFloatingView) child;
225 if (abs.isOfType(type)) {
226 abs.close(animate);
227 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700228 }
229 }
230 }
231
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700232 public static void closeAllOpenViews(ActivityContext activity, boolean animate) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700233 closeOpenViews(activity, animate, TYPE_ALL);
234 activity.finishAutoCancelActionMode();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700235 }
236
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700237 public static void closeAllOpenViews(ActivityContext activity) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700238 closeAllOpenViews(activity, true);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700239 }
240
Tracy Zhoud43e7c22018-10-16 14:49:55 -0700241 public static void closeAllOpenViewsExcept(ActivityContext activity, boolean animate,
242 @FloatingViewType int type) {
243 closeOpenViews(activity, animate, TYPE_ALL & ~type);
244 activity.finishAutoCancelActionMode();
245 }
246
247 public static void closeAllOpenViewsExcept(ActivityContext activity,
248 @FloatingViewType int type) {
249 closeAllOpenViewsExcept(activity, true, type);
250 }
251
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700252 public static AbstractFloatingView getTopOpenView(ActivityContext activity) {
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700253 return getTopOpenViewWithType(activity, TYPE_ALL);
254 }
255
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700256 public static AbstractFloatingView getTopOpenViewWithType(ActivityContext activity,
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700257 @FloatingViewType int type) {
258 return getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700259 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700260}