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..70c75dc3c6fc422f855df027fa34b80b984a5183 100644 |
--- a/content/browser/device_orientation/data_fetcher_impl_android.cc |
+++ b/content/browser/device_orientation/data_fetcher_impl_android.cc |
@@ -4,6 +4,7 @@ |
#include "content/browser/device_orientation/data_fetcher_impl_android.h" |
+#include <string.h> |
#include "base/android/jni_android.h" |
#include "base/logging.h" |
#include "content/browser/device_orientation/orientation.h" |
@@ -21,31 +22,38 @@ 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::instance() { |
+ CR_DEFINE_STATIC_LOCAL(DataFetcherImplAndroid, s_data_fetcher, ()); |
+ return &s_data_fetcher; |
+} |
+ |
+// 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::instance(); |
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() { |
bulach
2013/07/09 09:03:29
question: the destructor will never be called sinc
timvolodine
2013/07/09 18:59:56
hmm, very good point. This is actually a bug.
The
|
- // TODO(timvolodine): Support device motion as well. Only stop |
- // the active event type(s). |
Stop(DeviceData::kTypeOrientation); |
+ Stop(DeviceData::kTypeMotion); |
} |
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_[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_[DATA_ACCELERATION_INCLUDING_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_[DATA_ROTATION_RATE] = 1; |
+ CheckBufferReadyToRead(); |
+ } |
} |
bool DataFetcherImplAndroid::Start( |
@@ -117,4 +161,52 @@ int DataFetcherImplAndroid::GetNumberActiveDeviceMotionSensors() { |
AttachCurrentThread(), device_orientation_.obj()); |
} |
+ |
+// ----- New shared memory API methods |
+ |
+bool DataFetcherImplAndroid::FetchDeviceMotionDataIntoBuffer() { |
+ // This method should not be called because it is a push based fetcher. |
+ DCHECK(NeedsPolling()); |
bulach
2013/07/09 09:03:29
would NOTREACHED(); be better?
timvolodine
2013/07/09 18:59:56
Done.
|
+ return false; |
+} |
+ |
+void DataFetcherImplAndroid::CheckBufferReadyToRead() { |
+ if (received_motion_data_[DATA_ACCELERATION] |
+ + received_motion_data_[DATA_ACCELERATION_INCLUDING_GRAVITY] |
bulach
2013/07/09 09:03:29
nit: + and == always at the end of the previous li
timvolodine
2013/07/09 18:59:56
Done.
|
+ + received_motion_data_[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; |
+ memset(received_motion_data_, 0, sizeof(received_motion_data_)); |
bulach
2013/07/09 09:03:29
192-194 is the same as 207-209, perhaps "ClearInte
timvolodine
2013/07/09 18:59:56
Done.
|
+ number_active_device_motion_sensors_ = 0; |
+ SetBufferReadyStatus(false); |
+ 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::StopFetchingDeviceMotionData() { |
+ Stop(DeviceData::kTypeMotion); |
+ number_active_device_motion_sensors_ = 0; |
+ memset(received_motion_data_, 0, sizeof(received_motion_data_)); |
+ SetBufferReadyStatus(false); |
+} |
+ |
} // namespace content |