blob: 9633664a0e9680f9a1097120c45015c33a98d711 [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 Pradhan852db892023-04-06 22:16:44 +000024#include <ftl/mixins.h>
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000025#include <gui/WindowInfo.h>
Prabir Pradhan852db892023-04-06 22:16:44 +000026#include <input/InputDevice.h>
27#include <chrono>
Prabir Pradhanaff13032023-04-07 22:25:03 +000028#include <functional>
Prabir Pradhan852db892023-04-06 22:16:44 +000029#include <map>
Prabir Pradhanae10ee62023-05-12 19:44:18 +000030#include <set>
Prabir Pradhan852db892023-04-06 22:16:44 +000031#include <vector>
32
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000033namespace android {
34
35/**
36 * Logs metrics about registered input devices and their usages.
37 *
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000038 * All methods in the InputListenerInterface must be called from a single thread.
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;
53};
54
Prabir Pradhan852db892023-04-06 22:16:44 +000055/** The logging interface for the metrics collector, injected for testing. */
56class InputDeviceMetricsLogger {
57public:
58 virtual std::chrono::nanoseconds getCurrentTime() = 0;
Prabir Pradhanaff13032023-04-07 22:25:03 +000059
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 Pradhan44293792023-05-08 19:37:44 +000071 // Describes the breakdown of an input device usage session by the UIDs that it interacted with.
72 using UidUsageBreakdown =
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +000073 std::vector<std::pair<gui::Uid, std::chrono::nanoseconds /*duration*/>>;
Prabir Pradhan44293792023-05-08 19:37:44 +000074
Prabir Pradhanaff13032023-04-07 22:25:03 +000075 struct DeviceUsageReport {
76 std::chrono::nanoseconds usageDuration;
77 SourceUsageBreakdown sourceBreakdown;
Prabir Pradhan44293792023-05-08 19:37:44 +000078 UidUsageBreakdown uidBreakdown;
Prabir Pradhanaff13032023-04-07 22:25:03 +000079 };
80
Prabir Pradhan0dd48ae2023-09-08 21:33:51 +000081 // 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 Pradhan852db892023-04-06 22:16:44 +000094 virtual ~InputDeviceMetricsLogger() = default;
95};
96
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000097class InputDeviceMetricsCollector : public InputDeviceMetricsCollectorInterface {
98public:
99 explicit InputDeviceMetricsCollector(InputListenerInterface& listener);
100 ~InputDeviceMetricsCollector() override = default;
101
Prabir Pradhan852db892023-04-06 22:16:44 +0000102 // Test constructor
103 InputDeviceMetricsCollector(InputListenerInterface& listener, InputDeviceMetricsLogger& logger,
104 std::chrono::nanoseconds usageSessionTimeout);
105
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000106 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 Pradhan8ede1d12023-05-08 19:37:44 +0000116 void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000117 const std::set<gui::Uid>& uids) override;
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000118 void dump(std::string& dump) override;
119
120private:
121 InputListenerInterface& mNextListener;
Prabir Pradhan852db892023-04-06 22:16:44 +0000122 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 Pradhanaff13032023-04-07 22:25:03 +0000131 static inline std::string toString(const DeviceId& id) {
Prabir Pradhan852db892023-04-06 22:16:44 +0000132 return std::to_string(ftl::to_underlying(id));
133 }
134
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000135 using Uid = gui::Uid;
Prabir Pradhan0dd48ae2023-09-08 21:33:51 +0000136 using MetricsDeviceInfo = InputDeviceMetricsLogger::MetricsDeviceInfo;
Prabir Pradhan44293792023-05-08 19:37:44 +0000137
Prabir Pradhan0dd48ae2023-09-08 21:33:51 +0000138 std::map<DeviceId, MetricsDeviceInfo> mLoggedDeviceInfos;
Prabir Pradhan852db892023-04-06 22:16:44 +0000139
Prabir Pradhan44293792023-05-08 19:37:44 +0000140 using Interaction = std::tuple<DeviceId, std::chrono::nanoseconds, std::set<Uid>>;
141 SyncQueue<Interaction> mInteractionsQueue;
142
Prabir Pradhanaff13032023-04-07 22:25:03 +0000143 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 Pradhan44293792023-05-08 19:37:44 +0000148 void recordInteraction(const Interaction&);
Prabir Pradhanaff13032023-04-07 22:25:03 +0000149 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 Pradhan44293792023-05-08 19:37:44 +0000163
164 std::map<Uid, UsageSession> mActiveSessionsByUid{};
165 InputDeviceMetricsLogger::UidUsageBreakdown mUidUsageBreakdown{};
Prabir Pradhan852db892023-04-06 22:16:44 +0000166 };
Prabir Pradhanaff13032023-04-07 22:25:03 +0000167
Prabir Pradhan852db892023-04-06 22:16:44 +0000168 // The input devices that currently have active usage sessions.
Prabir Pradhanaff13032023-04-07 22:25:03 +0000169 std::map<DeviceId, ActiveSession> mActiveUsageSessions;
Prabir Pradhan852db892023-04-06 22:16:44 +0000170
171 void onInputDevicesChanged(const std::vector<InputDeviceInfo>& infos);
Prabir Pradhan0dd48ae2023-09-08 21:33:51 +0000172 void onInputDeviceRemoved(DeviceId deviceId, const MetricsDeviceInfo& info);
173 using SourceProvider =
174 std::function<std::set<InputDeviceUsageSource>(const MetricsDeviceInfo&)>;
Prabir Pradhanaff13032023-04-07 22:25:03 +0000175 void onInputDeviceUsage(DeviceId deviceId, std::chrono::nanoseconds eventTime,
176 const SourceProvider& getSources);
Prabir Pradhan44293792023-05-08 19:37:44 +0000177 void onInputDeviceInteraction(const Interaction&);
Prabir Pradhanaff13032023-04-07 22:25:03 +0000178 void reportCompletedSessions();
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000179};
180
181} // namespace android