blob: e2e79e45ee4e2c212a8a2c379c322551ea196a8b [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 Pradhanaddf8e92023-04-06 00:28:48 +000021
Prabir Pradhan852db892023-04-06 22:16:44 +000022#include <ftl/mixins.h>
23#include <input/InputDevice.h>
Prabir Pradhanae10ee62023-05-12 19:44:18 +000024#include <statslog.h>
Prabir Pradhan852db892023-04-06 22:16:44 +000025#include <chrono>
Prabir Pradhanaff13032023-04-07 22:25:03 +000026#include <functional>
Prabir Pradhan852db892023-04-06 22:16:44 +000027#include <map>
Prabir Pradhanae10ee62023-05-12 19:44:18 +000028#include <set>
Prabir Pradhan852db892023-04-06 22:16:44 +000029#include <vector>
30
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000031namespace android {
32
33/**
34 * Logs metrics about registered input devices and their usages.
35 *
36 * Not thread safe. Must be called from a single thread.
37 */
38class InputDeviceMetricsCollectorInterface : public InputListenerInterface {
39public:
40 /**
41 * Dump the state of the interaction blocker.
42 * This method may be called on any thread (usually by the input manager on a binder thread).
43 */
44 virtual void dump(std::string& dump) = 0;
45};
46
Prabir Pradhanae10ee62023-05-12 19:44:18 +000047/**
48 * Enum representation of the InputDeviceUsageSource.
49 */
50enum class InputDeviceUsageSource : int32_t {
51 UNKNOWN = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__UNKNOWN,
52 BUTTONS = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__BUTTONS,
53 KEYBOARD = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__KEYBOARD,
54 DPAD = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__DPAD,
55 GAMEPAD = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__GAMEPAD,
56 JOYSTICK = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__JOYSTICK,
57 MOUSE = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__MOUSE,
58 MOUSE_CAPTURED = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__MOUSE_CAPTURED,
59 TOUCHPAD = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TOUCHPAD,
60 TOUCHPAD_CAPTURED = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TOUCHPAD_CAPTURED,
61 ROTARY_ENCODER = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__ROTARY_ENCODER,
62 STYLUS_DIRECT = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__STYLUS_DIRECT,
63 STYLUS_INDIRECT = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__STYLUS_INDIRECT,
64 STYLUS_FUSED = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__STYLUS_FUSED,
65 TOUCH_NAVIGATION = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TOUCH_NAVIGATION,
66 TOUCHSCREEN = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TOUCHSCREEN,
67 TRACKBALL = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TRACKBALL,
68
69 ftl_first = UNKNOWN,
70 ftl_last = TRACKBALL,
71};
72
73/** Returns the InputDeviceUsageSource that corresponds to the key event. */
74InputDeviceUsageSource getUsageSourceForKeyArgs(const InputDeviceInfo&, const NotifyKeyArgs&);
75
76/** Returns the InputDeviceUsageSources that correspond to the motion event. */
77std::set<InputDeviceUsageSource> getUsageSourcesForMotionArgs(const NotifyMotionArgs&);
78
Prabir Pradhan852db892023-04-06 22:16:44 +000079/** The logging interface for the metrics collector, injected for testing. */
80class InputDeviceMetricsLogger {
81public:
82 virtual std::chrono::nanoseconds getCurrentTime() = 0;
Prabir Pradhanaff13032023-04-07 22:25:03 +000083
84 // Describes the breakdown of an input device usage session by its usage sources.
85 // An input device can have more than one usage source. For example, some game controllers have
86 // buttons, joysticks, and touchpads. We track usage by these sources to get a better picture of
87 // the device usage. The source breakdown of a 10 minute usage session could look like this:
88 // { {GAMEPAD, <9 mins>}, {TOUCHPAD, <2 mins>}, {TOUCHPAD, <3 mins>} }
89 // This would indicate that the GAMEPAD source was used first, and that source usage session
90 // lasted for 9 mins. During that time, the TOUCHPAD was used for 2 mins, until its source
91 // usage session expired. The TOUCHPAD was then used again later for another 3 mins.
92 using SourceUsageBreakdown =
93 std::vector<std::pair<InputDeviceUsageSource, std::chrono::nanoseconds /*duration*/>>;
94
95 struct DeviceUsageReport {
96 std::chrono::nanoseconds usageDuration;
97 SourceUsageBreakdown sourceBreakdown;
98 };
99
Prabir Pradhan852db892023-04-06 22:16:44 +0000100 virtual void logInputDeviceUsageReported(const InputDeviceIdentifier&,
Prabir Pradhanaff13032023-04-07 22:25:03 +0000101 const DeviceUsageReport&) = 0;
Prabir Pradhan852db892023-04-06 22:16:44 +0000102 virtual ~InputDeviceMetricsLogger() = default;
103};
104
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000105class InputDeviceMetricsCollector : public InputDeviceMetricsCollectorInterface {
106public:
107 explicit InputDeviceMetricsCollector(InputListenerInterface& listener);
108 ~InputDeviceMetricsCollector() override = default;
109
Prabir Pradhan852db892023-04-06 22:16:44 +0000110 // Test constructor
111 InputDeviceMetricsCollector(InputListenerInterface& listener, InputDeviceMetricsLogger& logger,
112 std::chrono::nanoseconds usageSessionTimeout);
113
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000114 void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override;
115 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override;
116 void notifyKey(const NotifyKeyArgs& args) override;
117 void notifyMotion(const NotifyMotionArgs& args) override;
118 void notifySwitch(const NotifySwitchArgs& args) override;
119 void notifySensor(const NotifySensorArgs& args) override;
120 void notifyVibratorState(const NotifyVibratorStateArgs& args) override;
121 void notifyDeviceReset(const NotifyDeviceResetArgs& args) override;
122 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override;
123
124 void dump(std::string& dump) override;
125
126private:
127 InputListenerInterface& mNextListener;
Prabir Pradhan852db892023-04-06 22:16:44 +0000128 InputDeviceMetricsLogger& mLogger;
129 const std::chrono::nanoseconds mUsageSessionTimeout;
130
131 // Type-safe wrapper for input device id.
132 struct DeviceId : ftl::Constructible<DeviceId, std::int32_t>,
133 ftl::Equatable<DeviceId>,
134 ftl::Orderable<DeviceId> {
135 using Constructible::Constructible;
136 };
Prabir Pradhanaff13032023-04-07 22:25:03 +0000137 static inline std::string toString(const DeviceId& id) {
Prabir Pradhan852db892023-04-06 22:16:44 +0000138 return std::to_string(ftl::to_underlying(id));
139 }
140
Prabir Pradhanaff13032023-04-07 22:25:03 +0000141 std::map<DeviceId, InputDeviceInfo> mLoggedDeviceInfos;
Prabir Pradhan852db892023-04-06 22:16:44 +0000142
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);
148 bool checkIfCompletedAt(std::chrono::nanoseconds timestamp);
149 InputDeviceMetricsLogger::DeviceUsageReport finishSession();
150
151 private:
152 struct UsageSession {
153 std::chrono::nanoseconds start{};
154 std::chrono::nanoseconds end{};
155 };
156
157 const std::chrono::nanoseconds mUsageSessionTimeout;
158 UsageSession mDeviceSession{};
159
160 std::map<InputDeviceUsageSource, UsageSession> mActiveSessionsBySource{};
161 InputDeviceMetricsLogger::SourceUsageBreakdown mSourceUsageBreakdown{};
Prabir Pradhan852db892023-04-06 22:16:44 +0000162 };
Prabir Pradhanaff13032023-04-07 22:25:03 +0000163
Prabir Pradhan852db892023-04-06 22:16:44 +0000164 // The input devices that currently have active usage sessions.
Prabir Pradhanaff13032023-04-07 22:25:03 +0000165 std::map<DeviceId, ActiveSession> mActiveUsageSessions;
Prabir Pradhan852db892023-04-06 22:16:44 +0000166
167 void onInputDevicesChanged(const std::vector<InputDeviceInfo>& infos);
168 void onInputDeviceRemoved(DeviceId deviceId, const InputDeviceIdentifier& identifier);
Prabir Pradhanaff13032023-04-07 22:25:03 +0000169 using SourceProvider = std::function<std::set<InputDeviceUsageSource>(const InputDeviceInfo&)>;
170 void onInputDeviceUsage(DeviceId deviceId, std::chrono::nanoseconds eventTime,
171 const SourceProvider& getSources);
172 void reportCompletedSessions();
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000173};
174
175} // namespace android