blob: e7d7a69fd674a60e590d45251fcea4977b72d552 [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_VIEW_FOCUSED;
20import 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 Wickham9791bd12019-04-05 13:52:35 -070026import android.animation.Animator;
Tony Wickham50e51652017-03-20 17:12:24 -070027import android.annotation.SuppressLint;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070028import android.content.Context;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070029import android.util.AttributeSet;
Sunny Goyalde753212018-05-15 13:55:57 -070030import android.util.Pair;
Tony Wickham50e51652017-03-20 17:12:24 -070031import android.view.MotionEvent;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070032import android.view.View;
33import android.widget.LinearLayout;
34
Tony Wickham9791bd12019-04-05 13:52:35 -070035import androidx.annotation.IntDef;
36import androidx.annotation.Nullable;
37
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,
Sunny Goyal02424b22018-01-19 11:24:32 -080061
Sunny Goyal02424b22018-01-19 11:24:32 -080062 TYPE_TASK_MENU,
63 TYPE_OPTIONS_POPUP
Tony Wickham50e51652017-03-20 17:12:24 -070064 })
Sunny Goyal740ac7f2016-09-28 16:47:32 -070065 @Retention(RetentionPolicy.SOURCE)
66 public @interface FloatingViewType {}
67 public static final int TYPE_FOLDER = 1 << 0;
Sunny Goyal10a1bd02017-10-09 14:56:21 -070068 public static final int TYPE_ACTION_POPUP = 1 << 1;
Tony Wickham343a77e2017-04-12 18:31:09 -070069 public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2;
Sunny Goyal37920962017-09-28 13:43:24 -070070 public static final int TYPE_WIDGET_RESIZE_FRAME = 1 << 3;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070071 public static final int TYPE_WIDGETS_FULL_SHEET = 1 << 4;
Sunny Goyal02424b22018-01-19 11:24:32 -080072 public static final int TYPE_ON_BOARD_POPUP = 1 << 5;
Vadim Tryshev17839d52018-05-23 14:45:56 -070073 public static final int TYPE_DISCOVERY_BOUNCE = 1 << 6;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070074 public static final int TYPE_SNACKBAR = 1 << 7;
Sunny Goyal02424b22018-01-19 11:24:32 -080075
76 // Popups related to quickstep UI
Sunny Goyal462551b2019-02-18 14:42:47 -080077 public static final int TYPE_TASK_MENU = 1 << 8;
78 public static final int TYPE_OPTIONS_POPUP = 1 << 9;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070079
80 public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP
Sunny Goyalf8088ee2017-11-10 14:52:00 -080081 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET
Sunny Goyal462551b2019-02-18 14:42:47 -080082 | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU
Tony Wickham6a71a5b2018-08-21 11:40:23 -070083 | TYPE_OPTIONS_POPUP | TYPE_SNACKBAR;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070084
Sunny Goyal7ede6112017-12-05 15:11:21 -080085 // Type of popups which should be kept open during launcher rebind
86 public static final int TYPE_REBIND_SAFE = TYPE_WIDGETS_FULL_SHEET
Sunny Goyal462551b2019-02-18 14:42:47 -080087 | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE;
Sunny Goyal7ede6112017-12-05 15:11:21 -080088
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -070089 // Usually we show the back button when a floating view is open. Instead, hide for these types.
Tony Wickham6a71a5b2018-08-21 11:40:23 -070090 public static final int TYPE_HIDE_BACK_BUTTON = TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE
91 | TYPE_SNACKBAR;
Vadim Tryshev17839d52018-05-23 14:45:56 -070092
Sunny Goyal462551b2019-02-18 14:42:47 -080093 public static final int TYPE_ACCESSIBLE = TYPE_ALL & ~TYPE_DISCOVERY_BOUNCE;
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -070094
Hyunyoung Songf58cf5e2018-09-19 16:06:16 -070095 // These view all have particular operation associated with swipe down interaction.
96 public static final int TYPE_STATUS_BAR_SWIPE_DOWN_DISALLOW = TYPE_WIDGETS_BOTTOM_SHEET |
97 TYPE_WIDGETS_FULL_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_ON_BOARD_POPUP |
98 TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU ;
99
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700100 protected boolean mIsOpen;
101
102 public AbstractFloatingView(Context context, AttributeSet attrs) {
103 super(context, attrs);
104 }
105
106 public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
107 super(context, attrs, defStyleAttr);
108 }
109
Tony Wickham50e51652017-03-20 17:12:24 -0700110 /**
111 * We need to handle touch events to prevent them from falling through to the workspace below.
112 */
113 @SuppressLint("ClickableViewAccessibility")
114 @Override
115 public boolean onTouchEvent(MotionEvent ev) {
116 return true;
117 }
118
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700119 public final void close(boolean animate) {
Tony Wickham8155fa22018-05-18 17:18:49 -0700120 animate &= !Utilities.isPowerSaverPreventingAnimation(getContext());
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700121 if (mIsOpen) {
122 BaseActivity.fromContext(getContext()).getUserEventDispatcher()
123 .resetElapsedContainerMillis("container closed");
124 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700125 handleClose(animate);
Jon Miranda83337f92018-04-24 12:21:28 -0700126 mIsOpen = false;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700127 }
128
129 protected abstract void handleClose(boolean animate);
130
Tony Wickham9791bd12019-04-05 13:52:35 -0700131 /**
132 * Creates a user-controlled animation to hint that the view will be closed if completed.
133 * @param distanceToMove The max distance that elements should move from their starting point.
134 */
135 public @Nullable Animator createHintCloseAnim(float distanceToMove) {
136 return null;
137 }
138
Sunny Goyal37920962017-09-28 13:43:24 -0700139 public abstract void logActionCommand(int command);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700140
Tony Wickham9791bd12019-04-05 13:52:35 -0700141 public int getLogContainerType() {
142 return ContainerType.DEFAULT_CONTAINERTYPE;
143 }
144
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700145 public final boolean isOpen() {
146 return mIsOpen;
147 }
148
149 protected abstract boolean isOfType(@FloatingViewType int type);
150
Tony Wickham52c1b662018-05-21 13:13:58 -0700151 /** @return Whether the back is consumed. If false, Launcher will handle the back as well. */
152 public boolean onBackPressed() {
Sunny Goyal37920962017-09-28 13:43:24 -0700153 logActionCommand(Action.Command.BACK);
154 close(true);
Tony Wickham52c1b662018-05-21 13:13:58 -0700155 return true;
Sunny Goyal37920962017-09-28 13:43:24 -0700156 }
157
158 @Override
159 public boolean onControllerTouchEvent(MotionEvent ev) {
160 return false;
161 }
162
Sunny Goyalde753212018-05-15 13:55:57 -0700163 protected void announceAccessibilityChanges() {
164 Pair<View, String> targetInfo = getAccessibilityTarget();
165 if (targetInfo == null || !isAccessibilityEnabled(getContext())) {
166 return;
167 }
168 sendCustomAccessibilityEvent(
169 targetInfo.first, TYPE_WINDOW_STATE_CHANGED, targetInfo.second);
170
171 if (mIsOpen) {
172 sendAccessibilityEvent(TYPE_VIEW_FOCUSED);
173 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700174 ActivityContext.lookupContext(getContext()).getDragLayer()
Sunny Goyalde753212018-05-15 13:55:57 -0700175 .sendAccessibilityEvent(TYPE_WINDOW_CONTENT_CHANGED);
176 }
177
178 protected Pair<View, String> getAccessibilityTarget() {
179 return null;
180 }
181
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700182 protected static <T extends AbstractFloatingView> T getOpenView(
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700183 ActivityContext activity, @FloatingViewType int type) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700184 BaseDragLayer dragLayer = activity.getDragLayer();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700185 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
186 // and will be one of the last views.
187 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
188 View child = dragLayer.getChildAt(i);
189 if (child instanceof AbstractFloatingView) {
190 AbstractFloatingView view = (AbstractFloatingView) child;
191 if (view.isOfType(type) && view.isOpen()) {
192 return (T) view;
193 }
194 }
195 }
196 return null;
197 }
198
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700199 public static void closeOpenContainer(ActivityContext activity,
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700200 @FloatingViewType int type) {
201 AbstractFloatingView view = getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700202 if (view != null) {
203 view.close(true);
204 }
205 }
206
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700207 public static void closeOpenViews(ActivityContext activity, boolean animate,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700208 @FloatingViewType int type) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700209 BaseDragLayer dragLayer = activity.getDragLayer();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700210 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
211 // and will be one of the last views.
212 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
213 View child = dragLayer.getChildAt(i);
214 if (child instanceof AbstractFloatingView) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700215 AbstractFloatingView abs = (AbstractFloatingView) child;
216 if (abs.isOfType(type)) {
217 abs.close(animate);
218 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700219 }
220 }
221 }
222
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700223 public static void closeAllOpenViews(ActivityContext activity, boolean animate) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700224 closeOpenViews(activity, animate, TYPE_ALL);
225 activity.finishAutoCancelActionMode();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700226 }
227
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700228 public static void closeAllOpenViews(ActivityContext activity) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700229 closeAllOpenViews(activity, true);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700230 }
231
Tracy Zhoud43e7c22018-10-16 14:49:55 -0700232 public static void closeAllOpenViewsExcept(ActivityContext activity, boolean animate,
233 @FloatingViewType int type) {
234 closeOpenViews(activity, animate, TYPE_ALL & ~type);
235 activity.finishAutoCancelActionMode();
236 }
237
238 public static void closeAllOpenViewsExcept(ActivityContext activity,
239 @FloatingViewType int type) {
240 closeAllOpenViewsExcept(activity, true, type);
241 }
242
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700243 public static AbstractFloatingView getTopOpenView(ActivityContext activity) {
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700244 return getTopOpenViewWithType(activity, TYPE_ALL);
245 }
246
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700247 public static AbstractFloatingView getTopOpenViewWithType(ActivityContext activity,
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700248 @FloatingViewType int type) {
249 return getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700250 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700251}