PlatformSensorProvider.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. package org.chromium.device.sensors;
  5. import android.content.Context;
  6. import android.hardware.Sensor;
  7. import android.hardware.SensorManager;
  8. import android.os.Build;
  9. import android.os.Handler;
  10. import android.os.HandlerThread;
  11. import org.chromium.base.ContextUtils;
  12. import org.chromium.base.annotations.CalledByNative;
  13. import org.chromium.base.annotations.JNINamespace;
  14. import org.chromium.device.mojom.SensorType;
  15. import java.util.HashSet;
  16. import java.util.List;
  17. import java.util.Set;
  18. /**
  19. * Lifetime is controlled by device::PlatformSensorProviderAndroid.
  20. */
  21. @JNINamespace("device")
  22. class PlatformSensorProvider {
  23. /**
  24. * SensorManager that is shared among PlatformSensor objects. It is used for Sensor object
  25. * creation and @see android.hardware.SensorEventListener registration.
  26. * @see android.hardware.SensorManager
  27. */
  28. private SensorManager mSensorManager;
  29. /**
  30. * Thread that is handling all sensor events.
  31. */
  32. private HandlerThread mSensorsThread;
  33. /**
  34. * Processes messages on #mSensorsThread message queue. Provided to #mSensorManager when
  35. * sensor should start polling for data.
  36. */
  37. private Handler mHandler;
  38. /**
  39. * Set of currently active PlatformSensor objects.
  40. */
  41. private final Set<PlatformSensor> mActiveSensors = new HashSet<PlatformSensor>();
  42. /**
  43. * Returns shared thread Handler.
  44. *
  45. * @return Handler thread handler.
  46. */
  47. public Handler getHandler() {
  48. return mHandler;
  49. }
  50. /**
  51. * Returns shared SensorManager.
  52. *
  53. * @return SensorManager sensor manager.
  54. */
  55. public SensorManager getSensorManager() {
  56. return mSensorManager;
  57. }
  58. /**
  59. * Notifies PlatformSensorProvider that sensor started polling for data. Adds sensor to
  60. * a set of active sensors, creates and starts new thread if needed.
  61. */
  62. public void sensorStarted(PlatformSensor sensor) {
  63. synchronized (mActiveSensors) {
  64. if (mActiveSensors.isEmpty()) startSensorThread();
  65. mActiveSensors.add(sensor);
  66. }
  67. }
  68. /**
  69. * Notifies PlatformSensorProvider that sensor is no longer polling for data. When
  70. * #mActiveSensors becomes empty thread is stopped.
  71. */
  72. public void sensorStopped(PlatformSensor sensor) {
  73. synchronized (mActiveSensors) {
  74. mActiveSensors.remove(sensor);
  75. if (mActiveSensors.isEmpty()) stopSensorThread();
  76. }
  77. }
  78. /**
  79. * Starts sensor handler thread.
  80. */
  81. protected void startSensorThread() {
  82. if (mSensorsThread == null) {
  83. mSensorsThread = new HandlerThread("SensorsHandlerThread");
  84. mSensorsThread.start();
  85. mHandler = new Handler(mSensorsThread.getLooper());
  86. }
  87. }
  88. /**
  89. * Stops sensor handler thread.
  90. */
  91. protected void stopSensorThread() {
  92. if (mSensorsThread != null) {
  93. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
  94. mSensorsThread.quitSafely();
  95. } else {
  96. mSensorsThread.quit();
  97. }
  98. mSensorsThread = null;
  99. mHandler = null;
  100. }
  101. }
  102. /**
  103. * Constructor.
  104. */
  105. protected PlatformSensorProvider(Context context) {
  106. mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
  107. }
  108. /**
  109. * Creates PlatformSensorProvider instance.
  110. *
  111. * @return PlatformSensorProvider new PlatformSensorProvider instance.
  112. */
  113. protected static PlatformSensorProvider createForTest(Context context) {
  114. return new PlatformSensorProvider(context);
  115. }
  116. /**
  117. * Creates PlatformSensorProvider instance.
  118. *
  119. * @return PlatformSensorProvider new PlatformSensorProvider instance.
  120. */
  121. @CalledByNative
  122. protected static PlatformSensorProvider create() {
  123. return new PlatformSensorProvider(ContextUtils.getApplicationContext());
  124. }
  125. /**
  126. * Sets |mSensorManager| to null for testing purposes.
  127. */
  128. @CalledByNative
  129. protected void setSensorManagerToNullForTesting() {
  130. mSensorManager = null;
  131. }
  132. /**
  133. * Checks if |type| sensor is available.
  134. *
  135. * @param type type of a sensor.
  136. * @return If |type| sensor is available, returns true; otherwise returns false.
  137. */
  138. @CalledByNative
  139. protected boolean hasSensorType(int type) {
  140. if (mSensorManager == null) return false;
  141. // Type of the sensor to be constructed. @see android.hardware.Sensor.TYPE_*
  142. int sensorType;
  143. switch (type) {
  144. case SensorType.AMBIENT_LIGHT:
  145. sensorType = Sensor.TYPE_LIGHT;
  146. break;
  147. case SensorType.ACCELEROMETER:
  148. sensorType = Sensor.TYPE_ACCELEROMETER;
  149. break;
  150. case SensorType.LINEAR_ACCELERATION:
  151. sensorType = Sensor.TYPE_LINEAR_ACCELERATION;
  152. break;
  153. case SensorType.GRAVITY:
  154. sensorType = Sensor.TYPE_GRAVITY;
  155. break;
  156. case SensorType.GYROSCOPE:
  157. sensorType = Sensor.TYPE_GYROSCOPE;
  158. break;
  159. case SensorType.MAGNETOMETER:
  160. sensorType = Sensor.TYPE_MAGNETIC_FIELD;
  161. break;
  162. case SensorType.ABSOLUTE_ORIENTATION_QUATERNION:
  163. sensorType = Sensor.TYPE_ROTATION_VECTOR;
  164. break;
  165. case SensorType.RELATIVE_ORIENTATION_QUATERNION:
  166. sensorType = Sensor.TYPE_GAME_ROTATION_VECTOR;
  167. break;
  168. default:
  169. return false;
  170. }
  171. List<Sensor> sensors = mSensorManager.getSensorList(sensorType);
  172. return !sensors.isEmpty();
  173. }
  174. }