blob: b885ba17f31671604e38f8e9fa0ebf9aef196cf5 [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>
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070024#include <gui/PidUid.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070025#include <input/Input.h>
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070026#include <input/InputDevice.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070027#include <utils/RefBase.h>
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000028#include <set>
Garfield Tane84e6f92019-08-29 17:28:41 -070029
30namespace android {
31
Garfield Tane84e6f92019-08-29 17:28:41 -070032/*
33 * Input dispatcher policy interface.
34 *
viktor8a478782023-06-17 09:06:40 +080035 * The input dispatcher policy is used by the input dispatcher to interact with the Window Manager
Garfield Tane84e6f92019-08-29 17:28:41 -070036 * and other system components.
37 *
38 * The actual implementation is partially supported by callbacks into the DVM
39 * via JNI. This interface is also mocked in the unit tests.
40 */
Prabir Pradhana41d2442023-04-20 21:30:40 +000041class InputDispatcherPolicyInterface {
Garfield Tane84e6f92019-08-29 17:28:41 -070042public:
Prabir Pradhana41d2442023-04-20 21:30:40 +000043 InputDispatcherPolicyInterface() = default;
44 virtual ~InputDispatcherPolicyInterface() = default;
45
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050046 /* Notifies the system that an application does not have a focused window.
47 */
48 virtual void notifyNoFocusedWindowAnr(
49 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) = 0;
50
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +000051 /* Notifies the system that a window just became unresponsive. This indicates that ANR
Prabir Pradhanedd96402022-02-15 01:46:16 -080052 * should be raised for this window. The window can be identified via its input token and the
53 * pid of the owner. The string reason contains information about the input event that we
54 * haven't received a response for.
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050055 */
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000056 virtual void notifyWindowUnresponsive(const sp<IBinder>& token, std::optional<gui::Pid> pid,
Prabir Pradhanedd96402022-02-15 01:46:16 -080057 const std::string& reason) = 0;
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050058
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +000059 /* Notifies the system that a window just became responsive. This is only called after the
60 * window was first marked "unresponsive". This indicates that ANR dialog (if any) should
61 * 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 -050062 * future.
63 */
Prabir Pradhanaeebeb42023-06-13 19:53:03 +000064 virtual void notifyWindowResponsive(const sp<IBinder>& token, std::optional<gui::Pid> pid) = 0;
Garfield Tane84e6f92019-08-29 17:28:41 -070065
66 /* Notifies the system that an input channel is unrecoverably broken. */
67 virtual void notifyInputChannelBroken(const sp<IBinder>& token) = 0;
68 virtual void notifyFocusChanged(const sp<IBinder>& oldToken, const sp<IBinder>& newToken) = 0;
Chris Yef59a2f42020-10-16 12:55:26 -070069 virtual void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
70 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
71 const std::vector<float>& values) = 0;
72 virtual void notifySensorAccuracy(int32_t deviceId, InputDeviceSensorType sensorType,
73 InputDeviceSensorAccuracy accuracy) = 0;
Chris Yefb552902021-02-03 17:18:37 -080074 virtual void notifyVibratorState(int32_t deviceId, bool isOn) = 0;
Garfield Tane84e6f92019-08-29 17:28:41 -070075
Arpit Singhb65e2bd2024-06-03 09:48:16 +000076 /*
77 * Notifies the system that focused display has changed.
78 */
79 virtual void notifyFocusedDisplayChanged(ui::LogicalDisplayId displayId) = 0;
80
Garfield Tane84e6f92019-08-29 17:28:41 -070081 /* Filters an input event.
82 * Return true to dispatch the event unmodified, false to consume the event.
83 * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED
84 * to injectInputEvent.
85 */
Prabir Pradhana41d2442023-04-20 21:30:40 +000086 virtual bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) = 0;
Garfield Tane84e6f92019-08-29 17:28:41 -070087
88 /* Intercepts a key event immediately before queueing it.
89 * The policy can use this method as an opportunity to perform power management functions
90 * and early event preprocessing such as updating policy flags.
91 *
92 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
93 * should be dispatched to applications.
94 */
Prabir Pradhana41d2442023-04-20 21:30:40 +000095 virtual void interceptKeyBeforeQueueing(const KeyEvent& keyEvent, uint32_t& policyFlags) = 0;
Garfield Tane84e6f92019-08-29 17:28:41 -070096
97 /* Intercepts a touch, trackball or other motion event before queueing it.
98 * The policy can use this method as an opportunity to perform power management functions
99 * and early event preprocessing such as updating policy flags.
100 *
101 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
102 * should be dispatched to applications.
103 */
Linnan Li13bf76a2024-05-05 19:18:02 +0800104 virtual void interceptMotionBeforeQueueing(ui::LogicalDisplayId displayId, uint32_t source,
105 int32_t action, nsecs_t when,
106 uint32_t& policyFlags) = 0;
Garfield Tane84e6f92019-08-29 17:28:41 -0700107
108 /* Allows the policy a chance to intercept a key before dispatching. */
109 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>& token,
Prabir Pradhana41d2442023-04-20 21:30:40 +0000110 const KeyEvent& keyEvent,
Garfield Tane84e6f92019-08-29 17:28:41 -0700111 uint32_t policyFlags) = 0;
112
113 /* Allows the policy a chance to perform default processing for an unhandled key.
Prabir Pradhana41d2442023-04-20 21:30:40 +0000114 * Returns an alternate key event to redispatch as a fallback, if needed. */
115 virtual std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>& token,
116 const KeyEvent& keyEvent,
117 uint32_t policyFlags) = 0;
Garfield Tane84e6f92019-08-29 17:28:41 -0700118
119 /* Notifies the policy about switch events.
120 */
121 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
122 uint32_t policyFlags) = 0;
123
124 /* Poke user activity for an event dispatched to a window. */
Linnan Li13bf76a2024-05-05 19:18:02 +0800125 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType,
126 ui::LogicalDisplayId displayId) = 0;
Garfield Tane84e6f92019-08-29 17:28:41 -0700127
Siarhei Vishniakoua7333112023-10-27 13:33:29 -0700128 /*
129 * Return true if the provided event is stale, and false otherwise. Used for determining
130 * whether the dispatcher should drop the event.
131 */
132 virtual bool isStaleEvent(nsecs_t currentTime, nsecs_t eventTime) {
133 static const std::chrono::duration STALE_EVENT_TIMEOUT =
134 std::chrono::seconds(10) * android::base::HwTimeoutMultiplier();
135 return std::chrono::nanoseconds(currentTime - eventTime) >= STALE_EVENT_TIMEOUT;
136 }
137
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -0800138 /**
139 * Get the additional latency to add while waiting for other input events to process before
140 * dispatching the pending key.
141 * If there are unprocessed events, the pending key will not be dispatched immediately. Instead,
142 * the dispatcher will wait for this timeout, to account for the possibility that the focus
143 * might change due to touch or other events (such as another app getting launched by keys).
144 * This would give the pending key the opportunity to go to a newly focused window instead.
145 */
146 virtual std::chrono::nanoseconds getKeyWaitingForEventsTimeout() {
147 return KEY_WAITING_FOR_EVENTS_TIMEOUT;
148 }
149
Garfield Tane84e6f92019-08-29 17:28:41 -0700150 /* Notifies the policy that a pointer down event has occurred outside the current focused
151 * window.
152 *
153 * The touchedToken passed as an argument is the window that received the input event.
154 */
155 virtual void onPointerDownOutsideFocus(const sp<IBinder>& touchedToken) = 0;
Prabir Pradhan99987712020-11-10 18:43:05 -0800156
157 /* Change the Pointer Capture state in InputReader.
158 *
159 * InputDispatcher is solely responsible for updating the Pointer Capture state.
160 */
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000161 virtual void setPointerCapture(const PointerCaptureRequest&) = 0;
arthurhungf452d0b2021-01-06 00:19:52 +0800162
163 /* Notifies the policy that the drag window has moved over to another window */
164 virtual void notifyDropWindow(const sp<IBinder>& token, float x, float y) = 0;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000165
166 /* Notifies the policy that there was an input device interaction with apps. */
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700167 virtual void notifyDeviceInteraction(DeviceId deviceId, nsecs_t timestamp,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000168 const std::set<gui::Uid>& uids) = 0;
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -0800169
170private:
171 // Additional key latency in case a connection is still processing some motion events.
172 // This will help with the case when a user touched a button that opens a new window,
173 // and gives us the chance to dispatch the key to this new window.
174 static constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT =
175 std::chrono::milliseconds(500);
Garfield Tane84e6f92019-08-29 17:28:41 -0700176};
177
178} // namespace android