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 import com.android.launcher3.tracing.InputConsumerProto;
25 import com.android.launcher3.tracing.TouchInteractionServiceProto;
26 
27 @TargetApi(Build.VERSION_CODES.O)
28 public interface InputConsumer {
29 
30     int TYPE_NO_OP = 1 << 0;
31     int TYPE_OVERVIEW = 1 << 1;
32     int TYPE_OTHER_ACTIVITY = 1 << 2;
33     int TYPE_ASSISTANT = 1 << 3;
34     int TYPE_DEVICE_LOCKED = 1 << 4;
35     int TYPE_ACCESSIBILITY = 1 << 5;
36     int TYPE_SCREEN_PINNED = 1 << 6;
37     int TYPE_OVERVIEW_WITHOUT_FOCUS = 1 << 7;
38     int TYPE_RESET_GESTURE = 1 << 8;
39     int TYPE_PROGRESS_DELEGATE = 1 << 9;
40     int TYPE_SYSUI_OVERLAY = 1 << 10;
41     int TYPE_ONE_HANDED = 1 << 11;
42     int TYPE_TASKBAR_STASH = 1 << 12;
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     };
59 
60     InputConsumer NO_OP = () -> TYPE_NO_OP;
61 
getType()62     int getType();
63 
64     /**
65      * Returns true if the user has crossed the threshold for it to be an explicit action.
66      */
allowInterceptByParent()67     default boolean allowInterceptByParent() {
68         return true;
69     }
70 
71     /**
72      * Returns true if the lifecycle of this input consumer is detached from the normal gesture
73      * down/up flow. If so, it is the responsibility of the input consumer to call back to
74      * {@link TouchInteractionService#onConsumerInactive(InputConsumer)} after the consumer is
75      * finished.
76      */
isConsumerDetachedFromGesture()77     default boolean isConsumerDetachedFromGesture() {
78         return false;
79     }
80 
81     /**
82      * Handle and specific setup necessary based on the orientation of the device
83      */
notifyOrientationSetup()84     default void notifyOrientationSetup() {}
85 
86     /**
87      * Returns the active input consumer is in the hierarchy of this input consumer.
88      */
getActiveConsumerInHierarchy()89     default InputConsumer getActiveConsumerInHierarchy() {
90         return this;
91     }
92 
93     /**
94      * Called by the event queue when the consumer is about to be switched to a new consumer.
95      * Consumers should update the state accordingly here before the state is passed to the new
96      * consumer.
97      */
onConsumerAboutToBeSwitched()98     default void onConsumerAboutToBeSwitched() { }
99 
onMotionEvent(MotionEvent ev)100     default void onMotionEvent(MotionEvent ev) { }
101 
onHoverEvent(MotionEvent ev)102     default void onHoverEvent(MotionEvent ev) { }
103 
onKeyEvent(KeyEvent ev)104     default void onKeyEvent(KeyEvent ev) { }
105 
onInputEvent(InputEvent ev)106     default void onInputEvent(InputEvent ev) {
107         if (ev instanceof MotionEvent) {
108             onMotionEvent((MotionEvent) ev);
109         } else {
110             onKeyEvent((KeyEvent) ev);
111         }
112     }
113 
getName()114     default String getName() {
115         StringBuilder name = new StringBuilder();
116         for (int i = 0; i < NAMES.length; i++) {
117             if ((getType() & (1 << i)) != 0) {
118                 if (name.length() > 0) {
119                     name.append(":");
120                 }
121                 name.append(NAMES[i]);
122             }
123         }
124         return name.toString();
125     }
126 
127     /**
128      * Used for winscope tracing, see launcher_trace.proto
129      * @see com.android.systemui.shared.tracing.ProtoTraceable#writeToProto
130      * @param serviceProto The parent of this proto message.
131      */
writeToProto(TouchInteractionServiceProto.Builder serviceProto)132     default void writeToProto(TouchInteractionServiceProto.Builder serviceProto) {
133         InputConsumerProto.Builder inputConsumerProto = InputConsumerProto.newBuilder();
134         inputConsumerProto.setName(getName());
135         writeToProtoInternal(inputConsumerProto);
136         serviceProto.setInputConsumer(inputConsumerProto);
137     }
138 
139     /**
140      * @see #writeToProto - allows subclasses to write additional info to the proto.
141      */
writeToProtoInternal(InputConsumerProto.Builder inputConsumerProto)142     default void writeToProtoInternal(InputConsumerProto.Builder inputConsumerProto) {}
143 }
144