Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2179)

Unified Diff: device/sensor/android/java/src/org/chromium/device/sensor/AmbientLightSensorImpl.java

Issue 1892083002: Generic Sensor API : Ambient Light Sensor API. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: device/sensor/android/java/src/org/chromium/device/sensor/AmbientLightSensorImpl.java
diff --git a/device/sensor/android/java/src/org/chromium/device/sensor/AmbientLightSensorImpl.java b/device/sensor/android/java/src/org/chromium/device/sensor/AmbientLightSensorImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..ee1f2f954612fdbf5911d01f1aba0e54496261bb
--- /dev/null
+++ b/device/sensor/android/java/src/org/chromium/device/sensor/AmbientLightSensorImpl.java
@@ -0,0 +1,100 @@
+// 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.sensor;
+
+import android.content.Context;
+import android.hardware.Sensor;
+import android.hardware.SensorEvent;
+import android.hardware.SensorEventListener;
+import android.hardware.SensorManager;
+
+import org.chromium.base.Log;
+import org.chromium.mojo.system.MojoException;
+import org.chromium.mojom.device.AmbientLightSensor;
+import org.chromium.mojom.device.AmbientLightSensorReading;
+
+import java.util.Date;
+
+/**
+ * Android implementation of Ambient Light Sensor service as defined in
+ * device/sensor/sensor.mojom.
+ */
+public class AmbientLightSensorImpl implements AmbientLightSensor, SensorEventListener {
+ private static final String TAG = "AmbientLightSensor";
+ private QueryNextReadingResponse mResponse;
+ // A reference to the application context in order to acquire the SensorService.
+ private final Context mAppContext;
+ private final SensorManager mSensorManager;
+ private final Sensor mAmbientLightSensor;
+ private boolean mHasNewValue;
+ private AmbientLightSensorReading mLastUpdate;
+
+ public AmbientLightSensorImpl(Context context) {
+ mAppContext = context.getApplicationContext();
+
+ mSensorManager = (SensorManager) mAppContext.getSystemService(Context.SENSOR_SERVICE);
+ mAmbientLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
+ if (mSensorManager == null) {
+ Log.w(TAG, "Sensor service not supported");
+ } else {
+ mHasNewValue = false;
+ mLastUpdate = new AmbientLightSensorReading();
+ }
+ }
+
+ @Override
+ public void queryNextReading(QueryNextReadingResponse callback) {
+ if (mResponse != null) {
+ Log.e(TAG, "Overlapped call to queryNextReading!");
+ return;
+ }
+
+ mResponse = callback;
+ if (mHasNewValue) reportReading();
+ }
+
+ @Override
+ public void startReading() {
+ mLastUpdate.timeStamp = new Date().getTime();
+ mLastUpdate.illuminance = Double.POSITIVE_INFINITY;
+ mSensorManager.registerListener(
+ this, mAmbientLightSensor, SensorManager.SENSOR_DELAY_NORMAL);
+ }
+
+ @Override
+ public void stopReading() {
+ mHasNewValue = false;
+ mLastUpdate.illuminance = Double.POSITIVE_INFINITY;
+ mSensorManager.unregisterListener(this);
+ }
+
+ void reportReading() {
+ mResponse.call(mLastUpdate);
+ mResponse = null;
+ mHasNewValue = false;
+ }
+
+ @Override
+ public void close() {
+ mSensorManager.unregisterListener(this);
+ }
+
+ @Override
+ public void onConnectionError(MojoException e) {}
+
+ @Override
+ public void onAccuracyChanged(Sensor sensor, int accuracy) {}
+
+ @Override
+ public void onSensorChanged(SensorEvent event) {
riju_ 2016/05/16 16:58:51 TODO(riju) : Use event.timestamp here.
+ if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
+ mLastUpdate.illuminance = event.values[0];
+ mHasNewValue = true;
+
+ reportReading();
+ if (mResponse != null) reportReading();
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698