Chromium Code Reviews| 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..c5f474670d697eb1f6710414a981ce9a6d2618ac 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,37 @@ const int kPeriodInMilliseconds = 100; |
| } // namespace |
| -DataFetcherImplAndroid::DataFetcherImplAndroid() { |
| +DataFetcherImplAndroid::DataFetcherImplAndroid() |
| + : device_motion_buffer_(NULL), |
| + is_buffer_ready_(false) { |
| + memset(received_motion_data_, 0, sizeof(received_motion_data_)); |
| device_orientation_.Reset( |
| Java_DeviceMotionAndOrientation_getInstance(AttachCurrentThread())); |
| } |
| -void DataFetcherImplAndroid::Init(JNIEnv* env) { |
| +void DataFetcherImplAndroid::Register(JNIEnv* env) { |
| bool result = RegisterNativesImpl(env); |
| DCHECK(result); |
| } |
| -// TODO(timvolodine): Modify this method to be able to distinguish |
| -// device motion from orientation. |
| +DataFetcherImplAndroid* DataFetcherImplAndroid::GetInstance() { |
| + return Singleton<DataFetcherImplAndroid, |
| + LeakySingletonTraits<DataFetcherImplAndroid> >::get(); |
| +} |
| + |
| +// TODO(timvolodine): Remove this method once orientation also switches to |
| +// shared memory implementation. |
| DataFetcher* DataFetcherImplAndroid::Create() { |
| - scoped_ptr<DataFetcherImplAndroid> fetcher(new DataFetcherImplAndroid); |
| + DataFetcherImplAndroid* fetcher = DataFetcherImplAndroid::GetInstance(); |
| if (fetcher->Start(DeviceData::kTypeOrientation, kPeriodInMilliseconds)) |
| - return fetcher.release(); |
| + return fetcher; |
| - LOG(ERROR) << "DataFetcherImplAndroid::Start failed!"; |
| + DVLOG(2) << "DataFetcherImplAndroid::Start failed!"; |
| return NULL; |
| } |
| DataFetcherImplAndroid::~DataFetcherImplAndroid() { |
| - // TODO(timvolodine): Support device motion as well. Only stop |
| - // the active event type(s). |
| - Stop(DeviceData::kTypeOrientation); |
| + NOTREACHED(); |
| } |
| const DeviceData* DataFetcherImplAndroid::GetDeviceData( |
| @@ -82,17 +90,53 @@ 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( |
| @@ -117,4 +161,58 @@ int DataFetcherImplAndroid::GetNumberActiveDeviceMotionSensors() { |
| AttachCurrentThread(), device_orientation_.obj()); |
| } |
| + |
| +// ----- New shared memory API methods |
|
bulach
2013/07/10 16:59:41
s/New// :) it won't be new for too long..
timvolodine
2013/07/11 14:31:34
Done.
|
| + |
| +bool DataFetcherImplAndroid::NeedsPolling() { |
| + return false; |
| +} |
| + |
| +bool DataFetcherImplAndroid::FetchDeviceMotionDataIntoBuffer() { |
| + // This method should not be called because it is a push based fetcher. |
| + NOTREACHED(); |
| + return false; |
| +} |
| + |
| +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; |
| +} |
| + |
| +bool DataFetcherImplAndroid::StartFetchingDeviceMotionData( |
| + DeviceMotionHardwareBuffer* buffer) { |
| + device_motion_buffer_ = buffer; |
| + ClearInternalBuffers(); |
| + bool success = Start(DeviceData::kTypeMotion, kPeriodInMilliseconds); |
| + |
| + // 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::ClearInternalBuffers() { |
| + memset(received_motion_data_, 0, sizeof(received_motion_data_)); |
| + number_active_device_motion_sensors_ = 0; |
| + SetBufferReadyStatus(false); |
| +} |
| + |
| +void DataFetcherImplAndroid::StopFetchingDeviceMotionData() { |
| + Stop(DeviceData::kTypeMotion); |
| + ClearInternalBuffers(); |
| +} |
| + |
| } // namespace content |