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 | |
| 19 | import android.hardware.HardwareBuffer; |
| 20 | import android.hardware.Sensor; |
| 21 | import android.hardware.SensorAdditionalInfo; |
| 22 | import android.hardware.SensorDirectChannel; |
| 23 | import android.hardware.SensorEventListener; |
| 24 | import android.hardware.SensorManager; |
| 25 | import android.hardware.TriggerEventListener; |
| 26 | import android.os.Handler; |
| 27 | import android.os.HandlerThread; |
| 28 | import android.os.MemoryFile; |
| 29 | import android.util.Log; |
| 30 | |
| 31 | import com.android.internal.util.Preconditions; |
| 32 | |
| 33 | import java.util.List; |
| 34 | |
| 35 | /** |
| 36 | * Wrapper around sensor manager that hides potential sources of latency. |
| 37 | * |
| 38 | * Offloads fetching (non-dynamic) sensors and (un)registering listeners onto a background thread |
| 39 | * without blocking. Note that this means registering listeners now always appears successful even |
| 40 | * if it is not. |
| 41 | */ |
| 42 | public class AsyncSensorManager extends SensorManager { |
| 43 | |
| 44 | private static final String TAG = "AsyncSensorManager"; |
| 45 | |
| 46 | private final SensorManager mInner; |
| 47 | private final List<Sensor> mSensorCache; |
| 48 | private final HandlerThread mHandlerThread = new HandlerThread("async_sensor"); |
| 49 | private final Handler mHandler; |
| 50 | |
| 51 | public AsyncSensorManager(SensorManager inner) { |
| 52 | mInner = inner; |
| 53 | mHandlerThread.start(); |
| 54 | mHandler = new Handler(mHandlerThread.getLooper()); |
| 55 | mSensorCache = mInner.getSensorList(Sensor.TYPE_ALL); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | protected List<Sensor> getFullSensorList() { |
| 60 | return mSensorCache; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | protected List<Sensor> getFullDynamicSensorList() { |
| 65 | return mInner.getDynamicSensorList(Sensor.TYPE_ALL); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | protected boolean registerListenerImpl(SensorEventListener listener, Sensor sensor, int delayUs, |
| 70 | Handler handler, int maxReportLatencyUs, int reservedFlags) { |
| 71 | mHandler.post(() -> { |
| 72 | if (!mInner.registerListener(listener, sensor, delayUs, maxReportLatencyUs, handler)) { |
| 73 | Log.e(TAG, "Registering " + listener + " for " + sensor + " failed."); |
| 74 | } |
| 75 | }); |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected boolean flushImpl(SensorEventListener listener) { |
| 81 | return mInner.flush(listener); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | protected SensorDirectChannel createDirectChannelImpl(MemoryFile memoryFile, |
| 86 | HardwareBuffer hardwareBuffer) { |
| 87 | throw new UnsupportedOperationException("not implemented"); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | protected void destroyDirectChannelImpl(SensorDirectChannel channel) { |
| 92 | throw new UnsupportedOperationException("not implemented"); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | protected int configureDirectChannelImpl(SensorDirectChannel channel, Sensor s, int rate) { |
| 97 | throw new UnsupportedOperationException("not implemented"); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | protected void registerDynamicSensorCallbackImpl(DynamicSensorCallback callback, |
| 102 | Handler handler) { |
| 103 | mHandler.post(() -> mInner.registerDynamicSensorCallback(callback, handler)); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | protected void unregisterDynamicSensorCallbackImpl(DynamicSensorCallback callback) { |
| 108 | mHandler.post(() -> mInner.unregisterDynamicSensorCallback(callback)); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | protected boolean requestTriggerSensorImpl(TriggerEventListener listener, Sensor sensor) { |
| 113 | mHandler.post(() -> { |
| 114 | if (!mInner.requestTriggerSensor(listener, sensor)) { |
| 115 | Log.e(TAG, "Requesting " + listener + " for " + sensor + " failed."); |
| 116 | } |
| 117 | }); |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | protected boolean cancelTriggerSensorImpl(TriggerEventListener listener, Sensor sensor, |
| 123 | boolean disable) { |
| 124 | Preconditions.checkArgument(disable); |
| 125 | |
| 126 | mHandler.post(() -> { |
| 127 | if (!mInner.cancelTriggerSensor(listener, sensor)) { |
| 128 | Log.e(TAG, "Canceling " + listener + " for " + sensor + " failed."); |
| 129 | } |
| 130 | }); |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | protected boolean initDataInjectionImpl(boolean enable) { |
| 136 | throw new UnsupportedOperationException("not implemented"); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | protected boolean injectSensorDataImpl(Sensor sensor, float[] values, int accuracy, |
| 141 | long timestamp) { |
| 142 | throw new UnsupportedOperationException("not implemented"); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | protected boolean setOperationParameterImpl(SensorAdditionalInfo parameter) { |
| 147 | mHandler.post(() -> mInner.setOperationParameter(parameter)); |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | protected void unregisterListenerImpl(SensorEventListener listener, Sensor sensor) { |
| 153 | mHandler.post(() -> mInner.unregisterListener(listener, sensor)); |
| 154 | } |
| 155 | } |