Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 1 | /* |
| 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 Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame^] | 20 | #include "NotifyArgs.h" |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 21 | |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 22 | #include <ftl/mixins.h> |
| 23 | #include <input/InputDevice.h> |
Prabir Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame^] | 24 | #include <statslog.h> |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 25 | #include <chrono> |
| 26 | #include <map> |
Prabir Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame^] | 27 | #include <set> |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 30 | namespace 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 | */ |
| 37 | class InputDeviceMetricsCollectorInterface : public InputListenerInterface { |
| 38 | public: |
| 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 Pradhan | ae10ee6 | 2023-05-12 19:44:18 +0000 | [diff] [blame^] | 46 | /** |
| 47 | * Enum representation of the InputDeviceUsageSource. |
| 48 | */ |
| 49 | enum 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. */ |
| 73 | InputDeviceUsageSource getUsageSourceForKeyArgs(const InputDeviceInfo&, const NotifyKeyArgs&); |
| 74 | |
| 75 | /** Returns the InputDeviceUsageSources that correspond to the motion event. */ |
| 76 | std::set<InputDeviceUsageSource> getUsageSourcesForMotionArgs(const NotifyMotionArgs&); |
| 77 | |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 78 | /** The logging interface for the metrics collector, injected for testing. */ |
| 79 | class InputDeviceMetricsLogger { |
| 80 | public: |
| 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 Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 87 | class InputDeviceMetricsCollector : public InputDeviceMetricsCollectorInterface { |
| 88 | public: |
| 89 | explicit InputDeviceMetricsCollector(InputListenerInterface& listener); |
| 90 | ~InputDeviceMetricsCollector() override = default; |
| 91 | |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 92 | // Test constructor |
| 93 | InputDeviceMetricsCollector(InputListenerInterface& listener, InputDeviceMetricsLogger& logger, |
| 94 | std::chrono::nanoseconds usageSessionTimeout); |
| 95 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 96 | 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 | |
| 108 | private: |
| 109 | InputListenerInterface& mNextListener; |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 110 | 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 Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | } // namespace android |