Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 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 | |
Prabir Pradhan | 4810866 | 2022-09-09 21:22:04 +0000 | [diff] [blame] | 17 | #pragma once |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 18 | |
| 19 | #include "InputDispatcherConfiguration.h" |
| 20 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 21 | #include <android-base/properties.h> |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 22 | #include <binder/IBinder.h> |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 23 | #include <gui/InputApplication.h> |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 24 | #include <gui/PidUid.h> |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 25 | #include <input/Input.h> |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 26 | #include <input/InputDevice.h> |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 27 | #include <utils/RefBase.h> |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 28 | #include <set> |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 32 | /* |
| 33 | * Input dispatcher policy interface. |
| 34 | * |
viktor | 8a47878 | 2023-06-17 09:06:40 +0800 | [diff] [blame] | 35 | * The input dispatcher policy is used by the input dispatcher to interact with the Window Manager |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 36 | * 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 Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 41 | class InputDispatcherPolicyInterface { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 42 | public: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 43 | InputDispatcherPolicyInterface() = default; |
| 44 | virtual ~InputDispatcherPolicyInterface() = default; |
| 45 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 46 | /* Notifies the system that a configuration change has occurred. */ |
| 47 | virtual void notifyConfigurationChanged(nsecs_t when) = 0; |
| 48 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 49 | /* Notifies the system that an application does not have a focused window. |
| 50 | */ |
| 51 | virtual void notifyNoFocusedWindowAnr( |
| 52 | const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) = 0; |
| 53 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 54 | /* Notifies the system that a window just became unresponsive. This indicates that ANR |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 55 | * should be raised for this window. The window can be identified via its input token and the |
| 56 | * pid of the owner. The string reason contains information about the input event that we |
| 57 | * haven't received a response for. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 58 | */ |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 59 | virtual void notifyWindowUnresponsive(const sp<IBinder>& token, std::optional<gui::Pid> pid, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 60 | const std::string& reason) = 0; |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 61 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 62 | /* Notifies the system that a window just became responsive. This is only called after the |
| 63 | * window was first marked "unresponsive". This indicates that ANR dialog (if any) should |
| 64 | * no longer should be shown to the user. The window is eligible to cause a new ANR in the |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 65 | * future. |
| 66 | */ |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 67 | virtual void notifyWindowResponsive(const sp<IBinder>& token, std::optional<gui::Pid> pid) = 0; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 68 | |
| 69 | /* Notifies the system that an input channel is unrecoverably broken. */ |
| 70 | virtual void notifyInputChannelBroken(const sp<IBinder>& token) = 0; |
| 71 | virtual void notifyFocusChanged(const sp<IBinder>& oldToken, const sp<IBinder>& newToken) = 0; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 72 | virtual void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 73 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 74 | const std::vector<float>& values) = 0; |
| 75 | virtual void notifySensorAccuracy(int32_t deviceId, InputDeviceSensorType sensorType, |
| 76 | InputDeviceSensorAccuracy accuracy) = 0; |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 77 | virtual void notifyVibratorState(int32_t deviceId, bool isOn) = 0; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 78 | |
Arpit Singh | b65e2bd | 2024-06-03 09:48:16 +0000 | [diff] [blame] | 79 | /* |
| 80 | * Notifies the system that focused display has changed. |
| 81 | */ |
| 82 | virtual void notifyFocusedDisplayChanged(ui::LogicalDisplayId displayId) = 0; |
| 83 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 84 | /* Filters an input event. |
| 85 | * Return true to dispatch the event unmodified, false to consume the event. |
| 86 | * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED |
| 87 | * to injectInputEvent. |
| 88 | */ |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 89 | virtual bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) = 0; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 90 | |
| 91 | /* Intercepts a key event immediately before queueing it. |
| 92 | * The policy can use this method as an opportunity to perform power management functions |
| 93 | * and early event preprocessing such as updating policy flags. |
| 94 | * |
| 95 | * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event |
| 96 | * should be dispatched to applications. |
| 97 | */ |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 98 | virtual void interceptKeyBeforeQueueing(const KeyEvent& keyEvent, uint32_t& policyFlags) = 0; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 99 | |
| 100 | /* Intercepts a touch, trackball or other motion event before queueing it. |
| 101 | * The policy can use this method as an opportunity to perform power management functions |
| 102 | * and early event preprocessing such as updating policy flags. |
| 103 | * |
| 104 | * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event |
| 105 | * should be dispatched to applications. |
| 106 | */ |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 107 | virtual void interceptMotionBeforeQueueing(ui::LogicalDisplayId displayId, uint32_t source, |
| 108 | int32_t action, nsecs_t when, |
| 109 | uint32_t& policyFlags) = 0; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 110 | |
| 111 | /* Allows the policy a chance to intercept a key before dispatching. */ |
| 112 | virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>& token, |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 113 | const KeyEvent& keyEvent, |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 114 | uint32_t policyFlags) = 0; |
| 115 | |
| 116 | /* Allows the policy a chance to perform default processing for an unhandled key. |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 117 | * Returns an alternate key event to redispatch as a fallback, if needed. */ |
| 118 | virtual std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>& token, |
| 119 | const KeyEvent& keyEvent, |
| 120 | uint32_t policyFlags) = 0; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 121 | |
| 122 | /* Notifies the policy about switch events. |
| 123 | */ |
| 124 | virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 125 | uint32_t policyFlags) = 0; |
| 126 | |
| 127 | /* Poke user activity for an event dispatched to a window. */ |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 128 | virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType, |
| 129 | ui::LogicalDisplayId displayId) = 0; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 130 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 131 | /* |
| 132 | * Return true if the provided event is stale, and false otherwise. Used for determining |
| 133 | * whether the dispatcher should drop the event. |
| 134 | */ |
| 135 | virtual bool isStaleEvent(nsecs_t currentTime, nsecs_t eventTime) { |
| 136 | static const std::chrono::duration STALE_EVENT_TIMEOUT = |
| 137 | std::chrono::seconds(10) * android::base::HwTimeoutMultiplier(); |
| 138 | return std::chrono::nanoseconds(currentTime - eventTime) >= STALE_EVENT_TIMEOUT; |
| 139 | } |
| 140 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 141 | /** |
| 142 | * Get the additional latency to add while waiting for other input events to process before |
| 143 | * dispatching the pending key. |
| 144 | * If there are unprocessed events, the pending key will not be dispatched immediately. Instead, |
| 145 | * the dispatcher will wait for this timeout, to account for the possibility that the focus |
| 146 | * might change due to touch or other events (such as another app getting launched by keys). |
| 147 | * This would give the pending key the opportunity to go to a newly focused window instead. |
| 148 | */ |
| 149 | virtual std::chrono::nanoseconds getKeyWaitingForEventsTimeout() { |
| 150 | return KEY_WAITING_FOR_EVENTS_TIMEOUT; |
| 151 | } |
| 152 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 153 | /* Notifies the policy that a pointer down event has occurred outside the current focused |
| 154 | * window. |
| 155 | * |
| 156 | * The touchedToken passed as an argument is the window that received the input event. |
| 157 | */ |
| 158 | virtual void onPointerDownOutsideFocus(const sp<IBinder>& touchedToken) = 0; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 159 | |
| 160 | /* Change the Pointer Capture state in InputReader. |
| 161 | * |
| 162 | * InputDispatcher is solely responsible for updating the Pointer Capture state. |
| 163 | */ |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 164 | virtual void setPointerCapture(const PointerCaptureRequest&) = 0; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 165 | |
| 166 | /* Notifies the policy that the drag window has moved over to another window */ |
| 167 | virtual void notifyDropWindow(const sp<IBinder>& token, float x, float y) = 0; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 168 | |
| 169 | /* Notifies the policy that there was an input device interaction with apps. */ |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 170 | virtual void notifyDeviceInteraction(DeviceId deviceId, nsecs_t timestamp, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 171 | const std::set<gui::Uid>& uids) = 0; |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 172 | |
| 173 | private: |
| 174 | // Additional key latency in case a connection is still processing some motion events. |
| 175 | // This will help with the case when a user touched a button that opens a new window, |
| 176 | // and gives us the chance to dispatch the key to this new window. |
| 177 | static constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = |
| 178 | std::chrono::milliseconds(500); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | } // namespace android |