OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/device_sensors/sensor_manager_android.h" | 5 #include "content/browser/device_sensors/sensor_manager_android.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "content/browser/device_sensors/inertial_sensor_consts.h" | 12 #include "content/browser/device_sensors/inertial_sensor_consts.h" |
13 #include "jni/DeviceSensors_jni.h" | 13 #include "jni/DeviceSensors_jni.h" |
14 | 14 |
15 using base::android::AttachCurrentThread; | 15 using base::android::AttachCurrentThread; |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 static void updateRotationVectorHistogram(bool value) { | 19 static void updateRotationVectorHistogram(bool value) { |
20 UMA_HISTOGRAM_BOOLEAN("InertialSensor.RotationVectorAndroidAvailable", value); | 20 UMA_HISTOGRAM_BOOLEAN("InertialSensor.RotationVectorAndroidAvailable", value); |
21 } | 21 } |
22 | 22 |
23 } | 23 } |
24 | 24 |
25 namespace content { | 25 namespace content { |
26 | 26 |
27 SensorManagerAndroid::SensorManagerAndroid() | 27 SensorManagerAndroid::SensorManagerAndroid() |
28 : number_active_device_motion_sensors_(0), | 28 : number_active_device_motion_sensors_(0), |
29 device_light_buffer_(NULL), | |
29 device_motion_buffer_(NULL), | 30 device_motion_buffer_(NULL), |
30 device_orientation_buffer_(NULL), | 31 device_orientation_buffer_(NULL), |
32 is_light_buffer_ready_(false), | |
31 is_motion_buffer_ready_(false), | 33 is_motion_buffer_ready_(false), |
32 is_orientation_buffer_ready_(false) { | 34 is_orientation_buffer_ready_(false) { |
33 memset(received_motion_data_, 0, sizeof(received_motion_data_)); | 35 memset(received_motion_data_, 0, sizeof(received_motion_data_)); |
34 device_orientation_.Reset( | 36 device_sensors_.Reset(Java_DeviceSensors_getInstance( |
35 Java_DeviceSensors_getInstance( | 37 AttachCurrentThread(), base::android::GetApplicationContext())); |
36 AttachCurrentThread(), | |
37 base::android::GetApplicationContext())); | |
38 } | 38 } |
39 | 39 |
40 SensorManagerAndroid::~SensorManagerAndroid() { | 40 SensorManagerAndroid::~SensorManagerAndroid() { |
41 } | 41 } |
42 | 42 |
43 bool SensorManagerAndroid::Register(JNIEnv* env) { | 43 bool SensorManagerAndroid::Register(JNIEnv* env) { |
44 return RegisterNativesImpl(env); | 44 return RegisterNativesImpl(env); |
45 } | 45 } |
46 | 46 |
47 SensorManagerAndroid* SensorManagerAndroid::GetInstance() { | 47 SensorManagerAndroid* SensorManagerAndroid::GetInstance() { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
130 device_motion_buffer_->data.rotationRateGamma = gamma; | 130 device_motion_buffer_->data.rotationRateGamma = gamma; |
131 device_motion_buffer_->data.hasRotationRateGamma = true; | 131 device_motion_buffer_->data.hasRotationRateGamma = true; |
132 device_motion_buffer_->seqlock.WriteEnd(); | 132 device_motion_buffer_->seqlock.WriteEnd(); |
133 | 133 |
134 if (!is_motion_buffer_ready_) { | 134 if (!is_motion_buffer_ready_) { |
135 received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] = 1; | 135 received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] = 1; |
136 CheckMotionBufferReadyToRead(); | 136 CheckMotionBufferReadyToRead(); |
137 } | 137 } |
138 } | 138 } |
139 | 139 |
140 void SensorManagerAndroid::GotLight(JNIEnv*, jobject, double value) { | |
141 base::AutoLock autolock(light_buffer_lock_); | |
142 | |
143 if (!device_light_buffer_) | |
144 return; | |
145 | |
146 device_light_buffer_->seqlock.WriteBegin(); | |
147 device_light_buffer_->data.value = value; | |
148 device_light_buffer_->seqlock.WriteEnd(); | |
149 | |
150 if (!is_light_buffer_ready_) { | |
151 SetLightBufferReadyStatus(true); | |
152 } | |
153 } | |
154 | |
140 bool SensorManagerAndroid::Start(EventType event_type) { | 155 bool SensorManagerAndroid::Start(EventType event_type) { |
141 DCHECK(!device_orientation_.is_null()); | 156 DCHECK(!device_sensors_.is_null()); |
142 return Java_DeviceSensors_start( | 157 return Java_DeviceSensors_start(AttachCurrentThread(), |
143 AttachCurrentThread(), device_orientation_.obj(), | 158 device_sensors_.obj(), |
144 reinterpret_cast<intptr_t>(this), static_cast<jint>(event_type), | 159 reinterpret_cast<intptr_t>(this), |
145 kInertialSensorIntervalMillis); | 160 static_cast<jint>(event_type), |
161 kInertialSensorIntervalMillis); | |
timvolodine
2014/07/16 15:39:38
do we need same interval for device light as for t
riju_
2014/07/18 15:59:17
Done. Now I have added kInertialLightSensorInterva
| |
146 } | 162 } |
147 | 163 |
148 void SensorManagerAndroid::Stop(EventType event_type) { | 164 void SensorManagerAndroid::Stop(EventType event_type) { |
149 DCHECK(!device_orientation_.is_null()); | 165 DCHECK(!device_sensors_.is_null()); |
150 Java_DeviceSensors_stop( | 166 Java_DeviceSensors_stop(AttachCurrentThread(), |
151 AttachCurrentThread(), device_orientation_.obj(), | 167 device_sensors_.obj(), |
152 static_cast<jint>(event_type)); | 168 static_cast<jint>(event_type)); |
153 } | 169 } |
154 | 170 |
155 int SensorManagerAndroid::GetNumberActiveDeviceMotionSensors() { | 171 int SensorManagerAndroid::GetNumberActiveDeviceMotionSensors() { |
156 DCHECK(!device_orientation_.is_null()); | 172 DCHECK(!device_sensors_.is_null()); |
157 return Java_DeviceSensors_getNumberActiveDeviceMotionSensors( | 173 return Java_DeviceSensors_getNumberActiveDeviceMotionSensors( |
158 AttachCurrentThread(), device_orientation_.obj()); | 174 AttachCurrentThread(), device_sensors_.obj()); |
159 } | 175 } |
160 | 176 |
161 | 177 |
162 // ----- Shared memory API methods | 178 // ----- Shared memory API methods |
163 | 179 |
180 // --- Device Light | |
181 | |
182 bool SensorManagerAndroid::StartFetchingDeviceLightData( | |
183 DeviceLightHardwareBuffer* buffer) { | |
184 DCHECK(buffer); | |
185 { | |
186 base::AutoLock autolock(light_buffer_lock_); | |
187 device_light_buffer_ = buffer; | |
188 ClearInternalLightBuffers(); | |
189 } | |
190 bool success = Start(kTypeLight); | |
191 | |
192 { | |
193 base::AutoLock autolock(light_buffer_lock_); | |
194 SetLightBufferReadyStatus(!success); | |
195 } | |
196 | |
197 return success; | |
198 } | |
199 | |
200 void SensorManagerAndroid::StopFetchingDeviceLightData() { | |
201 Stop(kTypeLight); | |
202 { | |
203 base::AutoLock autolock(light_buffer_lock_); | |
204 if (device_light_buffer_) { | |
205 ClearInternalLightBuffers(); | |
206 device_light_buffer_ = NULL; | |
207 } | |
208 } | |
209 } | |
210 | |
211 void SensorManagerAndroid::SetLightBufferReadyStatus(bool ready) { | |
212 is_light_buffer_ready_ = ready; | |
timvolodine
2014/07/16 15:39:38
why do you need this? where is it used?
riju_
2014/07/18 15:59:17
Done.
| |
213 } | |
214 | |
215 void SensorManagerAndroid::ClearInternalLightBuffers() { | |
216 SetLightBufferReadyStatus(false); | |
timvolodine
2014/07/16 15:39:38
should the lux value be set to -1 here?
riju_
2014/07/18 15:59:17
Done.
| |
217 } | |
164 // --- Device Motion | 218 // --- Device Motion |
165 | 219 |
166 bool SensorManagerAndroid::StartFetchingDeviceMotionData( | 220 bool SensorManagerAndroid::StartFetchingDeviceMotionData( |
167 DeviceMotionHardwareBuffer* buffer) { | 221 DeviceMotionHardwareBuffer* buffer) { |
168 DCHECK(buffer); | 222 DCHECK(buffer); |
169 { | 223 { |
170 base::AutoLock autolock(motion_buffer_lock_); | 224 base::AutoLock autolock(motion_buffer_lock_); |
171 device_motion_buffer_ = buffer; | 225 device_motion_buffer_ = buffer; |
172 ClearInternalMotionBuffers(); | 226 ClearInternalMotionBuffers(); |
173 } | 227 } |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 { | 321 { |
268 base::AutoLock autolock(orientation_buffer_lock_); | 322 base::AutoLock autolock(orientation_buffer_lock_); |
269 if (device_orientation_buffer_) { | 323 if (device_orientation_buffer_) { |
270 SetOrientationBufferReadyStatus(false); | 324 SetOrientationBufferReadyStatus(false); |
271 device_orientation_buffer_ = NULL; | 325 device_orientation_buffer_ = NULL; |
272 } | 326 } |
273 } | 327 } |
274 } | 328 } |
275 | 329 |
276 } // namespace content | 330 } // namespace content |
OLD | NEW |