Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 1 | /* |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 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 android.hardware; |
| 18 | |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 20 | import android.os.Handler; |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 21 | import android.os.Looper; |
| 22 | import android.os.MessageQueue; |
Jaikumar Ganesh | 2e90089 | 2013-04-11 10:40:33 -0700 | [diff] [blame] | 23 | import android.util.Log; |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 24 | import android.util.SparseArray; |
| 25 | import android.util.SparseBooleanArray; |
| 26 | import android.util.SparseIntArray; |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 27 | import dalvik.system.CloseGuard; |
| 28 | |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.HashMap; |
| 31 | import java.util.List; |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 32 | |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Sensor manager implementation that communicates with the built-in |
| 35 | * system sensors. |
| 36 | * |
| 37 | * @hide |
| 38 | */ |
| 39 | public class SystemSensorManager extends SensorManager { |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 40 | private static native void nativeClassInit(); |
| 41 | private static native int nativeGetNextSensor(Sensor sensor, int next); |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 42 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 43 | private static boolean sSensorModuleInitialized = false; |
| 44 | private static final Object sSensorModuleLock = new Object(); |
| 45 | private static final ArrayList<Sensor> sFullSensorsList = new ArrayList<Sensor>(); |
| 46 | private static final SparseArray<Sensor> sHandleToSensor = new SparseArray<Sensor>(); |
| 47 | |
| 48 | // Listener list |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 49 | private final HashMap<SensorEventListener, SensorEventQueue> mSensorListeners = |
| 50 | new HashMap<SensorEventListener, SensorEventQueue>(); |
| 51 | private final HashMap<TriggerEventListener, TriggerEventQueue> mTriggerListeners = |
| 52 | new HashMap<TriggerEventListener, TriggerEventQueue>(); |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 53 | |
Jeff Brown | 4481d9c | 2012-04-16 16:14:44 -0700 | [diff] [blame] | 54 | // Looper associated with the context in which this instance was created. |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 55 | private final Looper mMainLooper; |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 56 | private final int mTargetSdkLevel; |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame^] | 57 | private final String mPackageName; |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 58 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 59 | /** {@hide} */ |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 60 | public SystemSensorManager(Context context, Looper mainLooper) { |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 61 | mMainLooper = mainLooper; |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 62 | mTargetSdkLevel = context.getApplicationInfo().targetSdkVersion; |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame^] | 63 | mPackageName = context.getPackageName(); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 64 | synchronized(sSensorModuleLock) { |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 65 | if (!sSensorModuleInitialized) { |
| 66 | sSensorModuleInitialized = true; |
| 67 | |
| 68 | nativeClassInit(); |
| 69 | |
| 70 | // initialize the sensor list |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 71 | final ArrayList<Sensor> fullList = sFullSensorsList; |
| 72 | int i = 0; |
| 73 | do { |
| 74 | Sensor sensor = new Sensor(); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 75 | i = nativeGetNextSensor(sensor, i); |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 76 | if (i>=0) { |
| 77 | //Log.d(TAG, "found sensor: " + sensor.getName() + |
| 78 | // ", handle=" + sensor.getHandle()); |
| 79 | fullList.add(sensor); |
| 80 | sHandleToSensor.append(sensor.getHandle(), sensor); |
| 81 | } |
| 82 | } while (i>0); |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 87 | |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 88 | /** @hide */ |
| 89 | @Override |
| 90 | protected List<Sensor> getFullSensorList() { |
| 91 | return sFullSensorsList; |
| 92 | } |
| 93 | |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 94 | |
| 95 | /** @hide */ |
Jeff Brown | 4481d9c | 2012-04-16 16:14:44 -0700 | [diff] [blame] | 96 | @Override |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 97 | protected boolean registerListenerImpl(SensorEventListener listener, Sensor sensor, |
Aravind Akella | 4bdc37d | 2013-10-01 17:58:35 -0700 | [diff] [blame] | 98 | int delayUs, Handler handler, int maxBatchReportLatencyUs, int reservedFlags) { |
| 99 | if (listener == null || sensor == null) { |
| 100 | Log.e(TAG, "sensor or listener is null"); |
| 101 | return false; |
| 102 | } |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 103 | // Trigger Sensors should use the requestTriggerSensor call. |
Aravind Akella | 2790035 | 2014-06-03 19:20:42 -0700 | [diff] [blame] | 104 | if (sensor.getReportingMode() == Sensor.REPORTING_MODE_ONE_SHOT) { |
Aravind Akella | 4bdc37d | 2013-10-01 17:58:35 -0700 | [diff] [blame] | 105 | Log.e(TAG, "Trigger Sensors should use the requestTriggerSensor."); |
| 106 | return false; |
| 107 | } |
| 108 | if (maxBatchReportLatencyUs < 0 || delayUs < 0) { |
| 109 | Log.e(TAG, "maxBatchReportLatencyUs and delayUs should be non-negative"); |
| 110 | return false; |
| 111 | } |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 112 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 113 | // Invariants to preserve: |
| 114 | // - one Looper per SensorEventListener |
| 115 | // - one Looper per SensorEventQueue |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 116 | // We map SensorEventListener to a SensorEventQueue, which holds the looper |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 117 | synchronized (mSensorListeners) { |
| 118 | SensorEventQueue queue = mSensorListeners.get(listener); |
| 119 | if (queue == null) { |
Jaikumar Ganesh | 1aab1db | 2013-03-13 15:00:21 -0700 | [diff] [blame] | 120 | Looper looper = (handler != null) ? handler.getLooper() : mMainLooper; |
Aravind Akella | 4bdc37d | 2013-10-01 17:58:35 -0700 | [diff] [blame] | 121 | queue = new SensorEventQueue(listener, looper, this); |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame^] | 122 | if (!queue.addSensor(sensor, delayUs, maxBatchReportLatencyUs)) { |
Jaikumar Ganesh | 1aab1db | 2013-03-13 15:00:21 -0700 | [diff] [blame] | 123 | queue.dispose(); |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 124 | return false; |
Jaikumar Ganesh | 1aab1db | 2013-03-13 15:00:21 -0700 | [diff] [blame] | 125 | } |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 126 | mSensorListeners.put(listener, queue); |
| 127 | return true; |
| 128 | } else { |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame^] | 129 | return queue.addSensor(sensor, delayUs, maxBatchReportLatencyUs); |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** @hide */ |
| 135 | @Override |
| 136 | protected void unregisterListenerImpl(SensorEventListener listener, Sensor sensor) { |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 137 | // Trigger Sensors should use the cancelTriggerSensor call. |
Aravind Akella | 2790035 | 2014-06-03 19:20:42 -0700 | [diff] [blame] | 138 | if (sensor != null && sensor.getReportingMode() == Sensor.REPORTING_MODE_ONE_SHOT) { |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 139 | return; |
| 140 | } |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 141 | |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 142 | synchronized (mSensorListeners) { |
| 143 | SensorEventQueue queue = mSensorListeners.get(listener); |
Jaikumar Ganesh | 1aab1db | 2013-03-13 15:00:21 -0700 | [diff] [blame] | 144 | if (queue != null) { |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 145 | boolean result; |
| 146 | if (sensor == null) { |
| 147 | result = queue.removeAllSensors(); |
Jaikumar Ganesh | 1aab1db | 2013-03-13 15:00:21 -0700 | [diff] [blame] | 148 | } else { |
Jaikumar Ganesh | 2e90089 | 2013-04-11 10:40:33 -0700 | [diff] [blame] | 149 | result = queue.removeSensor(sensor, true); |
Jaikumar Ganesh | 1aab1db | 2013-03-13 15:00:21 -0700 | [diff] [blame] | 150 | } |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 151 | if (result && !queue.hasSensors()) { |
| 152 | mSensorListeners.remove(listener); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 153 | queue.dispose(); |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 159 | /** @hide */ |
| 160 | @Override |
| 161 | protected boolean requestTriggerSensorImpl(TriggerEventListener listener, Sensor sensor) { |
| 162 | if (sensor == null) throw new IllegalArgumentException("sensor cannot be null"); |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 163 | |
Aravind Akella | 2790035 | 2014-06-03 19:20:42 -0700 | [diff] [blame] | 164 | if (sensor.getReportingMode() != Sensor.REPORTING_MODE_ONE_SHOT) return false; |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 165 | |
| 166 | synchronized (mTriggerListeners) { |
| 167 | TriggerEventQueue queue = mTriggerListeners.get(listener); |
| 168 | if (queue == null) { |
| 169 | queue = new TriggerEventQueue(listener, mMainLooper, this); |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame^] | 170 | if (!queue.addSensor(sensor, 0, 0)) { |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 171 | queue.dispose(); |
| 172 | return false; |
| 173 | } |
| 174 | mTriggerListeners.put(listener, queue); |
| 175 | return true; |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 176 | } else { |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame^] | 177 | return queue.addSensor(sensor, 0, 0); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | } |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 181 | |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 182 | /** @hide */ |
| 183 | @Override |
Jaikumar Ganesh | 2e90089 | 2013-04-11 10:40:33 -0700 | [diff] [blame] | 184 | protected boolean cancelTriggerSensorImpl(TriggerEventListener listener, Sensor sensor, |
| 185 | boolean disable) { |
Aravind Akella | 2790035 | 2014-06-03 19:20:42 -0700 | [diff] [blame] | 186 | if (sensor != null && sensor.getReportingMode() != Sensor.REPORTING_MODE_ONE_SHOT) { |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 187 | return false; |
| 188 | } |
| 189 | synchronized (mTriggerListeners) { |
| 190 | TriggerEventQueue queue = mTriggerListeners.get(listener); |
| 191 | if (queue != null) { |
| 192 | boolean result; |
| 193 | if (sensor == null) { |
| 194 | result = queue.removeAllSensors(); |
| 195 | } else { |
Jaikumar Ganesh | 2e90089 | 2013-04-11 10:40:33 -0700 | [diff] [blame] | 196 | result = queue.removeSensor(sensor, disable); |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 197 | } |
| 198 | if (result && !queue.hasSensors()) { |
| 199 | mTriggerListeners.remove(listener); |
| 200 | queue.dispose(); |
| 201 | } |
| 202 | return result; |
| 203 | } |
| 204 | return false; |
| 205 | } |
| 206 | } |
| 207 | |
Aravind Akella | 4bdc37d | 2013-10-01 17:58:35 -0700 | [diff] [blame] | 208 | protected boolean flushImpl(SensorEventListener listener) { |
| 209 | if (listener == null) throw new IllegalArgumentException("listener cannot be null"); |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 210 | |
Aravind Akella | 4bdc37d | 2013-10-01 17:58:35 -0700 | [diff] [blame] | 211 | synchronized (mSensorListeners) { |
| 212 | SensorEventQueue queue = mSensorListeners.get(listener); |
| 213 | if (queue == null) { |
| 214 | return false; |
| 215 | } else { |
| 216 | return (queue.flush() == 0); |
| 217 | } |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 218 | } |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 221 | /* |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 222 | * BaseEventQueue is the communication channel with the sensor service, |
| 223 | * SensorEventQueue, TriggerEventQueue are subclases and there is one-to-one mapping between |
| 224 | * the queues and the listeners. |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 225 | */ |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 226 | private static abstract class BaseEventQueue { |
Ashok Bhat | 4838e33 | 2014-01-03 14:37:19 +0000 | [diff] [blame] | 227 | private native long nativeInitBaseEventQueue(BaseEventQueue eventQ, MessageQueue msgQ, |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame^] | 228 | float[] scratch, String packageName); |
Ashok Bhat | 4838e33 | 2014-01-03 14:37:19 +0000 | [diff] [blame] | 229 | private static native int nativeEnableSensor(long eventQ, int handle, int rateUs, |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame^] | 230 | int maxBatchReportLatencyUs); |
Ashok Bhat | 4838e33 | 2014-01-03 14:37:19 +0000 | [diff] [blame] | 231 | private static native int nativeDisableSensor(long eventQ, int handle); |
| 232 | private static native void nativeDestroySensorEventQueue(long eventQ); |
| 233 | private static native int nativeFlushSensor(long eventQ); |
| 234 | private long nSensorEventQueue; |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 235 | private final SparseBooleanArray mActiveSensors = new SparseBooleanArray(); |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 236 | protected final SparseIntArray mSensorAccuracies = new SparseIntArray(); |
| 237 | protected final SparseBooleanArray mFirstEvent = new SparseBooleanArray(); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 238 | private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 239 | private final float[] mScratch = new float[16]; |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 240 | protected final SystemSensorManager mManager; |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 241 | |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 242 | BaseEventQueue(Looper looper, SystemSensorManager manager) { |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame^] | 243 | nSensorEventQueue = nativeInitBaseEventQueue(this, looper.getQueue(), mScratch, |
| 244 | manager.mPackageName); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 245 | mCloseGuard.open("dispose"); |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 246 | mManager = manager; |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 247 | } |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 248 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 249 | public void dispose() { |
| 250 | dispose(false); |
| 251 | } |
| 252 | |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 253 | public boolean addSensor( |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame^] | 254 | Sensor sensor, int delayUs, int maxBatchReportLatencyUs) { |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 255 | // Check if already present. |
Jaikumar Ganesh | 2e90089 | 2013-04-11 10:40:33 -0700 | [diff] [blame] | 256 | int handle = sensor.getHandle(); |
| 257 | if (mActiveSensors.get(handle)) return false; |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 258 | |
Jaikumar Ganesh | 2e90089 | 2013-04-11 10:40:33 -0700 | [diff] [blame] | 259 | // Get ready to receive events before calling enable. |
| 260 | mActiveSensors.put(handle, true); |
| 261 | addSensorEvent(sensor); |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame^] | 262 | if (enableSensor(sensor, delayUs, maxBatchReportLatencyUs) != 0) { |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 263 | // Try continuous mode if batching fails. |
| 264 | if (maxBatchReportLatencyUs == 0 || |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame^] | 265 | maxBatchReportLatencyUs > 0 && enableSensor(sensor, delayUs, 0) != 0) { |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 266 | removeSensor(sensor, false); |
| 267 | return false; |
| 268 | } |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 269 | } |
Jaikumar Ganesh | 2e90089 | 2013-04-11 10:40:33 -0700 | [diff] [blame] | 270 | return true; |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 273 | public boolean removeAllSensors() { |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 274 | for (int i=0 ; i<mActiveSensors.size(); i++) { |
| 275 | if (mActiveSensors.valueAt(i) == true) { |
| 276 | int handle = mActiveSensors.keyAt(i); |
| 277 | Sensor sensor = sHandleToSensor.get(handle); |
| 278 | if (sensor != null) { |
| 279 | disableSensor(sensor); |
| 280 | mActiveSensors.put(handle, false); |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 281 | removeSensorEvent(sensor); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 282 | } else { |
| 283 | // it should never happen -- just ignore. |
| 284 | } |
| 285 | } |
| 286 | } |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 287 | return true; |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Jaikumar Ganesh | 2e90089 | 2013-04-11 10:40:33 -0700 | [diff] [blame] | 290 | public boolean removeSensor(Sensor sensor, boolean disable) { |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 291 | final int handle = sensor.getHandle(); |
| 292 | if (mActiveSensors.get(handle)) { |
Jaikumar Ganesh | 2e90089 | 2013-04-11 10:40:33 -0700 | [diff] [blame] | 293 | if (disable) disableSensor(sensor); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 294 | mActiveSensors.put(sensor.getHandle(), false); |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 295 | removeSensorEvent(sensor); |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 296 | return true; |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 297 | } |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 298 | return false; |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Aravind Akella | 4bdc37d | 2013-10-01 17:58:35 -0700 | [diff] [blame] | 301 | public int flush() { |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 302 | if (nSensorEventQueue == 0) throw new NullPointerException(); |
Aravind Akella | 4bdc37d | 2013-10-01 17:58:35 -0700 | [diff] [blame] | 303 | return nativeFlushSensor(nSensorEventQueue); |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 306 | public boolean hasSensors() { |
| 307 | // no more sensors are set |
| 308 | return mActiveSensors.indexOfValue(true) >= 0; |
| 309 | } |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 310 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 311 | @Override |
| 312 | protected void finalize() throws Throwable { |
| 313 | try { |
| 314 | dispose(true); |
| 315 | } finally { |
| 316 | super.finalize(); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | private void dispose(boolean finalized) { |
| 321 | if (mCloseGuard != null) { |
| 322 | if (finalized) { |
| 323 | mCloseGuard.warnIfOpen(); |
| 324 | } |
| 325 | mCloseGuard.close(); |
| 326 | } |
| 327 | if (nSensorEventQueue != 0) { |
| 328 | nativeDestroySensorEventQueue(nSensorEventQueue); |
| 329 | nSensorEventQueue = 0; |
| 330 | } |
| 331 | } |
| 332 | |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 333 | private int enableSensor( |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame^] | 334 | Sensor sensor, int rateUs, int maxBatchReportLatencyUs) { |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 335 | if (nSensorEventQueue == 0) throw new NullPointerException(); |
| 336 | if (sensor == null) throw new NullPointerException(); |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 337 | return nativeEnableSensor(nSensorEventQueue, sensor.getHandle(), rateUs, |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame^] | 338 | maxBatchReportLatencyUs); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 339 | } |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 340 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 341 | private int disableSensor(Sensor sensor) { |
| 342 | if (nSensorEventQueue == 0) throw new NullPointerException(); |
| 343 | if (sensor == null) throw new NullPointerException(); |
| 344 | return nativeDisableSensor(nSensorEventQueue, sensor.getHandle()); |
| 345 | } |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 346 | protected abstract void dispatchSensorEvent(int handle, float[] values, int accuracy, |
| 347 | long timestamp); |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 348 | protected abstract void dispatchFlushCompleteEvent(int handle); |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 349 | |
| 350 | protected abstract void addSensorEvent(Sensor sensor); |
| 351 | protected abstract void removeSensorEvent(Sensor sensor); |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static final class SensorEventQueue extends BaseEventQueue { |
| 355 | private final SensorEventListener mListener; |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 356 | private final SparseArray<SensorEvent> mSensorsEvents = new SparseArray<SensorEvent>(); |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 357 | |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 358 | public SensorEventQueue(SensorEventListener listener, Looper looper, |
Aravind Akella | 4bdc37d | 2013-10-01 17:58:35 -0700 | [diff] [blame] | 359 | SystemSensorManager manager) { |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 360 | super(looper, manager); |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 361 | mListener = listener; |
| 362 | } |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 363 | |
Aravind Akella | 31d14ce | 2013-10-22 20:04:22 -0700 | [diff] [blame] | 364 | @Override |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 365 | public void addSensorEvent(Sensor sensor) { |
| 366 | SensorEvent t = new SensorEvent(Sensor.getMaxLengthValuesArray(sensor, |
| 367 | mManager.mTargetSdkLevel)); |
Aravind Akella | 31d14ce | 2013-10-22 20:04:22 -0700 | [diff] [blame] | 368 | synchronized (mSensorsEvents) { |
| 369 | mSensorsEvents.put(sensor.getHandle(), t); |
| 370 | } |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Aravind Akella | 31d14ce | 2013-10-22 20:04:22 -0700 | [diff] [blame] | 373 | @Override |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 374 | public void removeSensorEvent(Sensor sensor) { |
Aravind Akella | 31d14ce | 2013-10-22 20:04:22 -0700 | [diff] [blame] | 375 | synchronized (mSensorsEvents) { |
| 376 | mSensorsEvents.delete(sensor.getHandle()); |
| 377 | } |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 380 | // Called from native code. |
| 381 | @SuppressWarnings("unused") |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 382 | @Override |
| 383 | protected void dispatchSensorEvent(int handle, float[] values, int inAccuracy, |
| 384 | long timestamp) { |
| 385 | final Sensor sensor = sHandleToSensor.get(handle); |
Aravind Akella | 31d14ce | 2013-10-22 20:04:22 -0700 | [diff] [blame] | 386 | SensorEvent t = null; |
| 387 | synchronized (mSensorsEvents) { |
| 388 | t = mSensorsEvents.get(handle); |
| 389 | } |
| 390 | |
Jaikumar Ganesh | 2e90089 | 2013-04-11 10:40:33 -0700 | [diff] [blame] | 391 | if (t == null) { |
Aravind Akella | 31d14ce | 2013-10-22 20:04:22 -0700 | [diff] [blame] | 392 | // This may happen if the client has unregistered and there are pending events in |
| 393 | // the queue waiting to be delivered. Ignore. |
Jaikumar Ganesh | 2e90089 | 2013-04-11 10:40:33 -0700 | [diff] [blame] | 394 | return; |
| 395 | } |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 396 | // Copy from the values array. |
| 397 | System.arraycopy(values, 0, t.values, 0, t.values.length); |
| 398 | t.timestamp = timestamp; |
| 399 | t.accuracy = inAccuracy; |
| 400 | t.sensor = sensor; |
Etienne Le Grand | af80510 | 2014-05-16 12:21:41 -0700 | [diff] [blame] | 401 | |
| 402 | // call onAccuracyChanged() only if the value changes |
| 403 | final int accuracy = mSensorAccuracies.get(handle); |
| 404 | if ((t.accuracy >= 0) && (accuracy != t.accuracy)) { |
| 405 | mSensorAccuracies.put(handle, t.accuracy); |
| 406 | mListener.onAccuracyChanged(t.sensor, t.accuracy); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 407 | } |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 408 | mListener.onSensorChanged(t); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 409 | } |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 410 | |
| 411 | @SuppressWarnings("unused") |
| 412 | protected void dispatchFlushCompleteEvent(int handle) { |
Aravind Akella | 4bdc37d | 2013-10-01 17:58:35 -0700 | [diff] [blame] | 413 | if (mListener instanceof SensorEventListener2) { |
| 414 | final Sensor sensor = sHandleToSensor.get(handle); |
| 415 | ((SensorEventListener2)mListener).onFlushCompleted(sensor); |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 416 | } |
| 417 | return; |
| 418 | } |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 421 | static final class TriggerEventQueue extends BaseEventQueue { |
| 422 | private final TriggerEventListener mListener; |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 423 | private final SparseArray<TriggerEvent> mTriggerEvents = new SparseArray<TriggerEvent>(); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 424 | |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 425 | public TriggerEventQueue(TriggerEventListener listener, Looper looper, |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 426 | SystemSensorManager manager) { |
| 427 | super(looper, manager); |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 428 | mListener = listener; |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Aravind Akella | 31d14ce | 2013-10-22 20:04:22 -0700 | [diff] [blame] | 431 | @Override |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 432 | public void addSensorEvent(Sensor sensor) { |
| 433 | TriggerEvent t = new TriggerEvent(Sensor.getMaxLengthValuesArray(sensor, |
| 434 | mManager.mTargetSdkLevel)); |
Aravind Akella | 31d14ce | 2013-10-22 20:04:22 -0700 | [diff] [blame] | 435 | synchronized (mTriggerEvents) { |
| 436 | mTriggerEvents.put(sensor.getHandle(), t); |
| 437 | } |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Aravind Akella | 31d14ce | 2013-10-22 20:04:22 -0700 | [diff] [blame] | 440 | @Override |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 441 | public void removeSensorEvent(Sensor sensor) { |
Aravind Akella | 31d14ce | 2013-10-22 20:04:22 -0700 | [diff] [blame] | 442 | synchronized (mTriggerEvents) { |
| 443 | mTriggerEvents.delete(sensor.getHandle()); |
| 444 | } |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 445 | } |
| 446 | |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 447 | // Called from native code. |
| 448 | @SuppressWarnings("unused") |
| 449 | @Override |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 450 | protected void dispatchSensorEvent(int handle, float[] values, int accuracy, |
| 451 | long timestamp) { |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 452 | final Sensor sensor = sHandleToSensor.get(handle); |
Aravind Akella | 31d14ce | 2013-10-22 20:04:22 -0700 | [diff] [blame] | 453 | TriggerEvent t = null; |
| 454 | synchronized (mTriggerEvents) { |
| 455 | t = mTriggerEvents.get(handle); |
| 456 | } |
Jaikumar Ganesh | 2e90089 | 2013-04-11 10:40:33 -0700 | [diff] [blame] | 457 | if (t == null) { |
| 458 | Log.e(TAG, "Error: Trigger Event is null for Sensor: " + sensor); |
| 459 | return; |
| 460 | } |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 461 | |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 462 | // Copy from the values array. |
| 463 | System.arraycopy(values, 0, t.values, 0, t.values.length); |
| 464 | t.timestamp = timestamp; |
| 465 | t.sensor = sensor; |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 466 | |
Jaikumar Ganesh | 2e90089 | 2013-04-11 10:40:33 -0700 | [diff] [blame] | 467 | // A trigger sensor is auto disabled. So just clean up and don't call native |
| 468 | // disable. |
| 469 | mManager.cancelTriggerSensorImpl(mListener, sensor, false); |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 470 | |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 471 | mListener.onTrigger(t); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 472 | } |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 473 | |
| 474 | @SuppressWarnings("unused") |
| 475 | protected void dispatchFlushCompleteEvent(int handle) { |
| 476 | } |
| 477 | } |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 478 | } |