| 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 | |
| Asmita Poddar | 631a14e | 2023-10-03 10:22:07 +0000 | [diff] [blame] | 19 | #include "InputDeviceMetricsSource.h" |
| Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 20 | #include "InputListener.h" |
| Prabir Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame] | 21 | #include "NotifyArgs.h" |
| Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 22 | #include "SyncQueue.h" |
| Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 23 | |
| Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 24 | #include <ftl/mixins.h> |
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 25 | #include <gui/WindowInfo.h> |
| Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 26 | #include <input/InputDevice.h> |
| 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 | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 55 | /** The logging interface for the metrics collector, injected for testing. */ |
| 56 | class InputDeviceMetricsLogger { |
| 57 | public: |
| 58 | virtual std::chrono::nanoseconds getCurrentTime() = 0; |
| Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 59 | |
| 60 | // Describes the breakdown of an input device usage session by its usage sources. |
| 61 | // An input device can have more than one usage source. For example, some game controllers have |
| 62 | // buttons, joysticks, and touchpads. We track usage by these sources to get a better picture of |
| 63 | // the device usage. The source breakdown of a 10 minute usage session could look like this: |
| 64 | // { {GAMEPAD, <9 mins>}, {TOUCHPAD, <2 mins>}, {TOUCHPAD, <3 mins>} } |
| 65 | // This would indicate that the GAMEPAD source was used first, and that source usage session |
| 66 | // lasted for 9 mins. During that time, the TOUCHPAD was used for 2 mins, until its source |
| 67 | // usage session expired. The TOUCHPAD was then used again later for another 3 mins. |
| 68 | using SourceUsageBreakdown = |
| 69 | std::vector<std::pair<InputDeviceUsageSource, std::chrono::nanoseconds /*duration*/>>; |
| 70 | |
| Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 71 | // Describes the breakdown of an input device usage session by the UIDs that it interacted with. |
| 72 | using UidUsageBreakdown = |
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 73 | std::vector<std::pair<gui::Uid, std::chrono::nanoseconds /*duration*/>>; |
| Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 74 | |
| Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 75 | struct DeviceUsageReport { |
| 76 | std::chrono::nanoseconds usageDuration; |
| 77 | SourceUsageBreakdown sourceBreakdown; |
| Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 78 | UidUsageBreakdown uidBreakdown; |
| Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| Prabir Pradhan | 0dd48ae | 2023-09-08 21:33:51 +0000 | [diff] [blame] | 81 | // A subset of information from the InputDeviceInfo class that is used for metrics collection, |
| 82 | // used to avoid copying and storing all of the fields and strings in InputDeviceInfo. |
| 83 | struct MetricsDeviceInfo { |
| 84 | int32_t deviceId; |
| 85 | int32_t vendor; |
| 86 | int32_t product; |
| 87 | int32_t version; |
| 88 | int32_t bus; |
| 89 | bool isUsiStylus; |
| 90 | int32_t keyboardType; |
| 91 | }; |
| 92 | virtual void logInputDeviceUsageReported(const MetricsDeviceInfo&, |
| 93 | const DeviceUsageReport&) = 0; |
| Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 94 | virtual ~InputDeviceMetricsLogger() = default; |
| 95 | }; |
| 96 | |
| Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 97 | class InputDeviceMetricsCollector : public InputDeviceMetricsCollectorInterface { |
| 98 | public: |
| 99 | explicit InputDeviceMetricsCollector(InputListenerInterface& listener); |
| 100 | ~InputDeviceMetricsCollector() override = default; |
| 101 | |
| Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 102 | // Test constructor |
| 103 | InputDeviceMetricsCollector(InputListenerInterface& listener, InputDeviceMetricsLogger& logger, |
| 104 | std::chrono::nanoseconds usageSessionTimeout); |
| 105 | |
| Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 106 | void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override; |
| 107 | void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override; |
| 108 | void notifyKey(const NotifyKeyArgs& args) override; |
| 109 | void notifyMotion(const NotifyMotionArgs& args) override; |
| 110 | void notifySwitch(const NotifySwitchArgs& args) override; |
| 111 | void notifySensor(const NotifySensorArgs& args) override; |
| 112 | void notifyVibratorState(const NotifyVibratorStateArgs& args) override; |
| 113 | void notifyDeviceReset(const NotifyDeviceResetArgs& args) override; |
| 114 | void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override; |
| 115 | |
| Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 116 | void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, |
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 117 | const std::set<gui::Uid>& uids) override; |
| Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 118 | void dump(std::string& dump) override; |
| 119 | |
| 120 | private: |
| 121 | InputListenerInterface& mNextListener; |
| Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 122 | InputDeviceMetricsLogger& mLogger; |
| 123 | const std::chrono::nanoseconds mUsageSessionTimeout; |
| 124 | |
| 125 | // Type-safe wrapper for input device id. |
| 126 | struct DeviceId : ftl::Constructible<DeviceId, std::int32_t>, |
| 127 | ftl::Equatable<DeviceId>, |
| 128 | ftl::Orderable<DeviceId> { |
| 129 | using Constructible::Constructible; |
| 130 | }; |
| Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 131 | static inline std::string toString(const DeviceId& id) { |
| Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 132 | return std::to_string(ftl::to_underlying(id)); |
| 133 | } |
| 134 | |
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 135 | using Uid = gui::Uid; |
| Prabir Pradhan | 0dd48ae | 2023-09-08 21:33:51 +0000 | [diff] [blame] | 136 | using MetricsDeviceInfo = InputDeviceMetricsLogger::MetricsDeviceInfo; |
| Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 137 | |
| Prabir Pradhan | 0dd48ae | 2023-09-08 21:33:51 +0000 | [diff] [blame] | 138 | std::map<DeviceId, MetricsDeviceInfo> mLoggedDeviceInfos; |
| Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 139 | |
| Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 140 | using Interaction = std::tuple<DeviceId, std::chrono::nanoseconds, std::set<Uid>>; |
| 141 | SyncQueue<Interaction> mInteractionsQueue; |
| 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); |
| Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 148 | void recordInteraction(const Interaction&); |
| Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 149 | bool checkIfCompletedAt(std::chrono::nanoseconds timestamp); |
| 150 | InputDeviceMetricsLogger::DeviceUsageReport finishSession(); |
| 151 | |
| 152 | private: |
| 153 | struct UsageSession { |
| 154 | std::chrono::nanoseconds start{}; |
| 155 | std::chrono::nanoseconds end{}; |
| 156 | }; |
| 157 | |
| 158 | const std::chrono::nanoseconds mUsageSessionTimeout; |
| 159 | UsageSession mDeviceSession{}; |
| 160 | |
| 161 | std::map<InputDeviceUsageSource, UsageSession> mActiveSessionsBySource{}; |
| 162 | InputDeviceMetricsLogger::SourceUsageBreakdown mSourceUsageBreakdown{}; |
| Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 163 | |
| 164 | std::map<Uid, UsageSession> mActiveSessionsByUid{}; |
| 165 | InputDeviceMetricsLogger::UidUsageBreakdown mUidUsageBreakdown{}; |
| Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 166 | }; |
| Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 167 | |
| Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 168 | // The input devices that currently have active usage sessions. |
| Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 169 | std::map<DeviceId, ActiveSession> mActiveUsageSessions; |
| Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 170 | |
| 171 | void onInputDevicesChanged(const std::vector<InputDeviceInfo>& infos); |
| Prabir Pradhan | 0dd48ae | 2023-09-08 21:33:51 +0000 | [diff] [blame] | 172 | void onInputDeviceRemoved(DeviceId deviceId, const MetricsDeviceInfo& info); |
| 173 | using SourceProvider = |
| 174 | std::function<std::set<InputDeviceUsageSource>(const MetricsDeviceInfo&)>; |
| Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 175 | void onInputDeviceUsage(DeviceId deviceId, std::chrono::nanoseconds eventTime, |
| 176 | const SourceProvider& getSources); |
| Prabir Pradhan | 4429379 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 177 | void onInputDeviceInteraction(const Interaction&); |
| Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame] | 178 | void reportCompletedSessions(); |
| Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | } // namespace android |