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 mActiveSensors.add(sensor); |
| 74 if (mSensorsThread == null) { |
| 75 mSensorsThread = new HandlerThread("SensorsHandlerThread"); |
| 76 mSensorsThread.start(); |
| 77 mHandler = new Handler(mSensorsThread.getLooper()); |
| 78 } |
| 79 } |
| 80 |
| 81 /** |
| 82 * Notifies PlatformSensorProvider that sensor is no longer polling for data
. When |
| 83 * #mActiveSensors becomes empty thread is stopped. |
| 84 */ |
| 85 public void sensorStopped(PlatformSensor sensor) { |
| 86 mActiveSensors.remove(sensor); |
| 87 if (mActiveSensors.isEmpty() && mSensorsThread != null) { |
| 88 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { |
| 89 mSensorsThread.quitSafely(); |
| 90 } else { |
| 91 mSensorsThread.quit(); |
| 92 } |
| 93 |
| 94 mSensorsThread = null; |
| 95 mHandler = null; |
| 96 } |
| 97 } |
| 98 |
| 99 private PlatformSensorProvider(Context context) { |
| 100 mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR
_SERVICE); |
| 101 } |
| 102 |
| 103 /** |
| 104 * Creates PlatformSensorProvider instance. |
| 105 * |
| 106 * @param context application context. |
| 107 * @return PlatformSensorProvider new PlatformSensorProvider instance. |
| 108 */ |
| 109 @CalledByNative |
| 110 protected static PlatformSensorProvider create(Context context) { |
| 111 return new PlatformSensorProvider(context); |
| 112 } |
| 113 |
| 114 /** |
| 115 * Creates PlatformSensor instance. |
| 116 * |
| 117 * @param type type of a sensor. |
| 118 * @return PlatformSensor new PlatformSensor instance or null if sensor cann
ot be created. |
| 119 */ |
| 120 @CalledByNative |
| 121 protected PlatformSensor createSensor(int type) { |
| 122 if (mSensorManager == null) return null; |
| 123 |
| 124 switch (type) { |
| 125 case SensorType.AMBIENT_LIGHT: |
| 126 return PlatformSensor.create(Sensor.TYPE_LIGHT, 1, this); |
| 127 case SensorType.ACCELEROMETER: |
| 128 return PlatformSensor.create(Sensor.TYPE_ACCELEROMETER, 3, this)
; |
| 129 case SensorType.LINEAR_ACCELERATION: |
| 130 return PlatformSensor.create(Sensor.TYPE_LINEAR_ACCELERATION, 3,
this); |
| 131 case SensorType.GYROSCOPE: |
| 132 return PlatformSensor.create(Sensor.TYPE_GYROSCOPE, 3, this); |
| 133 case SensorType.MAGNETOMETER: |
| 134 return PlatformSensor.create(Sensor.TYPE_MAGNETIC_FIELD, 3, this
); |
| 135 default: |
| 136 return null; |
| 137 } |
| 138 } |
| 139 } |
OLD | NEW |