| 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 "device/sensors/sensor_manager_android.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/android/context_utils.h" | 9 #include "base/android/context_utils.h" |
| 10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "jni/DeviceSensors_jni.h" | 15 #include "jni/DeviceSensors_jni.h" |
| 16 | 16 |
| 17 using base::android::AttachCurrentThread; | 17 using base::android::AttachCurrentThread; |
| 18 using base::android::JavaParamRef; | 18 using base::android::JavaParamRef; |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 void UpdateDeviceOrientationHistogram( | 22 void UpdateDeviceOrientationHistogram( |
| 23 content::SensorManagerAndroid::OrientationSensorType type) { | 23 device::SensorManagerAndroid::OrientationSensorType type) { |
| 24 UMA_HISTOGRAM_ENUMERATION("InertialSensor.DeviceOrientationSensorAndroid", | 24 UMA_HISTOGRAM_ENUMERATION( |
| 25 type, content::SensorManagerAndroid::ORIENTATION_SENSOR_MAX); | 25 "InertialSensor.DeviceOrientationSensorAndroid", type, |
| 26 device::SensorManagerAndroid::ORIENTATION_SENSOR_MAX); |
| 26 } | 27 } |
| 27 | 28 |
| 28 void SetOrientation(content::DeviceOrientationHardwareBuffer* buffer, | 29 void SetOrientation(device::DeviceOrientationHardwareBuffer* buffer, |
| 29 double alpha, double beta, double gamma) { | 30 double alpha, |
| 31 double beta, |
| 32 double gamma) { |
| 30 buffer->seqlock.WriteBegin(); | 33 buffer->seqlock.WriteBegin(); |
| 31 buffer->data.alpha = alpha; | 34 buffer->data.alpha = alpha; |
| 32 buffer->data.hasAlpha = true; | 35 buffer->data.hasAlpha = true; |
| 33 buffer->data.beta = beta; | 36 buffer->data.beta = beta; |
| 34 buffer->data.hasBeta = true; | 37 buffer->data.hasBeta = true; |
| 35 buffer->data.gamma = gamma; | 38 buffer->data.gamma = gamma; |
| 36 buffer->data.hasGamma = true; | 39 buffer->data.hasGamma = true; |
| 37 buffer->seqlock.WriteEnd(); | 40 buffer->seqlock.WriteEnd(); |
| 38 } | 41 } |
| 39 | 42 |
| 40 void SetOrientationBufferStatus( | 43 void SetOrientationBufferStatus(device::DeviceOrientationHardwareBuffer* buffer, |
| 41 content::DeviceOrientationHardwareBuffer* buffer, | 44 bool ready, |
| 42 bool ready, bool absolute) { | 45 bool absolute) { |
| 43 buffer->seqlock.WriteBegin(); | 46 buffer->seqlock.WriteBegin(); |
| 44 buffer->data.absolute = absolute; | 47 buffer->data.absolute = absolute; |
| 45 buffer->data.allAvailableSensorsAreActive = ready; | 48 buffer->data.allAvailableSensorsAreActive = ready; |
| 46 buffer->seqlock.WriteEnd(); | 49 buffer->seqlock.WriteEnd(); |
| 47 } | 50 } |
| 48 | 51 |
| 49 } // namespace | 52 } // namespace |
| 50 | 53 |
| 51 namespace content { | 54 namespace device { |
| 52 | 55 |
| 53 SensorManagerAndroid::SensorManagerAndroid() | 56 SensorManagerAndroid::SensorManagerAndroid() |
| 54 : number_active_device_motion_sensors_(0), | 57 : number_active_device_motion_sensors_(0), |
| 55 device_light_buffer_(nullptr), | 58 device_light_buffer_(nullptr), |
| 56 device_motion_buffer_(nullptr), | 59 device_motion_buffer_(nullptr), |
| 57 device_orientation_buffer_(nullptr), | 60 device_orientation_buffer_(nullptr), |
| 58 motion_buffer_initialized_(false), | 61 motion_buffer_initialized_(false), |
| 59 orientation_buffer_initialized_(false), | 62 orientation_buffer_initialized_(false), |
| 60 is_shutdown_(false) { | 63 is_shutdown_(false) { |
| 61 DCHECK(thread_checker_.CalledOnValidThread()); | 64 DCHECK(thread_checker_.CalledOnValidThread()); |
| 62 memset(received_motion_data_, 0, sizeof(received_motion_data_)); | 65 memset(received_motion_data_, 0, sizeof(received_motion_data_)); |
| 63 device_sensors_.Reset(Java_DeviceSensors_getInstance( | 66 device_sensors_.Reset(Java_DeviceSensors_getInstance( |
| 64 AttachCurrentThread(), base::android::GetApplicationContext())); | 67 AttachCurrentThread(), base::android::GetApplicationContext())); |
| 65 } | 68 } |
| 66 | 69 |
| 67 SensorManagerAndroid::~SensorManagerAndroid() { | 70 SensorManagerAndroid::~SensorManagerAndroid() {} |
| 68 } | |
| 69 | 71 |
| 70 bool SensorManagerAndroid::Register(JNIEnv* env) { | 72 bool SensorManagerAndroid::Register(JNIEnv* env) { |
| 71 return RegisterNativesImpl(env); | 73 return RegisterNativesImpl(env); |
| 72 } | 74 } |
| 73 | 75 |
| 74 SensorManagerAndroid* SensorManagerAndroid::GetInstance() { | 76 SensorManagerAndroid* SensorManagerAndroid::GetInstance() { |
| 75 DCHECK(base::MessageLoopForUI::IsCurrent()); | 77 DCHECK(base::MessageLoopForUI::IsCurrent()); |
| 76 return base::Singleton< | 78 return base::Singleton< |
| 77 SensorManagerAndroid, | 79 SensorManagerAndroid, |
| 78 base::LeakySingletonTraits<SensorManagerAndroid>>::get(); | 80 base::LeakySingletonTraits<SensorManagerAndroid>>::get(); |
| 79 } | 81 } |
| 80 | 82 |
| 81 void SensorManagerAndroid::GotOrientation(JNIEnv*, | 83 void SensorManagerAndroid::GotOrientation(JNIEnv*, |
| 82 const JavaParamRef<jobject>&, | 84 const JavaParamRef<jobject>&, |
| 83 double alpha, | 85 double alpha, |
| 84 double beta, | 86 double beta, |
| 85 double gamma) { | 87 double gamma) { |
| 86 base::AutoLock autolock(orientation_buffer_lock_); | 88 base::AutoLock autolock(orientation_buffer_lock_); |
| 87 | 89 |
| 88 if (!device_orientation_buffer_) | 90 if (!device_orientation_buffer_) |
| 89 return; | 91 return; |
| 90 | 92 |
| 91 SetOrientation(device_orientation_buffer_, alpha, beta, gamma); | 93 SetOrientation(device_orientation_buffer_, alpha, beta, gamma); |
| 92 | 94 |
| 93 if (!orientation_buffer_initialized_) { | 95 if (!orientation_buffer_initialized_) { |
| 94 OrientationSensorType type = | 96 OrientationSensorType type = |
| 95 static_cast<OrientationSensorType>(GetOrientationSensorTypeUsed()); | 97 static_cast<OrientationSensorType>(GetOrientationSensorTypeUsed()); |
| 96 SetOrientationBufferStatus(device_orientation_buffer_, true, | 98 SetOrientationBufferStatus(device_orientation_buffer_, true, |
| 97 type != GAME_ROTATION_VECTOR); | 99 type != GAME_ROTATION_VECTOR); |
| 98 orientation_buffer_initialized_ = true; | 100 orientation_buffer_initialized_ = true; |
| 99 UpdateDeviceOrientationHistogram(type); | 101 UpdateDeviceOrientationHistogram(type); |
| 100 } | 102 } |
| 101 } | 103 } |
| 102 | 104 |
| 103 void SensorManagerAndroid::GotOrientationAbsolute(JNIEnv*, | 105 void SensorManagerAndroid::GotOrientationAbsolute(JNIEnv*, |
| 104 const JavaParamRef<jobject>&, | 106 const JavaParamRef<jobject>&, |
| 105 double alpha, | 107 double alpha, |
| 106 double beta, | 108 double beta, |
| 107 double gamma) { | 109 double gamma) { |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 base::AutoLock autolock(motion_buffer_lock_); | 324 base::AutoLock autolock(motion_buffer_lock_); |
| 323 if (device_motion_buffer_) { | 325 if (device_motion_buffer_) { |
| 324 ClearInternalMotionBuffers(); | 326 ClearInternalMotionBuffers(); |
| 325 device_motion_buffer_ = nullptr; | 327 device_motion_buffer_ = nullptr; |
| 326 } | 328 } |
| 327 } | 329 } |
| 328 } | 330 } |
| 329 | 331 |
| 330 void SensorManagerAndroid::CheckMotionBufferReadyToRead() { | 332 void SensorManagerAndroid::CheckMotionBufferReadyToRead() { |
| 331 if (received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION] + | 333 if (received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION] + |
| 332 received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY] + | 334 received_motion_data_ |
| 333 received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] == | 335 [RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY] + |
| 336 received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] == |
| 334 number_active_device_motion_sensors_) { | 337 number_active_device_motion_sensors_) { |
| 335 device_motion_buffer_->seqlock.WriteBegin(); | 338 device_motion_buffer_->seqlock.WriteBegin(); |
| 336 device_motion_buffer_->data.interval = | 339 device_motion_buffer_->data.interval = |
| 337 kDeviceSensorIntervalMicroseconds / 1000.; | 340 kDeviceSensorIntervalMicroseconds / 1000.; |
| 338 device_motion_buffer_->seqlock.WriteEnd(); | 341 device_motion_buffer_->seqlock.WriteEnd(); |
| 339 SetMotionBufferReadyStatus(true); | 342 SetMotionBufferReadyStatus(true); |
| 340 | 343 |
| 341 UMA_HISTOGRAM_BOOLEAN("InertialSensor.AccelerometerAndroidAvailable", | 344 UMA_HISTOGRAM_BOOLEAN( |
| 345 "InertialSensor.AccelerometerAndroidAvailable", |
| 342 received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION] > 0); | 346 received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION] > 0); |
| 343 UMA_HISTOGRAM_BOOLEAN( | 347 UMA_HISTOGRAM_BOOLEAN( |
| 344 "InertialSensor.AccelerometerIncGravityAndroidAvailable", | 348 "InertialSensor.AccelerometerIncGravityAndroidAvailable", |
| 345 received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY] | 349 received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY] > |
| 346 > 0); | 350 0); |
| 347 UMA_HISTOGRAM_BOOLEAN("InertialSensor.GyroscopeAndroidAvailable", | 351 UMA_HISTOGRAM_BOOLEAN( |
| 352 "InertialSensor.GyroscopeAndroidAvailable", |
| 348 received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] > 0); | 353 received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] > 0); |
| 349 } | 354 } |
| 350 } | 355 } |
| 351 | 356 |
| 352 void SensorManagerAndroid::SetMotionBufferReadyStatus(bool ready) { | 357 void SensorManagerAndroid::SetMotionBufferReadyStatus(bool ready) { |
| 353 device_motion_buffer_->seqlock.WriteBegin(); | 358 device_motion_buffer_->seqlock.WriteBegin(); |
| 354 device_motion_buffer_->data.allAvailableSensorsAreActive = ready; | 359 device_motion_buffer_->data.allAvailableSensorsAreActive = ready; |
| 355 device_motion_buffer_->seqlock.WriteEnd(); | 360 device_motion_buffer_->seqlock.WriteEnd(); |
| 356 motion_buffer_initialized_ = ready; | 361 motion_buffer_initialized_ = ready; |
| 357 } | 362 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 375 base::AutoLock autolock(orientation_buffer_lock_); | 380 base::AutoLock autolock(orientation_buffer_lock_); |
| 376 device_orientation_buffer_ = buffer; | 381 device_orientation_buffer_ = buffer; |
| 377 } | 382 } |
| 378 bool success = Start(CONSUMER_TYPE_ORIENTATION); | 383 bool success = Start(CONSUMER_TYPE_ORIENTATION); |
| 379 | 384 |
| 380 { | 385 { |
| 381 base::AutoLock autolock(orientation_buffer_lock_); | 386 base::AutoLock autolock(orientation_buffer_lock_); |
| 382 // If Start() was unsuccessful then set the buffer ready flag to true | 387 // If Start() was unsuccessful then set the buffer ready flag to true |
| 383 // to start firing all-null events. | 388 // to start firing all-null events. |
| 384 SetOrientationBufferStatus(buffer, !success /* ready */, | 389 SetOrientationBufferStatus(buffer, !success /* ready */, |
| 385 false /* absolute */); | 390 false /* absolute */); |
| 386 orientation_buffer_initialized_ = !success; | 391 orientation_buffer_initialized_ = !success; |
| 387 } | 392 } |
| 388 | 393 |
| 389 if (!success) | 394 if (!success) |
| 390 UpdateDeviceOrientationHistogram(NOT_AVAILABLE); | 395 UpdateDeviceOrientationHistogram(NOT_AVAILABLE); |
| 391 } | 396 } |
| 392 | 397 |
| 393 void SensorManagerAndroid::StopFetchingDeviceOrientationData() { | 398 void SensorManagerAndroid::StopFetchingDeviceOrientationData() { |
| 394 DCHECK(thread_checker_.CalledOnValidThread()); | 399 DCHECK(thread_checker_.CalledOnValidThread()); |
| 395 if (is_shutdown_) | 400 if (is_shutdown_) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 417 base::AutoLock autolock(orientation_absolute_buffer_lock_); | 422 base::AutoLock autolock(orientation_absolute_buffer_lock_); |
| 418 device_orientation_absolute_buffer_ = buffer; | 423 device_orientation_absolute_buffer_ = buffer; |
| 419 } | 424 } |
| 420 bool success = Start(CONSUMER_TYPE_ORIENTATION_ABSOLUTE); | 425 bool success = Start(CONSUMER_TYPE_ORIENTATION_ABSOLUTE); |
| 421 | 426 |
| 422 { | 427 { |
| 423 base::AutoLock autolock(orientation_absolute_buffer_lock_); | 428 base::AutoLock autolock(orientation_absolute_buffer_lock_); |
| 424 // If Start() was unsuccessful then set the buffer ready flag to true | 429 // If Start() was unsuccessful then set the buffer ready flag to true |
| 425 // to start firing all-null events. | 430 // to start firing all-null events. |
| 426 SetOrientationBufferStatus(buffer, !success /* ready */, | 431 SetOrientationBufferStatus(buffer, !success /* ready */, |
| 427 false /* absolute */); | 432 false /* absolute */); |
| 428 orientation_absolute_buffer_initialized_ = !success; | 433 orientation_absolute_buffer_initialized_ = !success; |
| 429 } | 434 } |
| 430 } | 435 } |
| 431 | 436 |
| 432 void SensorManagerAndroid::StopFetchingDeviceOrientationAbsoluteData() { | 437 void SensorManagerAndroid::StopFetchingDeviceOrientationAbsoluteData() { |
| 433 DCHECK(thread_checker_.CalledOnValidThread()); | 438 DCHECK(thread_checker_.CalledOnValidThread()); |
| 434 if (is_shutdown_) | 439 if (is_shutdown_) |
| 435 return; | 440 return; |
| 436 | 441 |
| 437 Stop(CONSUMER_TYPE_ORIENTATION_ABSOLUTE); | 442 Stop(CONSUMER_TYPE_ORIENTATION_ABSOLUTE); |
| 438 { | 443 { |
| 439 base::AutoLock autolock(orientation_absolute_buffer_lock_); | 444 base::AutoLock autolock(orientation_absolute_buffer_lock_); |
| 440 if (device_orientation_absolute_buffer_) { | 445 if (device_orientation_absolute_buffer_) { |
| 441 SetOrientationBufferStatus(device_orientation_absolute_buffer_, false, | 446 SetOrientationBufferStatus(device_orientation_absolute_buffer_, false, |
| 442 false); | 447 false); |
| 443 orientation_absolute_buffer_initialized_ = false; | 448 orientation_absolute_buffer_initialized_ = false; |
| 444 device_orientation_absolute_buffer_ = nullptr; | 449 device_orientation_absolute_buffer_ = nullptr; |
| 445 } | 450 } |
| 446 } | 451 } |
| 447 } | 452 } |
| 448 | 453 |
| 449 void SensorManagerAndroid::Shutdown() { | 454 void SensorManagerAndroid::Shutdown() { |
| 450 DCHECK(thread_checker_.CalledOnValidThread()); | 455 DCHECK(thread_checker_.CalledOnValidThread()); |
| 451 is_shutdown_ = true; | 456 is_shutdown_ = true; |
| 452 } | 457 } |
| 453 | 458 |
| 454 } // namespace content | 459 } // namespace device |
| OLD | NEW |