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