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 | |
Sunny Goyal | 3792096 | 2017-09-28 13:43:24 -0700 | [diff] [blame] | 27 | import com.android.launcher3.userevent.nano.LauncherLogProto.Action; |
| 28 | import com.android.launcher3.util.TouchController; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 29 | import com.android.launcher3.views.BaseDragLayer; |
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 Wickham | 2fae2a0 | 2017-12-14 18:38:25 -0800 | [diff] [blame] | 45 | TYPE_ON_BOARD_POPUP, |
Sunny Goyal | 02424b2 | 2018-01-19 11:24:32 -0800 | [diff] [blame] | 46 | |
| 47 | TYPE_QUICKSTEP_PREVIEW, |
| 48 | TYPE_TASK_MENU, |
| 49 | TYPE_OPTIONS_POPUP |
Tony Wickham | 50e5165 | 2017-03-20 17:12:24 -0700 | [diff] [blame] | 50 | }) |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 51 | @Retention(RetentionPolicy.SOURCE) |
| 52 | public @interface FloatingViewType {} |
| 53 | public static final int TYPE_FOLDER = 1 << 0; |
Sunny Goyal | 10a1bd0 | 2017-10-09 14:56:21 -0700 | [diff] [blame] | 54 | public static final int TYPE_ACTION_POPUP = 1 << 1; |
Tony Wickham | 343a77e | 2017-04-12 18:31:09 -0700 | [diff] [blame] | 55 | public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2; |
Sunny Goyal | 3792096 | 2017-09-28 13:43:24 -0700 | [diff] [blame] | 56 | public static final int TYPE_WIDGET_RESIZE_FRAME = 1 << 3; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 57 | public static final int TYPE_WIDGETS_FULL_SHEET = 1 << 4; |
Sunny Goyal | 02424b2 | 2018-01-19 11:24:32 -0800 | [diff] [blame] | 58 | public static final int TYPE_ON_BOARD_POPUP = 1 << 5; |
| 59 | |
| 60 | // Popups related to quickstep UI |
| 61 | public static final int TYPE_QUICKSTEP_PREVIEW = 1 << 6; |
Tony Wickham | 2fae2a0 | 2017-12-14 18:38:25 -0800 | [diff] [blame] | 62 | public static final int TYPE_TASK_MENU = 1 << 7; |
Sunny Goyal | 02424b2 | 2018-01-19 11:24:32 -0800 | [diff] [blame] | 63 | public static final int TYPE_OPTIONS_POPUP = 1 << 8; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 64 | |
| 65 | public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP |
Sunny Goyal | f8088ee | 2017-11-10 14:52:00 -0800 | [diff] [blame] | 66 | | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET |
Sunny Goyal | 02424b2 | 2018-01-19 11:24:32 -0800 | [diff] [blame] | 67 | | TYPE_QUICKSTEP_PREVIEW | TYPE_ON_BOARD_POPUP | TYPE_TASK_MENU | TYPE_OPTIONS_POPUP; |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 68 | |
Sunny Goyal | 7ede611 | 2017-12-05 15:11:21 -0800 | [diff] [blame] | 69 | // Type of popups which should be kept open during launcher rebind |
| 70 | public static final int TYPE_REBIND_SAFE = TYPE_WIDGETS_FULL_SHEET |
Jon Miranda | fcb7e19 | 2018-04-05 11:32:41 -0700 | [diff] [blame] | 71 | | TYPE_QUICKSTEP_PREVIEW | TYPE_ON_BOARD_POPUP; |
Sunny Goyal | 7ede611 | 2017-12-05 15:11:21 -0800 | [diff] [blame] | 72 | |
Tony Wickham | df1eb8b | 2018-04-12 17:26:18 -0700 | [diff] [blame] | 73 | // Usually we show the back button when a floating view is open. Instead, hide for these types. |
| 74 | public static final int TYPE_HIDE_BACK_BUTTON = TYPE_ON_BOARD_POPUP; |
| 75 | |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 76 | protected boolean mIsOpen; |
| 77 | |
| 78 | public AbstractFloatingView(Context context, AttributeSet attrs) { |
| 79 | super(context, attrs); |
| 80 | } |
| 81 | |
| 82 | public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 83 | super(context, attrs, defStyleAttr); |
| 84 | } |
| 85 | |
Tony Wickham | 50e5165 | 2017-03-20 17:12:24 -0700 | [diff] [blame] | 86 | /** |
| 87 | * We need to handle touch events to prevent them from falling through to the workspace below. |
| 88 | */ |
| 89 | @SuppressLint("ClickableViewAccessibility") |
| 90 | @Override |
| 91 | public boolean onTouchEvent(MotionEvent ev) { |
| 92 | return true; |
| 93 | } |
| 94 | |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 95 | public final void close(boolean animate) { |
| 96 | animate &= !Utilities.isPowerSaverOn(getContext()); |
| 97 | handleClose(animate); |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 98 | BaseActivity.fromContext(getContext()).getUserEventDispatcher() |
Hyunyoung Song | b3fbc0b | 2018-02-14 13:40:25 -0800 | [diff] [blame] | 99 | .resetElapsedContainerMillis("container closed"); |
Jon Miranda | 83337f9 | 2018-04-24 12:21:28 -0700 | [diff] [blame^] | 100 | mIsOpen = false; |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | protected abstract void handleClose(boolean animate); |
| 104 | |
Sunny Goyal | 3792096 | 2017-09-28 13:43:24 -0700 | [diff] [blame] | 105 | public abstract void logActionCommand(int command); |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 106 | |
| 107 | public final boolean isOpen() { |
| 108 | return mIsOpen; |
| 109 | } |
| 110 | |
Tony Wickham | 26b1746 | 2017-03-20 17:12:24 -0700 | [diff] [blame] | 111 | protected void onWidgetsBound() { |
| 112 | } |
| 113 | |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 114 | protected abstract boolean isOfType(@FloatingViewType int type); |
| 115 | |
Sunny Goyal | 3792096 | 2017-09-28 13:43:24 -0700 | [diff] [blame] | 116 | public void onBackPressed() { |
| 117 | logActionCommand(Action.Command.BACK); |
| 118 | close(true); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public boolean onControllerTouchEvent(MotionEvent ev) { |
| 123 | return false; |
| 124 | } |
| 125 | |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 126 | protected static <T extends AbstractFloatingView> T getOpenView( |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 127 | BaseDraggingActivity activity, @FloatingViewType int type) { |
| 128 | BaseDragLayer dragLayer = activity.getDragLayer(); |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 129 | // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer, |
| 130 | // and will be one of the last views. |
| 131 | for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) { |
| 132 | View child = dragLayer.getChildAt(i); |
| 133 | if (child instanceof AbstractFloatingView) { |
| 134 | AbstractFloatingView view = (AbstractFloatingView) child; |
| 135 | if (view.isOfType(type) && view.isOpen()) { |
| 136 | return (T) view; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | return null; |
| 141 | } |
| 142 | |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 143 | public static void closeOpenContainer(BaseDraggingActivity activity, |
| 144 | @FloatingViewType int type) { |
| 145 | AbstractFloatingView view = getOpenView(activity, type); |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 146 | if (view != null) { |
| 147 | view.close(true); |
| 148 | } |
| 149 | } |
| 150 | |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 151 | public static void closeOpenViews(BaseDraggingActivity activity, boolean animate, |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 152 | @FloatingViewType int type) { |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 153 | BaseDragLayer dragLayer = activity.getDragLayer(); |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 154 | // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer, |
| 155 | // and will be one of the last views. |
| 156 | for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) { |
| 157 | View child = dragLayer.getChildAt(i); |
| 158 | if (child instanceof AbstractFloatingView) { |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 159 | AbstractFloatingView abs = (AbstractFloatingView) child; |
| 160 | if (abs.isOfType(type)) { |
| 161 | abs.close(animate); |
| 162 | } |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 167 | public static void closeAllOpenViews(BaseDraggingActivity activity, boolean animate) { |
| 168 | closeOpenViews(activity, animate, TYPE_ALL); |
| 169 | activity.finishAutoCancelActionMode(); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 172 | public static void closeAllOpenViews(BaseDraggingActivity activity) { |
| 173 | closeAllOpenViews(activity, true); |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 176 | public static AbstractFloatingView getTopOpenView(BaseDraggingActivity activity) { |
Tony Wickham | df1eb8b | 2018-04-12 17:26:18 -0700 | [diff] [blame] | 177 | return getTopOpenViewWithType(activity, TYPE_ALL); |
| 178 | } |
| 179 | |
| 180 | public static AbstractFloatingView getTopOpenViewWithType(BaseDraggingActivity activity, |
| 181 | @FloatingViewType int type) { |
| 182 | return getOpenView(activity, type); |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 183 | } |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 184 | } |