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