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