Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | #include "InputListener.h" |
Prabir Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame] | 20 | #include "NotifyArgs.h" |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 21 | #include "SyncQueue.h" |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 22 | |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 23 | #include <ftl/mixins.h> |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 24 | #include <gui/WindowInfo.h> |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 25 | #include <input/InputDevice.h> |
Prabir Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame] | 26 | #include <statslog.h> |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 27 | #include <chrono> |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 28 | #include <functional> |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 29 | #include <map> |
Prabir Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame] | 30 | #include <set> |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 33 | namespace android { |
| 34 | |
| 35 | /** |
| 36 | * Logs metrics about registered input devices and their usages. |
| 37 | * |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 38 | * All methods in the InputListenerInterface must be called from a single thread. |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 39 | */ |
| 40 | class InputDeviceMetricsCollectorInterface : public InputListenerInterface { |
| 41 | public: |
| 42 | /** |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 43 | * Notify the metrics collector that there was an input device interaction with apps. |
| 44 | * Called from the InputDispatcher thread. |
| 45 | */ |
| 46 | virtual void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 47 | const std::set<gui::Uid>& uids) = 0; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 48 | /** |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 49 | * Dump the state of the interaction blocker. |
| 50 | * This method may be called on any thread (usually by the input manager on a binder thread). |
| 51 | */ |
| 52 | virtual void dump(std::string& dump) = 0; |
| 53 | }; |
| 54 | |
Prabir Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame] | 55 | /** |
| 56 | * Enum representation of the InputDeviceUsageSource. |
| 57 | */ |
| 58 | enum class InputDeviceUsageSource : int32_t { |
| 59 | UNKNOWN = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__UNKNOWN, |
| 60 | BUTTONS = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__BUTTONS, |
| 61 | KEYBOARD = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__KEYBOARD, |
| 62 | DPAD = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__DPAD, |
| 63 | GAMEPAD = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__GAMEPAD, |
| 64 | JOYSTICK = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__JOYSTICK, |
| 65 | MOUSE = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__MOUSE, |
| 66 | MOUSE_CAPTURED = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__MOUSE_CAPTURED, |
| 67 | TOUCHPAD = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TOUCHPAD, |
| 68 | TOUCHPAD_CAPTURED = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TOUCHPAD_CAPTURED, |
| 69 | ROTARY_ENCODER = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__ROTARY_ENCODER, |
| 70 | STYLUS_DIRECT = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__STYLUS_DIRECT, |
| 71 | STYLUS_INDIRECT = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__STYLUS_INDIRECT, |
| 72 | STYLUS_FUSED = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__STYLUS_FUSED, |
| 73 | TOUCH_NAVIGATION = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TOUCH_NAVIGATION, |
| 74 | TOUCHSCREEN = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TOUCHSCREEN, |
| 75 | TRACKBALL = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TRACKBALL, |
| 76 | |
| 77 | ftl_first = UNKNOWN, |
| 78 | ftl_last = TRACKBALL, |
| 79 | }; |
| 80 | |
| 81 | /** Returns the InputDeviceUsageSource that corresponds to the key event. */ |
Prabir Pradhan | 0dd48ae | 2023-09-08 21:33:51 +0000 | [diff] [blame^] | 82 | InputDeviceUsageSource getUsageSourceForKeyArgs(int32_t keyboardType, const NotifyKeyArgs&); |
Prabir Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame] | 83 | |
| 84 | /** Returns the InputDeviceUsageSources that correspond to the motion event. */ |
| 85 | std::set<InputDeviceUsageSource> getUsageSourcesForMotionArgs(const NotifyMotionArgs&); |
| 86 | |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 87 | /** The logging interface for the metrics collector, injected for testing. */ |
| 88 | class InputDeviceMetricsLogger { |
| 89 | public: |
| 90 | virtual std::chrono::nanoseconds getCurrentTime() = 0; |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 91 | |
| 92 | // Describes the breakdown of an input device usage session by its usage sources. |
| 93 | // An input device can have more than one usage source. For example, some game controllers have |
| 94 | // buttons, joysticks, and touchpads. We track usage by these sources to get a better picture of |
| 95 | // the device usage. The source breakdown of a 10 minute usage session could look like this: |
| 96 | // { {GAMEPAD, <9 mins>}, {TOUCHPAD, <2 mins>}, {TOUCHPAD, <3 mins>} } |
| 97 | // This would indicate that the GAMEPAD source was used first, and that source usage session |
| 98 | // lasted for 9 mins. During that time, the TOUCHPAD was used for 2 mins, until its source |
| 99 | // usage session expired. The TOUCHPAD was then used again later for another 3 mins. |
| 100 | using SourceUsageBreakdown = |
| 101 | std::vector<std::pair<InputDeviceUsageSource, std::chrono::nanoseconds /*duration*/>>; |
| 102 | |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 103 | // Describes the breakdown of an input device usage session by the UIDs that it interacted with. |
| 104 | using UidUsageBreakdown = |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 105 | std::vector<std::pair<gui::Uid, std::chrono::nanoseconds /*duration*/>>; |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 106 | |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 107 | struct DeviceUsageReport { |
| 108 | std::chrono::nanoseconds usageDuration; |
| 109 | SourceUsageBreakdown sourceBreakdown; |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 110 | UidUsageBreakdown uidBreakdown; |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
Prabir Pradhan | 0dd48ae | 2023-09-08 21:33:51 +0000 | [diff] [blame^] | 113 | // A subset of information from the InputDeviceInfo class that is used for metrics collection, |
| 114 | // used to avoid copying and storing all of the fields and strings in InputDeviceInfo. |
| 115 | struct MetricsDeviceInfo { |
| 116 | int32_t deviceId; |
| 117 | int32_t vendor; |
| 118 | int32_t product; |
| 119 | int32_t version; |
| 120 | int32_t bus; |
| 121 | bool isUsiStylus; |
| 122 | int32_t keyboardType; |
| 123 | }; |
| 124 | virtual void logInputDeviceUsageReported(const MetricsDeviceInfo&, |
| 125 | const DeviceUsageReport&) = 0; |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 126 | virtual ~InputDeviceMetricsLogger() = default; |
| 127 | }; |
| 128 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 129 | class InputDeviceMetricsCollector : public InputDeviceMetricsCollectorInterface { |
| 130 | public: |
| 131 | explicit InputDeviceMetricsCollector(InputListenerInterface& listener); |
| 132 | ~InputDeviceMetricsCollector() override = default; |
| 133 | |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 134 | // Test constructor |
| 135 | InputDeviceMetricsCollector(InputListenerInterface& listener, InputDeviceMetricsLogger& logger, |
| 136 | std::chrono::nanoseconds usageSessionTimeout); |
| 137 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 138 | void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override; |
| 139 | void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override; |
| 140 | void notifyKey(const NotifyKeyArgs& args) override; |
| 141 | void notifyMotion(const NotifyMotionArgs& args) override; |
| 142 | void notifySwitch(const NotifySwitchArgs& args) override; |
| 143 | void notifySensor(const NotifySensorArgs& args) override; |
| 144 | void notifyVibratorState(const NotifyVibratorStateArgs& args) override; |
| 145 | void notifyDeviceReset(const NotifyDeviceResetArgs& args) override; |
| 146 | void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override; |
| 147 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 148 | void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 149 | const std::set<gui::Uid>& uids) override; |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 150 | void dump(std::string& dump) override; |
| 151 | |
| 152 | private: |
| 153 | InputListenerInterface& mNextListener; |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 154 | InputDeviceMetricsLogger& mLogger; |
| 155 | const std::chrono::nanoseconds mUsageSessionTimeout; |
| 156 | |
| 157 | // Type-safe wrapper for input device id. |
| 158 | struct DeviceId : ftl::Constructible<DeviceId, std::int32_t>, |
| 159 | ftl::Equatable<DeviceId>, |
| 160 | ftl::Orderable<DeviceId> { |
| 161 | using Constructible::Constructible; |
| 162 | }; |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 163 | static inline std::string toString(const DeviceId& id) { |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 164 | return std::to_string(ftl::to_underlying(id)); |
| 165 | } |
| 166 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 167 | using Uid = gui::Uid; |
Prabir Pradhan | 0dd48ae | 2023-09-08 21:33:51 +0000 | [diff] [blame^] | 168 | using MetricsDeviceInfo = InputDeviceMetricsLogger::MetricsDeviceInfo; |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 169 | |
Prabir Pradhan | 0dd48ae | 2023-09-08 21:33:51 +0000 | [diff] [blame^] | 170 | std::map<DeviceId, MetricsDeviceInfo> mLoggedDeviceInfos; |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 171 | |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 172 | using Interaction = std::tuple<DeviceId, std::chrono::nanoseconds, std::set<Uid>>; |
| 173 | SyncQueue<Interaction> mInteractionsQueue; |
| 174 | |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 175 | class ActiveSession { |
| 176 | public: |
| 177 | explicit ActiveSession(std::chrono::nanoseconds usageSessionTimeout, |
| 178 | std::chrono::nanoseconds startTime); |
| 179 | void recordUsage(std::chrono::nanoseconds eventTime, InputDeviceUsageSource source); |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 180 | void recordInteraction(const Interaction&); |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 181 | bool checkIfCompletedAt(std::chrono::nanoseconds timestamp); |
| 182 | InputDeviceMetricsLogger::DeviceUsageReport finishSession(); |
| 183 | |
| 184 | private: |
| 185 | struct UsageSession { |
| 186 | std::chrono::nanoseconds start{}; |
| 187 | std::chrono::nanoseconds end{}; |
| 188 | }; |
| 189 | |
| 190 | const std::chrono::nanoseconds mUsageSessionTimeout; |
| 191 | UsageSession mDeviceSession{}; |
| 192 | |
| 193 | std::map<InputDeviceUsageSource, UsageSession> mActiveSessionsBySource{}; |
| 194 | InputDeviceMetricsLogger::SourceUsageBreakdown mSourceUsageBreakdown{}; |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 195 | |
| 196 | std::map<Uid, UsageSession> mActiveSessionsByUid{}; |
| 197 | InputDeviceMetricsLogger::UidUsageBreakdown mUidUsageBreakdown{}; |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 198 | }; |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 199 | |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 200 | // The input devices that currently have active usage sessions. |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 201 | std::map<DeviceId, ActiveSession> mActiveUsageSessions; |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 202 | |
| 203 | void onInputDevicesChanged(const std::vector<InputDeviceInfo>& infos); |
Prabir Pradhan | 0dd48ae | 2023-09-08 21:33:51 +0000 | [diff] [blame^] | 204 | void onInputDeviceRemoved(DeviceId deviceId, const MetricsDeviceInfo& info); |
| 205 | using SourceProvider = |
| 206 | std::function<std::set<InputDeviceUsageSource>(const MetricsDeviceInfo&)>; |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 207 | void onInputDeviceUsage(DeviceId deviceId, std::chrono::nanoseconds eventTime, |
| 208 | const SourceProvider& getSources); |
Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 209 | void onInputDeviceInteraction(const Interaction&); |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 210 | void reportCompletedSessions(); |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | } // namespace android |