blob: c75509eaba2ecf1dc8c95de0ab5f5dca4aef1b4f [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;
28import android.support.annotation.IntDef;
29import 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
Sunny Goyal37920962017-09-28 13:43:24 -070035import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
36import com.android.launcher3.util.TouchController;
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
42/**
43 * Base class for a View which shows a floating UI on top of the launcher UI.
44 */
Sunny Goyal37920962017-09-28 13:43:24 -070045public abstract class AbstractFloatingView extends LinearLayout implements TouchController {
Sunny Goyal740ac7f2016-09-28 16:47:32 -070046
Tony Wickham50e51652017-03-20 17:12:24 -070047 @IntDef(flag = true, value = {
48 TYPE_FOLDER,
Sunny Goyal10a1bd02017-10-09 14:56:21 -070049 TYPE_ACTION_POPUP,
Sunny Goyal37920962017-09-28 13:43:24 -070050 TYPE_WIDGETS_BOTTOM_SHEET,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070051 TYPE_WIDGET_RESIZE_FRAME,
Sunny Goyalf8088ee2017-11-10 14:52:00 -080052 TYPE_WIDGETS_FULL_SHEET,
Tony Wickham2fae2a02017-12-14 18:38:25 -080053 TYPE_ON_BOARD_POPUP,
Sunny Goyal02424b22018-01-19 11:24:32 -080054
55 TYPE_QUICKSTEP_PREVIEW,
56 TYPE_TASK_MENU,
57 TYPE_OPTIONS_POPUP
Tony Wickham50e51652017-03-20 17:12:24 -070058 })
Sunny Goyal740ac7f2016-09-28 16:47:32 -070059 @Retention(RetentionPolicy.SOURCE)
60 public @interface FloatingViewType {}
61 public static final int TYPE_FOLDER = 1 << 0;
Sunny Goyal10a1bd02017-10-09 14:56:21 -070062 public static final int TYPE_ACTION_POPUP = 1 << 1;
Tony Wickham343a77e2017-04-12 18:31:09 -070063 public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2;
Sunny Goyal37920962017-09-28 13:43:24 -070064 public static final int TYPE_WIDGET_RESIZE_FRAME = 1 << 3;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070065 public static final int TYPE_WIDGETS_FULL_SHEET = 1 << 4;
Sunny Goyal02424b22018-01-19 11:24:32 -080066 public static final int TYPE_ON_BOARD_POPUP = 1 << 5;
67
68 // Popups related to quickstep UI
69 public static final int TYPE_QUICKSTEP_PREVIEW = 1 << 6;
Tony Wickham2fae2a02017-12-14 18:38:25 -080070 public static final int TYPE_TASK_MENU = 1 << 7;
Sunny Goyal02424b22018-01-19 11:24:32 -080071 public static final int TYPE_OPTIONS_POPUP = 1 << 8;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070072
73 public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP
Sunny Goyalf8088ee2017-11-10 14:52:00 -080074 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET
Sunny Goyal02424b22018-01-19 11:24:32 -080075 | TYPE_QUICKSTEP_PREVIEW | TYPE_ON_BOARD_POPUP | TYPE_TASK_MENU | TYPE_OPTIONS_POPUP;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070076
Sunny Goyal7ede6112017-12-05 15:11:21 -080077 // Type of popups which should be kept open during launcher rebind
78 public static final int TYPE_REBIND_SAFE = TYPE_WIDGETS_FULL_SHEET
Jon Mirandafcb7e192018-04-05 11:32:41 -070079 | TYPE_QUICKSTEP_PREVIEW | TYPE_ON_BOARD_POPUP;
Sunny Goyal7ede6112017-12-05 15:11:21 -080080
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -070081 // Usually we show the back button when a floating view is open. Instead, hide for these types.
82 public static final int TYPE_HIDE_BACK_BUTTON = TYPE_ON_BOARD_POPUP;
83
Sunny Goyal740ac7f2016-09-28 16:47:32 -070084 protected boolean mIsOpen;
85
86 public AbstractFloatingView(Context context, AttributeSet attrs) {
87 super(context, attrs);
88 }
89
90 public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
91 super(context, attrs, defStyleAttr);
92 }
93
Tony Wickham50e51652017-03-20 17:12:24 -070094 /**
95 * We need to handle touch events to prevent them from falling through to the workspace below.
96 */
97 @SuppressLint("ClickableViewAccessibility")
98 @Override
99 public boolean onTouchEvent(MotionEvent ev) {
100 return true;
101 }
102
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700103 public final void close(boolean animate) {
Tony Wickham8155fa22018-05-18 17:18:49 -0700104 animate &= !Utilities.isPowerSaverPreventingAnimation(getContext());
Hyunyoung Songbd6fba92018-05-16 15:54:31 -0700105 if (mIsOpen) {
106 BaseActivity.fromContext(getContext()).getUserEventDispatcher()
107 .resetElapsedContainerMillis("container closed");
108 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700109 handleClose(animate);
Jon Miranda83337f92018-04-24 12:21:28 -0700110 mIsOpen = false;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700111 }
112
113 protected abstract void handleClose(boolean animate);
114
Sunny Goyal37920962017-09-28 13:43:24 -0700115 public abstract void logActionCommand(int command);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700116
117 public final boolean isOpen() {
118 return mIsOpen;
119 }
120
Tony Wickham26b17462017-03-20 17:12:24 -0700121 protected void onWidgetsBound() {
122 }
123
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700124 protected abstract boolean isOfType(@FloatingViewType int type);
125
Tony Wickham52c1b662018-05-21 13:13:58 -0700126 /** @return Whether the back is consumed. If false, Launcher will handle the back as well. */
127 public boolean onBackPressed() {
Sunny Goyal37920962017-09-28 13:43:24 -0700128 logActionCommand(Action.Command.BACK);
129 close(true);
Tony Wickham52c1b662018-05-21 13:13:58 -0700130 return true;
Sunny Goyal37920962017-09-28 13:43:24 -0700131 }
132
133 @Override
134 public boolean onControllerTouchEvent(MotionEvent ev) {
135 return false;
136 }
137
Sunny Goyalde753212018-05-15 13:55:57 -0700138 protected void announceAccessibilityChanges() {
139 Pair<View, String> targetInfo = getAccessibilityTarget();
140 if (targetInfo == null || !isAccessibilityEnabled(getContext())) {
141 return;
142 }
143 sendCustomAccessibilityEvent(
144 targetInfo.first, TYPE_WINDOW_STATE_CHANGED, targetInfo.second);
145
146 if (mIsOpen) {
147 sendAccessibilityEvent(TYPE_VIEW_FOCUSED);
148 }
149 BaseDraggingActivity.fromContext(getContext()).getDragLayer()
150 .sendAccessibilityEvent(TYPE_WINDOW_CONTENT_CHANGED);
151 }
152
153 protected Pair<View, String> getAccessibilityTarget() {
154 return null;
155 }
156
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700157 protected static <T extends AbstractFloatingView> T getOpenView(
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700158 BaseDraggingActivity activity, @FloatingViewType int type) {
159 BaseDragLayer dragLayer = activity.getDragLayer();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700160 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
161 // and will be one of the last views.
162 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
163 View child = dragLayer.getChildAt(i);
164 if (child instanceof AbstractFloatingView) {
165 AbstractFloatingView view = (AbstractFloatingView) child;
166 if (view.isOfType(type) && view.isOpen()) {
167 return (T) view;
168 }
169 }
170 }
171 return null;
172 }
173
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700174 public static void closeOpenContainer(BaseDraggingActivity activity,
175 @FloatingViewType int type) {
176 AbstractFloatingView view = getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700177 if (view != null) {
178 view.close(true);
179 }
180 }
181
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700182 public static void closeOpenViews(BaseDraggingActivity activity, boolean animate,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700183 @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) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700190 AbstractFloatingView abs = (AbstractFloatingView) child;
191 if (abs.isOfType(type)) {
192 abs.close(animate);
193 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700194 }
195 }
196 }
197
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700198 public static void closeAllOpenViews(BaseDraggingActivity activity, boolean animate) {
199 closeOpenViews(activity, animate, TYPE_ALL);
200 activity.finishAutoCancelActionMode();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700201 }
202
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700203 public static void closeAllOpenViews(BaseDraggingActivity activity) {
204 closeAllOpenViews(activity, true);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700205 }
206
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700207 public static AbstractFloatingView getTopOpenView(BaseDraggingActivity activity) {
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700208 return getTopOpenViewWithType(activity, TYPE_ALL);
209 }
210
211 public static AbstractFloatingView getTopOpenViewWithType(BaseDraggingActivity activity,
212 @FloatingViewType int type) {
213 return getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700214 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700215}