blob: 49968189bb03e290e9afa47eecbc59d5af567aa1 [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,
41 TYPE_POPUP_CONTAINER_WITH_ARROW,
Sunny Goyal37920962017-09-28 13:43:24 -070042 TYPE_WIDGETS_BOTTOM_SHEET,
43 TYPE_WIDGET_RESIZE_FRAME
Tony Wickham50e51652017-03-20 17:12:24 -070044 })
Sunny Goyal740ac7f2016-09-28 16:47:32 -070045 @Retention(RetentionPolicy.SOURCE)
46 public @interface FloatingViewType {}
47 public static final int TYPE_FOLDER = 1 << 0;
Tony Wickham540913e2017-01-23 11:47:51 -080048 public static final int TYPE_POPUP_CONTAINER_WITH_ARROW = 1 << 1;
Tony Wickham343a77e2017-04-12 18:31:09 -070049 public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2;
Sunny Goyal37920962017-09-28 13:43:24 -070050 public static final int TYPE_WIDGET_RESIZE_FRAME = 1 << 3;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070051
52 protected boolean mIsOpen;
53
54 public AbstractFloatingView(Context context, AttributeSet attrs) {
55 super(context, attrs);
56 }
57
58 public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
59 super(context, attrs, defStyleAttr);
60 }
61
Tony Wickham50e51652017-03-20 17:12:24 -070062 /**
63 * We need to handle touch events to prevent them from falling through to the workspace below.
64 */
65 @SuppressLint("ClickableViewAccessibility")
66 @Override
67 public boolean onTouchEvent(MotionEvent ev) {
68 return true;
69 }
70
Sunny Goyal740ac7f2016-09-28 16:47:32 -070071 public final void close(boolean animate) {
72 animate &= !Utilities.isPowerSaverOn(getContext());
73 handleClose(animate);
74 Launcher.getLauncher(getContext()).getUserEventDispatcher().resetElapsedContainerMillis();
75 }
76
77 protected abstract void handleClose(boolean animate);
78
Sunny Goyal37920962017-09-28 13:43:24 -070079 public abstract void logActionCommand(int command);
Sunny Goyal740ac7f2016-09-28 16:47:32 -070080
81 public final boolean isOpen() {
82 return mIsOpen;
83 }
84
Tony Wickham26b17462017-03-20 17:12:24 -070085 protected void onWidgetsBound() {
86 }
87
Sunny Goyal740ac7f2016-09-28 16:47:32 -070088 protected abstract boolean isOfType(@FloatingViewType int type);
89
Sunny Goyal37920962017-09-28 13:43:24 -070090 public void onBackPressed() {
91 logActionCommand(Action.Command.BACK);
92 close(true);
93 }
94
95 @Override
96 public boolean onControllerTouchEvent(MotionEvent ev) {
97 return false;
98 }
99
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700100 protected static <T extends AbstractFloatingView> T getOpenView(
101 Launcher launcher, @FloatingViewType int type) {
102 DragLayer dragLayer = launcher.getDragLayer();
103 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
104 // and will be one of the last views.
105 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
106 View child = dragLayer.getChildAt(i);
107 if (child instanceof AbstractFloatingView) {
108 AbstractFloatingView view = (AbstractFloatingView) child;
109 if (view.isOfType(type) && view.isOpen()) {
110 return (T) view;
111 }
112 }
113 }
114 return null;
115 }
116
Tony Wickham51889b02017-02-27 16:30:47 -0800117 public static void closeOpenContainer(Launcher launcher, @FloatingViewType int type) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700118 AbstractFloatingView view = getOpenView(launcher, type);
119 if (view != null) {
120 view.close(true);
121 }
122 }
123
124 public static void closeAllOpenViews(Launcher launcher, boolean animate) {
125 DragLayer dragLayer = launcher.getDragLayer();
126 // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer,
127 // and will be one of the last views.
128 for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
129 View child = dragLayer.getChildAt(i);
130 if (child instanceof AbstractFloatingView) {
131 ((AbstractFloatingView) child).close(animate);
132 }
133 }
134 }
135
136 public static void closeAllOpenViews(Launcher launcher) {
137 closeAllOpenViews(launcher, true);
138 }
139
140 public static AbstractFloatingView getTopOpenView(Launcher launcher) {
Tony Wickham50e51652017-03-20 17:12:24 -0700141 return getOpenView(launcher, TYPE_FOLDER | TYPE_POPUP_CONTAINER_WITH_ARROW
Sunny Goyal37920962017-09-28 13:43:24 -0700142 | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700143 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700144}