Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher3; |
| 18 | |
Tony Wickham | 50e5165 | 2017-03-20 17:12:24 -0700 | [diff] [blame] | 19 | import android.annotation.SuppressLint; |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.support.annotation.IntDef; |
| 22 | import android.util.AttributeSet; |
Tony Wickham | 50e5165 | 2017-03-20 17:12:24 -0700 | [diff] [blame] | 23 | import android.view.MotionEvent; |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 24 | import android.view.View; |
| 25 | import android.widget.LinearLayout; |
| 26 | |
| 27 | import com.android.launcher3.dragndrop.DragLayer; |
Sunny Goyal | 3792096 | 2017-09-28 13:43:24 -0700 | [diff] [blame] | 28 | import com.android.launcher3.userevent.nano.LauncherLogProto.Action; |
| 29 | import com.android.launcher3.util.TouchController; |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 30 | |
| 31 | import java.lang.annotation.Retention; |
| 32 | import 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 Goyal | 3792096 | 2017-09-28 13:43:24 -0700 | [diff] [blame] | 37 | public abstract class AbstractFloatingView extends LinearLayout implements TouchController { |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 38 | |
Tony Wickham | 50e5165 | 2017-03-20 17:12:24 -0700 | [diff] [blame] | 39 | @IntDef(flag = true, value = { |
| 40 | TYPE_FOLDER, |
Sunny Goyal | 10a1bd0 | 2017-10-09 14:56:21 -0700 | [diff] [blame] | 41 | TYPE_ACTION_POPUP, |
Sunny Goyal | 3792096 | 2017-09-28 13:43:24 -0700 | [diff] [blame] | 42 | TYPE_WIDGETS_BOTTOM_SHEET, |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 43 | TYPE_WIDGET_RESIZE_FRAME, |
Sunny Goyal | f8088ee | 2017-11-10 14:52:00 -0800 | [diff] [blame] | 44 | TYPE_WIDGETS_FULL_SHEET, |
Tony Mak | 191b688 | 2017-11-29 18:39:49 +0000 | [diff] [blame] | 45 | TYPE_QUICKSTEP_PREVIEW, |
Tony Wickham | 2fae2a0 | 2017-12-14 18:38:25 -0800 | [diff] [blame] | 46 | TYPE_ON_BOARD_POPUP, |
| 47 | TYPE_TASK_MENU |
Tony Wickham | 50e5165 | 2017-03-20 17:12:24 -0700 | [diff] [blame] | 48 | }) |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 49 | @Retention(RetentionPolicy.SOURCE) |
| 50 | public @interface FloatingViewType {} |
| 51 | public static final int TYPE_FOLDER = 1 << 0; |
Sunny Goyal | 10a1bd0 | 2017-10-09 14:56:21 -0700 | [diff] [blame] | 52 | public static final int TYPE_ACTION_POPUP = 1 << 1; |
Tony Wickham | 343a77e | 2017-04-12 18:31:09 -0700 | [diff] [blame] | 53 | public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2; |
Sunny Goyal | 3792096 | 2017-09-28 13:43:24 -0700 | [diff] [blame] | 54 | public static final int TYPE_WIDGET_RESIZE_FRAME = 1 << 3; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 55 | public static final int TYPE_WIDGETS_FULL_SHEET = 1 << 4; |
Sunny Goyal | f8088ee | 2017-11-10 14:52:00 -0800 | [diff] [blame] | 56 | public static final int TYPE_QUICKSTEP_PREVIEW = 1 << 5; |
Tony Mak | 191b688 | 2017-11-29 18:39:49 +0000 | [diff] [blame] | 57 | public static final int TYPE_ON_BOARD_POPUP = 1 << 6; |
Tony Wickham | 2fae2a0 | 2017-12-14 18:38:25 -0800 | [diff] [blame] | 58 | public static final int TYPE_TASK_MENU = 1 << 7; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 59 | |
| 60 | public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP |
Sunny Goyal | f8088ee | 2017-11-10 14:52:00 -0800 | [diff] [blame] | 61 | | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET |
Tony Wickham | 2fae2a0 | 2017-12-14 18:38:25 -0800 | [diff] [blame] | 62 | | TYPE_QUICKSTEP_PREVIEW | TYPE_ON_BOARD_POPUP | TYPE_TASK_MENU; |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 63 | |
Sunny Goyal | 7ede611 | 2017-12-05 15:11:21 -0800 | [diff] [blame] | 64 | // Type of popups which should be kept open during launcher rebind |
| 65 | public static final int TYPE_REBIND_SAFE = TYPE_WIDGETS_FULL_SHEET |
| 66 | | TYPE_QUICKSTEP_PREVIEW | TYPE_ON_BOARD_POPUP; |
| 67 | |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 68 | protected boolean mIsOpen; |
| 69 | |
| 70 | public AbstractFloatingView(Context context, AttributeSet attrs) { |
| 71 | super(context, attrs); |
| 72 | } |
| 73 | |
| 74 | public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 75 | super(context, attrs, defStyleAttr); |
| 76 | } |
| 77 | |
Tony Wickham | 50e5165 | 2017-03-20 17:12:24 -0700 | [diff] [blame] | 78 | /** |
| 79 | * We need to handle touch events to prevent them from falling through to the workspace below. |
| 80 | */ |
| 81 | @SuppressLint("ClickableViewAccessibility") |
| 82 | @Override |
| 83 | public boolean onTouchEvent(MotionEvent ev) { |
| 84 | return true; |
| 85 | } |
| 86 | |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 87 | public final void close(boolean animate) { |
| 88 | animate &= !Utilities.isPowerSaverOn(getContext()); |
| 89 | handleClose(animate); |
| 90 | Launcher.getLauncher(getContext()).getUserEventDispatcher().resetElapsedContainerMillis(); |
| 91 | } |
| 92 | |
| 93 | protected abstract void handleClose(boolean animate); |
| 94 | |
Sunny Goyal | 3792096 | 2017-09-28 13:43:24 -0700 | [diff] [blame] | 95 | public abstract void logActionCommand(int command); |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 96 | |
| 97 | public final boolean isOpen() { |
| 98 | return mIsOpen; |
| 99 | } |
| 100 | |
Tony Wickham | 26b1746 | 2017-03-20 17:12:24 -0700 | [diff] [blame] | 101 | protected void onWidgetsBound() { |
| 102 | } |
| 103 | |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 104 | protected abstract boolean isOfType(@FloatingViewType int type); |
| 105 | |
Sunny Goyal | 3792096 | 2017-09-28 13:43:24 -0700 | [diff] [blame] | 106 | public void onBackPressed() { |
| 107 | logActionCommand(Action.Command.BACK); |
| 108 | close(true); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public boolean onControllerTouchEvent(MotionEvent ev) { |
| 113 | return false; |
| 114 | } |
| 115 | |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 116 | protected static <T extends AbstractFloatingView> T getOpenView( |
| 117 | Launcher launcher, @FloatingViewType int type) { |
| 118 | DragLayer dragLayer = launcher.getDragLayer(); |
| 119 | // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer, |
| 120 | // and will be one of the last views. |
| 121 | for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) { |
| 122 | View child = dragLayer.getChildAt(i); |
| 123 | if (child instanceof AbstractFloatingView) { |
| 124 | AbstractFloatingView view = (AbstractFloatingView) child; |
| 125 | if (view.isOfType(type) && view.isOpen()) { |
| 126 | return (T) view; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | return null; |
| 131 | } |
| 132 | |
Tony Wickham | 51889b0 | 2017-02-27 16:30:47 -0800 | [diff] [blame] | 133 | public static void closeOpenContainer(Launcher launcher, @FloatingViewType int type) { |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 134 | AbstractFloatingView view = getOpenView(launcher, type); |
| 135 | if (view != null) { |
| 136 | view.close(true); |
| 137 | } |
| 138 | } |
| 139 | |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 140 | public static void closeOpenViews(Launcher launcher, boolean animate, |
| 141 | @FloatingViewType int type) { |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 142 | DragLayer dragLayer = launcher.getDragLayer(); |
| 143 | // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer, |
| 144 | // and will be one of the last views. |
| 145 | for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) { |
| 146 | View child = dragLayer.getChildAt(i); |
| 147 | if (child instanceof AbstractFloatingView) { |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 148 | AbstractFloatingView abs = (AbstractFloatingView) child; |
| 149 | if (abs.isOfType(type)) { |
| 150 | abs.close(animate); |
| 151 | } |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 156 | public static void closeAllOpenViews(Launcher launcher, boolean animate) { |
| 157 | closeOpenViews(launcher, animate, TYPE_ALL); |
Sunny Goyal | 7c8a65e | 2017-12-22 11:11:33 -0800 | [diff] [blame] | 158 | launcher.finishAutoCancelActionMode(); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 161 | public static void closeAllOpenViews(Launcher launcher) { |
| 162 | closeAllOpenViews(launcher, true); |
| 163 | } |
| 164 | |
| 165 | public static AbstractFloatingView getTopOpenView(Launcher launcher) { |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 166 | return getOpenView(launcher, TYPE_ALL); |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 167 | } |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 168 | } |