Chromium Code Reviews| Index: content/public/android/java/src/org/chromium/content/browser/DeviceSensors.java |
| diff --git a/content/public/android/java/src/org/chromium/content/browser/DeviceSensors.java b/content/public/android/java/src/org/chromium/content/browser/DeviceSensors.java |
| index bdb7741c6f205c8412ef30415a6d27d1dfd7ade2..085a3aa1febcfb888b330a97bb245aecc4b78859 100644 |
| --- a/content/public/android/java/src/org/chromium/content/browser/DeviceSensors.java |
| +++ b/content/public/android/java/src/org/chromium/content/browser/DeviceSensors.java |
| @@ -26,12 +26,12 @@ import java.util.Set; |
| import java.util.concurrent.Callable; |
| /** |
| - * Android implementation of the device motion and orientation APIs. |
| + * Android implementation of the device {motion|orientation|light} APIs. |
| */ |
| @JNINamespace("content") |
| class DeviceSensors implements SensorEventListener { |
| - private static final String TAG = "DeviceMotionAndOrientation"; |
| + private static final String TAG = "DeviceSensors"; |
| // These fields are lazily initialized by getHandler(). |
| private Thread mThread; |
| @@ -66,6 +66,7 @@ class DeviceSensors implements SensorEventListener { |
| */ |
| static final int DEVICE_ORIENTATION = 0; |
| static final int DEVICE_MOTION = 1; |
| + static final int DEVICE_LIGHT = 2; |
| static final Set<Integer> DEVICE_ORIENTATION_SENSORS = CollectionUtil.newHashSet( |
| Sensor.TYPE_ROTATION_VECTOR); |
| @@ -74,9 +75,12 @@ class DeviceSensors implements SensorEventListener { |
| Sensor.TYPE_ACCELEROMETER, |
| Sensor.TYPE_LINEAR_ACCELERATION, |
| Sensor.TYPE_GYROSCOPE); |
| + static final Set<Integer> DEVICE_LIGHT_SENSORS = CollectionUtil.newHashSet( |
| + Sensor.TYPE_LIGHT); |
| @VisibleForTesting |
| final Set<Integer> mActiveSensors = new HashSet<Integer>(); |
| + boolean mDeviceLightIsActive = false; |
| boolean mDeviceMotionIsActive = false; |
| boolean mDeviceOrientationIsActive = false; |
| @@ -92,7 +96,7 @@ class DeviceSensors implements SensorEventListener { |
| * @param rateInMilliseconds Requested callback rate in milliseconds. The |
| * actual rate may be higher. Unwanted events should be ignored. |
| * @param eventType Type of event to listen to, can be either DEVICE_ORIENTATION or |
| - * DEVICE_MOTION. |
| + * DEVICE_MOTION or DEVICE_LIGHT. |
| * @return True on success. |
| */ |
| @CalledByNative |
| @@ -108,6 +112,9 @@ class DeviceSensors implements SensorEventListener { |
| // note: device motion spec does not require all sensors to be available |
| success = registerSensors(DEVICE_MOTION_SENSORS, rateInMilliseconds, false); |
| break; |
| + case DEVICE_LIGHT: |
| + success = registerSensors(DEVICE_LIGHT_SENSORS, rateInMilliseconds, false); |
|
timvolodine
2014/09/04 17:16:58
(..,.., true)?
riju_
2014/09/08 09:26:18
Done.
|
| + break; |
| default: |
| Log.e(TAG, "Unknown event type: " + eventType); |
| return false; |
| @@ -132,7 +139,7 @@ class DeviceSensors implements SensorEventListener { |
| * if they are still in use by a different event type. |
| * |
| * @param eventType Type of event to listen to, can be either DEVICE_ORIENTATION or |
| - * DEVICE_MOTION. |
| + * DEVICE_MOTION or DEVICE_LIGHT. |
| * We strictly guarantee that the corresponding native*() methods will not be called |
| * after this method returns. |
| */ |
| @@ -145,11 +152,25 @@ class DeviceSensors implements SensorEventListener { |
| if (mDeviceMotionIsActive) { |
| sensorsToRemainActive.addAll(DEVICE_MOTION_SENSORS); |
| } |
| + if (mDeviceLightIsActive) { |
| + sensorsToRemainActive.addAll(DEVICE_LIGHT_SENSORS); |
| + } |
| break; |
| case DEVICE_MOTION: |
| if (mDeviceOrientationIsActive) { |
| sensorsToRemainActive.addAll(DEVICE_ORIENTATION_SENSORS); |
| } |
| + if (mDeviceLightIsActive) { |
| + sensorsToRemainActive.addAll(DEVICE_LIGHT_SENSORS); |
| + } |
| + break; |
| + case DEVICE_LIGHT: |
| + if (mDeviceMotionIsActive) { |
| + sensorsToRemainActive.addAll(DEVICE_MOTION_SENSORS); |
| + } |
| + if (mDeviceOrientationIsActive) { |
| + sensorsToRemainActive.addAll(DEVICE_ORIENTATION_SENSORS); |
| + } |
| break; |
| default: |
| Log.e(TAG, "Unknown event type: " + eventType); |
| @@ -211,6 +232,11 @@ class DeviceSensors implements SensorEventListener { |
| } |
| } |
| break; |
| + case Sensor.TYPE_LIGHT: |
| + if (mDeviceLightIsActive) { |
| + gotLight(values[0]); |
| + } |
| + break; |
| default: |
| // Unexpected |
| return; |
| @@ -344,6 +370,9 @@ class DeviceSensors implements SensorEventListener { |
| case DEVICE_MOTION: |
| mDeviceMotionIsActive = value; |
| return; |
| + case DEVICE_LIGHT: |
| + mDeviceLightIsActive = value; |
| + return; |
| } |
| } |
| @@ -425,6 +454,14 @@ class DeviceSensors implements SensorEventListener { |
| } |
| } |
| + protected void gotLight(double value) { |
| + synchronized (mNativePtrLock) { |
| + if (mNativePtr != 0) { |
| + nativeGotLight(mNativePtr, value); |
| + } |
| + } |
| + } |
| + |
| private Handler getHandler() { |
| // TODO(timvolodine): Remove the mHandlerLock when sure that getHandler is not called |
| // from multiple threads. This will be the case when device motion and device orientation |
| @@ -483,6 +520,13 @@ class DeviceSensors implements SensorEventListener { |
| double alpha, double beta, double gamma); |
| /** |
| + * Device Light value from Ambient Light sensors. |
| + */ |
| + private native void nativeGotLight( |
| + long nativeSensorManagerAndroid, |
| + double value); |
| + |
| + /** |
| * Need the an interface for SensorManager for testing. |
| */ |
| interface SensorManagerProxy { |