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.quickstep;
17 
18 import android.annotation.TargetApi;
19 import android.os.Build;
20 import android.view.InputEvent;
21 import android.view.KeyEvent;
22 import android.view.MotionEvent;
23 
24 @TargetApi(Build.VERSION_CODES.O)
25 public interface InputConsumer {
26 
27     int TYPE_NO_OP = 1 << 0;
28     int TYPE_OVERVIEW = 1 << 1;
29     int TYPE_OTHER_ACTIVITY = 1 << 2;
30     int TYPE_ASSISTANT = 1 << 3;
31     int TYPE_DEVICE_LOCKED = 1 << 4;
32     int TYPE_ACCESSIBILITY = 1 << 5;
33     int TYPE_SCREEN_PINNED = 1 << 6;
34     int TYPE_OVERVIEW_WITHOUT_FOCUS = 1 << 7;
35     int TYPE_RESET_GESTURE = 1 << 8;
36     int TYPE_PROGRESS_DELEGATE = 1 << 9;
37     int TYPE_SYSUI_OVERLAY = 1 << 10;
38     int TYPE_ONE_HANDED = 1 << 11;
39     int TYPE_TASKBAR_STASH = 1 << 12;
40     int TYPE_STATUS_BAR = 1 << 13;
41     int TYPE_CURSOR_HOVER = 1 << 14;
42     int TYPE_NAV_HANDLE_LONG_PRESS = 1 << 15;
43 
44     String[] NAMES = new String[] {
45            "TYPE_NO_OP",                    // 0
46             "TYPE_OVERVIEW",                // 1
47             "TYPE_OTHER_ACTIVITY",          // 2
48             "TYPE_ASSISTANT",               // 3
49             "TYPE_DEVICE_LOCKED",           // 4
50             "TYPE_ACCESSIBILITY",           // 5
51             "TYPE_SCREEN_PINNED",           // 6
52             "TYPE_OVERVIEW_WITHOUT_FOCUS",  // 7
53             "TYPE_RESET_GESTURE",           // 8
54             "TYPE_PROGRESS_DELEGATE",       // 9
55             "TYPE_SYSUI_OVERLAY",           // 10
56             "TYPE_ONE_HANDED",              // 11
57             "TYPE_TASKBAR_STASH",           // 12
58             "TYPE_STATUS_BAR",              // 13
59             "TYPE_CURSOR_HOVER",            // 14
60             "TYPE_NAV_HANDLE_LONG_PRESS",   // 15
61     };
62 
63     InputConsumer NO_OP = () -> TYPE_NO_OP;
64 
getType()65     int getType();
66 
67     /**
68      * Returns true if the user has crossed the threshold for it to be an explicit action.
69      */
allowInterceptByParent()70     default boolean allowInterceptByParent() {
71         return true;
72     }
73 
74     /**
75      * Returns true if the lifecycle of this input consumer is detached from the normal gesture
76      * down/up flow. If so, it is the responsibility of the input consumer to call back to
77      * {@link TouchInteractionService#onConsumerInactive(InputConsumer)} after the consumer is
78      * finished.
79      */
isConsumerDetachedFromGesture()80     default boolean isConsumerDetachedFromGesture() {
81         return false;
82     }
83 
84     /**
85      * Handle and specific setup necessary based on the orientation of the device
86      */
notifyOrientationSetup()87     default void notifyOrientationSetup() {}
88 
89     /**
90      * Returns the active input consumer is in the hierarchy of this input consumer.
91      */
getActiveConsumerInHierarchy()92     default InputConsumer getActiveConsumerInHierarchy() {
93         return this;
94     }
95 
96     /**
97      * Called by the event queue when the consumer is about to be switched to a new consumer.
98      * Consumers should update the state accordingly here before the state is passed to the new
99      * consumer.
100      */
onConsumerAboutToBeSwitched()101     default void onConsumerAboutToBeSwitched() { }
102 
onMotionEvent(MotionEvent ev)103     default void onMotionEvent(MotionEvent ev) { }
104 
onHoverEvent(MotionEvent ev)105     default void onHoverEvent(MotionEvent ev) { }
106 
onKeyEvent(KeyEvent ev)107     default void onKeyEvent(KeyEvent ev) { }
108 
onInputEvent(InputEvent ev)109     default void onInputEvent(InputEvent ev) {
110         if (ev instanceof MotionEvent) {
111             onMotionEvent((MotionEvent) ev);
112         } else {
113             onKeyEvent((KeyEvent) ev);
114         }
115     }
116 
getName()117     default String getName() {
118         StringBuilder name = new StringBuilder();
119         for (int i = 0; i < NAMES.length; i++) {
120             if ((getType() & (1 << i)) != 0) {
121                 if (name.length() > 0) {
122                     name.append(":");
123                 }
124                 name.append(NAMES[i]);
125             }
126         }
127         return name.toString();
128     }
129 
130     /**
131      * Returns an input consumer of the given class type, or null if none is found.
132      */
getInputConsumerOfClass(Class<T> c)133     default <T extends InputConsumer> T getInputConsumerOfClass(Class<T> c) {
134         if (getClass().equals(c)) {
135             return c.cast(this);
136         }
137         return null;
138     }
139 }
140