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