blob: 12f022faf7d1d8fe91f2a34ca15e62a6ee0787b2 [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);
95 Launcher.getLauncher(getContext()).getUserEventDispatcher().resetElapsedContainerMillis();
96 }
97
98 protected abstract void handleClose(boolean animate);
99
Sunny Goyal37920962017-09-28 13:43:24 -0700100 public abstract void logActionCommand(int command);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700101
102 public final boolean isOpen() {
103 return mIsOpen;
104 }
105
Tony Wickham26b17462017-03-20 17:12:24 -0700106 protected void onWidgetsBound() {
107 }
108
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700109 protected abstract boolean isOfType(@FloatingViewType int type);
110
Sunny Goyal37920962017-09-28 13:43:24 -0700111 public void onBackPressed() {
112 logActionCommand(Action.Command.BACK);
113 close(true);
114 }
115
116 @Override
117 public boolean onControllerTouchEvent(MotionEvent ev) {
118 return false;
119 }
120
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700121 protected static <T extends AbstractFloatingView> T getOpenView(
122 Launcher launcher, @FloatingViewType int type) {
123 DragLayer dragLayer = launcher.getDragLayer();
124 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
125 // and will be one of the last views.
126 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
127 View child = dragLayer.getChildAt(i);
128 if (child instanceof AbstractFloatingView) {
129 AbstractFloatingView view = (AbstractFloatingView) child;
130 if (view.isOfType(type) && view.isOpen()) {
131 return (T) view;
132 }
133 }
134 }
135 return null;
136 }
137
Tony Wickham51889b02017-02-27 16:30:47 -0800138 public static void closeOpenContainer(Launcher launcher, @FloatingViewType int type) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700139 AbstractFloatingView view = getOpenView(launcher, type);
140 if (view != null) {
141 view.close(true);
142 }
143 }
144
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700145 public static void closeOpenViews(Launcher launcher, boolean animate,
146 @FloatingViewType int type) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700147 DragLayer dragLayer = launcher.getDragLayer();
148 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
149 // and will be one of the last views.
150 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
151 View child = dragLayer.getChildAt(i);
152 if (child instanceof AbstractFloatingView) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700153 AbstractFloatingView abs = (AbstractFloatingView) child;
154 if (abs.isOfType(type)) {
155 abs.close(animate);
156 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700157 }
158 }
159 }
160
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700161 public static void closeAllOpenViews(Launcher launcher, boolean animate) {
162 closeOpenViews(launcher, animate, TYPE_ALL);
Sunny Goyal7c8a65e2017-12-22 11:11:33 -0800163 launcher.finishAutoCancelActionMode();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700164 }
165
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700166 public static void closeAllOpenViews(Launcher launcher) {
167 closeAllOpenViews(launcher, true);
168 }
169
170 public static AbstractFloatingView getTopOpenView(Launcher launcher) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700171 return getOpenView(launcher, TYPE_ALL);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700172 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700173}