| 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..bb218ce0e732a4d9753e3d59a544fd74f54920c9
|
| --- /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");
|
| + 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 (SensorException e) {
|
| + return null;
|
| + }
|
| +
|
| + return null;
|
| + }
|
| +}
|
|
|