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" |
| 20 | |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 21 | #include <ftl/mixins.h> |
| 22 | #include <input/InputDevice.h> |
| 23 | #include <chrono> |
| 24 | #include <map> |
| 25 | #include <vector> |
| 26 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 27 | namespace android { |
| 28 | |
| 29 | /** |
| 30 | * Logs metrics about registered input devices and their usages. |
| 31 | * |
| 32 | * Not thread safe. Must be called from a single thread. |
| 33 | */ |
| 34 | class InputDeviceMetricsCollectorInterface : public InputListenerInterface { |
| 35 | public: |
| 36 | /** |
| 37 | * Dump the state of the interaction blocker. |
| 38 | * This method may be called on any thread (usually by the input manager on a binder thread). |
| 39 | */ |
| 40 | virtual void dump(std::string& dump) = 0; |
| 41 | }; |
| 42 | |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 43 | /** The logging interface for the metrics collector, injected for testing. */ |
| 44 | class InputDeviceMetricsLogger { |
| 45 | public: |
| 46 | virtual std::chrono::nanoseconds getCurrentTime() = 0; |
| 47 | virtual void logInputDeviceUsageReported(const InputDeviceIdentifier&, |
| 48 | std::chrono::nanoseconds duration) = 0; |
| 49 | virtual ~InputDeviceMetricsLogger() = default; |
| 50 | }; |
| 51 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 52 | class InputDeviceMetricsCollector : public InputDeviceMetricsCollectorInterface { |
| 53 | public: |
| 54 | explicit InputDeviceMetricsCollector(InputListenerInterface& listener); |
| 55 | ~InputDeviceMetricsCollector() override = default; |
| 56 | |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 57 | // Test constructor |
| 58 | InputDeviceMetricsCollector(InputListenerInterface& listener, InputDeviceMetricsLogger& logger, |
| 59 | std::chrono::nanoseconds usageSessionTimeout); |
| 60 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 61 | void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override; |
| 62 | void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override; |
| 63 | void notifyKey(const NotifyKeyArgs& args) override; |
| 64 | void notifyMotion(const NotifyMotionArgs& args) override; |
| 65 | void notifySwitch(const NotifySwitchArgs& args) override; |
| 66 | void notifySensor(const NotifySensorArgs& args) override; |
| 67 | void notifyVibratorState(const NotifyVibratorStateArgs& args) override; |
| 68 | void notifyDeviceReset(const NotifyDeviceResetArgs& args) override; |
| 69 | void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override; |
| 70 | |
| 71 | void dump(std::string& dump) override; |
| 72 | |
| 73 | private: |
| 74 | InputListenerInterface& mNextListener; |
Prabir Pradhan | 852db89 | 2023-04-06 22:16:44 +0000 | [diff] [blame] | 75 | InputDeviceMetricsLogger& mLogger; |
| 76 | const std::chrono::nanoseconds mUsageSessionTimeout; |
| 77 | |
| 78 | // Type-safe wrapper for input device id. |
| 79 | struct DeviceId : ftl::Constructible<DeviceId, std::int32_t>, |
| 80 | ftl::Equatable<DeviceId>, |
| 81 | ftl::Orderable<DeviceId> { |
| 82 | using Constructible::Constructible; |
| 83 | }; |
| 84 | static std::string toString(const DeviceId& id) { |
| 85 | return std::to_string(ftl::to_underlying(id)); |
| 86 | } |
| 87 | |
| 88 | std::map<DeviceId, InputDeviceIdentifier> mLoggedDeviceInfos; |
| 89 | |
| 90 | struct UsageSession { |
| 91 | std::chrono::nanoseconds start; |
| 92 | std::chrono::nanoseconds end; |
| 93 | }; |
| 94 | // The input devices that currently have active usage sessions. |
| 95 | std::map<DeviceId, UsageSession> mActiveUsageSessions; |
| 96 | |
| 97 | void onInputDevicesChanged(const std::vector<InputDeviceInfo>& infos); |
| 98 | void onInputDeviceRemoved(DeviceId deviceId, const InputDeviceIdentifier& identifier); |
| 99 | void onInputDeviceUsage(DeviceId deviceId, std::chrono::nanoseconds eventTime); |
| 100 | void processUsages(); |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | } // namespace android |