blob: 597e937039a9e56d9d1b82efd236b82bf6e8ad01 [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;
28
29import java.lang.annotation.Retention;
30import java.lang.annotation.RetentionPolicy;
31
32/**
33 * Base class for a View which shows a floating UI on top of the launcher UI.
34 */
35public abstract class AbstractFloatingView extends LinearLayout {
36
Tony Wickham50e51652017-03-20 17:12:24 -070037 @IntDef(flag = true, value = {
38 TYPE_FOLDER,
39 TYPE_POPUP_CONTAINER_WITH_ARROW,
Tony Wickham343a77e2017-04-12 18:31:09 -070040 TYPE_WIDGETS_BOTTOM_SHEET
Tony Wickham50e51652017-03-20 17:12:24 -070041 })
Sunny Goyal740ac7f2016-09-28 16:47:32 -070042 @Retention(RetentionPolicy.SOURCE)
43 public @interface FloatingViewType {}
44 public static final int TYPE_FOLDER = 1 << 0;
Tony Wickham540913e2017-01-23 11:47:51 -080045 public static final int TYPE_POPUP_CONTAINER_WITH_ARROW = 1 << 1;
Tony Wickham343a77e2017-04-12 18:31:09 -070046 public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2;
Sunny Goyal740ac7f2016-09-28 16:47:32 -070047
48 protected boolean mIsOpen;
49
50 public AbstractFloatingView(Context context, AttributeSet attrs) {
51 super(context, attrs);
52 }
53
54 public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
55 super(context, attrs, defStyleAttr);
56 }
57
Tony Wickham50e51652017-03-20 17:12:24 -070058 /**
59 * We need to handle touch events to prevent them from falling through to the workspace below.
60 */
61 @SuppressLint("ClickableViewAccessibility")
62 @Override
63 public boolean onTouchEvent(MotionEvent ev) {
64 return true;
65 }
66
Sunny Goyal740ac7f2016-09-28 16:47:32 -070067 public final void close(boolean animate) {
68 animate &= !Utilities.isPowerSaverOn(getContext());
69 handleClose(animate);
70 Launcher.getLauncher(getContext()).getUserEventDispatcher().resetElapsedContainerMillis();
71 }
72
73 protected abstract void handleClose(boolean animate);
74
75 /**
76 * If the view is current handling keyboard, return the active target, null otherwise
77 */
78 public ExtendedEditText getActiveTextView() {
79 return null;
80 }
81
82
83 /**
84 * Any additional view (outside of this container) where touch should be allowed while this
85 * view is visible.
86 */
87 public View getExtendedTouchView() {
88 return null;
89 }
90
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
100 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
Tony Wickham343a77e2017-04-12 18:31:09 -0700142 | TYPE_WIDGETS_BOTTOM_SHEET);
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700143 }
Jon Mirandac6cf4932017-02-07 17:12:36 -0800144
145 public abstract int getLogContainerType();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700146}