blob: 3cb6ba67f3333711a8539d18cfc1668f05620ec8 [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;
Jon Mirandade0093d2019-04-16 20:53:24 -070033import android.view.ViewGroup;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070034import android.widget.LinearLayout;
35
Tony Wickham9791bd12019-04-05 13:52:35 -070036import androidx.annotation.IntDef;
37import androidx.annotation.Nullable;
38
Sunny Goyal37920962017-09-28 13:43:24 -070039import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
Tony Wickham9791bd12019-04-05 13:52:35 -070040import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyal37920962017-09-28 13:43:24 -070041import com.android.launcher3.util.TouchController;
Sunny Goyal87b5eb62018-07-03 15:53:39 -070042import com.android.launcher3.views.ActivityContext;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070043import com.android.launcher3.views.BaseDragLayer;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070044
45import java.lang.annotation.Retention;
46import java.lang.annotation.RetentionPolicy;
47
48/**
49 * Base class for a View which shows a floating UI on top of the launcher UI.
50 */
Sunny Goyal37920962017-09-28 13:43:24 -070051public abstract class AbstractFloatingView extends LinearLayout implements TouchController {
Sunny Goyal740ac7f2016-09-28 16:47:32 -070052
Tony Wickham50e51652017-03-20 17:12:24 -070053 @IntDef(flag = true, value = {
54 TYPE_FOLDER,
Sunny Goyal10a1bd02017-10-09 14:56:21 -070055 TYPE_ACTION_POPUP,
Sunny Goyal37920962017-09-28 13:43:24 -070056 TYPE_WIDGETS_BOTTOM_SHEET,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070057 TYPE_WIDGET_RESIZE_FRAME,
Sunny Goyalf8088ee2017-11-10 14:52:00 -080058 TYPE_WIDGETS_FULL_SHEET,
Tony Wickham2fae2a02017-12-14 18:38:25 -080059 TYPE_ON_BOARD_POPUP,
Vadim Tryshev17839d52018-05-23 14:45:56 -070060 TYPE_DISCOVERY_BOUNCE,
Tony Wickham6a71a5b2018-08-21 11:40:23 -070061 TYPE_SNACKBAR,
Jon Mirandade0093d2019-04-16 20:53:24 -070062 TYPE_LISTENER,
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;
Sunny Goyal02424b22018-01-19 11:24:32 -080078
79 // Popups related to quickstep UI
Jon Mirandade0093d2019-04-16 20:53:24 -070080 public static final int TYPE_TASK_MENU = 1 << 9;
81 public static final int TYPE_OPTIONS_POPUP = 1 << 10;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070082
83 public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP
Sunny Goyalf8088ee2017-11-10 14:52:00 -080084 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET
Sunny Goyal462551b2019-02-18 14:42:47 -080085 | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU
Jon Mirandade0093d2019-04-16 20:53:24 -070086 | TYPE_OPTIONS_POPUP | TYPE_SNACKBAR | TYPE_LISTENER;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070087
Sunny Goyal7ede6112017-12-05 15:11:21 -080088 // Type of popups which should be kept open during launcher rebind
89 public static final int TYPE_REBIND_SAFE = TYPE_WIDGETS_FULL_SHEET
Sunny Goyal462551b2019-02-18 14:42:47 -080090 | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE;
Sunny Goyal7ede6112017-12-05 15:11:21 -080091
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -070092 // Usually we show the back button when a floating view is open. Instead, hide for these types.
Tony Wickham6a71a5b2018-08-21 11:40:23 -070093 public static final int TYPE_HIDE_BACK_BUTTON = TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE
94 | TYPE_SNACKBAR;
Vadim Tryshev17839d52018-05-23 14:45:56 -070095
Jon Mirandade0093d2019-04-16 20:53:24 -070096 public static final int TYPE_ACCESSIBLE = TYPE_ALL & ~TYPE_DISCOVERY_BOUNCE & ~TYPE_LISTENER;
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -070097
Hyunyoung Songf58cf5e2018-09-19 16:06:16 -070098 // These view all have particular operation associated with swipe down interaction.
99 public static final int TYPE_STATUS_BAR_SWIPE_DOWN_DISALLOW = TYPE_WIDGETS_BOTTOM_SHEET |
100 TYPE_WIDGETS_FULL_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_ON_BOARD_POPUP |
101 TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU ;
102
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700103 protected boolean mIsOpen;
104
105 public AbstractFloatingView(Context context, AttributeSet attrs) {
106 super(context, attrs);
107 }
108
109 public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
110 super(context, attrs, defStyleAttr);
111 }
112
Tony Wickham50e51652017-03-20 17:12:24 -0700113 /**
114 * We need to handle touch events to prevent them from falling through to the workspace below.
115 */
116 @SuppressLint("ClickableViewAccessibility")
117 @Override
118 public boolean onTouchEvent(MotionEvent ev) {
119 return true;
120 }
121
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700122 public final void close(boolean animate) {
Tony Wickham8155fa22018-05-18 17:18:49 -0700123 animate &= !Utilities.isPowerSaverPreventingAnimation(getContext());
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700124 if (mIsOpen) {
125 BaseActivity.fromContext(getContext()).getUserEventDispatcher()
126 .resetElapsedContainerMillis("container closed");
127 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700128 handleClose(animate);
Jon Miranda83337f92018-04-24 12:21:28 -0700129 mIsOpen = false;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700130 }
131
132 protected abstract void handleClose(boolean animate);
133
Tony Wickham9791bd12019-04-05 13:52:35 -0700134 /**
135 * Creates a user-controlled animation to hint that the view will be closed if completed.
136 * @param distanceToMove The max distance that elements should move from their starting point.
137 */
138 public @Nullable Animator createHintCloseAnim(float distanceToMove) {
139 return null;
140 }
141
Sunny Goyal37920962017-09-28 13:43:24 -0700142 public abstract void logActionCommand(int command);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700143
Tony Wickham9791bd12019-04-05 13:52:35 -0700144 public int getLogContainerType() {
145 return ContainerType.DEFAULT_CONTAINERTYPE;
146 }
147
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700148 public final boolean isOpen() {
149 return mIsOpen;
150 }
151
152 protected abstract boolean isOfType(@FloatingViewType int type);
153
Tony Wickham52c1b662018-05-21 13:13:58 -0700154 /** @return Whether the back is consumed. If false, Launcher will handle the back as well. */
155 public boolean onBackPressed() {
Sunny Goyal37920962017-09-28 13:43:24 -0700156 logActionCommand(Action.Command.BACK);
157 close(true);
Tony Wickham52c1b662018-05-21 13:13:58 -0700158 return true;
Sunny Goyal37920962017-09-28 13:43:24 -0700159 }
160
161 @Override
162 public boolean onControllerTouchEvent(MotionEvent ev) {
163 return false;
164 }
165
Sunny Goyalde753212018-05-15 13:55:57 -0700166 protected void announceAccessibilityChanges() {
167 Pair<View, String> targetInfo = getAccessibilityTarget();
168 if (targetInfo == null || !isAccessibilityEnabled(getContext())) {
169 return;
170 }
171 sendCustomAccessibilityEvent(
172 targetInfo.first, TYPE_WINDOW_STATE_CHANGED, targetInfo.second);
173
174 if (mIsOpen) {
175 sendAccessibilityEvent(TYPE_VIEW_FOCUSED);
176 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700177 ActivityContext.lookupContext(getContext()).getDragLayer()
Sunny Goyalde753212018-05-15 13:55:57 -0700178 .sendAccessibilityEvent(TYPE_WINDOW_CONTENT_CHANGED);
179 }
180
181 protected Pair<View, String> getAccessibilityTarget() {
182 return null;
183 }
184
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700185 protected static <T extends AbstractFloatingView> T getOpenView(
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700186 ActivityContext activity, @FloatingViewType int type) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700187 BaseDragLayer dragLayer = activity.getDragLayer();
vadimt5a22ef72019-04-17 18:39:00 -0700188 if (dragLayer == null) return null;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700189 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
190 // and will be one of the last views.
191 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
192 View child = dragLayer.getChildAt(i);
193 if (child instanceof AbstractFloatingView) {
194 AbstractFloatingView view = (AbstractFloatingView) child;
195 if (view.isOfType(type) && view.isOpen()) {
196 return (T) view;
197 }
198 }
199 }
200 return null;
201 }
202
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700203 public static void closeOpenContainer(ActivityContext activity,
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700204 @FloatingViewType int type) {
205 AbstractFloatingView view = getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700206 if (view != null) {
207 view.close(true);
208 }
209 }
210
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700211 public static void closeOpenViews(ActivityContext activity, boolean animate,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700212 @FloatingViewType int type) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700213 BaseDragLayer dragLayer = activity.getDragLayer();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700214 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
215 // and will be one of the last views.
216 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
217 View child = dragLayer.getChildAt(i);
218 if (child instanceof AbstractFloatingView) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700219 AbstractFloatingView abs = (AbstractFloatingView) child;
220 if (abs.isOfType(type)) {
221 abs.close(animate);
222 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700223 }
224 }
225 }
226
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700227 public static void closeAllOpenViews(ActivityContext activity, boolean animate) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700228 closeOpenViews(activity, animate, TYPE_ALL);
229 activity.finishAutoCancelActionMode();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700230 }
231
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700232 public static void closeAllOpenViews(ActivityContext activity) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700233 closeAllOpenViews(activity, true);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700234 }
235
Tracy Zhoud43e7c22018-10-16 14:49:55 -0700236 public static void closeAllOpenViewsExcept(ActivityContext activity, boolean animate,
237 @FloatingViewType int type) {
238 closeOpenViews(activity, animate, TYPE_ALL & ~type);
239 activity.finishAutoCancelActionMode();
240 }
241
242 public static void closeAllOpenViewsExcept(ActivityContext activity,
243 @FloatingViewType int type) {
244 closeAllOpenViewsExcept(activity, true, type);
245 }
246
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700247 public static AbstractFloatingView getTopOpenView(ActivityContext activity) {
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700248 return getTopOpenViewWithType(activity, TYPE_ALL);
249 }
250
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700251 public static AbstractFloatingView getTopOpenViewWithType(ActivityContext activity,
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700252 @FloatingViewType int type) {
253 return getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700254 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700255}