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