blob: d83924f2021698dd37652abf61b1f0873627ccd1 [file] [log] [blame]
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001/*
2 * Copyright 2023 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
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070019#include "InputDispatcherPolicyInterface.h"
20
Prabir Pradhan81e89fe2024-03-20 21:17:09 +000021#include "InputDispatcherInterface.h"
22#include "NotifyArgs.h"
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070023
Prabir Pradhan81e89fe2024-03-20 21:17:09 +000024#include <condition_variable>
25#include <functional>
26#include <memory>
27#include <mutex>
28#include <optional>
29#include <queue>
30#include <string>
31#include <vector>
32
33#include <android-base/logging.h>
34#include <android-base/thread_annotations.h>
35#include <binder/IBinder.h>
36#include <gui/PidUid.h>
37#include <gui/WindowInfo.h>
38#include <input/BlockingQueue.h>
39#include <input/Input.h>
40
41namespace android {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070042
43class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
44public:
45 FakeInputDispatcherPolicy() = default;
46 virtual ~FakeInputDispatcherPolicy() = default;
47
Prabir Pradhan81e89fe2024-03-20 21:17:09 +000048 struct AnrResult {
49 sp<IBinder> token{};
50 std::optional<gui::Pid> pid{};
51 };
52
53 struct UserActivityPokeEvent {
54 nsecs_t eventTime;
55 int32_t eventType;
56 int32_t displayId;
57
58 bool operator==(const UserActivityPokeEvent& rhs) const = default;
59 inline friend std::ostream& operator<<(std::ostream& os, const UserActivityPokeEvent& ev) {
60 os << "UserActivityPokeEvent[time=" << ev.eventTime << ", eventType=" << ev.eventType
61 << ", displayId=" << ev.displayId << "]";
62 return os;
63 }
64 };
65
66 void assertFilterInputEventWasCalled(const NotifyKeyArgs& args);
67 void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point);
68 void assertFilterInputEventWasNotCalled();
69 void assertNotifyConfigurationChangedWasCalled(nsecs_t when);
70 void assertNotifySwitchWasCalled(const NotifySwitchArgs& args);
71 void assertOnPointerDownEquals(const sp<IBinder>& touchedToken);
72 void assertOnPointerDownWasNotCalled();
73 /**
74 * This function must be called soon after the expected ANR timer starts,
75 * because we are also checking how much time has passed.
76 */
77 void assertNotifyNoFocusedWindowAnrWasCalled(
78 std::chrono::nanoseconds timeout,
79 const std::shared_ptr<InputApplicationHandle>& expectedApplication);
80 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
81 const sp<gui::WindowInfoHandle>& window);
82 void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout,
83 const sp<IBinder>& expectedToken,
84 std::optional<gui::Pid> expectedPid);
85 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
86 sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout);
87 void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken,
88 std::optional<gui::Pid> expectedPid);
89 /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */
90 sp<IBinder> getResponsiveWindowToken();
91 void assertNotifyAnrWasNotCalled();
92 PointerCaptureRequest assertSetPointerCaptureCalled(const sp<gui::WindowInfoHandle>& window,
93 bool enabled);
94 void assertSetPointerCaptureNotCalled();
95 void assertDropTargetEquals(const InputDispatcherInterface& dispatcher,
96 const sp<IBinder>& targetToken);
97 void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token);
98 /**
99 * Set policy timeout. A value of zero means next key will not be intercepted.
100 */
101 void setInterceptKeyTimeout(std::chrono::milliseconds timeout);
102 std::chrono::nanoseconds getKeyWaitingForEventsTimeout() override;
103 void setStaleEventTimeout(std::chrono::nanoseconds timeout);
104 void assertUserActivityNotPoked();
105 /**
106 * Asserts that a user activity poke has happened. The earliest recorded poke event will be
107 * cleared after this call.
108 *
109 * If an expected UserActivityPokeEvent is provided, asserts that the given event is the
110 * earliest recorded poke event.
111 */
112 void assertUserActivityPoked(std::optional<UserActivityPokeEvent> expectedPokeEvent = {});
113 void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids);
114 void assertNotifyDeviceInteractionWasNotCalled();
115 void setUnhandledKeyHandler(std::function<std::optional<KeyEvent>(const KeyEvent&)> handler);
116 void assertUnhandledKeyReported(int32_t keycode);
117 void assertUnhandledKeyNotReported();
118
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700119private:
Prabir Pradhan81e89fe2024-03-20 21:17:09 +0000120 std::mutex mLock;
121 std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock);
122 std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock);
123 sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock);
124 std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock);
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700125
Prabir Pradhan81e89fe2024-03-20 21:17:09 +0000126 std::condition_variable mPointerCaptureChangedCondition;
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700127
Prabir Pradhan81e89fe2024-03-20 21:17:09 +0000128 std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock);
129 // ANR handling
130 std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock);
131 std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock);
132 std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock);
133 std::condition_variable mNotifyAnr;
134 std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock);
135 std::condition_variable mNotifyInputChannelBroken;
136
137 sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock);
138 bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false;
139
140 std::condition_variable mNotifyUserActivity;
141 std::queue<UserActivityPokeEvent> mUserActivityPokeEvents;
142
143 std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
144
145 std::chrono::nanoseconds mStaleEventTimeout = 1000ms;
146
147 BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions;
148
149 std::condition_variable mNotifyUnhandledKey;
150 std::queue<int32_t> mReportedUnhandledKeycodes GUARDED_BY(mLock);
151 std::function<std::optional<KeyEvent>(const KeyEvent&)> mUnhandledKeyHandler GUARDED_BY(mLock);
152
153 /**
154 * All three ANR-related callbacks behave the same way, so we use this generic function to wait
155 * for a specific container to become non-empty. When the container is non-empty, return the
156 * first entry from the container and erase it.
157 */
158 template <class T>
159 T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage,
160 std::unique_lock<std::mutex>& lock) REQUIRES(mLock);
161
162 template <class T>
163 std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout,
164 std::queue<T>& storage,
165 std::unique_lock<std::mutex>& lock,
166 std::condition_variable& condition)
167 REQUIRES(mLock);
168
169 void notifyConfigurationChanged(nsecs_t when) override;
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700170 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<gui::Pid> pid,
Prabir Pradhan81e89fe2024-03-20 21:17:09 +0000171 const std::string&) override;
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700172 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
Prabir Pradhan81e89fe2024-03-20 21:17:09 +0000173 std::optional<gui::Pid> pid) override;
174 void notifyNoFocusedWindowAnr(
175 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override;
176 void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override;
177 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override;
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700178 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
179 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
Prabir Pradhan81e89fe2024-03-20 21:17:09 +0000180 const std::vector<float>& values) override;
181 void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType,
182 InputDeviceSensorAccuracy accuracy) override;
183 void notifyVibratorState(int32_t deviceId, bool isOn) override;
184 bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override;
185 void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override;
186 void interceptMotionBeforeQueueing(int32_t, uint32_t, int32_t, nsecs_t, uint32_t&) override;
187 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override;
188 std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent& event,
189 uint32_t) override;
190 void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
191 uint32_t policyFlags) override;
192 void pokeUserActivity(nsecs_t eventTime, int32_t eventType, int32_t displayId) override;
193 bool isStaleEvent(nsecs_t currentTime, nsecs_t eventTime) override;
194 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override;
195 void setPointerCapture(const PointerCaptureRequest& request) override;
196 void notifyDropWindow(const sp<IBinder>& token, float x, float y) override;
197 void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp,
198 const std::set<gui::Uid>& uids) override;
199 gui::Uid getPackageUid(std::string) override;
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700200
Prabir Pradhan81e89fe2024-03-20 21:17:09 +0000201 void assertFilterInputEventWasCalledInternal(
202 const std::function<void(const InputEvent&)>& verify);
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700203};
204
205} // namespace android