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