blob: 097c3415c1e1faf0078991e29af4be37a6ff473b [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");
Jon Miranda83337f92018-04-24 12:21:28 -0700100 mIsOpen = false;
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700101 }
102
103 protected abstract void handleClose(boolean animate);
104
Sunny Goyal37920962017-09-28 13:43:24 -0700105 public abstract void logActionCommand(int command);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700106
107 public final boolean isOpen() {
108 return mIsOpen;
109 }
110
Tony Wickham26b17462017-03-20 17:12:24 -0700111 protected void onWidgetsBound() {
112 }
113
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700114 protected abstract boolean isOfType(@FloatingViewType int type);
115
Sunny Goyal37920962017-09-28 13:43:24 -0700116 public void onBackPressed() {
117 logActionCommand(Action.Command.BACK);
118 close(true);
119 }
120
121 @Override
122 public boolean onControllerTouchEvent(MotionEvent ev) {
123 return false;
124 }
125
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700126 protected static <T extends AbstractFloatingView> T getOpenView(
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700127 BaseDraggingActivity activity, @FloatingViewType int type) {
128 BaseDragLayer dragLayer = activity.getDragLayer();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700129 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
130 // and will be one of the last views.
131 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
132 View child = dragLayer.getChildAt(i);
133 if (child instanceof AbstractFloatingView) {
134 AbstractFloatingView view = (AbstractFloatingView) child;
135 if (view.isOfType(type) && view.isOpen()) {
136 return (T) view;
137 }
138 }
139 }
140 return null;
141 }
142
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700143 public static void closeOpenContainer(BaseDraggingActivity activity,
144 @FloatingViewType int type) {
145 AbstractFloatingView view = getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700146 if (view != null) {
147 view.close(true);
148 }
149 }
150
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700151 public static void closeOpenViews(BaseDraggingActivity activity, boolean animate,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700152 @FloatingViewType int type) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700153 BaseDragLayer dragLayer = activity.getDragLayer();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700154 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
155 // and will be one of the last views.
156 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
157 View child = dragLayer.getChildAt(i);
158 if (child instanceof AbstractFloatingView) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700159 AbstractFloatingView abs = (AbstractFloatingView) child;
160 if (abs.isOfType(type)) {
161 abs.close(animate);
162 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700163 }
164 }
165 }
166
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700167 public static void closeAllOpenViews(BaseDraggingActivity activity, boolean animate) {
168 closeOpenViews(activity, animate, TYPE_ALL);
169 activity.finishAutoCancelActionMode();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700170 }
171
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700172 public static void closeAllOpenViews(BaseDraggingActivity activity) {
173 closeAllOpenViews(activity, true);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700174 }
175
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700176 public static AbstractFloatingView getTopOpenView(BaseDraggingActivity activity) {
Tony Wickhamdf1eb8b2018-04-12 17:26:18 -0700177 return getTopOpenViewWithType(activity, TYPE_ALL);
178 }
179
180 public static AbstractFloatingView getTopOpenViewWithType(BaseDraggingActivity activity,
181 @FloatingViewType int type) {
182 return getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700183 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700184}