Chromium Code Reviews| 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.Handler; | |
| 11 import android.os.HandlerThread; | |
| 12 | |
| 13 import org.chromium.base.annotations.CalledByNative; | |
| 14 import org.chromium.base.annotations.JNINamespace; | |
| 15 | |
| 16 import org.chromium.mojom.device.mojom.SensorType; | |
| 17 | |
| 18 /** | |
| 19 * Lifetime is controlled by device::PlatformSensorProviderAndroid. | |
| 20 */ | |
| 21 @JNINamespace("device") | |
| 22 final class PlatformSensorProvider { | |
| 23 private final SensorManager mSensorManager; | |
| 24 private final HandlerThread mSensorsThread; | |
| 25 private final Handler mHandler; | |
| 26 | |
| 27 private PlatformSensorProvider(Context context) { | |
| 28 mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR _SERVICE); | |
| 29 | |
| 30 if (mSensorManager != null) { | |
| 31 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
| |
| 32 mSensorsThread.start(); | |
| 33 mHandler = new Handler(mSensorsThread.getLooper()); | |
| 34 } else { | |
| 35 mSensorsThread = null; | |
| 36 mHandler = null; | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 @CalledByNative | |
| 41 private static PlatformSensorProvider create(Context context) { | |
| 42 return new PlatformSensorProvider(context); | |
| 43 } | |
| 44 | |
| 45 @CalledByNative | |
| 46 private PlatformSensor createSensor(int type) { | |
| 47 if (mSensorManager == null || mHandler == null) return null; | |
| 48 | |
| 49 try { | |
| 50 switch (type) { | |
| 51 case SensorType.AMBIENT_LIGHT: | |
| 52 return new GenericPlatformSensor( | |
| 53 Sensor.TYPE_LIGHT, 1, mSensorManager, mHandler); | |
| 54 case SensorType.ACCELEROMETER: | |
| 55 return new GenericPlatformSensor( | |
| 56 Sensor.TYPE_ACCELEROMETER, 3, mSensorManager, mHandl er); | |
| 57 case SensorType.LINEAR_ACCELERATION: | |
| 58 return new GenericPlatformSensor( | |
| 59 Sensor.TYPE_LINEAR_ACCELERATION, 3, mSensorManager, mHandler); | |
| 60 case SensorType.GYROSCOPE: | |
| 61 return new GenericPlatformSensor( | |
| 62 Sensor.TYPE_GYROSCOPE, 3, mSensorManager, mHandler); | |
| 63 case SensorType.MAGNETOMETER: | |
| 64 return new GenericPlatformSensor( | |
| 65 Sensor.TYPE_MAGNETIC_FIELD, 3, mSensorManager, mHand ler); | |
| 66 } | |
| 67 } catch (PlatformSensorException e) { | |
| 68 return null; | |
| 69 } | |
| 70 | |
| 71 return null; | |
| 72 } | |
| 73 } | |
| OLD | NEW |