blob: 62e0fb1bcd90a31920647bfbc6aa899370cef9a6 [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;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070023import android.util.Log;
Tony Wickham50e51652017-03-20 17:12:24 -070024import android.view.MotionEvent;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070025import android.view.View;
26import android.widget.LinearLayout;
27
28import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal37920962017-09-28 13:43:24 -070029import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
30import com.android.launcher3.util.TouchController;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070031
32import java.lang.annotation.Retention;
33import java.lang.annotation.RetentionPolicy;
34
35/**
36 * Base class for a View which shows a floating UI on top of the launcher UI.
37 */
Sunny Goyal37920962017-09-28 13:43:24 -070038public abstract class AbstractFloatingView extends LinearLayout implements TouchController {
Sunny Goyal740ac7f2016-09-28 16:47:32 -070039
Tony Wickham50e51652017-03-20 17:12:24 -070040 @IntDef(flag = true, value = {
41 TYPE_FOLDER,
Sunny Goyal10a1bd02017-10-09 14:56:21 -070042 TYPE_ACTION_POPUP,
Sunny Goyal37920962017-09-28 13:43:24 -070043 TYPE_WIDGETS_BOTTOM_SHEET,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070044 TYPE_WIDGET_RESIZE_FRAME,
45 TYPE_WIDGETS_FULL_SHEET
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;
54
55 public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP
56 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070057
58 protected boolean mIsOpen;
59
60 public AbstractFloatingView(Context context, AttributeSet attrs) {
61 super(context, attrs);
62 }
63
64 public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
65 super(context, attrs, defStyleAttr);
66 }
67
Tony Wickham50e51652017-03-20 17:12:24 -070068 /**
69 * We need to handle touch events to prevent them from falling through to the workspace below.
70 */
71 @SuppressLint("ClickableViewAccessibility")
72 @Override
73 public boolean onTouchEvent(MotionEvent ev) {
74 return true;
75 }
76
Sunny Goyal740ac7f2016-09-28 16:47:32 -070077 public final void close(boolean animate) {
78 animate &= !Utilities.isPowerSaverOn(getContext());
79 handleClose(animate);
80 Launcher.getLauncher(getContext()).getUserEventDispatcher().resetElapsedContainerMillis();
81 }
82
83 protected abstract void handleClose(boolean animate);
84
Sunny Goyal37920962017-09-28 13:43:24 -070085 public abstract void logActionCommand(int command);
Sunny Goyal740ac7f2016-09-28 16:47:32 -070086
87 public final boolean isOpen() {
88 return mIsOpen;
89 }
90
Tony Wickham26b17462017-03-20 17:12:24 -070091 protected void onWidgetsBound() {
92 }
93
Sunny Goyal740ac7f2016-09-28 16:47:32 -070094 protected abstract boolean isOfType(@FloatingViewType int type);
95
Sunny Goyal37920962017-09-28 13:43:24 -070096 public void onBackPressed() {
97 logActionCommand(Action.Command.BACK);
98 close(true);
99 }
100
101 @Override
102 public boolean onControllerTouchEvent(MotionEvent ev) {
103 return false;
104 }
105
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700106 protected static <T extends AbstractFloatingView> T getOpenView(
107 Launcher launcher, @FloatingViewType int type) {
108 DragLayer dragLayer = launcher.getDragLayer();
109 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
110 // and will be one of the last views.
111 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
112 View child = dragLayer.getChildAt(i);
113 if (child instanceof AbstractFloatingView) {
114 AbstractFloatingView view = (AbstractFloatingView) child;
115 if (view.isOfType(type) && view.isOpen()) {
116 return (T) view;
117 }
118 }
119 }
120 return null;
121 }
122
Tony Wickham51889b02017-02-27 16:30:47 -0800123 public static void closeOpenContainer(Launcher launcher, @FloatingViewType int type) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700124 AbstractFloatingView view = getOpenView(launcher, type);
125 if (view != null) {
126 view.close(true);
127 }
128 }
129
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700130 public static void closeOpenViews(Launcher launcher, boolean animate,
131 @FloatingViewType int type) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700132 DragLayer dragLayer = launcher.getDragLayer();
133 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
134 // and will be one of the last views.
135 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
136 View child = dragLayer.getChildAt(i);
137 if (child instanceof AbstractFloatingView) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700138 AbstractFloatingView abs = (AbstractFloatingView) child;
139 if (abs.isOfType(type)) {
140 abs.close(animate);
141 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700142 }
143 }
144 }
145
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700146 public static void closeAllOpenViews(Launcher launcher, boolean animate) {
147 closeOpenViews(launcher, animate, TYPE_ALL);
148 }
149
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700150 public static void closeAllOpenViews(Launcher launcher) {
151 closeAllOpenViews(launcher, true);
152 }
153
154 public static AbstractFloatingView getTopOpenView(Launcher launcher) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700155 return getOpenView(launcher, TYPE_ALL);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700156 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700157}