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