blob: 0a520e6869e7d4ee950e136b1d962a6d6d51b5b1 [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#pragma once
18
Asmita Poddar631a14e2023-10-03 10:22:07 +000019#include "InputDeviceMetricsSource.h"
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000020#include "InputListener.h"
Prabir Pradhanae10ee62023-05-12 19:44:18 +000021#include "NotifyArgs.h"
Prabir Pradhan44293792023-05-08 19:37:44 +000022#include "SyncQueue.h"
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000023
Prabir Pradhan95019ac2023-12-06 22:38:21 +000024#include <android-base/thread_annotations.h>
Prabir Pradhan852db892023-04-06 22:16:44 +000025#include <ftl/mixins.h>
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000026#include <gui/WindowInfo.h>
Prabir Pradhan852db892023-04-06 22:16:44 +000027#include <input/InputDevice.h>
28#include <chrono>
Prabir Pradhanaff13032023-04-07 22:25:03 +000029#include <functional>
Prabir Pradhan852db892023-04-06 22:16:44 +000030#include <map>
Prabir Pradhan95019ac2023-12-06 22:38:21 +000031#include <mutex>
Prabir Pradhanae10ee62023-05-12 19:44:18 +000032#include <set>
Prabir Pradhan852db892023-04-06 22:16:44 +000033#include <vector>
34
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000035namespace android {
36
37/**
38 * Logs metrics about registered input devices and their usages.
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000039 */
40class InputDeviceMetricsCollectorInterface : public InputListenerInterface {
41public:
42 /**
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000043 * 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 Pradhan8a5c41d2023-06-08 19:13:46 +000047 const std::set<gui::Uid>& uids) = 0;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000048 /**
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000049 * 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;
Prabir Pradhan95019ac2023-12-06 22:38:21 +000053
54 /** Called by the heartbeat to ensure that this component has not deadlocked. */
55 virtual void monitor() = 0;
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000056};
57
Prabir Pradhan852db892023-04-06 22:16:44 +000058/** The logging interface for the metrics collector, injected for testing. */
59class InputDeviceMetricsLogger {
60public:
61 virtual std::chrono::nanoseconds getCurrentTime() = 0;
Prabir Pradhanaff13032023-04-07 22:25:03 +000062
63 // Describes the breakdown of an input device usage session by its usage sources.
64 // An input device can have more than one usage source. For example, some game controllers have
65 // buttons, joysticks, and touchpads. We track usage by these sources to get a better picture of
66 // the device usage. The source breakdown of a 10 minute usage session could look like this:
67 // { {GAMEPAD, <9 mins>}, {TOUCHPAD, <2 mins>}, {TOUCHPAD, <3 mins>} }
68 // This would indicate that the GAMEPAD source was used first, and that source usage session
69 // lasted for 9 mins. During that time, the TOUCHPAD was used for 2 mins, until its source
70 // usage session expired. The TOUCHPAD was then used again later for another 3 mins.
71 using SourceUsageBreakdown =
72 std::vector<std::pair<InputDeviceUsageSource, std::chrono::nanoseconds /*duration*/>>;
73
Prabir Pradhan44293792023-05-08 19:37:44 +000074 // Describes the breakdown of an input device usage session by the UIDs that it interacted with.
75 using UidUsageBreakdown =
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000076 std::vector<std::pair<gui::Uid, std::chrono::nanoseconds /*duration*/>>;
Prabir Pradhan44293792023-05-08 19:37:44 +000077
Prabir Pradhanaff13032023-04-07 22:25:03 +000078 struct DeviceUsageReport {
79 std::chrono::nanoseconds usageDuration;
80 SourceUsageBreakdown sourceBreakdown;
Prabir Pradhan44293792023-05-08 19:37:44 +000081 UidUsageBreakdown uidBreakdown;
Prabir Pradhanaff13032023-04-07 22:25:03 +000082 };
83
Prabir Pradhan0dd48ae2023-09-08 21:33:51 +000084 // A subset of information from the InputDeviceInfo class that is used for metrics collection,
85 // used to avoid copying and storing all of the fields and strings in InputDeviceInfo.
86 struct MetricsDeviceInfo {
87 int32_t deviceId;
88 int32_t vendor;
89 int32_t product;
90 int32_t version;
91 int32_t bus;
92 bool isUsiStylus;
93 int32_t keyboardType;
94 };
95 virtual void logInputDeviceUsageReported(const MetricsDeviceInfo&,
96 const DeviceUsageReport&) = 0;
Prabir Pradhan852db892023-04-06 22:16:44 +000097 virtual ~InputDeviceMetricsLogger() = default;
98};
99
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000100class InputDeviceMetricsCollector : public InputDeviceMetricsCollectorInterface {
101public:
102 explicit InputDeviceMetricsCollector(InputListenerInterface& listener);
103 ~InputDeviceMetricsCollector() override = default;
104
Prabir Pradhan852db892023-04-06 22:16:44 +0000105 // Test constructor
106 InputDeviceMetricsCollector(InputListenerInterface& listener, InputDeviceMetricsLogger& logger,
107 std::chrono::nanoseconds usageSessionTimeout);
108
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000109 void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override;
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000110 void notifyKey(const NotifyKeyArgs& args) override;
111 void notifyMotion(const NotifyMotionArgs& args) override;
112 void notifySwitch(const NotifySwitchArgs& args) override;
113 void notifySensor(const NotifySensorArgs& args) override;
114 void notifyVibratorState(const NotifyVibratorStateArgs& args) override;
115 void notifyDeviceReset(const NotifyDeviceResetArgs& args) override;
116 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override;
117
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000118 void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000119 const std::set<gui::Uid>& uids) override;
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000120 void dump(std::string& dump) override;
Prabir Pradhan95019ac2023-12-06 22:38:21 +0000121 void monitor() override;
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000122
123private:
Prabir Pradhan95019ac2023-12-06 22:38:21 +0000124 std::mutex mLock;
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000125 InputListenerInterface& mNextListener;
Prabir Pradhan95019ac2023-12-06 22:38:21 +0000126 InputDeviceMetricsLogger& mLogger GUARDED_BY(mLock);
Prabir Pradhan852db892023-04-06 22:16:44 +0000127 const std::chrono::nanoseconds mUsageSessionTimeout;
128
129 // Type-safe wrapper for input device id.
130 struct DeviceId : ftl::Constructible<DeviceId, std::int32_t>,
131 ftl::Equatable<DeviceId>,
132 ftl::Orderable<DeviceId> {
133 using Constructible::Constructible;
134 };
Prabir Pradhanaff13032023-04-07 22:25:03 +0000135 static inline std::string toString(const DeviceId& id) {
Prabir Pradhan852db892023-04-06 22:16:44 +0000136 return std::to_string(ftl::to_underlying(id));
137 }
138
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000139 using Uid = gui::Uid;
Prabir Pradhan0dd48ae2023-09-08 21:33:51 +0000140 using MetricsDeviceInfo = InputDeviceMetricsLogger::MetricsDeviceInfo;
Prabir Pradhan44293792023-05-08 19:37:44 +0000141
Prabir Pradhan95019ac2023-12-06 22:38:21 +0000142 std::map<DeviceId, MetricsDeviceInfo> mLoggedDeviceInfos GUARDED_BY(mLock);
Prabir Pradhan852db892023-04-06 22:16:44 +0000143
Prabir Pradhan44293792023-05-08 19:37:44 +0000144 using Interaction = std::tuple<DeviceId, std::chrono::nanoseconds, std::set<Uid>>;
Prabir Pradhan95019ac2023-12-06 22:38:21 +0000145 SyncQueue<Interaction> mInteractionsQueue GUARDED_BY(mLock);
Prabir Pradhan44293792023-05-08 19:37:44 +0000146
Prabir Pradhanaff13032023-04-07 22:25:03 +0000147 class ActiveSession {
148 public:
149 explicit ActiveSession(std::chrono::nanoseconds usageSessionTimeout,
150 std::chrono::nanoseconds startTime);
151 void recordUsage(std::chrono::nanoseconds eventTime, InputDeviceUsageSource source);
Prabir Pradhan44293792023-05-08 19:37:44 +0000152 void recordInteraction(const Interaction&);
Prabir Pradhanaff13032023-04-07 22:25:03 +0000153 bool checkIfCompletedAt(std::chrono::nanoseconds timestamp);
154 InputDeviceMetricsLogger::DeviceUsageReport finishSession();
155
156 private:
157 struct UsageSession {
158 std::chrono::nanoseconds start{};
159 std::chrono::nanoseconds end{};
160 };
161
162 const std::chrono::nanoseconds mUsageSessionTimeout;
163 UsageSession mDeviceSession{};
164
165 std::map<InputDeviceUsageSource, UsageSession> mActiveSessionsBySource{};
166 InputDeviceMetricsLogger::SourceUsageBreakdown mSourceUsageBreakdown{};
Prabir Pradhan44293792023-05-08 19:37:44 +0000167
168 std::map<Uid, UsageSession> mActiveSessionsByUid{};
169 InputDeviceMetricsLogger::UidUsageBreakdown mUidUsageBreakdown{};
Prabir Pradhan852db892023-04-06 22:16:44 +0000170 };
Prabir Pradhanaff13032023-04-07 22:25:03 +0000171
Prabir Pradhan852db892023-04-06 22:16:44 +0000172 // The input devices that currently have active usage sessions.
Prabir Pradhan95019ac2023-12-06 22:38:21 +0000173 std::map<DeviceId, ActiveSession> mActiveUsageSessions GUARDED_BY(mLock);
Prabir Pradhan852db892023-04-06 22:16:44 +0000174
Prabir Pradhan95019ac2023-12-06 22:38:21 +0000175 void onInputDevicesChanged(const std::vector<InputDeviceInfo>& infos) REQUIRES(mLock);
176 void onInputDeviceRemoved(DeviceId deviceId, const MetricsDeviceInfo& info) REQUIRES(mLock);
Prabir Pradhan0dd48ae2023-09-08 21:33:51 +0000177 using SourceProvider =
178 std::function<std::set<InputDeviceUsageSource>(const MetricsDeviceInfo&)>;
Prabir Pradhanaff13032023-04-07 22:25:03 +0000179 void onInputDeviceUsage(DeviceId deviceId, std::chrono::nanoseconds eventTime,
Prabir Pradhan95019ac2023-12-06 22:38:21 +0000180 const SourceProvider& getSources) REQUIRES(mLock);
181 void onInputDeviceInteraction(const Interaction&) REQUIRES(mLock);
182 void reportCompletedSessions() REQUIRES(mLock);
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000183};
184
185} // namespace android