1 /*
2  * Copyright (C) 2019 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 #ifndef _UI_INPUT_INPUTDISPATCHER_INPUTDISPATCHERPOLICYINTERFACE_H
18 #define _UI_INPUT_INPUTDISPATCHER_INPUTDISPATCHERPOLICYINTERFACE_H
19 
20 #include "InputDispatcherConfiguration.h"
21 
22 #include <binder/IBinder.h>
23 #include <input/Input.h>
24 #include <utils/RefBase.h>
25 
26 namespace android {
27 
28 class InputApplicationHandle;
29 
30 /*
31  * Input dispatcher policy interface.
32  *
33  * The input reader policy is used by the input reader to interact with the Window Manager
34  * and other system components.
35  *
36  * The actual implementation is partially supported by callbacks into the DVM
37  * via JNI.  This interface is also mocked in the unit tests.
38  */
39 class InputDispatcherPolicyInterface : public virtual RefBase {
40 protected:
InputDispatcherPolicyInterface()41     InputDispatcherPolicyInterface() {}
~InputDispatcherPolicyInterface()42     virtual ~InputDispatcherPolicyInterface() {}
43 
44 public:
45     /* Notifies the system that a configuration change has occurred. */
46     virtual void notifyConfigurationChanged(nsecs_t when) = 0;
47 
48     /* Notifies the system that an application is not responding.
49      * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
50     virtual nsecs_t notifyAnr(const sp<InputApplicationHandle>& inputApplicationHandle,
51                               const sp<IBinder>& token, const std::string& reason) = 0;
52 
53     /* Notifies the system that an input channel is unrecoverably broken. */
54     virtual void notifyInputChannelBroken(const sp<IBinder>& token) = 0;
55     virtual void notifyFocusChanged(const sp<IBinder>& oldToken, const sp<IBinder>& newToken) = 0;
56 
57     /* Gets the input dispatcher configuration. */
58     virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) = 0;
59 
60     /* Filters an input event.
61      * Return true to dispatch the event unmodified, false to consume the event.
62      * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED
63      * to injectInputEvent.
64      */
65     virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) = 0;
66 
67     /* Intercepts a key event immediately before queueing it.
68      * The policy can use this method as an opportunity to perform power management functions
69      * and early event preprocessing such as updating policy flags.
70      *
71      * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
72      * should be dispatched to applications.
73      */
74     virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0;
75 
76     /* Intercepts a touch, trackball or other motion event before queueing it.
77      * The policy can use this method as an opportunity to perform power management functions
78      * and early event preprocessing such as updating policy flags.
79      *
80      * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
81      * should be dispatched to applications.
82      */
83     virtual void interceptMotionBeforeQueueing(const int32_t displayId, nsecs_t when,
84                                                uint32_t& policyFlags) = 0;
85 
86     /* Allows the policy a chance to intercept a key before dispatching. */
87     virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>& token,
88                                                   const KeyEvent* keyEvent,
89                                                   uint32_t policyFlags) = 0;
90 
91     /* Allows the policy a chance to perform default processing for an unhandled key.
92      * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */
93     virtual bool dispatchUnhandledKey(const sp<IBinder>& token, const KeyEvent* keyEvent,
94                                       uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0;
95 
96     /* Notifies the policy about switch events.
97      */
98     virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
99                               uint32_t policyFlags) = 0;
100 
101     /* Poke user activity for an event dispatched to a window. */
102     virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
103 
104     /* Checks whether a given application pid/uid has permission to inject input events
105      * into other applications.
106      *
107      * This method is special in that its implementation promises to be non-reentrant and
108      * is safe to call while holding other locks.  (Most other methods make no such guarantees!)
109      */
110     virtual bool checkInjectEventsPermissionNonReentrant(int32_t injectorPid,
111                                                          int32_t injectorUid) = 0;
112 
113     /* Notifies the policy that a pointer down event has occurred outside the current focused
114      * window.
115      *
116      * The touchedToken passed as an argument is the window that received the input event.
117      */
118     virtual void onPointerDownOutsideFocus(const sp<IBinder>& touchedToken) = 0;
119 };
120 
121 } // namespace android
122 
123 #endif // _UI_INPUT_INPUTDISPATCHER_INPUTDISPATCHERPOLICYINTERFACE_H
124