blob: 4ef860ff10006c6ffdf91083ea4d2b1b0a38bb14 [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>
26#include <map>
Prabir Pradhanae10ee62023-05-12 19:44:18 +000027#include <set>
Prabir Pradhan852db892023-04-06 22:16:44 +000028#include <vector>
29
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000030namespace android {
31
32/**
33 * Logs metrics about registered input devices and their usages.
34 *
35 * Not thread safe. Must be called from a single thread.
36 */
37class InputDeviceMetricsCollectorInterface : public InputListenerInterface {
38public:
39 /**
40 * Dump the state of the interaction blocker.
41 * This method may be called on any thread (usually by the input manager on a binder thread).
42 */
43 virtual void dump(std::string& dump) = 0;
44};
45
Prabir Pradhanae10ee62023-05-12 19:44:18 +000046/**
47 * Enum representation of the InputDeviceUsageSource.
48 */
49enum class InputDeviceUsageSource : int32_t {
50 UNKNOWN = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__UNKNOWN,
51 BUTTONS = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__BUTTONS,
52 KEYBOARD = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__KEYBOARD,
53 DPAD = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__DPAD,
54 GAMEPAD = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__GAMEPAD,
55 JOYSTICK = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__JOYSTICK,
56 MOUSE = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__MOUSE,
57 MOUSE_CAPTURED = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__MOUSE_CAPTURED,
58 TOUCHPAD = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TOUCHPAD,
59 TOUCHPAD_CAPTURED = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TOUCHPAD_CAPTURED,
60 ROTARY_ENCODER = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__ROTARY_ENCODER,
61 STYLUS_DIRECT = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__STYLUS_DIRECT,
62 STYLUS_INDIRECT = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__STYLUS_INDIRECT,
63 STYLUS_FUSED = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__STYLUS_FUSED,
64 TOUCH_NAVIGATION = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TOUCH_NAVIGATION,
65 TOUCHSCREEN = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TOUCHSCREEN,
66 TRACKBALL = util::INPUT_DEVICE_USAGE_REPORTED__USAGE_SOURCES__TRACKBALL,
67
68 ftl_first = UNKNOWN,
69 ftl_last = TRACKBALL,
70};
71
72/** Returns the InputDeviceUsageSource that corresponds to the key event. */
73InputDeviceUsageSource getUsageSourceForKeyArgs(const InputDeviceInfo&, const NotifyKeyArgs&);
74
75/** Returns the InputDeviceUsageSources that correspond to the motion event. */
76std::set<InputDeviceUsageSource> getUsageSourcesForMotionArgs(const NotifyMotionArgs&);
77
Prabir Pradhan852db892023-04-06 22:16:44 +000078/** The logging interface for the metrics collector, injected for testing. */
79class InputDeviceMetricsLogger {
80public:
81 virtual std::chrono::nanoseconds getCurrentTime() = 0;
82 virtual void logInputDeviceUsageReported(const InputDeviceIdentifier&,
83 std::chrono::nanoseconds duration) = 0;
84 virtual ~InputDeviceMetricsLogger() = default;
85};
86
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000087class InputDeviceMetricsCollector : public InputDeviceMetricsCollectorInterface {
88public:
89 explicit InputDeviceMetricsCollector(InputListenerInterface& listener);
90 ~InputDeviceMetricsCollector() override = default;
91
Prabir Pradhan852db892023-04-06 22:16:44 +000092 // Test constructor
93 InputDeviceMetricsCollector(InputListenerInterface& listener, InputDeviceMetricsLogger& logger,
94 std::chrono::nanoseconds usageSessionTimeout);
95
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000096 void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override;
97 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override;
98 void notifyKey(const NotifyKeyArgs& args) override;
99 void notifyMotion(const NotifyMotionArgs& args) override;
100 void notifySwitch(const NotifySwitchArgs& args) override;
101 void notifySensor(const NotifySensorArgs& args) override;
102 void notifyVibratorState(const NotifyVibratorStateArgs& args) override;
103 void notifyDeviceReset(const NotifyDeviceResetArgs& args) override;
104 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override;
105
106 void dump(std::string& dump) override;
107
108private:
109 InputListenerInterface& mNextListener;
Prabir Pradhan852db892023-04-06 22:16:44 +0000110 InputDeviceMetricsLogger& mLogger;
111 const std::chrono::nanoseconds mUsageSessionTimeout;
112
113 // Type-safe wrapper for input device id.
114 struct DeviceId : ftl::Constructible<DeviceId, std::int32_t>,
115 ftl::Equatable<DeviceId>,
116 ftl::Orderable<DeviceId> {
117 using Constructible::Constructible;
118 };
119 static std::string toString(const DeviceId& id) {
120 return std::to_string(ftl::to_underlying(id));
121 }
122
123 std::map<DeviceId, InputDeviceIdentifier> mLoggedDeviceInfos;
124
125 struct UsageSession {
126 std::chrono::nanoseconds start;
127 std::chrono::nanoseconds end;
128 };
129 // The input devices that currently have active usage sessions.
130 std::map<DeviceId, UsageSession> mActiveUsageSessions;
131
132 void onInputDevicesChanged(const std::vector<InputDeviceInfo>& infos);
133 void onInputDeviceRemoved(DeviceId deviceId, const InputDeviceIdentifier& identifier);
134 void onInputDeviceUsage(DeviceId deviceId, std::chrono::nanoseconds eventTime);
135 void processUsages();
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000136};
137
138} // namespace android