blob: 26024e5310767265a0cf1179727eaf83bc29cbea [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,
45 TYPE_QUICKSTEP_PREVIEW
Tony Wickham50e51652017-03-20 17:12:24 -070046 })
Sunny Goyal740ac7f2016-09-28 16:47:32 -070047 @Retention(RetentionPolicy.SOURCE)
48 public @interface FloatingViewType {}
49 public static final int TYPE_FOLDER = 1 << 0;
Sunny Goyal10a1bd02017-10-09 14:56:21 -070050 public static final int TYPE_ACTION_POPUP = 1 << 1;
Tony Wickham343a77e2017-04-12 18:31:09 -070051 public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2;
Sunny Goyal37920962017-09-28 13:43:24 -070052 public static final int TYPE_WIDGET_RESIZE_FRAME = 1 << 3;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070053 public static final int TYPE_WIDGETS_FULL_SHEET = 1 << 4;
Sunny Goyalf8088ee2017-11-10 14:52:00 -080054 public static final int TYPE_QUICKSTEP_PREVIEW = 1 << 5;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070055
56 public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP
Sunny Goyalf8088ee2017-11-10 14:52:00 -080057 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET
58 | TYPE_QUICKSTEP_PREVIEW;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070059
60 protected boolean mIsOpen;
61
62 public AbstractFloatingView(Context context, AttributeSet attrs) {
63 super(context, attrs);
64 }
65
66 public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
67 super(context, attrs, defStyleAttr);
68 }
69
Tony Wickham50e51652017-03-20 17:12:24 -070070 /**
71 * We need to handle touch events to prevent them from falling through to the workspace below.
72 */
73 @SuppressLint("ClickableViewAccessibility")
74 @Override
75 public boolean onTouchEvent(MotionEvent ev) {
76 return true;
77 }
78
Sunny Goyal740ac7f2016-09-28 16:47:32 -070079 public final void close(boolean animate) {
80 animate &= !Utilities.isPowerSaverOn(getContext());
81 handleClose(animate);
82 Launcher.getLauncher(getContext()).getUserEventDispatcher().resetElapsedContainerMillis();
83 }
84
85 protected abstract void handleClose(boolean animate);
86
Sunny Goyal37920962017-09-28 13:43:24 -070087 public abstract void logActionCommand(int command);
Sunny Goyal740ac7f2016-09-28 16:47:32 -070088
89 public final boolean isOpen() {
90 return mIsOpen;
91 }
92
Tony Wickham26b17462017-03-20 17:12:24 -070093 protected void onWidgetsBound() {
94 }
95
Sunny Goyal740ac7f2016-09-28 16:47:32 -070096 protected abstract boolean isOfType(@FloatingViewType int type);
97
Sunny Goyal37920962017-09-28 13:43:24 -070098 public void onBackPressed() {
99 logActionCommand(Action.Command.BACK);
100 close(true);
101 }
102
103 @Override
104 public boolean onControllerTouchEvent(MotionEvent ev) {
105 return false;
106 }
107
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700108 protected static <T extends AbstractFloatingView> T getOpenView(
109 Launcher launcher, @FloatingViewType int type) {
110 DragLayer dragLayer = launcher.getDragLayer();
111 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
112 // and will be one of the last views.
113 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
114 View child = dragLayer.getChildAt(i);
115 if (child instanceof AbstractFloatingView) {
116 AbstractFloatingView view = (AbstractFloatingView) child;
117 if (view.isOfType(type) && view.isOpen()) {
118 return (T) view;
119 }
120 }
121 }
122 return null;
123 }
124
Tony Wickham51889b02017-02-27 16:30:47 -0800125 public static void closeOpenContainer(Launcher launcher, @FloatingViewType int type) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700126 AbstractFloatingView view = getOpenView(launcher, type);
127 if (view != null) {
128 view.close(true);
129 }
130 }
131
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700132 public static void closeOpenViews(Launcher launcher, boolean animate,
133 @FloatingViewType int type) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700134 DragLayer dragLayer = launcher.getDragLayer();
135 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
136 // and will be one of the last views.
137 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
138 View child = dragLayer.getChildAt(i);
139 if (child instanceof AbstractFloatingView) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700140 AbstractFloatingView abs = (AbstractFloatingView) child;
141 if (abs.isOfType(type)) {
142 abs.close(animate);
143 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700144 }
145 }
146 }
147
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700148 public static void closeAllOpenViews(Launcher launcher, boolean animate) {
149 closeOpenViews(launcher, animate, TYPE_ALL);
150 }
151
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700152 public static void closeAllOpenViews(Launcher launcher) {
153 closeAllOpenViews(launcher, true);
154 }
155
156 public static AbstractFloatingView getTopOpenView(Launcher launcher) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700157 return getOpenView(launcher, TYPE_ALL);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700158 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700159}