blob: 2c6629d595dca66b13cb838673389ecee14ae763 [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 Mak191b6882017-11-29 18:39:49 +000045 TYPE_QUICKSTEP_PREVIEW,
46 TYPE_ON_BOARD_POPUP
Tony Wickham50e51652017-03-20 17:12:24 -070047 })
Sunny Goyal740ac7f2016-09-28 16:47:32 -070048 @Retention(RetentionPolicy.SOURCE)
49 public @interface FloatingViewType {}
50 public static final int TYPE_FOLDER = 1 << 0;
Sunny Goyal10a1bd02017-10-09 14:56:21 -070051 public static final int TYPE_ACTION_POPUP = 1 << 1;
Tony Wickham343a77e2017-04-12 18:31:09 -070052 public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2;
Sunny Goyal37920962017-09-28 13:43:24 -070053 public static final int TYPE_WIDGET_RESIZE_FRAME = 1 << 3;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070054 public static final int TYPE_WIDGETS_FULL_SHEET = 1 << 4;
Sunny Goyalf8088ee2017-11-10 14:52:00 -080055 public static final int TYPE_QUICKSTEP_PREVIEW = 1 << 5;
Tony Mak191b6882017-11-29 18:39:49 +000056 public static final int TYPE_ON_BOARD_POPUP = 1 << 6;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070057
58 public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP
Sunny Goyalf8088ee2017-11-10 14:52:00 -080059 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET
Tony Mak191b6882017-11-29 18:39:49 +000060 | TYPE_QUICKSTEP_PREVIEW | TYPE_ON_BOARD_POPUP;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070061
62 protected boolean mIsOpen;
63
64 public AbstractFloatingView(Context context, AttributeSet attrs) {
65 super(context, attrs);
66 }
67
68 public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
69 super(context, attrs, defStyleAttr);
70 }
71
Tony Wickham50e51652017-03-20 17:12:24 -070072 /**
73 * We need to handle touch events to prevent them from falling through to the workspace below.
74 */
75 @SuppressLint("ClickableViewAccessibility")
76 @Override
77 public boolean onTouchEvent(MotionEvent ev) {
78 return true;
79 }
80
Sunny Goyal740ac7f2016-09-28 16:47:32 -070081 public final void close(boolean animate) {
82 animate &= !Utilities.isPowerSaverOn(getContext());
83 handleClose(animate);
84 Launcher.getLauncher(getContext()).getUserEventDispatcher().resetElapsedContainerMillis();
85 }
86
87 protected abstract void handleClose(boolean animate);
88
Sunny Goyal37920962017-09-28 13:43:24 -070089 public abstract void logActionCommand(int command);
Sunny Goyal740ac7f2016-09-28 16:47:32 -070090
91 public final boolean isOpen() {
92 return mIsOpen;
93 }
94
Tony Wickham26b17462017-03-20 17:12:24 -070095 protected void onWidgetsBound() {
96 }
97
Sunny Goyal740ac7f2016-09-28 16:47:32 -070098 protected abstract boolean isOfType(@FloatingViewType int type);
99
Sunny Goyal37920962017-09-28 13:43:24 -0700100 public void onBackPressed() {
101 logActionCommand(Action.Command.BACK);
102 close(true);
103 }
104
105 @Override
106 public boolean onControllerTouchEvent(MotionEvent ev) {
107 return false;
108 }
109
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700110 protected static <T extends AbstractFloatingView> T getOpenView(
111 Launcher launcher, @FloatingViewType int type) {
112 DragLayer dragLayer = launcher.getDragLayer();
113 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
114 // and will be one of the last views.
115 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
116 View child = dragLayer.getChildAt(i);
117 if (child instanceof AbstractFloatingView) {
118 AbstractFloatingView view = (AbstractFloatingView) child;
119 if (view.isOfType(type) && view.isOpen()) {
120 return (T) view;
121 }
122 }
123 }
124 return null;
125 }
126
Tony Wickham51889b02017-02-27 16:30:47 -0800127 public static void closeOpenContainer(Launcher launcher, @FloatingViewType int type) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700128 AbstractFloatingView view = getOpenView(launcher, type);
129 if (view != null) {
130 view.close(true);
131 }
132 }
133
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700134 public static void closeOpenViews(Launcher launcher, boolean animate,
135 @FloatingViewType int type) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700136 DragLayer dragLayer = launcher.getDragLayer();
137 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
138 // and will be one of the last views.
139 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
140 View child = dragLayer.getChildAt(i);
141 if (child instanceof AbstractFloatingView) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700142 AbstractFloatingView abs = (AbstractFloatingView) child;
143 if (abs.isOfType(type)) {
144 abs.close(animate);
145 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700146 }
147 }
148 }
149
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700150 public static void closeAllOpenViews(Launcher launcher, boolean animate) {
151 closeOpenViews(launcher, animate, TYPE_ALL);
152 }
153
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700154 public static void closeAllOpenViews(Launcher launcher) {
155 closeAllOpenViews(launcher, true);
156 }
157
158 public static AbstractFloatingView getTopOpenView(Launcher launcher) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700159 return getOpenView(launcher, TYPE_ALL);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700160 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700161}