blob: b0c5baf6b524f75cebffea7a7a02e22123f376d3 [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
Sunny Goyal740ac7f2016-09-28 16:47:32 -070073 protected boolean mIsOpen;
74
75 public AbstractFloatingView(Context context, AttributeSet attrs) {
76 super(context, attrs);
77 }
78
79 public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
80 super(context, attrs, defStyleAttr);
81 }
82
Tony Wickham50e51652017-03-20 17:12:24 -070083 /**
84 * We need to handle touch events to prevent them from falling through to the workspace below.
85 */
86 @SuppressLint("ClickableViewAccessibility")
87 @Override
88 public boolean onTouchEvent(MotionEvent ev) {
89 return true;
90 }
91
Sunny Goyal740ac7f2016-09-28 16:47:32 -070092 public final void close(boolean animate) {
93 animate &= !Utilities.isPowerSaverOn(getContext());
94 handleClose(animate);
Sunny Goyal0b0847b2018-03-14 12:30:11 -070095 BaseActivity.fromContext(getContext()).getUserEventDispatcher()
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -080096 .resetElapsedContainerMillis("container closed");
Sunny Goyal740ac7f2016-09-28 16:47:32 -070097 }
98
99 protected abstract void handleClose(boolean animate);
100
Sunny Goyal37920962017-09-28 13:43:24 -0700101 public abstract void logActionCommand(int command);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700102
103 public final boolean isOpen() {
104 return mIsOpen;
105 }
106
Tony Wickham26b17462017-03-20 17:12:24 -0700107 protected void onWidgetsBound() {
108 }
109
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700110 protected abstract boolean isOfType(@FloatingViewType int type);
111
Sunny Goyal37920962017-09-28 13:43:24 -0700112 public void onBackPressed() {
113 logActionCommand(Action.Command.BACK);
114 close(true);
115 }
116
117 @Override
118 public boolean onControllerTouchEvent(MotionEvent ev) {
119 return false;
120 }
121
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700122 protected static <T extends AbstractFloatingView> T getOpenView(
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700123 BaseDraggingActivity activity, @FloatingViewType int type) {
124 BaseDragLayer dragLayer = activity.getDragLayer();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700125 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
126 // and will be one of the last views.
127 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
128 View child = dragLayer.getChildAt(i);
129 if (child instanceof AbstractFloatingView) {
130 AbstractFloatingView view = (AbstractFloatingView) child;
131 if (view.isOfType(type) && view.isOpen()) {
132 return (T) view;
133 }
134 }
135 }
136 return null;
137 }
138
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700139 public static void closeOpenContainer(BaseDraggingActivity activity,
140 @FloatingViewType int type) {
141 AbstractFloatingView view = getOpenView(activity, type);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700142 if (view != null) {
143 view.close(true);
144 }
145 }
146
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700147 public static void closeOpenViews(BaseDraggingActivity activity, boolean animate,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700148 @FloatingViewType int type) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700149 BaseDragLayer dragLayer = activity.getDragLayer();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700150 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
151 // and will be one of the last views.
152 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
153 View child = dragLayer.getChildAt(i);
154 if (child instanceof AbstractFloatingView) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700155 AbstractFloatingView abs = (AbstractFloatingView) child;
156 if (abs.isOfType(type)) {
157 abs.close(animate);
158 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700159 }
160 }
161 }
162
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700163 public static void closeAllOpenViews(BaseDraggingActivity activity, boolean animate) {
164 closeOpenViews(activity, animate, TYPE_ALL);
165 activity.finishAutoCancelActionMode();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700166 }
167
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700168 public static void closeAllOpenViews(BaseDraggingActivity activity) {
169 closeAllOpenViews(activity, true);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700170 }
171
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700172 public static AbstractFloatingView getTopOpenView(BaseDraggingActivity activity) {
173 return getOpenView(activity, TYPE_ALL);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700174 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700175}