blob: bf4880480ce7cdb121a72618614ae17c39fafecb [file] [log] [blame]
Garfield Tane84e6f92019-08-29 17:28:41 -07001/*
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
Prabir Pradhan48108662022-09-09 21:22:04 +000017#pragma once
Garfield Tane84e6f92019-08-29 17:28:41 -070018
19#include "InputDispatcherConfiguration.h"
20
Siarhei Vishniakoua7333112023-10-27 13:33:29 -070021#include <android-base/properties.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070022#include <binder/IBinder.h>
chaviw98318de2021-05-19 16:45:23 -050023#include <gui/InputApplication.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070024#include <input/Input.h>
25#include <utils/RefBase.h>
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000026#include <set>
Garfield Tane84e6f92019-08-29 17:28:41 -070027
28namespace android {
29
Garfield Tane84e6f92019-08-29 17:28:41 -070030/*
31 * Input dispatcher policy interface.
32 *
viktor8a478782023-06-17 09:06:40 +080033 * The input dispatcher policy is used by the input dispatcher to interact with the Window Manager
Garfield Tane84e6f92019-08-29 17:28:41 -070034 * 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 */
Prabir Pradhana41d2442023-04-20 21:30:40 +000039class InputDispatcherPolicyInterface {
Garfield Tane84e6f92019-08-29 17:28:41 -070040public:
Prabir Pradhana41d2442023-04-20 21:30:40 +000041 InputDispatcherPolicyInterface() = default;
42 virtual ~InputDispatcherPolicyInterface() = default;
43
Garfield Tane84e6f92019-08-29 17:28:41 -070044 /* Notifies the system that a configuration change has occurred. */
45 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
46
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050047 /* Notifies the system that an application does not have a focused window.
48 */
49 virtual void notifyNoFocusedWindowAnr(
50 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) = 0;
51
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +000052 /* Notifies the system that a window just became unresponsive. This indicates that ANR
Prabir Pradhanedd96402022-02-15 01:46:16 -080053 * should be raised for this window. The window can be identified via its input token and the
54 * pid of the owner. The string reason contains information about the input event that we
55 * haven't received a response for.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050056 */
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000057 virtual void notifyWindowUnresponsive(const sp<IBinder>& token, std::optional<gui::Pid> pid,
Prabir Pradhanedd96402022-02-15 01:46:16 -080058 const std::string& reason) = 0;
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050059
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +000060 /* Notifies the system that a window just became responsive. This is only called after the
61 * window was first marked "unresponsive". This indicates that ANR dialog (if any) should
62 * no longer should be shown to the user. The window is eligible to cause a new ANR in the
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050063 * future.
64 */
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000065 virtual void notifyWindowResponsive(const sp<IBinder>& token, std::optional<gui::Pid> pid) = 0;
Garfield Tane84e6f92019-08-29 17:28:41 -070066
67 /* Notifies the system that an input channel is unrecoverably broken. */
68 virtual void notifyInputChannelBroken(const sp<IBinder>& token) = 0;
69 virtual void notifyFocusChanged(const sp<IBinder>& oldToken, const sp<IBinder>& newToken) = 0;
Chris Yef59a2f42020-10-16 12:55:26 -070070 virtual void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
71 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
72 const std::vector<float>& values) = 0;
73 virtual void notifySensorAccuracy(int32_t deviceId, InputDeviceSensorType sensorType,
74 InputDeviceSensorAccuracy accuracy) = 0;
Chris Yefb552902021-02-03 17:18:37 -080075 virtual void notifyVibratorState(int32_t deviceId, bool isOn) = 0;
Garfield Tane84e6f92019-08-29 17:28:41 -070076
Garfield Tane84e6f92019-08-29 17:28:41 -070077 /* Filters an input event.
78 * Return true to dispatch the event unmodified, false to consume the event.
79 * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED
80 * to injectInputEvent.
81 */
Prabir Pradhana41d2442023-04-20 21:30:40 +000082 virtual bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) = 0;
Garfield Tane84e6f92019-08-29 17:28:41 -070083
84 /* Intercepts a key event immediately before queueing it.
85 * The policy can use this method as an opportunity to perform power management functions
86 * and early event preprocessing such as updating policy flags.
87 *
88 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
89 * should be dispatched to applications.
90 */
Prabir Pradhana41d2442023-04-20 21:30:40 +000091 virtual void interceptKeyBeforeQueueing(const KeyEvent& keyEvent, uint32_t& policyFlags) = 0;
Garfield Tane84e6f92019-08-29 17:28:41 -070092
93 /* Intercepts a touch, trackball or other motion event before queueing it.
94 * The policy can use this method as an opportunity to perform power management functions
95 * and early event preprocessing such as updating policy flags.
96 *
97 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
98 * should be dispatched to applications.
99 */
Prabir Pradhana41d2442023-04-20 21:30:40 +0000100 virtual void interceptMotionBeforeQueueing(int32_t displayId, nsecs_t when,
Garfield Tane84e6f92019-08-29 17:28:41 -0700101 uint32_t& policyFlags) = 0;
102
103 /* Allows the policy a chance to intercept a key before dispatching. */
104 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>& token,
Prabir Pradhana41d2442023-04-20 21:30:40 +0000105 const KeyEvent& keyEvent,
Garfield Tane84e6f92019-08-29 17:28:41 -0700106 uint32_t policyFlags) = 0;
107
108 /* Allows the policy a chance to perform default processing for an unhandled key.
Prabir Pradhana41d2442023-04-20 21:30:40 +0000109 * Returns an alternate key event to redispatch as a fallback, if needed. */
110 virtual std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>& token,
111 const KeyEvent& keyEvent,
112 uint32_t policyFlags) = 0;
Garfield Tane84e6f92019-08-29 17:28:41 -0700113
114 /* Notifies the policy about switch events.
115 */
116 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
117 uint32_t policyFlags) = 0;
118
119 /* Poke user activity for an event dispatched to a window. */
Sean Stoutb4e0a592021-02-23 07:34:53 -0800120 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType, int32_t displayId) = 0;
Garfield Tane84e6f92019-08-29 17:28:41 -0700121
Siarhei Vishniakoua7333112023-10-27 13:33:29 -0700122 /*
123 * Return true if the provided event is stale, and false otherwise. Used for determining
124 * whether the dispatcher should drop the event.
125 */
126 virtual bool isStaleEvent(nsecs_t currentTime, nsecs_t eventTime) {
127 static const std::chrono::duration STALE_EVENT_TIMEOUT =
128 std::chrono::seconds(10) * android::base::HwTimeoutMultiplier();
129 return std::chrono::nanoseconds(currentTime - eventTime) >= STALE_EVENT_TIMEOUT;
130 }
131
Garfield Tane84e6f92019-08-29 17:28:41 -0700132 /* Notifies the policy that a pointer down event has occurred outside the current focused
133 * window.
134 *
135 * The touchedToken passed as an argument is the window that received the input event.
136 */
137 virtual void onPointerDownOutsideFocus(const sp<IBinder>& touchedToken) = 0;
Prabir Pradhan99987712020-11-10 18:43:05 -0800138
139 /* Change the Pointer Capture state in InputReader.
140 *
141 * InputDispatcher is solely responsible for updating the Pointer Capture state.
142 */
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000143 virtual void setPointerCapture(const PointerCaptureRequest&) = 0;
arthurhungf452d0b2021-01-06 00:19:52 +0800144
145 /* Notifies the policy that the drag window has moved over to another window */
146 virtual void notifyDropWindow(const sp<IBinder>& token, float x, float y) = 0;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000147
148 /* Notifies the policy that there was an input device interaction with apps. */
149 virtual void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000150 const std::set<gui::Uid>& uids) = 0;
Garfield Tane84e6f92019-08-29 17:28:41 -0700151};
152
153} // namespace android