blob: f92c4f47f1ec7034708b43251999ff0e64a987ad [file] [log] [blame]
Prabir Pradhanaddf8e92023-04-06 00:28:48 +00001/*
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 Pradhanae10ee62023-05-12 19:44:18 +000020#include "KeyCodeClassifications.h"
21
Prabir Pradhan852db892023-04-06 22:16:44 +000022#include <android-base/stringprintf.h>
23#include <input/PrintTools.h>
24#include <linux/input.h>
Prabir Pradhan852db892023-04-06 22:16:44 +000025
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000026namespace android {
27
Prabir Pradhan852db892023-04-06 22:16:44 +000028using android::base::StringPrintf;
29using std::chrono::nanoseconds;
Prabir Pradhan44293792023-05-08 19:37:44 +000030using std::chrono_literals::operator""ns;
Prabir Pradhan852db892023-04-06 22:16:44 +000031
32namespace {
33
34constexpr nanoseconds DEFAULT_USAGE_SESSION_TIMEOUT = std::chrono::seconds(5);
35
36/**
37 * Log debug messages about metrics events logged to statsd.
38 * Enable this via "adb shell setprop log.tag.InputDeviceMetricsCollector DEBUG" (requires restart)
39 */
40const bool DEBUG = __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG, ANDROID_LOG_INFO);
41
42int32_t linuxBusToInputDeviceBusEnum(int32_t linuxBus) {
43 switch (linuxBus) {
44 case BUS_USB:
45 return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__USB;
46 case BUS_BLUETOOTH:
47 return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__BLUETOOTH;
48 default:
49 return util::INPUT_DEVICE_USAGE_REPORTED__DEVICE_BUS__OTHER;
50 }
51}
52
53class : public InputDeviceMetricsLogger {
54 nanoseconds getCurrentTime() override { return nanoseconds(systemTime(SYSTEM_TIME_MONOTONIC)); }
55
56 void logInputDeviceUsageReported(const InputDeviceIdentifier& identifier,
Prabir Pradhanaff13032023-04-07 22:25:03 +000057 const DeviceUsageReport& report) override {
Prabir Pradhan852db892023-04-06 22:16:44 +000058 const int32_t durationMillis =
Prabir Pradhanaff13032023-04-07 22:25:03 +000059 std::chrono::duration_cast<std::chrono::milliseconds>(report.usageDuration).count();
Prabir Pradhan852db892023-04-06 22:16:44 +000060 const static std::vector<int32_t> empty;
61
62 ALOGD_IF(DEBUG, "Usage session reported for device: %s", identifier.name.c_str());
63 ALOGD_IF(DEBUG, " Total duration: %dms", durationMillis);
Prabir Pradhanaff13032023-04-07 22:25:03 +000064 ALOGD_IF(DEBUG, " Source breakdown:");
65
66 std::vector<int32_t> sources;
67 std::vector<int32_t> durationsPerSource;
68 for (auto& [src, dur] : report.sourceBreakdown) {
69 sources.push_back(ftl::to_underlying(src));
70 int32_t durMillis = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
71 durationsPerSource.emplace_back(durMillis);
72 ALOGD_IF(DEBUG, " - usageSource: %s\t duration: %dms",
73 ftl::enum_string(src).c_str(), durMillis);
74 }
Prabir Pradhan852db892023-04-06 22:16:44 +000075
Prabir Pradhan44293792023-05-08 19:37:44 +000076 ALOGD_IF(DEBUG, " Uid breakdown:");
77
78 std::vector<int32_t> uids;
79 std::vector<int32_t> durationsPerUid;
80 for (auto& [uid, dur] : report.uidBreakdown) {
81 uids.push_back(uid);
82 int32_t durMillis = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
83 durationsPerUid.push_back(durMillis);
84 ALOGD_IF(DEBUG, " - uid: %d\t duration: %dms", uid, durMillis);
85 }
Prabir Pradhan852db892023-04-06 22:16:44 +000086 util::stats_write(util::INPUTDEVICE_USAGE_REPORTED, identifier.vendor, identifier.product,
87 identifier.version, linuxBusToInputDeviceBusEnum(identifier.bus),
Prabir Pradhan44293792023-05-08 19:37:44 +000088 durationMillis, sources, durationsPerSource, uids, durationsPerUid);
Prabir Pradhan852db892023-04-06 22:16:44 +000089 }
90} sStatsdLogger;
91
92bool isIgnoredInputDeviceId(int32_t deviceId) {
93 switch (deviceId) {
94 case INVALID_INPUT_DEVICE_ID:
95 case VIRTUAL_KEYBOARD_ID:
96 return true;
97 default:
98 return false;
99 }
100}
101
102} // namespace
103
Prabir Pradhanae10ee62023-05-12 19:44:18 +0000104InputDeviceUsageSource getUsageSourceForKeyArgs(const InputDeviceInfo& info,
105 const NotifyKeyArgs& keyArgs) {
106 if (!isFromSource(keyArgs.source, AINPUT_SOURCE_KEYBOARD)) {
107 return InputDeviceUsageSource::UNKNOWN;
108 }
109
110 if (isFromSource(keyArgs.source, AINPUT_SOURCE_DPAD) &&
111 DPAD_ALL_KEYCODES.count(keyArgs.keyCode) != 0) {
112 return InputDeviceUsageSource::DPAD;
113 }
114
115 if (isFromSource(keyArgs.source, AINPUT_SOURCE_GAMEPAD) &&
116 GAMEPAD_KEYCODES.count(keyArgs.keyCode) != 0) {
117 return InputDeviceUsageSource::GAMEPAD;
118 }
119
120 if (info.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
121 return InputDeviceUsageSource::KEYBOARD;
122 }
123
124 return InputDeviceUsageSource::BUTTONS;
125}
126
127std::set<InputDeviceUsageSource> getUsageSourcesForMotionArgs(const NotifyMotionArgs& motionArgs) {
128 LOG_ALWAYS_FATAL_IF(motionArgs.pointerCount < 1, "Received motion args without pointers");
129 std::set<InputDeviceUsageSource> sources;
130
131 for (uint32_t i = 0; i < motionArgs.pointerCount; i++) {
132 const auto toolType = motionArgs.pointerProperties[i].toolType;
133 if (isFromSource(motionArgs.source, AINPUT_SOURCE_MOUSE)) {
134 if (toolType == ToolType::MOUSE) {
135 sources.emplace(InputDeviceUsageSource::MOUSE);
136 continue;
137 }
138 if (toolType == ToolType::FINGER) {
139 sources.emplace(InputDeviceUsageSource::TOUCHPAD);
140 continue;
141 }
142 if (isStylusToolType(toolType)) {
143 sources.emplace(InputDeviceUsageSource::STYLUS_INDIRECT);
144 continue;
145 }
146 }
147 if (isFromSource(motionArgs.source, AINPUT_SOURCE_MOUSE_RELATIVE) &&
148 toolType == ToolType::MOUSE) {
149 sources.emplace(InputDeviceUsageSource::MOUSE_CAPTURED);
150 continue;
151 }
152 if (isFromSource(motionArgs.source, AINPUT_SOURCE_TOUCHPAD) &&
153 toolType == ToolType::FINGER) {
154 sources.emplace(InputDeviceUsageSource::TOUCHPAD_CAPTURED);
155 continue;
156 }
157 if (isFromSource(motionArgs.source, AINPUT_SOURCE_BLUETOOTH_STYLUS) &&
158 isStylusToolType(toolType)) {
159 sources.emplace(InputDeviceUsageSource::STYLUS_FUSED);
160 continue;
161 }
162 if (isFromSource(motionArgs.source, AINPUT_SOURCE_STYLUS) && isStylusToolType(toolType)) {
163 sources.emplace(InputDeviceUsageSource::STYLUS_DIRECT);
164 continue;
165 }
166 if (isFromSource(motionArgs.source, AINPUT_SOURCE_TOUCH_NAVIGATION)) {
167 sources.emplace(InputDeviceUsageSource::TOUCH_NAVIGATION);
168 continue;
169 }
170 if (isFromSource(motionArgs.source, AINPUT_SOURCE_JOYSTICK)) {
171 sources.emplace(InputDeviceUsageSource::JOYSTICK);
172 continue;
173 }
174 if (isFromSource(motionArgs.source, AINPUT_SOURCE_ROTARY_ENCODER)) {
175 sources.emplace(InputDeviceUsageSource::ROTARY_ENCODER);
176 continue;
177 }
178 if (isFromSource(motionArgs.source, AINPUT_SOURCE_TRACKBALL)) {
179 sources.emplace(InputDeviceUsageSource::TRACKBALL);
180 continue;
181 }
182 if (isFromSource(motionArgs.source, AINPUT_SOURCE_TOUCHSCREEN)) {
183 sources.emplace(InputDeviceUsageSource::TOUCHSCREEN);
184 continue;
185 }
186 sources.emplace(InputDeviceUsageSource::UNKNOWN);
187 }
188
189 return sources;
190}
191
192// --- InputDeviceMetricsCollector ---
193
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000194InputDeviceMetricsCollector::InputDeviceMetricsCollector(InputListenerInterface& listener)
Prabir Pradhan852db892023-04-06 22:16:44 +0000195 : InputDeviceMetricsCollector(listener, sStatsdLogger, DEFAULT_USAGE_SESSION_TIMEOUT) {}
196
197InputDeviceMetricsCollector::InputDeviceMetricsCollector(InputListenerInterface& listener,
198 InputDeviceMetricsLogger& logger,
199 nanoseconds usageSessionTimeout)
200 : mNextListener(listener), mLogger(logger), mUsageSessionTimeout(usageSessionTimeout) {}
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000201
202void InputDeviceMetricsCollector::notifyInputDevicesChanged(
203 const NotifyInputDevicesChangedArgs& args) {
Prabir Pradhanaff13032023-04-07 22:25:03 +0000204 reportCompletedSessions();
Prabir Pradhan852db892023-04-06 22:16:44 +0000205 onInputDevicesChanged(args.inputDeviceInfos);
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000206 mNextListener.notify(args);
207}
208
209void InputDeviceMetricsCollector::notifyConfigurationChanged(
210 const NotifyConfigurationChangedArgs& args) {
Prabir Pradhanaff13032023-04-07 22:25:03 +0000211 reportCompletedSessions();
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000212 mNextListener.notify(args);
213}
214
215void InputDeviceMetricsCollector::notifyKey(const NotifyKeyArgs& args) {
Prabir Pradhanaff13032023-04-07 22:25:03 +0000216 reportCompletedSessions();
217 const SourceProvider getSources = [&args](const InputDeviceInfo& info) {
218 return std::set{getUsageSourceForKeyArgs(info, args)};
219 };
220 onInputDeviceUsage(DeviceId{args.deviceId}, nanoseconds(args.eventTime), getSources);
Prabir Pradhan852db892023-04-06 22:16:44 +0000221
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000222 mNextListener.notify(args);
223}
224
225void InputDeviceMetricsCollector::notifyMotion(const NotifyMotionArgs& args) {
Prabir Pradhanaff13032023-04-07 22:25:03 +0000226 reportCompletedSessions();
227 onInputDeviceUsage(DeviceId{args.deviceId}, nanoseconds(args.eventTime),
228 [&args](const auto&) { return getUsageSourcesForMotionArgs(args); });
Prabir Pradhan852db892023-04-06 22:16:44 +0000229
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000230 mNextListener.notify(args);
231}
232
233void InputDeviceMetricsCollector::notifySwitch(const NotifySwitchArgs& args) {
Prabir Pradhanaff13032023-04-07 22:25:03 +0000234 reportCompletedSessions();
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000235 mNextListener.notify(args);
236}
237
238void InputDeviceMetricsCollector::notifySensor(const NotifySensorArgs& args) {
Prabir Pradhanaff13032023-04-07 22:25:03 +0000239 reportCompletedSessions();
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000240 mNextListener.notify(args);
241}
242
243void InputDeviceMetricsCollector::notifyVibratorState(const NotifyVibratorStateArgs& args) {
Prabir Pradhanaff13032023-04-07 22:25:03 +0000244 reportCompletedSessions();
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000245 mNextListener.notify(args);
246}
247
248void InputDeviceMetricsCollector::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
Prabir Pradhanaff13032023-04-07 22:25:03 +0000249 reportCompletedSessions();
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000250 mNextListener.notify(args);
251}
252
253void InputDeviceMetricsCollector::notifyPointerCaptureChanged(
254 const NotifyPointerCaptureChangedArgs& args) {
Prabir Pradhanaff13032023-04-07 22:25:03 +0000255 reportCompletedSessions();
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000256 mNextListener.notify(args);
257}
258
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000259void InputDeviceMetricsCollector::notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp,
260 const std::set<int32_t>& uids) {
Prabir Pradhan44293792023-05-08 19:37:44 +0000261 std::set<Uid> typeSafeUids;
262 for (auto uid : uids) {
263 typeSafeUids.emplace(uid);
264 }
265 mInteractionsQueue.push(DeviceId{deviceId}, timestamp, typeSafeUids);
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000266}
267
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000268void InputDeviceMetricsCollector::dump(std::string& dump) {
269 dump += "InputDeviceMetricsCollector:\n";
Prabir Pradhan852db892023-04-06 22:16:44 +0000270
271 dump += " Logged device IDs: " + dumpMapKeys(mLoggedDeviceInfos, &toString) + "\n";
272 dump += " Devices with active usage sessions: " +
273 dumpMapKeys(mActiveUsageSessions, &toString) + "\n";
274}
275
276void InputDeviceMetricsCollector::onInputDevicesChanged(const std::vector<InputDeviceInfo>& infos) {
Prabir Pradhanaff13032023-04-07 22:25:03 +0000277 std::map<DeviceId, InputDeviceInfo> newDeviceInfos;
Prabir Pradhan852db892023-04-06 22:16:44 +0000278
279 for (const InputDeviceInfo& info : infos) {
280 if (isIgnoredInputDeviceId(info.getId())) {
281 continue;
282 }
Prabir Pradhanaff13032023-04-07 22:25:03 +0000283 newDeviceInfos.emplace(info.getId(), info);
Prabir Pradhan852db892023-04-06 22:16:44 +0000284 }
285
Prabir Pradhanaff13032023-04-07 22:25:03 +0000286 for (auto [deviceId, info] : mLoggedDeviceInfos) {
287 if (newDeviceInfos.count(deviceId) != 0) {
Prabir Pradhan852db892023-04-06 22:16:44 +0000288 continue;
289 }
Prabir Pradhanaff13032023-04-07 22:25:03 +0000290 onInputDeviceRemoved(deviceId, info.getIdentifier());
Prabir Pradhan852db892023-04-06 22:16:44 +0000291 }
292
Prabir Pradhanaff13032023-04-07 22:25:03 +0000293 std::swap(newDeviceInfos, mLoggedDeviceInfos);
Prabir Pradhan852db892023-04-06 22:16:44 +0000294}
295
296void InputDeviceMetricsCollector::onInputDeviceRemoved(DeviceId deviceId,
297 const InputDeviceIdentifier& identifier) {
Prabir Pradhan852db892023-04-06 22:16:44 +0000298 auto it = mActiveUsageSessions.find(deviceId);
Prabir Pradhanaff13032023-04-07 22:25:03 +0000299 if (it == mActiveUsageSessions.end()) {
300 return;
Prabir Pradhan852db892023-04-06 22:16:44 +0000301 }
Prabir Pradhanaff13032023-04-07 22:25:03 +0000302 // Report usage for that device if there is an active session.
303 auto& [_, activeSession] = *it;
304 mLogger.logInputDeviceUsageReported(identifier, activeSession.finishSession());
305 mActiveUsageSessions.erase(it);
306
Prabir Pradhan852db892023-04-06 22:16:44 +0000307 // We don't remove this from mLoggedDeviceInfos because it will be updated in
308 // onInputDevicesChanged().
309}
310
Prabir Pradhanaff13032023-04-07 22:25:03 +0000311void InputDeviceMetricsCollector::onInputDeviceUsage(DeviceId deviceId, nanoseconds eventTime,
312 const SourceProvider& getSources) {
313 auto infoIt = mLoggedDeviceInfos.find(deviceId);
314 if (infoIt == mLoggedDeviceInfos.end()) {
Prabir Pradhan852db892023-04-06 22:16:44 +0000315 // Do not track usage for devices that are not logged.
316 return;
317 }
318
Prabir Pradhanaff13032023-04-07 22:25:03 +0000319 auto [sessionIt, _] =
320 mActiveUsageSessions.try_emplace(deviceId, mUsageSessionTimeout, eventTime);
321 for (InputDeviceUsageSource source : getSources(infoIt->second)) {
322 sessionIt->second.recordUsage(eventTime, source);
Prabir Pradhan852db892023-04-06 22:16:44 +0000323 }
324}
325
Prabir Pradhan44293792023-05-08 19:37:44 +0000326void InputDeviceMetricsCollector::onInputDeviceInteraction(const Interaction& interaction) {
327 auto activeSessionIt = mActiveUsageSessions.find(std::get<DeviceId>(interaction));
328 if (activeSessionIt == mActiveUsageSessions.end()) {
329 return;
330 }
Prabir Pradhan852db892023-04-06 22:16:44 +0000331
Prabir Pradhan44293792023-05-08 19:37:44 +0000332 activeSessionIt->second.recordInteraction(interaction);
333}
334
335void InputDeviceMetricsCollector::reportCompletedSessions() {
336 // Process all pending interactions.
337 for (auto interaction = mInteractionsQueue.pop(); interaction;
338 interaction = mInteractionsQueue.pop()) {
339 onInputDeviceInteraction(*interaction);
340 }
341
342 const auto currentTime = mLogger.getCurrentTime();
Prabir Pradhan852db892023-04-06 22:16:44 +0000343 std::vector<DeviceId> completedUsageSessions;
344
Prabir Pradhan44293792023-05-08 19:37:44 +0000345 // Process usages for all active session to determine if any sessions have expired.
Prabir Pradhanaff13032023-04-07 22:25:03 +0000346 for (auto& [deviceId, activeSession] : mActiveUsageSessions) {
347 if (activeSession.checkIfCompletedAt(currentTime)) {
Prabir Pradhan852db892023-04-06 22:16:44 +0000348 completedUsageSessions.emplace_back(deviceId);
349 }
350 }
351
Prabir Pradhan44293792023-05-08 19:37:44 +0000352 // Close out and log all expired usage sessions.
Prabir Pradhan852db892023-04-06 22:16:44 +0000353 for (DeviceId deviceId : completedUsageSessions) {
Prabir Pradhanaff13032023-04-07 22:25:03 +0000354 const auto infoIt = mLoggedDeviceInfos.find(deviceId);
355 LOG_ALWAYS_FATAL_IF(infoIt == mLoggedDeviceInfos.end());
Prabir Pradhan852db892023-04-06 22:16:44 +0000356
Prabir Pradhanaff13032023-04-07 22:25:03 +0000357 auto activeSessionIt = mActiveUsageSessions.find(deviceId);
358 LOG_ALWAYS_FATAL_IF(activeSessionIt == mActiveUsageSessions.end());
359 auto& [_, activeSession] = *activeSessionIt;
360 mLogger.logInputDeviceUsageReported(infoIt->second.getIdentifier(),
361 activeSession.finishSession());
362 mActiveUsageSessions.erase(activeSessionIt);
Prabir Pradhan852db892023-04-06 22:16:44 +0000363 }
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000364}
365
Prabir Pradhanaff13032023-04-07 22:25:03 +0000366// --- InputDeviceMetricsCollector::ActiveSession ---
367
368InputDeviceMetricsCollector::ActiveSession::ActiveSession(nanoseconds usageSessionTimeout,
369 nanoseconds startTime)
370 : mUsageSessionTimeout(usageSessionTimeout), mDeviceSession({startTime, startTime}) {}
371
372void InputDeviceMetricsCollector::ActiveSession::recordUsage(nanoseconds eventTime,
373 InputDeviceUsageSource source) {
374 // We assume that event times for subsequent events are always monotonically increasing for each
375 // input device.
376 auto [activeSourceIt, inserted] =
377 mActiveSessionsBySource.try_emplace(source, eventTime, eventTime);
378 if (!inserted) {
379 activeSourceIt->second.end = eventTime;
380 }
381 mDeviceSession.end = eventTime;
382}
383
Prabir Pradhan44293792023-05-08 19:37:44 +0000384void InputDeviceMetricsCollector::ActiveSession::recordInteraction(const Interaction& interaction) {
385 const auto sessionExpiryTime = mDeviceSession.end + mUsageSessionTimeout;
386 const auto timestamp = std::get<nanoseconds>(interaction);
387 if (timestamp >= sessionExpiryTime) {
388 // This interaction occurred after the device's current active session is set to expire.
389 // Ignore it.
390 return;
391 }
392
393 for (Uid uid : std::get<std::set<Uid>>(interaction)) {
394 auto [activeUidIt, inserted] = mActiveSessionsByUid.try_emplace(uid, timestamp, timestamp);
395 if (!inserted) {
396 activeUidIt->second.end = timestamp;
397 }
398 }
399}
400
Prabir Pradhanaff13032023-04-07 22:25:03 +0000401bool InputDeviceMetricsCollector::ActiveSession::checkIfCompletedAt(nanoseconds timestamp) {
402 const auto sessionExpiryTime = timestamp - mUsageSessionTimeout;
403 std::vector<InputDeviceUsageSource> completedSourceSessionsForDevice;
404 for (auto& [source, session] : mActiveSessionsBySource) {
405 if (session.end <= sessionExpiryTime) {
406 completedSourceSessionsForDevice.emplace_back(source);
407 }
408 }
409 for (InputDeviceUsageSource source : completedSourceSessionsForDevice) {
410 auto it = mActiveSessionsBySource.find(source);
411 const auto& [_, session] = *it;
412 mSourceUsageBreakdown.emplace_back(source, session.end - session.start);
413 mActiveSessionsBySource.erase(it);
414 }
Prabir Pradhan44293792023-05-08 19:37:44 +0000415
416 std::vector<Uid> completedUidSessionsForDevice;
417 for (auto& [uid, session] : mActiveSessionsByUid) {
418 if (session.end <= sessionExpiryTime) {
419 completedUidSessionsForDevice.emplace_back(uid);
420 }
421 }
422 for (Uid uid : completedUidSessionsForDevice) {
423 auto it = mActiveSessionsByUid.find(uid);
424 const auto& [_, session] = *it;
425 mUidUsageBreakdown.emplace_back(uid, session.end - session.start);
426 mActiveSessionsByUid.erase(it);
427 }
428
429 // This active session has expired if there are no more active source sessions tracked.
Prabir Pradhanaff13032023-04-07 22:25:03 +0000430 return mActiveSessionsBySource.empty();
431}
432
433InputDeviceMetricsLogger::DeviceUsageReport
434InputDeviceMetricsCollector::ActiveSession::finishSession() {
435 const auto deviceUsageDuration = mDeviceSession.end - mDeviceSession.start;
436
437 for (const auto& [source, sourceSession] : mActiveSessionsBySource) {
438 mSourceUsageBreakdown.emplace_back(source, sourceSession.end - sourceSession.start);
439 }
440 mActiveSessionsBySource.clear();
441
Prabir Pradhan44293792023-05-08 19:37:44 +0000442 for (const auto& [uid, uidSession] : mActiveSessionsByUid) {
443 mUidUsageBreakdown.emplace_back(uid, uidSession.end - uidSession.start);
444 }
445 mActiveSessionsByUid.clear();
446
447 return {deviceUsageDuration, mSourceUsageBreakdown, mUidUsageBreakdown};
Prabir Pradhanaff13032023-04-07 22:25:03 +0000448}
449
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000450} // namespace android