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; |
| 28 | |
| 29 | import java.lang.annotation.Retention; |
| 30 | import java.lang.annotation.RetentionPolicy; |
| 31 | |
| 32 | /** |
| 33 | * Base class for a View which shows a floating UI on top of the launcher UI. |
| 34 | */ |
| 35 | public abstract class AbstractFloatingView extends LinearLayout { |
| 36 | |
Tony Wickham | 50e5165 | 2017-03-20 17:12:24 -0700 | [diff] [blame] | 37 | @IntDef(flag = true, value = { |
| 38 | TYPE_FOLDER, |
| 39 | TYPE_POPUP_CONTAINER_WITH_ARROW, |
Tony Wickham | 343a77e | 2017-04-12 18:31:09 -0700 | [diff] [blame^] | 40 | TYPE_WIDGETS_BOTTOM_SHEET |
Tony Wickham | 50e5165 | 2017-03-20 17:12:24 -0700 | [diff] [blame] | 41 | }) |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 42 | @Retention(RetentionPolicy.SOURCE) |
| 43 | public @interface FloatingViewType {} |
| 44 | public static final int TYPE_FOLDER = 1 << 0; |
Tony Wickham | 540913e | 2017-01-23 11:47:51 -0800 | [diff] [blame] | 45 | public static final int TYPE_POPUP_CONTAINER_WITH_ARROW = 1 << 1; |
Tony Wickham | 343a77e | 2017-04-12 18:31:09 -0700 | [diff] [blame^] | 46 | public static final int TYPE_WIDGETS_BOTTOM_SHEET = 1 << 2; |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 47 | |
| 48 | protected boolean mIsOpen; |
| 49 | |
| 50 | public AbstractFloatingView(Context context, AttributeSet attrs) { |
| 51 | super(context, attrs); |
| 52 | } |
| 53 | |
| 54 | public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 55 | super(context, attrs, defStyleAttr); |
| 56 | } |
| 57 | |
Tony Wickham | 50e5165 | 2017-03-20 17:12:24 -0700 | [diff] [blame] | 58 | /** |
| 59 | * We need to handle touch events to prevent them from falling through to the workspace below. |
| 60 | */ |
| 61 | @SuppressLint("ClickableViewAccessibility") |
| 62 | @Override |
| 63 | public boolean onTouchEvent(MotionEvent ev) { |
| 64 | return true; |
| 65 | } |
| 66 | |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 67 | public final void close(boolean animate) { |
| 68 | animate &= !Utilities.isPowerSaverOn(getContext()); |
| 69 | handleClose(animate); |
| 70 | Launcher.getLauncher(getContext()).getUserEventDispatcher().resetElapsedContainerMillis(); |
| 71 | } |
| 72 | |
| 73 | protected abstract void handleClose(boolean animate); |
| 74 | |
| 75 | /** |
| 76 | * If the view is current handling keyboard, return the active target, null otherwise |
| 77 | */ |
| 78 | public ExtendedEditText getActiveTextView() { |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /** |
| 84 | * Any additional view (outside of this container) where touch should be allowed while this |
| 85 | * view is visible. |
| 86 | */ |
| 87 | public View getExtendedTouchView() { |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | public final boolean isOpen() { |
| 92 | return mIsOpen; |
| 93 | } |
| 94 | |
Tony Wickham | 26b1746 | 2017-03-20 17:12:24 -0700 | [diff] [blame] | 95 | protected void onWidgetsBound() { |
| 96 | } |
| 97 | |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 98 | protected abstract boolean isOfType(@FloatingViewType int type); |
| 99 | |
| 100 | protected static <T extends AbstractFloatingView> T getOpenView( |
| 101 | Launcher launcher, @FloatingViewType int type) { |
| 102 | DragLayer dragLayer = launcher.getDragLayer(); |
| 103 | // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer, |
| 104 | // and will be one of the last views. |
| 105 | for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) { |
| 106 | View child = dragLayer.getChildAt(i); |
| 107 | if (child instanceof AbstractFloatingView) { |
| 108 | AbstractFloatingView view = (AbstractFloatingView) child; |
| 109 | if (view.isOfType(type) && view.isOpen()) { |
| 110 | return (T) view; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | return null; |
| 115 | } |
| 116 | |
Tony Wickham | 51889b0 | 2017-02-27 16:30:47 -0800 | [diff] [blame] | 117 | public static void closeOpenContainer(Launcher launcher, @FloatingViewType int type) { |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 118 | AbstractFloatingView view = getOpenView(launcher, type); |
| 119 | if (view != null) { |
| 120 | view.close(true); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | public static void closeAllOpenViews(Launcher launcher, boolean animate) { |
| 125 | DragLayer dragLayer = launcher.getDragLayer(); |
| 126 | // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer, |
| 127 | // and will be one of the last views. |
| 128 | for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) { |
| 129 | View child = dragLayer.getChildAt(i); |
| 130 | if (child instanceof AbstractFloatingView) { |
| 131 | ((AbstractFloatingView) child).close(animate); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | public static void closeAllOpenViews(Launcher launcher) { |
| 137 | closeAllOpenViews(launcher, true); |
| 138 | } |
| 139 | |
| 140 | public static AbstractFloatingView getTopOpenView(Launcher launcher) { |
Tony Wickham | 50e5165 | 2017-03-20 17:12:24 -0700 | [diff] [blame] | 141 | return getOpenView(launcher, TYPE_FOLDER | TYPE_POPUP_CONTAINER_WITH_ARROW |
Tony Wickham | 343a77e | 2017-04-12 18:31:09 -0700 | [diff] [blame^] | 142 | | TYPE_WIDGETS_BOTTOM_SHEET); |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 143 | } |
Jon Miranda | c6cf493 | 2017-02-07 17:12:36 -0800 | [diff] [blame] | 144 | |
| 145 | public abstract int getLogContainerType(); |
Sunny Goyal | 740ac7f | 2016-09-28 16:47:32 -0700 | [diff] [blame] | 146 | } |