OLD | NEW |
(Empty) | |
| 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 |
| 5 package org.chromium.device.sensors; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.hardware.Sensor; |
| 9 import android.hardware.SensorManager; |
| 10 import android.os.Build; |
| 11 import android.os.Handler; |
| 12 import android.os.HandlerThread; |
| 13 |
| 14 import org.chromium.base.annotations.CalledByNative; |
| 15 import org.chromium.base.annotations.JNINamespace; |
| 16 |
| 17 import org.chromium.mojom.device.mojom.SensorType; |
| 18 |
| 19 import java.util.HashSet; |
| 20 import java.util.Set; |
| 21 |
| 22 /** |
| 23 * Lifetime is controlled by device::PlatformSensorProviderAndroid. |
| 24 */ |
| 25 @JNINamespace("device") |
| 26 class PlatformSensorProvider { |
| 27 /** |
| 28 * SensorManager that is shared among PlatformSensor objects. It is used for
Sensor object |
| 29 * creation and @see android.hardware.SensorEventListener registration. |
| 30 * @see android.hardware.SensorManager |
| 31 */ |
| 32 private final SensorManager mSensorManager; |
| 33 |
| 34 /** |
| 35 * Thread that is handling all sensor events. |
| 36 */ |
| 37 private HandlerThread mSensorsThread; |
| 38 |
| 39 /** |
| 40 * Processes messages on #mSensorsThread message queue. Provided to #mSensor
Manager when |
| 41 * sensor should start polling for data. |
| 42 */ |
| 43 private Handler mHandler; |
| 44 |
| 45 /** |
| 46 * Set of currently active PlatformSensor objects. |
| 47 */ |
| 48 private final Set<PlatformSensor> mActiveSensors = new HashSet<PlatformSenso
r>(); |
| 49 |
| 50 /** |
| 51 * Returns shared thread Handler. |
| 52 * |
| 53 * @return Handler thread handler. |
| 54 */ |
| 55 public Handler getHandler() { |
| 56 return mHandler; |
| 57 } |
| 58 |
| 59 /** |
| 60 * Returns shared SensorManager. |
| 61 * |
| 62 * @return SensorManager sensor manager. |
| 63 */ |
| 64 public SensorManager getSensorManager() { |
| 65 return mSensorManager; |
| 66 } |
| 67 |
| 68 /** |
| 69 * Notifies PlatformSensorProvider that sensor started polling for data. Add
s sensor to |
| 70 * a set of active sensors, creates and starts new thread if needed. |
| 71 */ |
| 72 public void sensorStarted(PlatformSensor sensor) { |
| 73 if (mActiveSensors.isEmpty()) startSensorThread(); |
| 74 mActiveSensors.add(sensor); |
| 75 } |
| 76 |
| 77 /** |
| 78 * Notifies PlatformSensorProvider that sensor is no longer polling for data
. When |
| 79 * #mActiveSensors becomes empty thread is stopped. |
| 80 */ |
| 81 public void sensorStopped(PlatformSensor sensor) { |
| 82 mActiveSensors.remove(sensor); |
| 83 if (mActiveSensors.isEmpty()) stopSensorThread(); |
| 84 } |
| 85 |
| 86 /** |
| 87 * Starts sensor handler thread. |
| 88 */ |
| 89 protected void startSensorThread() { |
| 90 if (mSensorsThread == null) { |
| 91 mSensorsThread = new HandlerThread("SensorsHandlerThread"); |
| 92 mSensorsThread.start(); |
| 93 mHandler = new Handler(mSensorsThread.getLooper()); |
| 94 } |
| 95 } |
| 96 |
| 97 /** |
| 98 * Stops sensor handler thread. |
| 99 */ |
| 100 protected void stopSensorThread() { |
| 101 if (mSensorsThread != null) { |
| 102 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { |
| 103 mSensorsThread.quitSafely(); |
| 104 } else { |
| 105 mSensorsThread.quit(); |
| 106 } |
| 107 mSensorsThread = null; |
| 108 mHandler = null; |
| 109 } |
| 110 } |
| 111 |
| 112 /** |
| 113 * Constructor. |
| 114 * |
| 115 * @param context application context. |
| 116 */ |
| 117 protected PlatformSensorProvider(Context context) { |
| 118 mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR
_SERVICE); |
| 119 } |
| 120 |
| 121 /** |
| 122 * Creates PlatformSensorProvider instance. |
| 123 * |
| 124 * @param context application context. |
| 125 * @return PlatformSensorProvider new PlatformSensorProvider instance. |
| 126 */ |
| 127 @CalledByNative |
| 128 protected static PlatformSensorProvider create(Context context) { |
| 129 return new PlatformSensorProvider(context); |
| 130 } |
| 131 |
| 132 /** |
| 133 * Creates PlatformSensor instance. |
| 134 * |
| 135 * @param type type of a sensor. |
| 136 * @return PlatformSensor new PlatformSensor instance or null if sensor cann
ot be created. |
| 137 */ |
| 138 @CalledByNative |
| 139 protected PlatformSensor createSensor(int type) { |
| 140 if (mSensorManager == null) return null; |
| 141 |
| 142 switch (type) { |
| 143 case SensorType.AMBIENT_LIGHT: |
| 144 return PlatformSensor.create(Sensor.TYPE_LIGHT, 1, this); |
| 145 case SensorType.ACCELEROMETER: |
| 146 return PlatformSensor.create(Sensor.TYPE_ACCELEROMETER, 3, this)
; |
| 147 case SensorType.LINEAR_ACCELERATION: |
| 148 return PlatformSensor.create(Sensor.TYPE_LINEAR_ACCELERATION, 3,
this); |
| 149 case SensorType.GYROSCOPE: |
| 150 return PlatformSensor.create(Sensor.TYPE_GYROSCOPE, 3, this); |
| 151 case SensorType.MAGNETOMETER: |
| 152 return PlatformSensor.create(Sensor.TYPE_MAGNETIC_FIELD, 3, this
); |
| 153 default: |
| 154 return null; |
| 155 } |
| 156 } |
| 157 } |
OLD | NEW |