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 | |
| 19 | import android.content.Context; |
| 20 | import android.support.annotation.IntDef; |
| 21 | import android.util.AttributeSet; |
| 22 | import android.view.View; |
| 23 | import android.widget.LinearLayout; |
| 24 | |
| 25 | import com.android.launcher3.dragndrop.DragLayer; |
| 26 | |
| 27 | import java.lang.annotation.Retention; |
| 28 | import java.lang.annotation.RetentionPolicy; |
| 29 | |
| 30 | /** |
| 31 | * Base class for a View which shows a floating UI on top of the launcher UI. |
| 32 | */ |
| 33 | public abstract class AbstractFloatingView extends LinearLayout { |
| 34 | |
| 35 | @IntDef(flag = true, value = {TYPE_FOLDER, TYPE_DEEPSHORTCUT_CONTAINER}) |
| 36 | @Retention(RetentionPolicy.SOURCE) |
| 37 | public @interface FloatingViewType {} |
| 38 | public static final int TYPE_FOLDER = 1 << 0; |
| 39 | public static final int TYPE_DEEPSHORTCUT_CONTAINER = 1 << 1; |
| 40 | |
| 41 | protected boolean mIsOpen; |
| 42 | |
| 43 | public AbstractFloatingView(Context context, AttributeSet attrs) { |
| 44 | super(context, attrs); |
| 45 | } |
| 46 | |
| 47 | public AbstractFloatingView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 48 | super(context, attrs, defStyleAttr); |
| 49 | } |
| 50 | |
| 51 | public final void close(boolean animate) { |
| 52 | animate &= !Utilities.isPowerSaverOn(getContext()); |
| 53 | handleClose(animate); |
| 54 | Launcher.getLauncher(getContext()).getUserEventDispatcher().resetElapsedContainerMillis(); |
| 55 | } |
| 56 | |
| 57 | protected abstract void handleClose(boolean animate); |
| 58 | |
| 59 | /** |
| 60 | * If the view is current handling keyboard, return the active target, null otherwise |
| 61 | */ |
| 62 | public ExtendedEditText getActiveTextView() { |
| 63 | return null; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | /** |
| 68 | * Any additional view (outside of this container) where touch should be allowed while this |
| 69 | * view is visible. |
| 70 | */ |
| 71 | public View getExtendedTouchView() { |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | public final boolean isOpen() { |
| 76 | return mIsOpen; |
| 77 | } |
| 78 | |
| 79 | protected abstract boolean isOfType(@FloatingViewType int type); |
| 80 | |
| 81 | protected static <T extends AbstractFloatingView> T getOpenView( |
| 82 | Launcher launcher, @FloatingViewType int type) { |
| 83 | DragLayer dragLayer = launcher.getDragLayer(); |
| 84 | // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer, |
| 85 | // and will be one of the last views. |
| 86 | for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) { |
| 87 | View child = dragLayer.getChildAt(i); |
| 88 | if (child instanceof AbstractFloatingView) { |
| 89 | AbstractFloatingView view = (AbstractFloatingView) child; |
| 90 | if (view.isOfType(type) && view.isOpen()) { |
| 91 | return (T) view; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | protected static void closeOpenContainer(Launcher launcher, @FloatingViewType int type) { |
| 99 | AbstractFloatingView view = getOpenView(launcher, type); |
| 100 | if (view != null) { |
| 101 | view.close(true); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | public static void closeAllOpenViews(Launcher launcher, boolean animate) { |
| 106 | DragLayer dragLayer = launcher.getDragLayer(); |
| 107 | // Iterate in reverse order. AbstractFloatingView is added later to the dragLayer, |
| 108 | // and will be one of the last views. |
| 109 | for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) { |
| 110 | View child = dragLayer.getChildAt(i); |
| 111 | if (child instanceof AbstractFloatingView) { |
| 112 | ((AbstractFloatingView) child).close(animate); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | public static void closeAllOpenViews(Launcher launcher) { |
| 118 | closeAllOpenViews(launcher, true); |
| 119 | } |
| 120 | |
| 121 | public static AbstractFloatingView getTopOpenView(Launcher launcher) { |
| 122 | return getOpenView(launcher, TYPE_FOLDER | TYPE_DEEPSHORTCUT_CONTAINER); |
| 123 | } |
| 124 | } |