blob: 387786fe10f5019bb206fcc64ac03ccc26fa9ebf [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
19#include "InputListener.h"
Prabir Pradhanae10ee62023-05-12 19:44:18 +000020#include "NotifyArgs.h"
Prabir Pradhan44293792023-05-08 19:37:44 +000021#include "SyncQueue.h"
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000022
Prabir Pradhan852db892023-04-06 22:16:44 +000023#include <ftl/mixins.h>
24#include <input/InputDevice.h>
Prabir Pradhanae10ee62023-05-12 19:44:18 +000025#include <statslog.h>
Prabir Pradhan852db892023-04-06 22:16:44 +000026#include <chrono>
Prabir Pradhanaff13032023-04-07 22:25:03 +000027#include <functional>
Prabir Pradhan852db892023-04-06 22:16:44 +000028#include <map>
Prabir Pradhanae10ee62023-05-12 19:44:18 +000029#include <set>
Prabir Pradhan852db892023-04-06 22:16:44 +000030#include <vector>
31
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000032namespace android {
33
34/**
35 * Logs metrics about registered input devices and their usages.
36 *
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000037 * All methods in the InputListenerInterface must be called from a single thread.
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000038 */
39class InputDeviceMetricsCollectorInterface : public InputListenerInterface {
40public:
41 /**
Prabir Pradhan8ede1d12023-05-08 19:37:44 +000042 * Notify the metrics collector that there was an input device interaction with apps.
43 * Called from the InputDispatcher thread.
44 */
45 virtual void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp,
46 const std::set<int32_t>& uids) = 0;
47 /**
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000048 * Dump the state of the interaction blocker.
49 * This method may be called on any thread (usually by the input manager on a binder thread).
50 */
51 virtual void dump(std::string& dump) = 0;
52};
53
Prabir Pradhanae10ee62023-05-12 19:44:18 +000054/**
55 * Enum representation of the InputDeviceUsageSource.
56 */
57enum class InputDeviceUsageSource : int32_t {
58 UNKNOWN = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__UNKNOWN,
59 BUTTONS = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__BUTTONS,
60 KEYBOARD = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__KEYBOARD,
61 DPAD = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__DPAD,
62 GAMEPAD = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__GAMEPAD,
63 JOYSTICK = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__JOYSTICK,
64 MOUSE = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__MOUSE,
65 MOUSE_CAPTURED = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__MOUSE_CAPTURED,
66 TOUCHPAD = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TOUCHPAD,
67 TOUCHPAD_CAPTURED = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TOUCHPAD_CAPTURED,
68 ROTARY_ENCODER = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__ROTARY_ENCODER,
69 STYLUS_DIRECT = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__STYLUS_DIRECT,
70 STYLUS_INDIRECT = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__STYLUS_INDIRECT,
71 STYLUS_FUSED = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__STYLUS_FUSED,
72 TOUCH_NAVIGATION = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TOUCH_NAVIGATION,
73 TOUCHSCREEN = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TOUCHSCREEN,
74 TRACKBALL = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TRACKBALL,
75
76 ftl_first = UNKNOWN,
77 ftl_last = TRACKBALL,
78};
79
80/** Returns the InputDeviceUsageSource that corresponds to the key event. */
81InputDeviceUsageSource getUsageSourceForKeyArgs(const InputDeviceInfo&, const NotifyKeyArgs&);
82
83/** Returns the InputDeviceUsageSources that correspond to the motion event. */
84std::set<InputDeviceUsageSource> getUsageSourcesForMotionArgs(const NotifyMotionArgs&);
85
Prabir Pradhan852db892023-04-06 22:16:44 +000086/** The logging interface for the metrics collector, injected for testing. */
87class InputDeviceMetricsLogger {
88public:
89 virtual std::chrono::nanoseconds getCurrentTime() = 0;
Prabir Pradhanaff13032023-04-07 22:25:03 +000090
91 // Describes the breakdown of an input device usage session by its usage sources.
92 // An input device can have more than one usage source. For example, some game controllers have
93 // buttons, joysticks, and touchpads. We track usage by these sources to get a better picture of
94 // the device usage. The source breakdown of a 10 minute usage session could look like this:
95 // { {GAMEPAD, <9 mins>}, {TOUCHPAD, <2 mins>}, {TOUCHPAD, <3 mins>} }
96 // This would indicate that the GAMEPAD source was used first, and that source usage session
97 // lasted for 9 mins. During that time, the TOUCHPAD was used for 2 mins, until its source
98 // usage session expired. The TOUCHPAD was then used again later for another 3 mins.
99 using SourceUsageBreakdown =
100 std::vector<std::pair<InputDeviceUsageSource, std::chrono::nanoseconds /*duration*/>>;
101
Prabir Pradhan44293792023-05-08 19:37:44 +0000102 // Describes the breakdown of an input device usage session by the UIDs that it interacted with.
103 using UidUsageBreakdown =
104 std::vector<std::pair<int32_t /*uid*/, std::chrono::nanoseconds /*duration*/>>;
105
Prabir Pradhanaff13032023-04-07 22:25:03 +0000106 struct DeviceUsageReport {
107 std::chrono::nanoseconds usageDuration;
108 SourceUsageBreakdown sourceBreakdown;
Prabir Pradhan44293792023-05-08 19:37:44 +0000109 UidUsageBreakdown uidBreakdown;
Prabir Pradhanaff13032023-04-07 22:25:03 +0000110 };
111
Prabir Pradhan852db892023-04-06 22:16:44 +0000112 virtual void logInputDeviceUsageReported(const InputDeviceIdentifier&,
Prabir Pradhanaff13032023-04-07 22:25:03 +0000113 const DeviceUsageReport&) = 0;
Prabir Pradhan852db892023-04-06 22:16:44 +0000114 virtual ~InputDeviceMetricsLogger() = default;
115};
116
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000117class InputDeviceMetricsCollector : public InputDeviceMetricsCollectorInterface {
118public:
119 explicit InputDeviceMetricsCollector(InputListenerInterface& listener);
120 ~InputDeviceMetricsCollector() override = default;
121
Prabir Pradhan852db892023-04-06 22:16:44 +0000122 // Test constructor
123 InputDeviceMetricsCollector(InputListenerInterface& listener, InputDeviceMetricsLogger& logger,
124 std::chrono::nanoseconds usageSessionTimeout);
125
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000126 void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override;
127 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override;
128 void notifyKey(const NotifyKeyArgs& args) override;
129 void notifyMotion(const NotifyMotionArgs& args) override;
130 void notifySwitch(const NotifySwitchArgs& args) override;
131 void notifySensor(const NotifySensorArgs& args) override;
132 void notifyVibratorState(const NotifyVibratorStateArgs& args) override;
133 void notifyDeviceReset(const NotifyDeviceResetArgs& args) override;
134 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override;
135
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000136 void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp,
137 const std::set<int32_t>& uids) override;
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000138 void dump(std::string& dump) override;
139
140private:
141 InputListenerInterface& mNextListener;
Prabir Pradhan852db892023-04-06 22:16:44 +0000142 InputDeviceMetricsLogger& mLogger;
143 const std::chrono::nanoseconds mUsageSessionTimeout;
144
145 // Type-safe wrapper for input device id.
146 struct DeviceId : ftl::Constructible<DeviceId, std::int32_t>,
147 ftl::Equatable<DeviceId>,
148 ftl::Orderable<DeviceId> {
149 using Constructible::Constructible;
150 };
Prabir Pradhanaff13032023-04-07 22:25:03 +0000151 static inline std::string toString(const DeviceId& id) {
Prabir Pradhan852db892023-04-06 22:16:44 +0000152 return std::to_string(ftl::to_underlying(id));
153 }
154
Prabir Pradhan44293792023-05-08 19:37:44 +0000155 // Type-safe wrapper for a UID.
156 struct Uid : ftl::Constructible<Uid, std::int32_t>, ftl::Equatable<Uid>, ftl::Orderable<Uid> {
157 using Constructible::Constructible;
158 };
159 static inline std::string toString(const Uid& src) {
160 return std::to_string(ftl::to_underlying(src));
161 }
162
Prabir Pradhanaff13032023-04-07 22:25:03 +0000163 std::map<DeviceId, InputDeviceInfo> mLoggedDeviceInfos;
Prabir Pradhan852db892023-04-06 22:16:44 +0000164
Prabir Pradhan44293792023-05-08 19:37:44 +0000165 using Interaction = std::tuple<DeviceId, std::chrono::nanoseconds, std::set<Uid>>;
166 SyncQueue<Interaction> mInteractionsQueue;
167
Prabir Pradhanaff13032023-04-07 22:25:03 +0000168 class ActiveSession {
169 public:
170 explicit ActiveSession(std::chrono::nanoseconds usageSessionTimeout,
171 std::chrono::nanoseconds startTime);
172 void recordUsage(std::chrono::nanoseconds eventTime, InputDeviceUsageSource source);
Prabir Pradhan44293792023-05-08 19:37:44 +0000173 void recordInteraction(const Interaction&);
Prabir Pradhanaff13032023-04-07 22:25:03 +0000174 bool checkIfCompletedAt(std::chrono::nanoseconds timestamp);
175 InputDeviceMetricsLogger::DeviceUsageReport finishSession();
176
177 private:
178 struct UsageSession {
179 std::chrono::nanoseconds start{};
180 std::chrono::nanoseconds end{};
181 };
182
183 const std::chrono::nanoseconds mUsageSessionTimeout;
184 UsageSession mDeviceSession{};
185
186 std::map<InputDeviceUsageSource, UsageSession> mActiveSessionsBySource{};
187 InputDeviceMetricsLogger::SourceUsageBreakdown mSourceUsageBreakdown{};
Prabir Pradhan44293792023-05-08 19:37:44 +0000188
189 std::map<Uid, UsageSession> mActiveSessionsByUid{};
190 InputDeviceMetricsLogger::UidUsageBreakdown mUidUsageBreakdown{};
Prabir Pradhan852db892023-04-06 22:16:44 +0000191 };
Prabir Pradhanaff13032023-04-07 22:25:03 +0000192
Prabir Pradhan852db892023-04-06 22:16:44 +0000193 // The input devices that currently have active usage sessions.
Prabir Pradhanaff13032023-04-07 22:25:03 +0000194 std::map<DeviceId, ActiveSession> mActiveUsageSessions;
Prabir Pradhan852db892023-04-06 22:16:44 +0000195
196 void onInputDevicesChanged(const std::vector<InputDeviceInfo>& infos);
197 void onInputDeviceRemoved(DeviceId deviceId, const InputDeviceIdentifier& identifier);
Prabir Pradhanaff13032023-04-07 22:25:03 +0000198 using SourceProvider = std::function<std::set<InputDeviceUsageSource>(const InputDeviceInfo&)>;
199 void onInputDeviceUsage(DeviceId deviceId, std::chrono::nanoseconds eventTime,
200 const SourceProvider& getSources);
Prabir Pradhan44293792023-05-08 19:37:44 +0000201 void onInputDeviceInteraction(const Interaction&);
Prabir Pradhanaff13032023-04-07 22:25:03 +0000202 void reportCompletedSessions();
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000203};
204
205} // namespace android