1 /* 2 * Copyright (C) 2018 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 package com.android.launcher3.views; 17 18 import android.content.Context; 19 import android.content.ContextWrapper; 20 import android.graphics.Rect; 21 import android.view.LayoutInflater; 22 import android.view.View.AccessibilityDelegate; 23 24 import com.android.launcher3.DeviceProfile; 25 import com.android.launcher3.dot.DotInfo; 26 import com.android.launcher3.dragndrop.DragController; 27 import com.android.launcher3.model.data.ItemInfo; 28 import com.android.launcher3.util.ViewCache; 29 30 /** 31 * An interface to be used along with a context for various activities in Launcher. This allows a 32 * generic class to depend on Context subclass instead of an Activity. 33 */ 34 public interface ActivityContext { 35 finishAutoCancelActionMode()36 default boolean finishAutoCancelActionMode() { 37 return false; 38 } 39 getDotInfoForItem(ItemInfo info)40 default DotInfo getDotInfoForItem(ItemInfo info) { 41 return null; 42 } 43 44 /** 45 * For items with tree hierarchy, notifies the activity to invalidate the parent when a root 46 * is invalidated 47 * @param info info associated with a root node. 48 */ invalidateParent(ItemInfo info)49 default void invalidateParent(ItemInfo info) { } 50 getAccessibilityDelegate()51 default AccessibilityDelegate getAccessibilityDelegate() { 52 return null; 53 } 54 getFolderBoundingBox()55 default Rect getFolderBoundingBox() { 56 return getDeviceProfile().getAbsoluteOpenFolderBounds(); 57 } 58 59 /** 60 * After calling {@link #getFolderBoundingBox()}, we calculate a (left, top) position for a 61 * Folder of size width x height to be within those bounds. However, the chosen position may 62 * not be visually ideal (e.g. uncanny valley of centeredness), so here's a chance to update it. 63 * @param inOutPosition A 2-size array where the first element is the left position of the open 64 * folder and the second element is the top position. Should be updated in place if desired. 65 * @param bounds The bounds that the open folder should fit inside. 66 * @param width The width of the open folder. 67 * @param height The height of the open folder. 68 */ updateOpenFolderPosition(int[] inOutPosition, Rect bounds, int width, int height)69 default void updateOpenFolderPosition(int[] inOutPosition, Rect bounds, int width, int height) { 70 } 71 72 /** 73 * Returns a LayoutInflater that is cloned in this Context, so that Views inflated by it will 74 * have the same Context. (i.e. {@link #lookupContext(Context)} will find this ActivityContext.) 75 */ getLayoutInflater()76 default LayoutInflater getLayoutInflater() { 77 if (this instanceof Context) { 78 Context context = (Context) this; 79 return LayoutInflater.from(context).cloneInContext(context); 80 } 81 return null; 82 } 83 84 /** 85 * The root view to support drag-and-drop and popup support. 86 */ getDragLayer()87 BaseDragLayer getDragLayer(); 88 getDeviceProfile()89 DeviceProfile getDeviceProfile(); 90 getViewCache()91 default ViewCache getViewCache() { 92 return new ViewCache(); 93 } 94 95 /** 96 * Controller for supporting item drag-and-drop 97 */ getDragController()98 default <T extends DragController> T getDragController() { 99 return null; 100 } 101 102 /** 103 * Returns the ActivityContext associated with the given Context. 104 */ lookupContext(Context context)105 static <T extends Context & ActivityContext> T lookupContext(Context context) { 106 if (context instanceof ActivityContext) { 107 return (T) context; 108 } else if (context instanceof ContextWrapper) { 109 return lookupContext(((ContextWrapper) context).getBaseContext()); 110 } else { 111 throw new IllegalArgumentException("Cannot find ActivityContext in parent tree"); 112 } 113 } 114 } 115