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 | #define LOG_TAG "InputDeviceMetricsCollector" |
| 18 | #include "InputDeviceMetricsCollector.h" |
| 19 | |
Prabir Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame] | 20 | #include "KeyCodeClassifications.h" |
| 21 | |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
| 23 | #include <input/PrintTools.h> |
| 24 | #include <linux/input.h> |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 25 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 26 | namespace android { |
| 27 | |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 28 | using android::base::StringPrintf; |
| 29 | using std::chrono::nanoseconds; |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | constexpr nanoseconds DEFAULT_USAGE_SESSION_TIMEOUT = std::chrono::seconds(5); |
| 34 | |
| 35 | /** |
| 36 | * Log debug messages about metrics events logged to statsd. |
| 37 | * Enable this via "adb shell setprop log.tag.InputDeviceMetricsCollector DEBUG" (requires restart) |
| 38 | */ |
| 39 | const bool DEBUG = __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG, ANDROID_LOG_INFO); |
| 40 | |
| 41 | int32_t linuxBusToInputDeviceBusEnum(int32_t linuxBus) { |
| 42 | switch (linuxBus) { |
| 43 | case BUS_USB: |
| 44 | return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__USB; |
| 45 | case BUS_BLUETOOTH: |
| 46 | return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__BLUETOOTH; |
| 47 | default: |
| 48 | return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__OTHER; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | class : public InputDeviceMetricsLogger { |
| 53 | nanoseconds getCurrentTime() override { return nanoseconds(systemTime(SYSTEM_TIME_MONOTONIC)); } |
| 54 | |
| 55 | void logInputDeviceUsageReported(const InputDeviceIdentifier& identifier, |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 56 | const DeviceUsageReport& report) override { |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 57 | const int32_t durationMillis = |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 58 | std::chrono::duration_cast<std::chrono::milliseconds>(report.usageDuration).count(); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 59 | const static std::vector<int32_t> empty; |
| 60 | |
| 61 | ALOGD_IF(DEBUG, "Usage session reported for device: %s", identifier.name.c_str()); |
| 62 | ALOGD_IF(DEBUG, " Total duration: %dms", durationMillis); |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 63 | ALOGD_IF(DEBUG, " Source breakdown:"); |
| 64 | |
| 65 | std::vector<int32_t> sources; |
| 66 | std::vector<int32_t> durationsPerSource; |
| 67 | for (auto& [src, dur] : report.sourceBreakdown) { |
| 68 | sources.push_back(ftl::to_underlying(src)); |
| 69 | int32_t durMillis = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count(); |
| 70 | durationsPerSource.emplace_back(durMillis); |
| 71 | ALOGD_IF(DEBUG, " - usageSource: %s\t duration: %dms", |
| 72 | ftl::enum_string(src).c_str(), durMillis); |
| 73 | } |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 74 | |
| 75 | util::stats_write(util::INPUTDEVICE_USAGE_REPORTED, identifier.vendor, identifier.product, |
| 76 | identifier.version, linuxBusToInputDeviceBusEnum(identifier.bus), |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 77 | durationMillis, sources, durationsPerSource, /*uids=*/empty, |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 78 | /*usage_durations_per_uid=*/empty); |
| 79 | } |
| 80 | } sStatsdLogger; |
| 81 | |
| 82 | bool isIgnoredInputDeviceId(int32_t deviceId) { |
| 83 | switch (deviceId) { |
| 84 | case INVALID_INPUT_DEVICE_ID: |
| 85 | case VIRTUAL_KEYBOARD_ID: |
| 86 | return true; |
| 87 | default: |
| 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | } // namespace |
| 93 | |
Prabir Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame] | 94 | InputDeviceUsageSource getUsageSourceForKeyArgs(const InputDeviceInfo& info, |
| 95 | const NotifyKeyArgs& keyArgs) { |
| 96 | if (!isFromSource(keyArgs.source, AINPUT_SOURCE_KEYBOARD)) { |
| 97 | return InputDeviceUsageSource::UNKNOWN; |
| 98 | } |
| 99 | |
| 100 | if (isFromSource(keyArgs.source, AINPUT_SOURCE_DPAD) && |
| 101 | DPAD_ALL_KEYCODES.count(keyArgs.keyCode) != 0) { |
| 102 | return InputDeviceUsageSource::DPAD; |
| 103 | } |
| 104 | |
| 105 | if (isFromSource(keyArgs.source, AINPUT_SOURCE_GAMEPAD) && |
| 106 | GAMEPAD_KEYCODES.count(keyArgs.keyCode) != 0) { |
| 107 | return InputDeviceUsageSource::GAMEPAD; |
| 108 | } |
| 109 | |
| 110 | if (info.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) { |
| 111 | return InputDeviceUsageSource::KEYBOARD; |
| 112 | } |
| 113 | |
| 114 | return InputDeviceUsageSource::BUTTONS; |
| 115 | } |
| 116 | |
| 117 | std::set<InputDeviceUsageSource> getUsageSourcesForMotionArgs(const NotifyMotionArgs& motionArgs) { |
| 118 | LOG_ALWAYS_FATAL_IF(motionArgs.pointerCount < 1, "Received motion args without pointers"); |
| 119 | std::set<InputDeviceUsageSource> sources; |
| 120 | |
| 121 | for (uint32_t i = 0; i < motionArgs.pointerCount; i++) { |
| 122 | const auto toolType = motionArgs.pointerProperties[i].toolType; |
| 123 | if (isFromSource(motionArgs.source, AINPUT_SOURCE_MOUSE)) { |
| 124 | if (toolType == ToolType::MOUSE) { |
| 125 | sources.emplace(InputDeviceUsageSource::MOUSE); |
| 126 | continue; |
| 127 | } |
| 128 | if (toolType == ToolType::FINGER) { |
| 129 | sources.emplace(InputDeviceUsageSource::TOUCHPAD); |
| 130 | continue; |
| 131 | } |
| 132 | if (isStylusToolType(toolType)) { |
| 133 | sources.emplace(InputDeviceUsageSource::STYLUS_INDIRECT); |
| 134 | continue; |
| 135 | } |
| 136 | } |
| 137 | if (isFromSource(motionArgs.source, AINPUT_SOURCE_MOUSE_RELATIVE) && |
| 138 | toolType == ToolType::MOUSE) { |
| 139 | sources.emplace(InputDeviceUsageSource::MOUSE_CAPTURED); |
| 140 | continue; |
| 141 | } |
| 142 | if (isFromSource(motionArgs.source, AINPUT_SOURCE_TOUCHPAD) && |
| 143 | toolType == ToolType::FINGER) { |
| 144 | sources.emplace(InputDeviceUsageSource::TOUCHPAD_CAPTURED); |
| 145 | continue; |
| 146 | } |
| 147 | if (isFromSource(motionArgs.source, AINPUT_SOURCE_BLUETOOTH_STYLUS) && |
| 148 | isStylusToolType(toolType)) { |
| 149 | sources.emplace(InputDeviceUsageSource::STYLUS_FUSED); |
| 150 | continue; |
| 151 | } |
| 152 | if (isFromSource(motionArgs.source, AINPUT_SOURCE_STYLUS) && isStylusToolType(toolType)) { |
| 153 | sources.emplace(InputDeviceUsageSource::STYLUS_DIRECT); |
| 154 | continue; |
| 155 | } |
| 156 | if (isFromSource(motionArgs.source, AINPUT_SOURCE_TOUCH_NAVIGATION)) { |
| 157 | sources.emplace(InputDeviceUsageSource::TOUCH_NAVIGATION); |
| 158 | continue; |
| 159 | } |
| 160 | if (isFromSource(motionArgs.source, AINPUT_SOURCE_JOYSTICK)) { |
| 161 | sources.emplace(InputDeviceUsageSource::JOYSTICK); |
| 162 | continue; |
| 163 | } |
| 164 | if (isFromSource(motionArgs.source, AINPUT_SOURCE_ROTARY_ENCODER)) { |
| 165 | sources.emplace(InputDeviceUsageSource::ROTARY_ENCODER); |
| 166 | continue; |
| 167 | } |
| 168 | if (isFromSource(motionArgs.source, AINPUT_SOURCE_TRACKBALL)) { |
| 169 | sources.emplace(InputDeviceUsageSource::TRACKBALL); |
| 170 | continue; |
| 171 | } |
| 172 | if (isFromSource(motionArgs.source, AINPUT_SOURCE_TOUCHSCREEN)) { |
| 173 | sources.emplace(InputDeviceUsageSource::TOUCHSCREEN); |
| 174 | continue; |
| 175 | } |
| 176 | sources.emplace(InputDeviceUsageSource::UNKNOWN); |
| 177 | } |
| 178 | |
| 179 | return sources; |
| 180 | } |
| 181 | |
| 182 | // --- InputDeviceMetricsCollector --- |
| 183 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 184 | InputDeviceMetricsCollector::InputDeviceMetricsCollector(InputListenerInterface& listener) |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 185 | : InputDeviceMetricsCollector(listener, sStatsdLogger, DEFAULT_USAGE_SESSION_TIMEOUT) {} |
| 186 | |
| 187 | InputDeviceMetricsCollector::InputDeviceMetricsCollector(InputListenerInterface& listener, |
| 188 | InputDeviceMetricsLogger& logger, |
| 189 | nanoseconds usageSessionTimeout) |
| 190 | : mNextListener(listener), mLogger(logger), mUsageSessionTimeout(usageSessionTimeout) {} |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 191 | |
| 192 | void InputDeviceMetricsCollector::notifyInputDevicesChanged( |
| 193 | const NotifyInputDevicesChangedArgs& args) { |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 194 | reportCompletedSessions(); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 195 | onInputDevicesChanged(args.inputDeviceInfos); |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 196 | mNextListener.notify(args); |
| 197 | } |
| 198 | |
| 199 | void InputDeviceMetricsCollector::notifyConfigurationChanged( |
| 200 | const NotifyConfigurationChangedArgs& args) { |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 201 | reportCompletedSessions(); |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 202 | mNextListener.notify(args); |
| 203 | } |
| 204 | |
| 205 | void InputDeviceMetricsCollector::notifyKey(const NotifyKeyArgs& args) { |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 206 | reportCompletedSessions(); |
| 207 | const SourceProvider getSources = [&args](const InputDeviceInfo& info) { |
| 208 | return std::set{getUsageSourceForKeyArgs(info, args)}; |
| 209 | }; |
| 210 | onInputDeviceUsage(DeviceId{args.deviceId}, nanoseconds(args.eventTime), getSources); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 211 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 212 | mNextListener.notify(args); |
| 213 | } |
| 214 | |
| 215 | void InputDeviceMetricsCollector::notifyMotion(const NotifyMotionArgs& args) { |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 216 | reportCompletedSessions(); |
| 217 | onInputDeviceUsage(DeviceId{args.deviceId}, nanoseconds(args.eventTime), |
| 218 | [&args](const auto&) { return getUsageSourcesForMotionArgs(args); }); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 219 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 220 | mNextListener.notify(args); |
| 221 | } |
| 222 | |
| 223 | void InputDeviceMetricsCollector::notifySwitch(const NotifySwitchArgs& args) { |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 224 | reportCompletedSessions(); |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 225 | mNextListener.notify(args); |
| 226 | } |
| 227 | |
| 228 | void InputDeviceMetricsCollector::notifySensor(const NotifySensorArgs& args) { |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 229 | reportCompletedSessions(); |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 230 | mNextListener.notify(args); |
| 231 | } |
| 232 | |
| 233 | void InputDeviceMetricsCollector::notifyVibratorState(const NotifyVibratorStateArgs& args) { |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 234 | reportCompletedSessions(); |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 235 | mNextListener.notify(args); |
| 236 | } |
| 237 | |
| 238 | void InputDeviceMetricsCollector::notifyDeviceReset(const NotifyDeviceResetArgs& args) { |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 239 | reportCompletedSessions(); |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 240 | mNextListener.notify(args); |
| 241 | } |
| 242 | |
| 243 | void InputDeviceMetricsCollector::notifyPointerCaptureChanged( |
| 244 | const NotifyPointerCaptureChangedArgs& args) { |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 245 | reportCompletedSessions(); |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 246 | mNextListener.notify(args); |
| 247 | } |
| 248 | |
| 249 | void InputDeviceMetricsCollector::dump(std::string& dump) { |
| 250 | dump += "InputDeviceMetricsCollector:\n"; |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 251 | |
| 252 | dump += " Logged device IDs: " + dumpMapKeys(mLoggedDeviceInfos, &toString) + "\n"; |
| 253 | dump += " Devices with active usage sessions: " + |
| 254 | dumpMapKeys(mActiveUsageSessions, &toString) + "\n"; |
| 255 | } |
| 256 | |
| 257 | void InputDeviceMetricsCollector::onInputDevicesChanged(const std::vector<InputDeviceInfo>& infos) { |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 258 | std::map<DeviceId, InputDeviceInfo> newDeviceInfos; |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 259 | |
| 260 | for (const InputDeviceInfo& info : infos) { |
| 261 | if (isIgnoredInputDeviceId(info.getId())) { |
| 262 | continue; |
| 263 | } |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 264 | newDeviceInfos.emplace(info.getId(), info); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 267 | for (auto [deviceId, info] : mLoggedDeviceInfos) { |
| 268 | if (newDeviceInfos.count(deviceId) != 0) { |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 269 | continue; |
| 270 | } |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 271 | onInputDeviceRemoved(deviceId, info.getIdentifier()); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 274 | std::swap(newDeviceInfos, mLoggedDeviceInfos); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void InputDeviceMetricsCollector::onInputDeviceRemoved(DeviceId deviceId, |
| 278 | const InputDeviceIdentifier& identifier) { |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 279 | auto it = mActiveUsageSessions.find(deviceId); |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 280 | if (it == mActiveUsageSessions.end()) { |
| 281 | return; |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 282 | } |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 283 | // Report usage for that device if there is an active session. |
| 284 | auto& [_, activeSession] = *it; |
| 285 | mLogger.logInputDeviceUsageReported(identifier, activeSession.finishSession()); |
| 286 | mActiveUsageSessions.erase(it); |
| 287 | |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 288 | // We don't remove this from mLoggedDeviceInfos because it will be updated in |
| 289 | // onInputDevicesChanged(). |
| 290 | } |
| 291 | |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 292 | void InputDeviceMetricsCollector::onInputDeviceUsage(DeviceId deviceId, nanoseconds eventTime, |
| 293 | const SourceProvider& getSources) { |
| 294 | auto infoIt = mLoggedDeviceInfos.find(deviceId); |
| 295 | if (infoIt == mLoggedDeviceInfos.end()) { |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 296 | // Do not track usage for devices that are not logged. |
| 297 | return; |
| 298 | } |
| 299 | |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 300 | auto [sessionIt, _] = |
| 301 | mActiveUsageSessions.try_emplace(deviceId, mUsageSessionTimeout, eventTime); |
| 302 | for (InputDeviceUsageSource source : getSources(infoIt->second)) { |
| 303 | sessionIt->second.recordUsage(eventTime, source); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 307 | void InputDeviceMetricsCollector::reportCompletedSessions() { |
| 308 | const auto currentTime = mLogger.getCurrentTime(); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 309 | |
| 310 | std::vector<DeviceId> completedUsageSessions; |
| 311 | |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 312 | for (auto& [deviceId, activeSession] : mActiveUsageSessions) { |
| 313 | if (activeSession.checkIfCompletedAt(currentTime)) { |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 314 | completedUsageSessions.emplace_back(deviceId); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | for (DeviceId deviceId : completedUsageSessions) { |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 319 | const auto infoIt = mLoggedDeviceInfos.find(deviceId); |
| 320 | LOG_ALWAYS_FATAL_IF(infoIt == mLoggedDeviceInfos.end()); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 321 | |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 322 | auto activeSessionIt = mActiveUsageSessions.find(deviceId); |
| 323 | LOG_ALWAYS_FATAL_IF(activeSessionIt == mActiveUsageSessions.end()); |
| 324 | auto& [_, activeSession] = *activeSessionIt; |
| 325 | mLogger.logInputDeviceUsageReported(infoIt->second.getIdentifier(), |
| 326 | activeSession.finishSession()); |
| 327 | mActiveUsageSessions.erase(activeSessionIt); |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 328 | } |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Prabir Pradhan | aff1303 | 2023-04-07 22:25:03 +0000 | [diff] [blame^] | 331 | // --- InputDeviceMetricsCollector::ActiveSession --- |
| 332 | |
| 333 | InputDeviceMetricsCollector::ActiveSession::ActiveSession(nanoseconds usageSessionTimeout, |
| 334 | nanoseconds startTime) |
| 335 | : mUsageSessionTimeout(usageSessionTimeout), mDeviceSession({startTime, startTime}) {} |
| 336 | |
| 337 | void InputDeviceMetricsCollector::ActiveSession::recordUsage(nanoseconds eventTime, |
| 338 | InputDeviceUsageSource source) { |
| 339 | // We assume that event times for subsequent events are always monotonically increasing for each |
| 340 | // input device. |
| 341 | auto [activeSourceIt, inserted] = |
| 342 | mActiveSessionsBySource.try_emplace(source, eventTime, eventTime); |
| 343 | if (!inserted) { |
| 344 | activeSourceIt->second.end = eventTime; |
| 345 | } |
| 346 | mDeviceSession.end = eventTime; |
| 347 | } |
| 348 | |
| 349 | bool InputDeviceMetricsCollector::ActiveSession::checkIfCompletedAt(nanoseconds timestamp) { |
| 350 | const auto sessionExpiryTime = timestamp - mUsageSessionTimeout; |
| 351 | std::vector<InputDeviceUsageSource> completedSourceSessionsForDevice; |
| 352 | for (auto& [source, session] : mActiveSessionsBySource) { |
| 353 | if (session.end <= sessionExpiryTime) { |
| 354 | completedSourceSessionsForDevice.emplace_back(source); |
| 355 | } |
| 356 | } |
| 357 | for (InputDeviceUsageSource source : completedSourceSessionsForDevice) { |
| 358 | auto it = mActiveSessionsBySource.find(source); |
| 359 | const auto& [_, session] = *it; |
| 360 | mSourceUsageBreakdown.emplace_back(source, session.end - session.start); |
| 361 | mActiveSessionsBySource.erase(it); |
| 362 | } |
| 363 | return mActiveSessionsBySource.empty(); |
| 364 | } |
| 365 | |
| 366 | InputDeviceMetricsLogger::DeviceUsageReport |
| 367 | InputDeviceMetricsCollector::ActiveSession::finishSession() { |
| 368 | const auto deviceUsageDuration = mDeviceSession.end - mDeviceSession.start; |
| 369 | |
| 370 | for (const auto& [source, sourceSession] : mActiveSessionsBySource) { |
| 371 | mSourceUsageBreakdown.emplace_back(source, sourceSession.end - sourceSession.start); |
| 372 | } |
| 373 | mActiveSessionsBySource.clear(); |
| 374 | |
| 375 | return {deviceUsageDuration, mSourceUsageBreakdown}; |
| 376 | } |
| 377 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 378 | } // namespace android |