OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.hardware.Sensor; | 8 import android.hardware.Sensor; |
9 import android.hardware.SensorEvent; | 9 import android.hardware.SensorEvent; |
10 import android.hardware.SensorEventListener; | 10 import android.hardware.SensorEventListener; |
11 import android.hardware.SensorManager; | 11 import android.hardware.SensorManager; |
12 import android.os.Handler; | 12 import android.os.Handler; |
13 import android.os.Looper; | 13 import android.os.Looper; |
14 import android.util.Log; | |
15 | |
16 import com.google.common.annotations.VisibleForTesting; | |
17 import com.google.common.collect.ImmutableSet; | |
18 import com.google.common.collect.Sets; | |
14 | 19 |
15 import org.chromium.base.CalledByNative; | 20 import org.chromium.base.CalledByNative; |
16 import org.chromium.base.JNINamespace; | 21 import org.chromium.base.JNINamespace; |
17 import org.chromium.base.WeakContext; | 22 import org.chromium.base.WeakContext; |
18 | 23 |
19 import java.util.List; | 24 import java.util.List; |
25 import java.util.Set; | |
20 | 26 |
21 /** | 27 /** |
22 * Android implementation of the DeviceOrientation API. | 28 * Android implementation of the device motion and orientation APIs. |
23 */ | 29 */ |
24 @JNINamespace("content") | 30 @JNINamespace("content") |
25 class DeviceOrientation implements SensorEventListener { | 31 class DeviceMotionAndOrientation implements SensorEventListener { |
32 | |
33 private static final String TAG = "DeviceMotionAndOrientation"; | |
26 | 34 |
27 // These fields are lazily initialized by getHandler(). | 35 // These fields are lazily initialized by getHandler(). |
28 private Thread mThread; | 36 private Thread mThread; |
29 private Handler mHandler; | 37 private Handler mHandler; |
30 | 38 |
31 // The lock to access the mHandler. | 39 // The lock to access the mHandler. |
32 private Object mHandlerLock = new Object(); | 40 private Object mHandlerLock = new Object(); |
33 | 41 |
34 // Non-zero if and only if we're listening for events. | 42 // Non-zero if and only if we're listening for events. |
35 // To avoid race conditions on the C++ side, access must be synchronized. | 43 // To avoid race conditions on the C++ side, access must be synchronized. |
36 private int mNativePtr; | 44 private int mNativePtr; |
37 | 45 |
38 // The lock to access the mNativePtr. | 46 // The lock to access the mNativePtr. |
39 private Object mNativePtrLock = new Object(); | 47 private Object mNativePtrLock = new Object(); |
40 | 48 |
41 // The gravity vector expressed in the body frame. | 49 // The acceleration vector including gravity expressed in the body frame. |
42 private float[] mGravityVector; | 50 private float[] mAccelerationVector; |
43 | 51 |
44 // The geomagnetic vector expressed in the body frame. | 52 // The geomagnetic vector expressed in the body frame. |
45 private float[] mMagneticFieldVector; | 53 private float[] mMagneticFieldVector; |
46 | 54 |
47 // Lazily initialized when registering for notifications. | 55 // Lazily initialized when registering for notifications. |
48 private SensorManager mSensorManager; | 56 private SensorManagerProxy mSensorManagerProxy; |
49 | 57 |
50 // The only instance of that class and its associated lock. | 58 // The only instance of that class and its associated lock. |
51 private static DeviceOrientation sSingleton; | 59 private static DeviceMotionAndOrientation sSingleton; |
52 private static Object sSingletonLock = new Object(); | 60 private static Object sSingletonLock = new Object(); |
53 | 61 |
54 private DeviceOrientation() { | 62 /** |
63 * constants for using in JNI calls, also see | |
64 * content/browser/device_orientation/data_fetcher_impl_android.cc | |
65 */ | |
66 static final int DEVICE_ORIENTATION = 0; | |
67 static final int DEVICE_MOTION = 1; | |
68 | |
69 static final ImmutableSet<Integer> DEVICE_ORIENTATION_SENSORS = ImmutableSet .of( | |
70 Sensor.TYPE_ACCELEROMETER, | |
71 Sensor.TYPE_MAGNETIC_FIELD); | |
72 | |
73 static final ImmutableSet<Integer> DEVICE_MOTION_SENSORS = ImmutableSet.of( | |
74 Sensor.TYPE_ACCELEROMETER, | |
75 Sensor.TYPE_LINEAR_ACCELERATION, | |
76 Sensor.TYPE_GYROSCOPE); | |
77 | |
78 @VisibleForTesting | |
79 final Set<Integer> mActiveSensors = Sets.newHashSet(); | |
80 boolean mDeviceMotionIsActive = false; | |
81 boolean mDeviceOrientationIsActive = false; | |
82 | |
83 protected DeviceMotionAndOrientation() { | |
55 } | 84 } |
56 | 85 |
57 /** | 86 /** |
58 * Start listening for sensor events. If this object is already listening | 87 * Start listening for sensor events. If this object is already listening |
59 * for events, the old callback is unregistered first. | 88 * for events, the old callback is unregistered first. |
60 * | 89 * |
61 * @param nativePtr Value to pass to nativeGotOrientation() for each event. | 90 * @param nativePtr Value to pass to nativeGotOrientation() for each event. |
62 * @param rateInMilliseconds Requested callback rate in milliseconds. The | 91 * @param rateInMilliseconds Requested callback rate in milliseconds. The |
63 * actual rate may be higher. Unwanted events should be ignored. | 92 * actual rate may be higher. Unwanted events should be ignored. |
93 * @param eventType Type of event to listen to, can be either DEVICE_ORIENTA TION or | |
94 * DEVICE_MOTION. | |
64 * @return True on success. | 95 * @return True on success. |
65 */ | 96 */ |
66 @CalledByNative | 97 @CalledByNative |
67 public boolean start(int nativePtr, int rateInMilliseconds) { | 98 public boolean start(int nativePtr, int eventType, int rateInMilliseconds) { |
99 boolean success = false; | |
68 synchronized (mNativePtrLock) { | 100 synchronized (mNativePtrLock) { |
69 stop(); | 101 switch (eventType) { |
70 if (registerForSensors(rateInMilliseconds)) { | 102 case DEVICE_ORIENTATION: |
103 success = registerSensors(DEVICE_ORIENTATION_SENSORS, rateIn Milliseconds, | |
104 true); | |
105 break; | |
106 case DEVICE_MOTION: | |
107 // note: device motion spec does not require all sensors to be available | |
108 success = registerSensors(DEVICE_MOTION_SENSORS, rateInMilli seconds, false); | |
109 break; | |
110 default: | |
111 Log.e(TAG, "Unknown event type: " + eventType); | |
112 return false; | |
113 } | |
114 if (success) { | |
71 mNativePtr = nativePtr; | 115 mNativePtr = nativePtr; |
72 return true; | 116 setEventTypeActive(eventType, true); |
73 } | 117 } |
74 return false; | 118 return success; |
75 } | 119 } |
76 } | 120 } |
77 | 121 |
78 /** | 122 /** |
79 * Stop listening for sensor events. Always succeeds. | 123 * Stop listening to sensors for a given event type. Ensures that sensors ar e not disabled |
124 * if they are still in use by a different event type. | |
80 * | 125 * |
81 * We strictly guarantee that nativeGotOrientation() will not be called | 126 * @param eventType Type of event to listen to, can be either DEVICE_ORIENTA TION or |
127 * DEVICE_MOTION. | |
128 * We strictly guarantee that the corresponding native*() methods will not b e called | |
82 * after this method returns. | 129 * after this method returns. |
83 */ | 130 */ |
84 @CalledByNative | 131 @CalledByNative |
85 public void stop() { | 132 public void stop(int eventType) { |
133 Set<Integer> sensorsToRemainActive = Sets.newHashSet(); | |
86 synchronized (mNativePtrLock) { | 134 synchronized (mNativePtrLock) { |
87 if (mNativePtr != 0) { | 135 switch (eventType) { |
136 case DEVICE_ORIENTATION: | |
137 if (mDeviceMotionIsActive) { | |
138 sensorsToRemainActive.addAll(DEVICE_MOTION_SENSORS); | |
139 } | |
140 break; | |
141 case DEVICE_MOTION: | |
142 if (mDeviceOrientationIsActive) { | |
143 sensorsToRemainActive.addAll(DEVICE_ORIENTATION_SENSORS) ; | |
144 } | |
145 break; | |
146 default: | |
147 Log.e(TAG, "Unknown event type: " + eventType); | |
148 return; | |
149 } | |
150 | |
151 Set<Integer> sensorsToDeactivate = Sets.newHashSet(mActiveSensors); | |
152 sensorsToDeactivate.removeAll(sensorsToRemainActive); | |
153 unregisterSensors(sensorsToDeactivate); | |
154 setEventTypeActive(eventType, false); | |
155 if (mActiveSensors.isEmpty()) { | |
88 mNativePtr = 0; | 156 mNativePtr = 0; |
89 unregisterForSensors(); | |
90 } | 157 } |
91 } | 158 } |
92 } | 159 } |
93 | 160 |
94 @Override | 161 @Override |
95 public void onAccuracyChanged(Sensor sensor, int accuracy) { | 162 public void onAccuracyChanged(Sensor sensor, int accuracy) { |
96 // Nothing | 163 // Nothing |
97 } | 164 } |
98 | 165 |
99 @Override | 166 @Override |
100 public void onSensorChanged(SensorEvent event) { | 167 public void onSensorChanged(SensorEvent event) { |
101 switch (event.sensor.getType()) { | 168 sensorChanged(event.sensor.getType(), event.values); |
169 } | |
170 | |
171 @VisibleForTesting | |
172 void sensorChanged(int type, float[] values) { | |
173 | |
174 switch (type) { | |
102 case Sensor.TYPE_ACCELEROMETER: | 175 case Sensor.TYPE_ACCELEROMETER: |
103 if (mGravityVector == null) { | 176 if (mAccelerationVector == null) { |
104 mGravityVector = new float[3]; | 177 mAccelerationVector = new float[3]; |
105 } | 178 } |
106 System.arraycopy(event.values, 0, mGravityVector, 0, mGravityVec tor.length); | 179 System.arraycopy(values, 0, mAccelerationVector, 0, |
180 mAccelerationVector.length); | |
181 if (mDeviceMotionIsActive) { | |
182 gotAccelerationIncludingGravity(mAccelerationVector[0], mAcc elerationVector[1], | |
183 mAccelerationVector[2]); | |
184 } | |
107 break; | 185 break; |
108 | 186 case Sensor.TYPE_LINEAR_ACCELERATION: |
187 if (mDeviceMotionIsActive) { | |
188 gotAcceleration(values[0], values[1], values[2]); | |
189 } | |
190 break; | |
191 case Sensor.TYPE_GYROSCOPE: | |
192 if (mDeviceMotionIsActive) { | |
193 gotRotationRate(values[0], values[1], values[2]); | |
194 } | |
195 break; | |
109 case Sensor.TYPE_MAGNETIC_FIELD: | 196 case Sensor.TYPE_MAGNETIC_FIELD: |
110 if (mMagneticFieldVector == null) { | 197 if (mMagneticFieldVector == null) { |
111 mMagneticFieldVector = new float[3]; | 198 mMagneticFieldVector = new float[3]; |
112 } | 199 } |
113 System.arraycopy(event.values, 0, mMagneticFieldVector, 0, | 200 System.arraycopy(values, 0, mMagneticFieldVector, 0, |
114 mMagneticFieldVector.length); | 201 mMagneticFieldVector.length); |
115 break; | 202 break; |
116 | |
117 default: | 203 default: |
118 // Unexpected | 204 // Unexpected |
119 return; | 205 return; |
120 } | 206 } |
121 | 207 |
122 getOrientationUsingGetRotationMatrix(); | 208 if (mDeviceOrientationIsActive) { |
209 getOrientationUsingGetRotationMatrix(); | |
210 } | |
123 } | 211 } |
124 | 212 |
125 void getOrientationUsingGetRotationMatrix() { | 213 private void getOrientationUsingGetRotationMatrix() { |
126 if (mGravityVector == null || mMagneticFieldVector == null) { | 214 if (mAccelerationVector == null || mMagneticFieldVector == null) { |
127 return; | 215 return; |
128 } | 216 } |
129 | 217 |
130 // Get the rotation matrix. | 218 // Get the rotation matrix. |
131 // The rotation matrix that transforms from the body frame to the earth | 219 // The rotation matrix that transforms from the body frame to the earth |
132 // frame. | 220 // frame. |
133 float[] deviceRotationMatrix = new float[9]; | 221 float[] deviceRotationMatrix = new float[9]; |
134 if (!SensorManager.getRotationMatrix(deviceRotationMatrix, null, mGravit yVector, | 222 if (!SensorManager.getRotationMatrix(deviceRotationMatrix, null, mAccele rationVector, |
135 mMagneticFieldVector)) { | 223 mMagneticFieldVector)) { |
136 return; | 224 return; |
137 } | 225 } |
138 | 226 |
139 // Convert rotation matrix to rotation angles. | 227 // Convert rotation matrix to rotation angles. |
140 // Assuming that the rotations are appied in the order listed at | 228 // Assuming that the rotations are appied in the order listed at |
141 // http://developer.android.com/reference/android/hardware/SensorEvent.h tml#values | 229 // http://developer.android.com/reference/android/hardware/SensorEvent.h tml#values |
142 // the rotations are applied about the same axes and in the same order a s required by the | 230 // the rotations are applied about the same axes and in the same order a s required by the |
143 // API. The only conversions are sign changes as follows. The angles ar e in radians | 231 // API. The only conversions are sign changes as follows. The angles ar e in radians |
144 | 232 |
(...skipping 11 matching lines...) Expand all Loading... | |
156 } | 244 } |
157 | 245 |
158 double gamma = Math.toDegrees(rotationAngles[2]); | 246 double gamma = Math.toDegrees(rotationAngles[2]); |
159 while (gamma < -90.0) { | 247 while (gamma < -90.0) { |
160 gamma += 360.0; // [-90, 90) | 248 gamma += 360.0; // [-90, 90) |
161 } | 249 } |
162 | 250 |
163 gotOrientation(alpha, beta, gamma); | 251 gotOrientation(alpha, beta, gamma); |
164 } | 252 } |
165 | 253 |
166 boolean registerForSensors(int rateInMilliseconds) { | 254 private SensorManagerProxy getSensorManagerProxy() { |
167 if (registerForSensorType(Sensor.TYPE_ACCELEROMETER, rateInMilliseconds) | 255 if (mSensorManagerProxy != null) { |
168 && registerForSensorType(Sensor.TYPE_MAGNETIC_FIELD, rateInMilli seconds)) { | 256 return mSensorManagerProxy; |
169 return true; | |
170 } | 257 } |
171 unregisterForSensors(); | 258 SensorManager sensorManager = (SensorManager)WeakContext.getSystemServic e( |
172 return false; | 259 Context.SENSOR_SERVICE); |
260 if (sensorManager != null) { | |
261 mSensorManagerProxy = new SensorManagerProxyImpl(sensorManager); | |
262 } | |
263 return mSensorManagerProxy; | |
173 } | 264 } |
174 | 265 |
175 private SensorManager getSensorManager() { | 266 @VisibleForTesting |
176 if (mSensorManager != null) { | 267 void setSensorManagerProxy(SensorManagerProxy sensorManagerProxy) { |
177 return mSensorManager; | 268 mSensorManagerProxy = sensorManagerProxy; |
178 } | |
179 mSensorManager = (SensorManager)WeakContext.getSystemService(Context.SEN SOR_SERVICE); | |
180 return mSensorManager; | |
181 } | 269 } |
182 | 270 |
183 void unregisterForSensors() { | 271 private void setEventTypeActive(int eventType, boolean value) { |
184 SensorManager sensorManager = getSensorManager(); | 272 switch (eventType) { |
185 if (sensorManager == null) { | 273 case DEVICE_ORIENTATION: |
186 return; | 274 mDeviceOrientationIsActive = value; |
275 return; | |
276 case DEVICE_MOTION: | |
277 mDeviceMotionIsActive = value; | |
278 return; | |
187 } | 279 } |
188 sensorManager.unregisterListener(this); | |
189 } | 280 } |
190 | 281 |
191 boolean registerForSensorType(int type, int rateInMilliseconds) { | 282 /** |
192 SensorManager sensorManager = getSensorManager(); | 283 * @param sensorTypes List of sensors to activate. |
284 * @param rateInMilliseconds Intended delay (in milliseconds) between sensor readings. | |
285 * @param failOnMissingSensor If true the method returns true only if all se nsors could be | |
286 * activated. When false the method return true i f at least one | |
287 * sensor in sensorTypes could be activated. | |
288 */ | |
289 private boolean registerSensors(Iterable<Integer> sensorTypes, int rateInMil liseconds, | |
290 boolean failOnMissingSensor) { | |
291 Set<Integer> sensorsToActivate = Sets.newHashSet(sensorTypes); | |
292 sensorsToActivate.removeAll(mActiveSensors); | |
293 boolean success = false; | |
294 | |
295 for (Integer sensorType : sensorsToActivate) { | |
296 boolean result = registerForSensorType(sensorType, rateInMillisecond s); | |
297 if (!result && failOnMissingSensor) { | |
298 // restore the previous state upon failure | |
299 unregisterSensors(sensorsToActivate); | |
300 return false; | |
301 } | |
302 if (result) { | |
303 mActiveSensors.add(sensorType); | |
304 success = true; | |
305 } | |
306 } | |
307 return success; | |
308 } | |
309 | |
310 private void unregisterSensors(Iterable<Integer> sensorTypes) { | |
311 for (Integer sensorType : sensorTypes) { | |
312 if (mActiveSensors.contains(sensorType)) { | |
313 getSensorManagerProxy().unregisterListener(this, sensorType); | |
314 mActiveSensors.remove(sensorType); | |
315 } | |
316 } | |
317 } | |
318 | |
319 private boolean registerForSensorType(int type, int rateInMilliseconds) { | |
320 SensorManagerProxy sensorManager = getSensorManagerProxy(); | |
193 if (sensorManager == null) { | 321 if (sensorManager == null) { |
194 return false; | 322 return false; |
195 } | 323 } |
196 List<Sensor> sensors = sensorManager.getSensorList(type); | |
197 if (sensors.isEmpty()) { | |
198 return false; | |
199 } | |
200 | |
201 final int rateInMicroseconds = 1000 * rateInMilliseconds; | 324 final int rateInMicroseconds = 1000 * rateInMilliseconds; |
202 // We want to err on the side of getting more events than we need. | 325 return sensorManager.registerListener(this, type, rateInMicroseconds, ge tHandler()); |
203 final int requestedRate = rateInMicroseconds / 2; | |
204 | |
205 // TODO(steveblock): Consider handling multiple sensors. | |
206 return sensorManager.registerListener(this, sensors.get(0), requestedRat e, getHandler()); | |
207 } | 326 } |
208 | 327 |
209 void gotOrientation(double alpha, double beta, double gamma) { | 328 protected void gotOrientation(double alpha, double beta, double gamma) { |
210 synchronized (mNativePtrLock) { | 329 synchronized (mNativePtrLock) { |
211 if (mNativePtr != 0) { | 330 if (mNativePtr != 0) { |
212 nativeGotOrientation(mNativePtr, alpha, beta, gamma); | 331 nativeGotOrientation(mNativePtr, alpha, beta, gamma); |
213 } | 332 } |
214 } | 333 } |
215 } | 334 } |
335 | |
336 protected void gotAcceleration(double x, double y, double z) { | |
337 synchronized (mNativePtrLock) { | |
338 if (mNativePtr != 0) { | |
339 nativeGotAcceleration(mNativePtr, x, y, z); | |
340 } | |
341 } | |
342 } | |
343 | |
344 protected void gotAccelerationIncludingGravity(double x, double y, double z) { | |
345 synchronized (mNativePtrLock) { | |
346 if (mNativePtr != 0) { | |
347 nativeGotAccelerationIncludingGravity(mNativePtr, x, y, z); | |
348 } | |
349 } | |
350 } | |
351 | |
352 protected void gotRotationRate(double alpha, double beta, double gamma) { | |
353 synchronized (mNativePtrLock) { | |
354 if (mNativePtr != 0) { | |
355 nativeGotRotationRate(mNativePtr, alpha, beta, gamma); | |
356 } | |
357 } | |
358 } | |
216 | 359 |
217 private Handler getHandler() { | 360 private Handler getHandler() { |
joth
2013/04/17 21:02:26
this whole function could be
private Handler getHa
timvolodine
2013/04/18 13:44:12
Done.
| |
218 synchronized (mHandlerLock) { | 361 synchronized (mHandlerLock) { |
219 // If we don't have a background thread, start it now. | 362 // If we don't have a background thread, start it now. |
220 if (mThread == null) { | 363 if (mThread == null) { |
221 mThread = new Thread(new Runnable() { | 364 mThread = new Thread(new Runnable() { |
222 @Override | 365 @Override |
223 public void run() { | 366 public void run() { |
224 Looper.prepare(); | 367 Looper.prepare(); |
225 // Our Handler doesn't actually have to do anything, bec ause | 368 // Our Handler doesn't actually have to do anything, bec ause |
226 // SensorManager posts directly to the underlying Looper . | 369 // SensorManager posts directly to the underlying Looper . |
227 setHandler(new Handler()); | 370 setHandler(new Handler()); |
(...skipping 16 matching lines...) Expand all Loading... | |
244 } | 387 } |
245 | 388 |
246 private void setHandler(Handler handler) { | 389 private void setHandler(Handler handler) { |
247 synchronized (mHandlerLock) { | 390 synchronized (mHandlerLock) { |
248 mHandler = handler; | 391 mHandler = handler; |
249 mHandlerLock.notify(); | 392 mHandlerLock.notify(); |
250 } | 393 } |
251 } | 394 } |
252 | 395 |
253 @CalledByNative | 396 @CalledByNative |
254 private static DeviceOrientation getInstance() { | 397 static DeviceMotionAndOrientation getInstance() { |
255 synchronized (sSingletonLock) { | 398 synchronized (sSingletonLock) { |
256 if (sSingleton == null) { | 399 if (sSingleton == null) { |
257 sSingleton = new DeviceOrientation(); | 400 sSingleton = new DeviceMotionAndOrientation(); |
258 } | 401 } |
259 return sSingleton; | 402 return sSingleton; |
260 } | 403 } |
261 } | 404 } |
262 | 405 |
263 /** | 406 /** |
264 * See chrome/browser/device_orientation/data_fetcher_impl_android.cc | 407 * Native JNI calls, |
408 * see content/browser/device_orientation/data_fetcher_impl_android.cc | |
409 */ | |
410 | |
411 /** | |
412 * Orientation of the device with respect to its reference frame. | |
265 */ | 413 */ |
266 private native void nativeGotOrientation( | 414 private native void nativeGotOrientation( |
267 int nativeDataFetcherImplAndroid, | 415 int nativeDataFetcherImplAndroid, |
268 double alpha, double beta, double gamma); | 416 double alpha, double beta, double gamma); |
269 } | 417 |
418 /** | |
419 * Linear acceleration without gravity of the device with respect to its bod y frame. | |
420 */ | |
421 private native void nativeGotAcceleration( | |
422 int nativeDataFetcherImplAndroid, | |
423 double x, double y, double z); | |
424 | |
425 /** | |
426 * Acceleration including gravity of the device with respect to its body fra me. | |
427 */ | |
428 private native void nativeGotAccelerationIncludingGravity( | |
429 int nativeDataFetcherImplAndroid, | |
430 double x, double y, double z); | |
431 | |
432 /** | |
433 * Rotation rate of the device with respect to its body frame. | |
434 */ | |
435 private native void nativeGotRotationRate( | |
436 int nativeDataFetcherImplAndroid, | |
437 double alpha, double beta, double gamma); | |
438 | |
439 /** | |
440 * Need the an interface for SensorManager for testing. | |
441 */ | |
442 interface SensorManagerProxy { | |
443 public boolean registerListener(SensorEventListener listener, int sensor Type, int rate, | |
444 Handler handler); | |
445 public void unregisterListener(SensorEventListener listener, int sensorT ype); | |
446 } | |
447 | |
448 static class SensorManagerProxyImpl implements SensorManagerProxy { | |
449 private final SensorManager mSensorManager; | |
450 | |
451 SensorManagerProxyImpl(SensorManager sensorManager) { | |
452 mSensorManager = sensorManager; | |
453 } | |
454 | |
455 public boolean registerListener(SensorEventListener listener, int sensor Type, int rate, | |
456 Handler handler) { | |
457 List<Sensor> sensors = mSensorManager.getSensorList(sensorType); | |
458 if (sensors.isEmpty()) { | |
459 return false; | |
460 } | |
461 return mSensorManager.registerListener(listener, sensors.get(0), rat e, handler); | |
462 } | |
463 | |
464 public void unregisterListener(SensorEventListener listener, int sensorT ype) { | |
465 List<Sensor> sensors = mSensorManager.getSensorList(sensorType); | |
466 if (!sensors.isEmpty()) { | |
467 mSensorManager.unregisterListener(listener, sensors.get(0)); | |
468 } | |
469 } | |
470 } | |
471 | |
472 } | |
OLD | NEW |