Index: content/browser/device_orientation/data_fetcher_impl_android.cc |
diff --git a/content/browser/device_orientation/data_fetcher_impl_android.cc b/content/browser/device_orientation/data_fetcher_impl_android.cc |
index 069454db1d7faafc243b2bf02287c1148eab600d..65d3dd58dc4c5e92c2b162e9134a506582bc4677 100644 |
--- a/content/browser/device_orientation/data_fetcher_impl_android.cc |
+++ b/content/browser/device_orientation/data_fetcher_impl_android.cc |
@@ -4,8 +4,10 @@ |
#include "content/browser/device_orientation/data_fetcher_impl_android.h" |
+#include <string.h> |
+ |
#include "base/android/jni_android.h" |
-#include "base/logging.h" |
+#include "base/memory/singleton.h" |
#include "content/browser/device_orientation/orientation.h" |
#include "jni/DeviceMotionAndOrientation_jni.h" |
@@ -21,31 +23,26 @@ const int kPeriodInMilliseconds = 100; |
} // namespace |
-DataFetcherImplAndroid::DataFetcherImplAndroid() { |
+DataFetcherImplAndroid::DataFetcherImplAndroid() |
+ : number_active_device_motion_sensors_(0), |
+ device_motion_buffer_(NULL), |
+ is_buffer_ready_(false) { |
+ memset(received_motion_data_, 0, sizeof(received_motion_data_)); |
device_orientation_.Reset( |
Java_DeviceMotionAndOrientation_getInstance(AttachCurrentThread())); |
} |
+DataFetcherImplAndroid::~DataFetcherImplAndroid() { |
+} |
+ |
void DataFetcherImplAndroid::Init(JNIEnv* env) { |
bool result = RegisterNativesImpl(env); |
DCHECK(result); |
} |
-// TODO(timvolodine): Modify this method to be able to distinguish |
-// device motion from orientation. |
-DataFetcher* DataFetcherImplAndroid::Create() { |
- scoped_ptr<DataFetcherImplAndroid> fetcher(new DataFetcherImplAndroid); |
- if (fetcher->Start(DeviceData::kTypeOrientation, kPeriodInMilliseconds)) |
- return fetcher.release(); |
- |
- LOG(ERROR) << "DataFetcherImplAndroid::Start failed!"; |
- return NULL; |
-} |
- |
-DataFetcherImplAndroid::~DataFetcherImplAndroid() { |
- // TODO(timvolodine): Support device motion as well. Only stop |
- // the active event type(s). |
- Stop(DeviceData::kTypeOrientation); |
+DataFetcherImplAndroid* DataFetcherImplAndroid::GetInstance() { |
+ return Singleton<DataFetcherImplAndroid, |
+ LeakySingletonTraits<DataFetcherImplAndroid> >::get(); |
} |
const DeviceData* DataFetcherImplAndroid::GetDeviceData( |
@@ -82,26 +79,61 @@ void DataFetcherImplAndroid::GotOrientation( |
void DataFetcherImplAndroid::GotAcceleration( |
JNIEnv*, jobject, double x, double y, double z) { |
- NOTIMPLEMENTED(); |
+ device_motion_buffer_->seqlock.WriteBegin(); |
+ device_motion_buffer_->data.accelerationX = x; |
+ device_motion_buffer_->data.hasAccelerationX = true; |
+ device_motion_buffer_->data.accelerationY = y; |
+ device_motion_buffer_->data.hasAccelerationY = true; |
+ device_motion_buffer_->data.accelerationZ = z; |
+ device_motion_buffer_->data.hasAccelerationZ = true; |
+ device_motion_buffer_->seqlock.WriteEnd(); |
+ |
+ if (!is_buffer_ready_) { |
+ received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION] = 1; |
+ CheckBufferReadyToRead(); |
+ } |
} |
void DataFetcherImplAndroid::GotAccelerationIncludingGravity( |
JNIEnv*, jobject, double x, double y, double z) { |
- NOTIMPLEMENTED(); |
+ device_motion_buffer_->seqlock.WriteBegin(); |
+ device_motion_buffer_->data.accelerationIncludingGravityX = x; |
+ device_motion_buffer_->data.hasAccelerationIncludingGravityX = true; |
+ device_motion_buffer_->data.accelerationIncludingGravityY = y; |
+ device_motion_buffer_->data.hasAccelerationIncludingGravityY = true; |
+ device_motion_buffer_->data.accelerationIncludingGravityZ = z; |
+ device_motion_buffer_->data.hasAccelerationIncludingGravityZ = true; |
+ device_motion_buffer_->seqlock.WriteEnd(); |
+ |
+ if (!is_buffer_ready_) { |
+ received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY] = 1; |
+ CheckBufferReadyToRead(); |
+ } |
} |
void DataFetcherImplAndroid::GotRotationRate( |
JNIEnv*, jobject, double alpha, double beta, double gamma) { |
- NOTIMPLEMENTED(); |
+ device_motion_buffer_->seqlock.WriteBegin(); |
+ device_motion_buffer_->data.rotationRateAlpha = alpha; |
+ device_motion_buffer_->data.hasRotationRateAlpha = true; |
+ device_motion_buffer_->data.rotationRateBeta = beta; |
+ device_motion_buffer_->data.hasRotationRateBeta = true; |
+ device_motion_buffer_->data.rotationRateGamma = gamma; |
+ device_motion_buffer_->data.hasRotationRateGamma = true; |
+ device_motion_buffer_->seqlock.WriteEnd(); |
+ |
+ if (!is_buffer_ready_) { |
+ received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] = 1; |
+ CheckBufferReadyToRead(); |
+ } |
} |
-bool DataFetcherImplAndroid::Start( |
- DeviceData::Type event_type, int rate_in_milliseconds) { |
+bool DataFetcherImplAndroid::Start(DeviceData::Type event_type) { |
DCHECK(!device_orientation_.is_null()); |
return Java_DeviceMotionAndOrientation_start( |
AttachCurrentThread(), device_orientation_.obj(), |
reinterpret_cast<jint>(this), static_cast<jint>(event_type), |
- rate_in_milliseconds); |
+ kPeriodInMilliseconds); |
} |
void DataFetcherImplAndroid::Stop(DeviceData::Type event_type) { |
@@ -117,4 +149,48 @@ int DataFetcherImplAndroid::GetNumberActiveDeviceMotionSensors() { |
AttachCurrentThread(), device_orientation_.obj()); |
} |
+ |
+// ----- Shared memory API methods |
+ |
+bool DataFetcherImplAndroid::StartFetchingDeviceMotionData( |
+ DeviceMotionHardwareBuffer* buffer) { |
+ device_motion_buffer_ = buffer; |
+ ClearInternalBuffers(); |
+ bool success = Start(DeviceData::kTypeMotion); |
+ |
+ // If no motion data can ever be provided, the number of active device motion |
+ // sensors will be zero. In that case flag the shared memory buffer |
+ // as ready to read, as it will not change anyway. |
+ number_active_device_motion_sensors_ = GetNumberActiveDeviceMotionSensors(); |
+ CheckBufferReadyToRead(); |
+ return success; |
+} |
+ |
+void DataFetcherImplAndroid::StopFetchingDeviceMotionData() { |
+ Stop(DeviceData::kTypeMotion); |
+ ClearInternalBuffers(); |
+} |
+ |
+void DataFetcherImplAndroid::CheckBufferReadyToRead() { |
+ if (received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION] + |
+ received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY] + |
+ received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] == |
+ number_active_device_motion_sensors_) { |
+ SetBufferReadyStatus(true); |
+ } |
+} |
+ |
+void DataFetcherImplAndroid::SetBufferReadyStatus(bool ready) { |
+ device_motion_buffer_->seqlock.WriteBegin(); |
+ device_motion_buffer_->data.allAvailableSensorsAreActive = ready; |
+ device_motion_buffer_->seqlock.WriteEnd(); |
+ is_buffer_ready_ = ready; |
+} |
+ |
+void DataFetcherImplAndroid::ClearInternalBuffers() { |
+ memset(received_motion_data_, 0, sizeof(received_motion_data_)); |
+ number_active_device_motion_sensors_ = 0; |
+ SetBufferReadyStatus(false); |
+} |
+ |
} // namespace content |