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.view.ContextThemeWrapper;
21 import android.view.View.AccessibilityDelegate;
22 
23 import com.android.launcher3.DeviceProfile;
24 import com.android.launcher3.ItemInfo;
25 import com.android.launcher3.graphics.RotationMode;
26 import com.android.launcher3.dot.DotInfo;
27 
28 /**
29  * An interface to be used along with a context for various activities in Launcher. This allows a
30  * generic class to depend on Context subclass instead of an Activity.
31  */
32 public interface ActivityContext {
33 
finishAutoCancelActionMode()34     default boolean finishAutoCancelActionMode() {
35         return false;
36     }
37 
getDotInfoForItem(ItemInfo info)38     default DotInfo getDotInfoForItem(ItemInfo info) {
39         return null;
40     }
41 
42     /**
43      * For items with tree hierarchy, notifies the activity to invalidate the parent when a root
44      * is invalidated
45      * @param info info associated with a root node.
46      */
invalidateParent(ItemInfo info)47     default void invalidateParent(ItemInfo info) { }
48 
getAccessibilityDelegate()49     default AccessibilityDelegate getAccessibilityDelegate() {
50         return null;
51     }
52 
53     /**
54      * The root view to support drag-and-drop and popup support.
55      */
getDragLayer()56     BaseDragLayer getDragLayer();
57 
getDeviceProfile()58     DeviceProfile getDeviceProfile();
59 
60     /**
61      * Device profile to be used by UI elements which are shown directly on top of the wallpaper
62      * and whose presentation is tied to the wallpaper (and physical device) and not the activity
63      * configuration.
64      */
getWallpaperDeviceProfile()65     default DeviceProfile getWallpaperDeviceProfile() {
66         return getDeviceProfile();
67     }
68 
getRotationMode()69     default RotationMode getRotationMode() {
70         return RotationMode.NORMAL;
71     }
72 
lookupContext(Context context)73     static ActivityContext lookupContext(Context context) {
74         if (context instanceof ActivityContext) {
75             return (ActivityContext) context;
76         } else if (context instanceof ContextThemeWrapper) {
77             return lookupContext(((ContextWrapper) context).getBaseContext());
78         } else {
79             throw new IllegalArgumentException("Cannot find ActivityContext in parent tree");
80         }
81     }
82 }
83