blob: 599a3533d35fa89a43799488160f19039d0b5148 [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;
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;
31import android.widget.LinearLayout;
32
Sunny Goyal37920962017-09-28 13:43:24 -070033import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
34import com.android.launcher3.util.TouchController;
Sunny Goyal87b5eb62018-07-03 15:53:39 -070035import com.android.launcher3.views.ActivityContext;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070036import com.android.launcher3.views.BaseDragLayer;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070037
38import java.lang.annotation.Retention;
39import java.lang.annotation.RetentionPolicy;
40
Sunny Goyald2303072018-08-14 15:21:45 -070041import androidx.annotation.IntDef;
42
Sunny Goyal740ac7f2016-09-28 16:47:32 -070043/**
44 * Base class for a View which shows a floating UI on top of the launcher UI.
45 */
Sunny Goyal37920962017-09-28 13:43:24 -070046public abstract class AbstractFloatingView extends LinearLayout implements TouchController {
Sunny Goyal740ac7f2016-09-28 16:47:32 -070047
Tony Wickham50e51652017-03-20 17:12:24 -070048 @IntDef(flag = true, value = {
49 TYPE_FOLDER,
Sunny Goyal10a1bd02017-10-09 14:56:21 -070050 TYPE_ACTION_POPUP,
Sunny Goyal37920962017-09-28 13:43:24 -070051 TYPE_WIDGETS_BOTTOM_SHEET,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070052 TYPE_WIDGET_RESIZE_FRAME,
Sunny Goyalf8088ee2017-11-10 14:52:00 -080053 TYPE_WIDGETS_FULL_SHEET,
Tony Wickham2fae2a02017-12-14 18:38:25 -080054 TYPE_ON_BOARD_POPUP,
Vadim Tryshev17839d52018-05-23 14:45:56 -070055 TYPE_DISCOVERY_BOUNCE,
Tony Wickham6a71a5b2018-08-21 11:40:23 -070056 TYPE_SNACKBAR,
Sunny Goyal02424b22018-01-19 11:24:32 -080057
Sunny Goyal02424b22018-01-19 11:24:32 -080058 TYPE_TASK_MENU,
59 TYPE_OPTIONS_POPUP
Tony Wickham50e51652017-03-20 17:12:24 -070060 })
Sunny Goyal740ac7f2016-09-28 16:47:32 -070061 @Retention(RetentionPolicy.SOURCE)
62 public @interface FloatingViewType {}
63 public static final int TYPE_FOLDER = 1 << 0;
Sunny Goyal10a1bd02017-10-09 14:56:21 -070064 public static final int TYPE_ACTION_POPUP = 1 << 1;
Tony Wickham343a77e2017-04-12 18:31:09 -070065 public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2;
Sunny Goyal37920962017-09-28 13:43:24 -070066 public static final int TYPE_WIDGET_RESIZE_FRAME = 1 << 3;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070067 public static final int TYPE_WIDGETS_FULL_SHEET = 1 << 4;
Sunny Goyal02424b22018-01-19 11:24:32 -080068 public static final int TYPE_ON_BOARD_POPUP = 1 << 5;
Vadim Tryshev17839d52018-05-23 14:45:56 -070069 public static final int TYPE_DISCOVERY_BOUNCE = 1 << 6;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070070 public static final int TYPE_SNACKBAR = 1 << 7;
Sunny Goyal02424b22018-01-19 11:24:32 -080071
72 // Popups related to quickstep UI
Sunny Goyal462551b2019-02-18 14:42:47 -080073 public static final int TYPE_TASK_MENU = 1 << 8;
74 public static final int TYPE_OPTIONS_POPUP = 1 << 9;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070075
76 public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP
Sunny Goyalf8088ee2017-11-10 14:52:00 -080077 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET
Sunny Goyal462551b2019-02-18 14:42:47 -080078 | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU
Tony Wickham6a71a5b2018-08-21 11:40:23 -070079 | TYPE_OPTIONS_POPUP | TYPE_SNACKBAR;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070080
Sunny Goyal7ede6112017-12-05 15:11:21 -080081 // Type of popups which should be kept open during launcher rebind
82 public static final int TYPE_REBIND_SAFE = TYPE_WIDGETS_FULL_SHEET
Sunny Goyal462551b2019-02-18 14:42:47 -080083 | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE;
Sunny Goyal7ede6112017-12-05 15:11:21 -080084
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -070085 // Usually we show the back button when a floating view is open. Instead, hide for these types.
Tony Wickham6a71a5b2018-08-21 11:40:23 -070086 public static final int TYPE_HIDE_BACK_BUTTON = TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE
87 | TYPE_SNACKBAR;
Vadim Tryshev17839d52018-05-23 14:45:56 -070088
Sunny Goyal462551b2019-02-18 14:42:47 -080089 public static final int TYPE_ACCESSIBLE = TYPE_ALL & ~TYPE_DISCOVERY_BOUNCE;
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -070090
Hyunyoung Songf58cf5e2018-09-19 16:06:16 -070091 // These view all have particular operation associated with swipe down interaction.
92 public static final int TYPE_STATUS_BAR_SWIPE_DOWN_DISALLOW = TYPE_WIDGETS_BOTTOM_SHEET |
93 TYPE_WIDGETS_FULL_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_ON_BOARD_POPUP |
94 TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU ;
95
Sunny Goyal740ac7f2016-09-28 16:47:32 -070096 protected boolean mIsOpen;
97
98 public AbstractFloatingView(Context context, AttributeSet attrs) {
99 super(context, attrs);
100 }
101
102 public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
103 super(context, attrs, defStyleAttr);
104 }
105
Tony Wickham50e51652017-03-20 17:12:24 -0700106 /**
107 * We need to handle touch events to prevent them from falling through to the workspace below.
108 */
109 @SuppressLint("ClickableViewAccessibility")
110 @Override
111 public boolean onTouchEvent(MotionEvent ev) {
112 return true;
113 }
114
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700115 public final void close(boolean animate) {
Tony Wickham8155fa22018-05-18 17:18:49 -0700116 animate &= !Utilities.isPowerSaverPreventingAnimation(getContext());
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700117 if (mIsOpen) {
118 BaseActivity.fromContext(getContext()).getUserEventDispatcher()
119 .resetElapsedContainerMillis("container closed");
120 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700121 handleClose(animate);
Jon Miranda83337f92018-04-24 12:21:28 -0700122 mIsOpen = false;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700123 }
124
125 protected abstract void handleClose(boolean animate);
126
Sunny Goyal37920962017-09-28 13:43:24 -0700127 public abstract void logActionCommand(int command);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700128
129 public final boolean isOpen() {
130 return mIsOpen;
131 }
132
133 protected abstract boolean isOfType(@FloatingViewType int type);
134
Tony Wickham52c1b662018-05-21 13:13:58 -0700135 /** @return Whether the back is consumed. If false, Launcher will handle the back as well. */
136 public boolean onBackPressed() {
Sunny Goyal37920962017-09-28 13:43:24 -0700137 logActionCommand(Action.Command.BACK);
138 close(true);
Tony Wickham52c1b662018-05-21 13:13:58 -0700139 return true;
Sunny Goyal37920962017-09-28 13:43:24 -0700140 }
141
142 @Override
143 public boolean onControllerTouchEvent(MotionEvent ev) {
144 return false;
145 }
146
Sunny Goyalde753212018-05-15 13:55:57 -0700147 protected void announceAccessibilityChanges() {
148 Pair<View, String> targetInfo = getAccessibilityTarget();
149 if (targetInfo == null || !isAccessibilityEnabled(getContext())) {
150 return;
151 }
152 sendCustomAccessibilityEvent(
153 targetInfo.first, TYPE_WINDOW_STATE_CHANGED, targetInfo.second);
154
155 if (mIsOpen) {
156 sendAccessibilityEvent(TYPE_VIEW_FOCUSED);
157 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700158 ActivityContext.lookupContext(getContext()).getDragLayer()
Sunny Goyalde753212018-05-15 13:55:57 -0700159 .sendAccessibilityEvent(TYPE_WINDOW_CONTENT_CHANGED);
160 }
161
162 protected Pair<View, String> getAccessibilityTarget() {
163 return null;
164 }
165
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700166 protected static <T extends AbstractFloatingView> T getOpenView(
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700167 ActivityContext activity, @FloatingViewType int type) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700168 BaseDragLayer dragLayer = activity.getDragLayer();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700169 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
170 // and will be one of the last views.
171 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
172 View child = dragLayer.getChildAt(i);
173 if (child instanceof AbstractFloatingView) {
174 AbstractFloatingView view = (AbstractFloatingView) child;
175 if (view.isOfType(type) && view.isOpen()) {
176 return (T) view;
177 }
178 }
179 }
180 return null;
181 }
182
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700183 public static void closeOpenContainer(ActivityContext activity,
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700184 @FloatingViewType int type) {
185 AbstractFloatingView view = getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700186 if (view != null) {
187 view.close(true);
188 }
189 }
190
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700191 public static void closeOpenViews(ActivityContext activity, boolean animate,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700192 @FloatingViewType int type) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700193 BaseDragLayer dragLayer = activity.getDragLayer();
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) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700199 AbstractFloatingView abs = (AbstractFloatingView) child;
200 if (abs.isOfType(type)) {
201 abs.close(animate);
202 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700203 }
204 }
205 }
206
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700207 public static void closeAllOpenViews(ActivityContext activity, boolean animate) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700208 closeOpenViews(activity, animate, TYPE_ALL);
209 activity.finishAutoCancelActionMode();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700210 }
211
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700212 public static void closeAllOpenViews(ActivityContext activity) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700213 closeAllOpenViews(activity, true);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700214 }
215
Tracy Zhoud43e7c22018-10-16 14:49:55 -0700216 public static void closeAllOpenViewsExcept(ActivityContext activity, boolean animate,
217 @FloatingViewType int type) {
218 closeOpenViews(activity, animate, TYPE_ALL & ~type);
219 activity.finishAutoCancelActionMode();
220 }
221
222 public static void closeAllOpenViewsExcept(ActivityContext activity,
223 @FloatingViewType int type) {
224 closeAllOpenViewsExcept(activity, true, type);
225 }
226
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700227 public static AbstractFloatingView getTopOpenView(ActivityContext activity) {
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700228 return getTopOpenViewWithType(activity, TYPE_ALL);
229 }
230
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700231 public static AbstractFloatingView getTopOpenViewWithType(ActivityContext activity,
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700232 @FloatingViewType int type) {
233 return getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700234 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700235}