Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | package com.android.systemui.util; |
| 18 | |
Kevin Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 20 | import android.hardware.HardwareBuffer; |
| 21 | import android.hardware.Sensor; |
| 22 | import android.hardware.SensorAdditionalInfo; |
| 23 | import android.hardware.SensorDirectChannel; |
| 24 | import android.hardware.SensorEventListener; |
| 25 | import android.hardware.SensorManager; |
| 26 | import android.hardware.TriggerEventListener; |
| 27 | import android.os.Handler; |
| 28 | import android.os.HandlerThread; |
| 29 | import android.os.MemoryFile; |
| 30 | import android.util.Log; |
| 31 | |
Adrian Roos | b1f7724 | 2017-08-04 12:35:24 +0200 | [diff] [blame] | 32 | import com.android.internal.annotations.VisibleForTesting; |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 33 | import com.android.internal.util.Preconditions; |
Kevin Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 34 | import com.android.systemui.plugins.PluginListener; |
| 35 | import com.android.systemui.plugins.SensorManagerPlugin; |
| 36 | import com.android.systemui.shared.plugins.PluginManager; |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 37 | |
Kevin Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 38 | import java.util.ArrayList; |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 39 | import java.util.List; |
| 40 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 41 | import javax.inject.Inject; |
| 42 | import javax.inject.Singleton; |
| 43 | |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 44 | /** |
| 45 | * Wrapper around sensor manager that hides potential sources of latency. |
| 46 | * |
| 47 | * Offloads fetching (non-dynamic) sensors and (un)registering listeners onto a background thread |
| 48 | * without blocking. Note that this means registering listeners now always appears successful even |
| 49 | * if it is not. |
| 50 | */ |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 51 | @Singleton |
Kevin Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 52 | public class AsyncSensorManager extends SensorManager |
| 53 | implements PluginListener<SensorManagerPlugin> { |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 54 | |
| 55 | private static final String TAG = "AsyncSensorManager"; |
| 56 | |
| 57 | private final SensorManager mInner; |
| 58 | private final List<Sensor> mSensorCache; |
| 59 | private final HandlerThread mHandlerThread = new HandlerThread("async_sensor"); |
Adrian Roos | b1f7724 | 2017-08-04 12:35:24 +0200 | [diff] [blame] | 60 | @VisibleForTesting final Handler mHandler; |
Kevin Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 61 | private final List<SensorManagerPlugin> mPlugins; |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 62 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 63 | @Inject |
| 64 | public AsyncSensorManager(Context context, PluginManager pluginManager) { |
| 65 | this(context.getSystemService(SensorManager.class), pluginManager); |
| 66 | } |
| 67 | |
| 68 | @VisibleForTesting |
| 69 | AsyncSensorManager(SensorManager sensorManager, PluginManager pluginManager) { |
| 70 | mInner = sensorManager; |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 71 | mHandlerThread.start(); |
| 72 | mHandler = new Handler(mHandlerThread.getLooper()); |
| 73 | mSensorCache = mInner.getSensorList(Sensor.TYPE_ALL); |
Kevin Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 74 | mPlugins = new ArrayList<>(); |
| 75 | pluginManager.addPluginListener(this, SensorManagerPlugin.class, true /* allowMultiple */); |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | protected List<Sensor> getFullSensorList() { |
| 80 | return mSensorCache; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | protected List<Sensor> getFullDynamicSensorList() { |
Kevin Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 85 | return mInner.getSensorList(Sensor.TYPE_ALL); |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
Lucas Dupin | 1ae6cf9 | 2018-12-14 18:06:38 -0800 | [diff] [blame] | 89 | protected boolean registerListenerImpl(SensorEventListener listener, |
| 90 | Sensor sensor, int delayUs, Handler handler, int maxReportLatencyUs, |
| 91 | int reservedFlags) { |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 92 | mHandler.post(() -> { |
| 93 | if (!mInner.registerListener(listener, sensor, delayUs, maxReportLatencyUs, handler)) { |
| 94 | Log.e(TAG, "Registering " + listener + " for " + sensor + " failed."); |
| 95 | } |
| 96 | }); |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | protected boolean flushImpl(SensorEventListener listener) { |
| 102 | return mInner.flush(listener); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | protected SensorDirectChannel createDirectChannelImpl(MemoryFile memoryFile, |
| 107 | HardwareBuffer hardwareBuffer) { |
| 108 | throw new UnsupportedOperationException("not implemented"); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | protected void destroyDirectChannelImpl(SensorDirectChannel channel) { |
| 113 | throw new UnsupportedOperationException("not implemented"); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | protected int configureDirectChannelImpl(SensorDirectChannel channel, Sensor s, int rate) { |
| 118 | throw new UnsupportedOperationException("not implemented"); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | protected void registerDynamicSensorCallbackImpl(DynamicSensorCallback callback, |
| 123 | Handler handler) { |
| 124 | mHandler.post(() -> mInner.registerDynamicSensorCallback(callback, handler)); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | protected void unregisterDynamicSensorCallbackImpl(DynamicSensorCallback callback) { |
| 129 | mHandler.post(() -> mInner.unregisterDynamicSensorCallback(callback)); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | protected boolean requestTriggerSensorImpl(TriggerEventListener listener, Sensor sensor) { |
| 134 | mHandler.post(() -> { |
| 135 | if (!mInner.requestTriggerSensor(listener, sensor)) { |
| 136 | Log.e(TAG, "Requesting " + listener + " for " + sensor + " failed."); |
| 137 | } |
| 138 | }); |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | protected boolean cancelTriggerSensorImpl(TriggerEventListener listener, Sensor sensor, |
| 144 | boolean disable) { |
| 145 | Preconditions.checkArgument(disable); |
| 146 | |
| 147 | mHandler.post(() -> { |
| 148 | if (!mInner.cancelTriggerSensor(listener, sensor)) { |
| 149 | Log.e(TAG, "Canceling " + listener + " for " + sensor + " failed."); |
| 150 | } |
| 151 | }); |
| 152 | return true; |
| 153 | } |
| 154 | |
Kevin Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 155 | /** |
| 156 | * Requests for all sensors that match the given type from all plugins. |
| 157 | * @param sensor |
| 158 | * @param listener |
| 159 | */ |
Lucas Dupin | 1ae6cf9 | 2018-12-14 18:06:38 -0800 | [diff] [blame] | 160 | public void registerPluginListener(SensorManagerPlugin.Sensor sensor, |
| 161 | SensorManagerPlugin.SensorEventListener listener) { |
Kevin Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 162 | if (mPlugins.isEmpty()) { |
| 163 | Log.w(TAG, "No plugins registered"); |
| 164 | } |
| 165 | mHandler.post(() -> { |
| 166 | for (int i = 0; i < mPlugins.size(); i++) { |
Lucas Dupin | 1ae6cf9 | 2018-12-14 18:06:38 -0800 | [diff] [blame] | 167 | mPlugins.get(i).registerListener(sensor, listener); |
Kevin Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 168 | } |
| 169 | }); |
| 170 | } |
| 171 | |
Lucas Dupin | 1ae6cf9 | 2018-12-14 18:06:38 -0800 | [diff] [blame] | 172 | /** |
| 173 | * Unregisters all sensors that match the give type for all plugins. |
| 174 | * @param sensor |
| 175 | * @param listener |
| 176 | */ |
| 177 | public void unregisterPluginListener(SensorManagerPlugin.Sensor sensor, |
| 178 | SensorManagerPlugin.SensorEventListener listener) { |
Kevin Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 179 | mHandler.post(() -> { |
| 180 | for (int i = 0; i < mPlugins.size(); i++) { |
Lucas Dupin | 1ae6cf9 | 2018-12-14 18:06:38 -0800 | [diff] [blame] | 181 | mPlugins.get(i).unregisterListener(sensor, listener); |
Kevin Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 182 | } |
| 183 | }); |
| 184 | } |
| 185 | |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 186 | @Override |
| 187 | protected boolean initDataInjectionImpl(boolean enable) { |
| 188 | throw new UnsupportedOperationException("not implemented"); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | protected boolean injectSensorDataImpl(Sensor sensor, float[] values, int accuracy, |
| 193 | long timestamp) { |
| 194 | throw new UnsupportedOperationException("not implemented"); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | protected boolean setOperationParameterImpl(SensorAdditionalInfo parameter) { |
| 199 | mHandler.post(() -> mInner.setOperationParameter(parameter)); |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | @Override |
Lucas Dupin | 1ae6cf9 | 2018-12-14 18:06:38 -0800 | [diff] [blame] | 204 | protected void unregisterListenerImpl(SensorEventListener listener, |
| 205 | Sensor sensor) { |
Adrian Roos | b1f7724 | 2017-08-04 12:35:24 +0200 | [diff] [blame] | 206 | mHandler.post(() -> { |
| 207 | if (sensor == null) { |
| 208 | mInner.unregisterListener(listener); |
| 209 | } else { |
| 210 | mInner.unregisterListener(listener, sensor); |
| 211 | } |
| 212 | }); |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 213 | } |
Kevin Chyn | ac56f74 | 2018-10-09 16:20:25 -0700 | [diff] [blame] | 214 | |
| 215 | @Override |
| 216 | public void onPluginConnected(SensorManagerPlugin plugin, Context pluginContext) { |
| 217 | mPlugins.add(plugin); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void onPluginDisconnected(SensorManagerPlugin plugin) { |
| 222 | mPlugins.remove(plugin); |
| 223 | } |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 224 | } |