blob: fc5ce8f2b7e0460e7f97cea8372e2c6f62edcdb3 [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
27import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal37920962017-09-28 13:43:24 -070028import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
29import com.android.launcher3.util.TouchController;
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
Sunny Goyal02424b22018-01-19 11:24:32 -080071 | TYPE_QUICKSTEP_PREVIEW | TYPE_ON_BOARD_POPUP | TYPE_OPTIONS_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);
Hyunyoung Songb3fbc0b2018-02-14 13:40:25 -080095 Launcher.getLauncher(getContext()).getUserEventDispatcher()
96 .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(
123 Launcher launcher, @FloatingViewType int type) {
124 DragLayer dragLayer = launcher.getDragLayer();
125 // 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
Tony Wickham51889b02017-02-27 16:30:47 -0800139 public static void closeOpenContainer(Launcher launcher, @FloatingViewType int type) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700140 AbstractFloatingView view = getOpenView(launcher, type);
141 if (view != null) {
142 view.close(true);
143 }
144 }
145
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700146 public static void closeOpenViews(Launcher launcher, boolean animate,
147 @FloatingViewType int type) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700148 DragLayer dragLayer = launcher.getDragLayer();
149 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
150 // and will be one of the last views.
151 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
152 View child = dragLayer.getChildAt(i);
153 if (child instanceof AbstractFloatingView) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700154 AbstractFloatingView abs = (AbstractFloatingView) child;
155 if (abs.isOfType(type)) {
156 abs.close(animate);
157 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700158 }
159 }
160 }
161
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700162 public static void closeAllOpenViews(Launcher launcher, boolean animate) {
163 closeOpenViews(launcher, animate, TYPE_ALL);
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800164 launcher.finishAutoCancelActionMode();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700165 }
166
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700167 public static void closeAllOpenViews(Launcher launcher) {
168 closeAllOpenViews(launcher, true);
169 }
170
171 public static AbstractFloatingView getTopOpenView(Launcher launcher) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700172 return getOpenView(launcher, TYPE_ALL);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700173 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700174}