blob: 1bcd527626ba1a92bff18d895f5ea4fad2acdd07 [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;
110 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override;
111 void notifyKey(const NotifyKeyArgs& args) override;
112 void notifyMotion(const NotifyMotionArgs& args) override;
113 void notifySwitch(const NotifySwitchArgs& args) override;
114 void notifySensor(const NotifySensorArgs& args) override;
115 void notifyVibratorState(const NotifyVibratorStateArgs& args) override;
116 void notifyDeviceReset(const NotifyDeviceResetArgs& args) override;
117 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override;
118
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000119 void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000120 const std::set<gui::Uid>& uids) override;
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000121 void dump(std::string& dump) override;
Prabir Pradhan95019ac2023-12-06 22:38:21 +0000122 void monitor() override;
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000123
124private:
Prabir Pradhan95019ac2023-12-06 22:38:21 +0000125 std::mutex mLock;
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000126 InputListenerInterface& mNextListener;
Prabir Pradhan95019ac2023-12-06 22:38:21 +0000127 InputDeviceMetricsLogger& mLogger GUARDED_BY(mLock);
Prabir Pradhan852db892023-04-06 22:16:44 +0000128 const std::chrono::nanoseconds mUsageSessionTimeout;
129
130 // Type-safe wrapper for input device id.
131 struct DeviceId : ftl::Constructible<DeviceId, std::int32_t>,
132 ftl::Equatable<DeviceId>,
133 ftl::Orderable<DeviceId> {
134 using Constructible::Constructible;
135 };
Prabir Pradhanaff13032023-04-07 22:25:03 +0000136 static inline std::string toString(const DeviceId& id) {
Prabir Pradhan852db892023-04-06 22:16:44 +0000137 return std::to_string(ftl::to_underlying(id));
138 }
139
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000140 using Uid = gui::Uid;
Prabir Pradhan0dd48ae2023-09-08 21:33:51 +0000141 using MetricsDeviceInfo = InputDeviceMetricsLogger::MetricsDeviceInfo;
Prabir Pradhan44293792023-05-08 19:37:44 +0000142
Prabir Pradhan95019ac2023-12-06 22:38:21 +0000143 std::map<DeviceId, MetricsDeviceInfo> mLoggedDeviceInfos GUARDED_BY(mLock);
Prabir Pradhan852db892023-04-06 22:16:44 +0000144
Prabir Pradhan44293792023-05-08 19:37:44 +0000145 using Interaction = std::tuple<DeviceId, std::chrono::nanoseconds, std::set<Uid>>;
Prabir Pradhan95019ac2023-12-06 22:38:21 +0000146 SyncQueue<Interaction> mInteractionsQueue GUARDED_BY(mLock);
Prabir Pradhan44293792023-05-08 19:37:44 +0000147
Prabir Pradhanaff13032023-04-07 22:25:03 +0000148 class ActiveSession {
149 public:
150 explicit ActiveSession(std::chrono::nanoseconds usageSessionTimeout,
151 std::chrono::nanoseconds startTime);
152 void recordUsage(std::chrono::nanoseconds eventTime, InputDeviceUsageSource source);
Prabir Pradhan44293792023-05-08 19:37:44 +0000153 void recordInteraction(const Interaction&);
Prabir Pradhanaff13032023-04-07 22:25:03 +0000154 bool checkIfCompletedAt(std::chrono::nanoseconds timestamp);
155 InputDeviceMetricsLogger::DeviceUsageReport finishSession();
156
157 private:
158 struct UsageSession {
159 std::chrono::nanoseconds start{};
160 std::chrono::nanoseconds end{};
161 };
162
163 const std::chrono::nanoseconds mUsageSessionTimeout;
164 UsageSession mDeviceSession{};
165
166 std::map<InputDeviceUsageSource, UsageSession> mActiveSessionsBySource{};
167 InputDeviceMetricsLogger::SourceUsageBreakdown mSourceUsageBreakdown{};
Prabir Pradhan44293792023-05-08 19:37:44 +0000168
169 std::map<Uid, UsageSession> mActiveSessionsByUid{};
170 InputDeviceMetricsLogger::UidUsageBreakdown mUidUsageBreakdown{};
Prabir Pradhan852db892023-04-06 22:16:44 +0000171 };
Prabir Pradhanaff13032023-04-07 22:25:03 +0000172
Prabir Pradhan852db892023-04-06 22:16:44 +0000173 // The input devices that currently have active usage sessions.
Prabir Pradhan95019ac2023-12-06 22:38:21 +0000174 std::map<DeviceId, ActiveSession> mActiveUsageSessions GUARDED_BY(mLock);
Prabir Pradhan852db892023-04-06 22:16:44 +0000175
Prabir Pradhan95019ac2023-12-06 22:38:21 +0000176 void onInputDevicesChanged(const std::vector<InputDeviceInfo>& infos) REQUIRES(mLock);
177 void onInputDeviceRemoved(DeviceId deviceId, const MetricsDeviceInfo& info) REQUIRES(mLock);
Prabir Pradhan0dd48ae2023-09-08 21:33:51 +0000178 using SourceProvider =
179 std::function<std::set<InputDeviceUsageSource>(const MetricsDeviceInfo&)>;
Prabir Pradhanaff13032023-04-07 22:25:03 +0000180 void onInputDeviceUsage(DeviceId deviceId, std::chrono::nanoseconds eventTime,
Prabir Pradhan95019ac2023-12-06 22:38:21 +0000181 const SourceProvider& getSources) REQUIRES(mLock);
182 void onInputDeviceInteraction(const Interaction&) REQUIRES(mLock);
183 void reportCompletedSessions() REQUIRES(mLock);
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000184};
185
186} // namespace android