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.sensor; | |
6 | |
7 import android.content.Context; | |
8 import android.hardware.Sensor; | |
9 import android.hardware.SensorEvent; | |
10 import android.hardware.SensorEventListener; | |
11 import android.hardware.SensorManager; | |
12 | |
13 import org.chromium.base.Log; | |
14 import org.chromium.mojo.system.MojoException; | |
15 import org.chromium.mojom.device.AmbientLightSensor; | |
16 import org.chromium.mojom.device.AmbientLightSensorReading; | |
17 | |
18 import java.util.Date; | |
19 | |
20 /** | |
21 * Android implementation of Ambient Light Sensor service as defined in | |
22 * device/sensor/sensor.mojom. | |
23 */ | |
24 public class AmbientLightSensorImpl implements AmbientLightSensor, SensorEventLi stener { | |
25 private static final String TAG = "AmbientLightSensor"; | |
26 private QueryNextReadingResponse mResponse; | |
27 // A reference to the application context in order to acquire the SensorServ ice. | |
28 private final Context mAppContext; | |
29 private final SensorManager mSensorManager; | |
30 private final Sensor mAmbientLightSensor; | |
31 private boolean mHasNewValue; | |
32 private AmbientLightSensorReading mLastUpdate; | |
33 | |
34 public AmbientLightSensorImpl(Context context) { | |
35 mAppContext = context.getApplicationContext(); | |
36 | |
37 mSensorManager = (SensorManager) mAppContext.getSystemService(Context.SE NSOR_SERVICE); | |
38 mAmbientLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT) ; | |
39 if (mSensorManager == null) { | |
40 Log.w(TAG, "Sensor service not supported"); | |
41 } else { | |
42 mHasNewValue = false; | |
43 mLastUpdate = new AmbientLightSensorReading(); | |
44 } | |
45 } | |
46 | |
47 @Override | |
48 public void queryNextReading(QueryNextReadingResponse callback) { | |
49 if (mResponse != null) { | |
50 Log.e(TAG, "Overlapped call to queryNextReading!"); | |
51 return; | |
52 } | |
53 | |
54 mResponse = callback; | |
55 if (mHasNewValue) reportReading(); | |
56 } | |
57 | |
58 @Override | |
59 public void startReading() { | |
60 mLastUpdate.timeStamp = new Date().getTime(); | |
61 mLastUpdate.illuminance = Double.POSITIVE_INFINITY; | |
62 mSensorManager.registerListener( | |
63 this, mAmbientLightSensor, SensorManager.SENSOR_DELAY_NORMAL); | |
64 } | |
65 | |
66 @Override | |
67 public void stopReading() { | |
68 mHasNewValue = false; | |
69 mLastUpdate.illuminance = Double.POSITIVE_INFINITY; | |
70 mSensorManager.unregisterListener(this); | |
71 } | |
72 | |
73 void reportReading() { | |
74 mResponse.call(mLastUpdate); | |
75 mResponse = null; | |
76 mHasNewValue = false; | |
77 } | |
78 | |
79 @Override | |
80 public void close() { | |
81 mSensorManager.unregisterListener(this); | |
82 } | |
83 | |
84 @Override | |
85 public void onConnectionError(MojoException e) {} | |
86 | |
87 @Override | |
88 public void onAccuracyChanged(Sensor sensor, int accuracy) {} | |
89 | |
90 @Override | |
91 public void onSensorChanged(SensorEvent event) { | |
riju_
2016/05/16 16:58:51
TODO(riju) : Use event.timestamp here.
| |
92 if (event.sensor.getType() == Sensor.TYPE_LIGHT) { | |
93 mLastUpdate.illuminance = event.values[0]; | |
94 mHasNewValue = true; | |
95 | |
96 reportReading(); | |
97 if (mResponse != null) reportReading(); | |
98 } | |
99 } | |
100 } | |
OLD | NEW |