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