Chromium Code Reviews| Index: device/generic_sensor/android/java/src/org/chromium/device/sensors/PlatformSensorProvider.java |
| diff --git a/device/generic_sensor/android/java/src/org/chromium/device/sensors/PlatformSensorProvider.java b/device/generic_sensor/android/java/src/org/chromium/device/sensors/PlatformSensorProvider.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ec63f88d277a90b693ebc03711a309908fbd3b56 |
| --- /dev/null |
| +++ b/device/generic_sensor/android/java/src/org/chromium/device/sensors/PlatformSensorProvider.java |
| @@ -0,0 +1,73 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.device.sensors; |
| + |
| +import android.content.Context; |
| +import android.hardware.Sensor; |
| +import android.hardware.SensorManager; |
| +import android.os.Handler; |
| +import android.os.HandlerThread; |
| + |
| +import org.chromium.base.annotations.CalledByNative; |
| +import org.chromium.base.annotations.JNINamespace; |
| + |
| +import org.chromium.mojom.device.mojom.SensorType; |
| + |
| +/** |
| + * Lifetime is controlled by device::PlatformSensorProviderAndroid. |
| + */ |
| +@JNINamespace("device") |
| +final class PlatformSensorProvider { |
| + private final SensorManager mSensorManager; |
| + private final HandlerThread mSensorsThread; |
| + private final Handler mHandler; |
| + |
| + private PlatformSensorProvider(Context context) { |
| + mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); |
| + |
| + if (mSensorManager != null) { |
| + mSensorsThread = new HandlerThread("SensorsHandlerThread"); |
|
Ted C
2016/09/01 00:12:36
what thread are sensors handled on for other platf
shalamov
2016/09/06 12:36:43
Added code that starts / stops thread when there a
|
| + mSensorsThread.start(); |
| + mHandler = new Handler(mSensorsThread.getLooper()); |
| + } else { |
| + mSensorsThread = null; |
| + mHandler = null; |
| + } |
| + } |
| + |
| + @CalledByNative |
| + private static PlatformSensorProvider create(Context context) { |
| + return new PlatformSensorProvider(context); |
| + } |
| + |
| + @CalledByNative |
| + private PlatformSensor createSensor(int type) { |
| + if (mSensorManager == null || mHandler == null) return null; |
| + |
| + try { |
| + switch (type) { |
| + case SensorType.AMBIENT_LIGHT: |
| + return new GenericPlatformSensor( |
| + Sensor.TYPE_LIGHT, 1, mSensorManager, mHandler); |
| + case SensorType.ACCELEROMETER: |
| + return new GenericPlatformSensor( |
| + Sensor.TYPE_ACCELEROMETER, 3, mSensorManager, mHandler); |
| + case SensorType.LINEAR_ACCELERATION: |
| + return new GenericPlatformSensor( |
| + Sensor.TYPE_LINEAR_ACCELERATION, 3, mSensorManager, mHandler); |
| + case SensorType.GYROSCOPE: |
| + return new GenericPlatformSensor( |
| + Sensor.TYPE_GYROSCOPE, 3, mSensorManager, mHandler); |
| + case SensorType.MAGNETOMETER: |
| + return new GenericPlatformSensor( |
| + Sensor.TYPE_MAGNETIC_FIELD, 3, mSensorManager, mHandler); |
| + } |
| + } catch (PlatformSensorException e) { |
| + return null; |
| + } |
| + |
| + return null; |
| + } |
| +} |