blob: 1c07ea4a7e28cbc941748be3619ca2fb71164dd2 [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;
22
23import 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;
32import android.widget.LinearLayout;
33
Sunny Goyal37920962017-09-28 13:43:24 -070034import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
35import com.android.launcher3.util.TouchController;
Sunny Goyal87b5eb62018-07-03 15:53:39 -070036import com.android.launcher3.views.ActivityContext;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070037import com.android.launcher3.views.BaseDragLayer;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070038
39import java.lang.annotation.Retention;
40import java.lang.annotation.RetentionPolicy;
41
Sunny Goyald2303072018-08-14 15:21:45 -070042import androidx.annotation.IntDef;
43
Sunny Goyal740ac7f2016-09-28 16:47:32 -070044/**
45 * Base class for a View which shows a floating UI on top of the launcher UI.
46 */
Sunny Goyal37920962017-09-28 13:43:24 -070047public abstract class AbstractFloatingView extends LinearLayout implements TouchController {
Sunny Goyal740ac7f2016-09-28 16:47:32 -070048
Tony Wickham50e51652017-03-20 17:12:24 -070049 @IntDef(flag = true, value = {
50 TYPE_FOLDER,
Sunny Goyal10a1bd02017-10-09 14:56:21 -070051 TYPE_ACTION_POPUP,
Sunny Goyal37920962017-09-28 13:43:24 -070052 TYPE_WIDGETS_BOTTOM_SHEET,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070053 TYPE_WIDGET_RESIZE_FRAME,
Sunny Goyalf8088ee2017-11-10 14:52:00 -080054 TYPE_WIDGETS_FULL_SHEET,
Tony Wickham2fae2a02017-12-14 18:38:25 -080055 TYPE_ON_BOARD_POPUP,
Vadim Tryshev17839d52018-05-23 14:45:56 -070056 TYPE_DISCOVERY_BOUNCE,
Sunny Goyal02424b22018-01-19 11:24:32 -080057
58 TYPE_QUICKSTEP_PREVIEW,
59 TYPE_TASK_MENU,
60 TYPE_OPTIONS_POPUP
Tony Wickham50e51652017-03-20 17:12:24 -070061 })
Sunny Goyal740ac7f2016-09-28 16:47:32 -070062 @Retention(RetentionPolicy.SOURCE)
63 public @interface FloatingViewType {}
64 public static final int TYPE_FOLDER = 1 << 0;
Sunny Goyal10a1bd02017-10-09 14:56:21 -070065 public static final int TYPE_ACTION_POPUP = 1 << 1;
Tony Wickham343a77e2017-04-12 18:31:09 -070066 public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2;
Sunny Goyal37920962017-09-28 13:43:24 -070067 public static final int TYPE_WIDGET_RESIZE_FRAME = 1 << 3;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070068 public static final int TYPE_WIDGETS_FULL_SHEET = 1 << 4;
Sunny Goyal02424b22018-01-19 11:24:32 -080069 public static final int TYPE_ON_BOARD_POPUP = 1 << 5;
Vadim Tryshev17839d52018-05-23 14:45:56 -070070 public static final int TYPE_DISCOVERY_BOUNCE = 1 << 6;
Sunny Goyal02424b22018-01-19 11:24:32 -080071
72 // Popups related to quickstep UI
Tobias Dubois7b5834d2018-06-11 16:25:55 +020073 public static final int TYPE_QUICKSTEP_PREVIEW = 1 << 7;
74 public static final int TYPE_TASK_MENU = 1 << 8;
75 public static final int TYPE_OPTIONS_POPUP = 1 << 9;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070076
77 public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP
Sunny Goyalf8088ee2017-11-10 14:52:00 -080078 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET
Vadim Tryshev17839d52018-05-23 14:45:56 -070079 | TYPE_QUICKSTEP_PREVIEW | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE | TYPE_TASK_MENU
80 | TYPE_OPTIONS_POPUP;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070081
Sunny Goyal7ede6112017-12-05 15:11:21 -080082 // Type of popups which should be kept open during launcher rebind
83 public static final int TYPE_REBIND_SAFE = TYPE_WIDGETS_FULL_SHEET
Vadim Tryshev17839d52018-05-23 14:45:56 -070084 | TYPE_QUICKSTEP_PREVIEW | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE;
Sunny Goyal7ede6112017-12-05 15:11:21 -080085
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -070086 // Usually we show the back button when a floating view is open. Instead, hide for these types.
Vadim Tryshev17839d52018-05-23 14:45:56 -070087 public static final int TYPE_HIDE_BACK_BUTTON = TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE;
88
Sunny Goyal32f91ab2018-06-28 15:52:35 -070089 public static final int TYPE_ACCESSIBLE = TYPE_ALL
90 & ~TYPE_DISCOVERY_BOUNCE & ~TYPE_QUICKSTEP_PREVIEW;
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -070091
Sunny Goyal740ac7f2016-09-28 16:47:32 -070092 protected boolean mIsOpen;
93
94 public AbstractFloatingView(Context context, AttributeSet attrs) {
95 super(context, attrs);
96 }
97
98 public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
99 super(context, attrs, defStyleAttr);
100 }
101
Tony Wickham50e51652017-03-20 17:12:24 -0700102 /**
103 * We need to handle touch events to prevent them from falling through to the workspace below.
104 */
105 @SuppressLint("ClickableViewAccessibility")
106 @Override
107 public boolean onTouchEvent(MotionEvent ev) {
108 return true;
109 }
110
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700111 public final void close(boolean animate) {
Tony Wickham8155fa22018-05-18 17:18:49 -0700112 animate &= !Utilities.isPowerSaverPreventingAnimation(getContext());
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700113 if (mIsOpen) {
114 BaseActivity.fromContext(getContext()).getUserEventDispatcher()
115 .resetElapsedContainerMillis("container closed");
116 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700117 handleClose(animate);
Jon Miranda83337f92018-04-24 12:21:28 -0700118 mIsOpen = false;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700119 }
120
121 protected abstract void handleClose(boolean animate);
122
Sunny Goyal37920962017-09-28 13:43:24 -0700123 public abstract void logActionCommand(int command);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700124
125 public final boolean isOpen() {
126 return mIsOpen;
127 }
128
Tony Wickham26b17462017-03-20 17:12:24 -0700129 protected void onWidgetsBound() {
130 }
131
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700132 protected abstract boolean isOfType(@FloatingViewType int type);
133
Tony Wickham52c1b662018-05-21 13:13:58 -0700134 /** @return Whether the back is consumed. If false, Launcher will handle the back as well. */
135 public boolean onBackPressed() {
Sunny Goyal37920962017-09-28 13:43:24 -0700136 logActionCommand(Action.Command.BACK);
137 close(true);
Tony Wickham52c1b662018-05-21 13:13:58 -0700138 return true;
Sunny Goyal37920962017-09-28 13:43:24 -0700139 }
140
141 @Override
142 public boolean onControllerTouchEvent(MotionEvent ev) {
143 return false;
144 }
145
Sunny Goyalde753212018-05-15 13:55:57 -0700146 protected void announceAccessibilityChanges() {
147 Pair<View, String> targetInfo = getAccessibilityTarget();
148 if (targetInfo == null || !isAccessibilityEnabled(getContext())) {
149 return;
150 }
151 sendCustomAccessibilityEvent(
152 targetInfo.first, TYPE_WINDOW_STATE_CHANGED, targetInfo.second);
153
154 if (mIsOpen) {
155 sendAccessibilityEvent(TYPE_VIEW_FOCUSED);
156 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700157 ActivityContext.lookupContext(getContext()).getDragLayer()
Sunny Goyalde753212018-05-15 13:55:57 -0700158 .sendAccessibilityEvent(TYPE_WINDOW_CONTENT_CHANGED);
159 }
160
161 protected Pair<View, String> getAccessibilityTarget() {
162 return null;
163 }
164
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700165 protected static <T extends AbstractFloatingView> T getOpenView(
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700166 ActivityContext activity, @FloatingViewType int type) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700167 BaseDragLayer dragLayer = activity.getDragLayer();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700168 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
169 // and will be one of the last views.
170 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
171 View child = dragLayer.getChildAt(i);
172 if (child instanceof AbstractFloatingView) {
173 AbstractFloatingView view = (AbstractFloatingView) child;
174 if (view.isOfType(type) && view.isOpen()) {
175 return (T) view;
176 }
177 }
178 }
179 return null;
180 }
181
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700182 public static void closeOpenContainer(ActivityContext activity,
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700183 @FloatingViewType int type) {
184 AbstractFloatingView view = getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700185 if (view != null) {
186 view.close(true);
187 }
188 }
189
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700190 public static void closeOpenViews(ActivityContext activity, boolean animate,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700191 @FloatingViewType int type) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700192 BaseDragLayer dragLayer = activity.getDragLayer();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700193 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
194 // and will be one of the last views.
195 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
196 View child = dragLayer.getChildAt(i);
197 if (child instanceof AbstractFloatingView) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700198 AbstractFloatingView abs = (AbstractFloatingView) child;
199 if (abs.isOfType(type)) {
200 abs.close(animate);
201 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700202 }
203 }
204 }
205
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700206 public static void closeAllOpenViews(ActivityContext activity, boolean animate) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700207 closeOpenViews(activity, animate, TYPE_ALL);
208 activity.finishAutoCancelActionMode();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700209 }
210
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700211 public static void closeAllOpenViews(ActivityContext activity) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700212 closeAllOpenViews(activity, true);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700213 }
214
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700215 public static AbstractFloatingView getTopOpenView(ActivityContext activity) {
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700216 return getTopOpenViewWithType(activity, TYPE_ALL);
217 }
218
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700219 public static AbstractFloatingView getTopOpenViewWithType(ActivityContext activity,
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700220 @FloatingViewType int type) {
221 return getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700222 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700223}