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