blob: 693cf313750bf53cccfdbad5d691ddd78d42fb63 [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
Tony Wickham50e51652017-03-20 17:12:24 -070019import android.annotation.SuppressLint;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070020import android.content.Context;
21import android.support.annotation.IntDef;
22import android.util.AttributeSet;
Tony Wickham50e51652017-03-20 17:12:24 -070023import android.view.MotionEvent;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070024import android.view.View;
25import android.widget.LinearLayout;
26
Sunny Goyal37920962017-09-28 13:43:24 -070027import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
28import com.android.launcher3.util.TouchController;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070029import com.android.launcher3.views.BaseDragLayer;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070030
31import java.lang.annotation.Retention;
32import java.lang.annotation.RetentionPolicy;
33
34/**
35 * Base class for a View which shows a floating UI on top of the launcher UI.
36 */
Sunny Goyal37920962017-09-28 13:43:24 -070037public abstract class AbstractFloatingView extends LinearLayout implements TouchController {
Sunny Goyal740ac7f2016-09-28 16:47:32 -070038
Tony Wickham50e51652017-03-20 17:12:24 -070039 @IntDef(flag = true, value = {
40 TYPE_FOLDER,
Sunny Goyal10a1bd02017-10-09 14:56:21 -070041 TYPE_ACTION_POPUP,
Sunny Goyal37920962017-09-28 13:43:24 -070042 TYPE_WIDGETS_BOTTOM_SHEET,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070043 TYPE_WIDGET_RESIZE_FRAME,
Sunny Goyalf8088ee2017-11-10 14:52:00 -080044 TYPE_WIDGETS_FULL_SHEET,
Tony Wickham2fae2a02017-12-14 18:38:25 -080045 TYPE_ON_BOARD_POPUP,
Sunny Goyal02424b22018-01-19 11:24:32 -080046
47 TYPE_QUICKSTEP_PREVIEW,
48 TYPE_TASK_MENU,
49 TYPE_OPTIONS_POPUP
Tony Wickham50e51652017-03-20 17:12:24 -070050 })
Sunny Goyal740ac7f2016-09-28 16:47:32 -070051 @Retention(RetentionPolicy.SOURCE)
52 public @interface FloatingViewType {}
53 public static final int TYPE_FOLDER = 1 << 0;
Sunny Goyal10a1bd02017-10-09 14:56:21 -070054 public static final int TYPE_ACTION_POPUP = 1 << 1;
Tony Wickham343a77e2017-04-12 18:31:09 -070055 public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2;
Sunny Goyal37920962017-09-28 13:43:24 -070056 public static final int TYPE_WIDGET_RESIZE_FRAME = 1 << 3;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070057 public static final int TYPE_WIDGETS_FULL_SHEET = 1 << 4;
Sunny Goyal02424b22018-01-19 11:24:32 -080058 public static final int TYPE_ON_BOARD_POPUP = 1 << 5;
59
60 // Popups related to quickstep UI
61 public static final int TYPE_QUICKSTEP_PREVIEW = 1 << 6;
Tony Wickham2fae2a02017-12-14 18:38:25 -080062 public static final int TYPE_TASK_MENU = 1 << 7;
Sunny Goyal02424b22018-01-19 11:24:32 -080063 public static final int TYPE_OPTIONS_POPUP = 1 << 8;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070064
65 public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP
Sunny Goyalf8088ee2017-11-10 14:52:00 -080066 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET
Sunny Goyal02424b22018-01-19 11:24:32 -080067 | TYPE_QUICKSTEP_PREVIEW | TYPE_ON_BOARD_POPUP | TYPE_TASK_MENU | TYPE_OPTIONS_POPUP;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070068
Sunny Goyal7ede6112017-12-05 15:11:21 -080069 // Type of popups which should be kept open during launcher rebind
70 public static final int TYPE_REBIND_SAFE = TYPE_WIDGETS_FULL_SHEET
Jon Mirandafcb7e192018-04-05 11:32:41 -070071 | TYPE_QUICKSTEP_PREVIEW | TYPE_ON_BOARD_POPUP;
Sunny Goyal7ede6112017-12-05 15:11:21 -080072
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -070073 // Usually we show the back button when a floating view is open. Instead, hide for these types.
74 public static final int TYPE_HIDE_BACK_BUTTON = TYPE_ON_BOARD_POPUP;
75
Sunny Goyal740ac7f2016-09-28 16:47:32 -070076 protected boolean mIsOpen;
77
78 public AbstractFloatingView(Context context, AttributeSet attrs) {
79 super(context, attrs);
80 }
81
82 public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
83 super(context, attrs, defStyleAttr);
84 }
85
Tony Wickham50e51652017-03-20 17:12:24 -070086 /**
87 * We need to handle touch events to prevent them from falling through to the workspace below.
88 */
89 @SuppressLint("ClickableViewAccessibility")
90 @Override
91 public boolean onTouchEvent(MotionEvent ev) {
92 return true;
93 }
94
Sunny Goyal740ac7f2016-09-28 16:47:32 -070095 public final void close(boolean animate) {
96 animate &= !Utilities.isPowerSaverOn(getContext());
97 handleClose(animate);
Sunny Goyal0b0847b2018-03-14 12:30:11 -070098 BaseActivity.fromContext(getContext()).getUserEventDispatcher()
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -080099 .resetElapsedContainerMillis("container closed");
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700100 }
101
102 protected abstract void handleClose(boolean animate);
103
Sunny Goyal37920962017-09-28 13:43:24 -0700104 public abstract void logActionCommand(int command);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700105
106 public final boolean isOpen() {
107 return mIsOpen;
108 }
109
Tony Wickham26b17462017-03-20 17:12:24 -0700110 protected void onWidgetsBound() {
111 }
112
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700113 protected abstract boolean isOfType(@FloatingViewType int type);
114
Sunny Goyal37920962017-09-28 13:43:24 -0700115 public void onBackPressed() {
116 logActionCommand(Action.Command.BACK);
117 close(true);
118 }
119
120 @Override
121 public boolean onControllerTouchEvent(MotionEvent ev) {
122 return false;
123 }
124
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700125 protected static <T extends AbstractFloatingView> T getOpenView(
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700126 BaseDraggingActivity activity, @FloatingViewType int type) {
127 BaseDragLayer dragLayer = activity.getDragLayer();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700128 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
129 // and will be one of the last views.
130 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
131 View child = dragLayer.getChildAt(i);
132 if (child instanceof AbstractFloatingView) {
133 AbstractFloatingView view = (AbstractFloatingView) child;
134 if (view.isOfType(type) && view.isOpen()) {
135 return (T) view;
136 }
137 }
138 }
139 return null;
140 }
141
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700142 public static void closeOpenContainer(BaseDraggingActivity activity,
143 @FloatingViewType int type) {
144 AbstractFloatingView view = getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700145 if (view != null) {
146 view.close(true);
147 }
148 }
149
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700150 public static void closeOpenViews(BaseDraggingActivity activity, boolean animate,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700151 @FloatingViewType int type) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700152 BaseDragLayer dragLayer = activity.getDragLayer();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700153 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
154 // and will be one of the last views.
155 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
156 View child = dragLayer.getChildAt(i);
157 if (child instanceof AbstractFloatingView) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700158 AbstractFloatingView abs = (AbstractFloatingView) child;
159 if (abs.isOfType(type)) {
160 abs.close(animate);
161 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700162 }
163 }
164 }
165
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700166 public static void closeAllOpenViews(BaseDraggingActivity activity, boolean animate) {
167 closeOpenViews(activity, animate, TYPE_ALL);
168 activity.finishAutoCancelActionMode();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700169 }
170
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700171 public static void closeAllOpenViews(BaseDraggingActivity activity) {
172 closeAllOpenViews(activity, true);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700173 }
174
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700175 public static AbstractFloatingView getTopOpenView(BaseDraggingActivity activity) {
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700176 return getTopOpenViewWithType(activity, TYPE_ALL);
177 }
178
179 public static AbstractFloatingView getTopOpenViewWithType(BaseDraggingActivity activity,
180 @FloatingViewType int type) {
181 return getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700182 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700183}