| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_orientation/data_fetcher_impl_android.h" | 5 #include "content/browser/device_orientation/data_fetcher_impl_android.h" |
| 6 | 6 |
| 7 #include <string.h> |
| 8 |
| 7 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 8 #include "base/logging.h" | 10 #include "base/memory/singleton.h" |
| 9 #include "content/browser/device_orientation/orientation.h" | 11 #include "content/browser/device_orientation/orientation.h" |
| 10 #include "jni/DeviceMotionAndOrientation_jni.h" | 12 #include "jni/DeviceMotionAndOrientation_jni.h" |
| 11 | 13 |
| 12 using base::android::AttachCurrentThread; | 14 using base::android::AttachCurrentThread; |
| 13 | 15 |
| 14 namespace content { | 16 namespace content { |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 18 // This should match ProviderImpl::kDesiredSamplingIntervalMs. | 20 // This should match ProviderImpl::kDesiredSamplingIntervalMs. |
| 19 // TODO(husky): Make that constant public so we can use it directly. | 21 // TODO(husky): Make that constant public so we can use it directly. |
| 20 const int kPeriodInMilliseconds = 100; | 22 const int kPeriodInMilliseconds = 100; |
| 21 | 23 |
| 22 } // namespace | 24 } // namespace |
| 23 | 25 |
| 24 DataFetcherImplAndroid::DataFetcherImplAndroid() { | 26 DataFetcherImplAndroid::DataFetcherImplAndroid() |
| 27 : number_active_device_motion_sensors_(0), |
| 28 device_motion_buffer_(NULL), |
| 29 is_buffer_ready_(false) { |
| 30 memset(received_motion_data_, 0, sizeof(received_motion_data_)); |
| 25 device_orientation_.Reset( | 31 device_orientation_.Reset( |
| 26 Java_DeviceMotionAndOrientation_getInstance(AttachCurrentThread())); | 32 Java_DeviceMotionAndOrientation_getInstance(AttachCurrentThread())); |
| 27 } | 33 } |
| 28 | 34 |
| 35 DataFetcherImplAndroid::~DataFetcherImplAndroid() { |
| 36 } |
| 37 |
| 29 void DataFetcherImplAndroid::Init(JNIEnv* env) { | 38 void DataFetcherImplAndroid::Init(JNIEnv* env) { |
| 30 bool result = RegisterNativesImpl(env); | 39 bool result = RegisterNativesImpl(env); |
| 31 DCHECK(result); | 40 DCHECK(result); |
| 32 } | 41 } |
| 33 | 42 |
| 34 // TODO(timvolodine): Modify this method to be able to distinguish | 43 DataFetcherImplAndroid* DataFetcherImplAndroid::GetInstance() { |
| 35 // device motion from orientation. | 44 return Singleton<DataFetcherImplAndroid, |
| 36 DataFetcher* DataFetcherImplAndroid::Create() { | 45 LeakySingletonTraits<DataFetcherImplAndroid> >::get(); |
| 37 scoped_ptr<DataFetcherImplAndroid> fetcher(new DataFetcherImplAndroid); | |
| 38 if (fetcher->Start(DeviceData::kTypeOrientation, kPeriodInMilliseconds)) | |
| 39 return fetcher.release(); | |
| 40 | |
| 41 LOG(ERROR) << "DataFetcherImplAndroid::Start failed!"; | |
| 42 return NULL; | |
| 43 } | |
| 44 | |
| 45 DataFetcherImplAndroid::~DataFetcherImplAndroid() { | |
| 46 // TODO(timvolodine): Support device motion as well. Only stop | |
| 47 // the active event type(s). | |
| 48 Stop(DeviceData::kTypeOrientation); | |
| 49 } | 46 } |
| 50 | 47 |
| 51 const DeviceData* DataFetcherImplAndroid::GetDeviceData( | 48 const DeviceData* DataFetcherImplAndroid::GetDeviceData( |
| 52 DeviceData::Type type) { | 49 DeviceData::Type type) { |
| 53 if (type != DeviceData::kTypeOrientation) | 50 if (type != DeviceData::kTypeOrientation) |
| 54 return NULL; | 51 return NULL; |
| 55 return GetOrientation(); | 52 return GetOrientation(); |
| 56 } | 53 } |
| 57 | 54 |
| 58 const Orientation* DataFetcherImplAndroid::GetOrientation() { | 55 const Orientation* DataFetcherImplAndroid::GetOrientation() { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 75 Orientation* orientation = new Orientation(); | 72 Orientation* orientation = new Orientation(); |
| 76 orientation->set_alpha(alpha); | 73 orientation->set_alpha(alpha); |
| 77 orientation->set_beta(beta); | 74 orientation->set_beta(beta); |
| 78 orientation->set_gamma(gamma); | 75 orientation->set_gamma(gamma); |
| 79 orientation->set_absolute(true); | 76 orientation->set_absolute(true); |
| 80 next_orientation_ = orientation; | 77 next_orientation_ = orientation; |
| 81 } | 78 } |
| 82 | 79 |
| 83 void DataFetcherImplAndroid::GotAcceleration( | 80 void DataFetcherImplAndroid::GotAcceleration( |
| 84 JNIEnv*, jobject, double x, double y, double z) { | 81 JNIEnv*, jobject, double x, double y, double z) { |
| 85 NOTIMPLEMENTED(); | 82 device_motion_buffer_->seqlock.WriteBegin(); |
| 83 device_motion_buffer_->data.accelerationX = x; |
| 84 device_motion_buffer_->data.hasAccelerationX = true; |
| 85 device_motion_buffer_->data.accelerationY = y; |
| 86 device_motion_buffer_->data.hasAccelerationY = true; |
| 87 device_motion_buffer_->data.accelerationZ = z; |
| 88 device_motion_buffer_->data.hasAccelerationZ = true; |
| 89 device_motion_buffer_->seqlock.WriteEnd(); |
| 90 |
| 91 if (!is_buffer_ready_) { |
| 92 received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION] = 1; |
| 93 CheckBufferReadyToRead(); |
| 94 } |
| 86 } | 95 } |
| 87 | 96 |
| 88 void DataFetcherImplAndroid::GotAccelerationIncludingGravity( | 97 void DataFetcherImplAndroid::GotAccelerationIncludingGravity( |
| 89 JNIEnv*, jobject, double x, double y, double z) { | 98 JNIEnv*, jobject, double x, double y, double z) { |
| 90 NOTIMPLEMENTED(); | 99 device_motion_buffer_->seqlock.WriteBegin(); |
| 100 device_motion_buffer_->data.accelerationIncludingGravityX = x; |
| 101 device_motion_buffer_->data.hasAccelerationIncludingGravityX = true; |
| 102 device_motion_buffer_->data.accelerationIncludingGravityY = y; |
| 103 device_motion_buffer_->data.hasAccelerationIncludingGravityY = true; |
| 104 device_motion_buffer_->data.accelerationIncludingGravityZ = z; |
| 105 device_motion_buffer_->data.hasAccelerationIncludingGravityZ = true; |
| 106 device_motion_buffer_->seqlock.WriteEnd(); |
| 107 |
| 108 if (!is_buffer_ready_) { |
| 109 received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY] = 1; |
| 110 CheckBufferReadyToRead(); |
| 111 } |
| 91 } | 112 } |
| 92 | 113 |
| 93 void DataFetcherImplAndroid::GotRotationRate( | 114 void DataFetcherImplAndroid::GotRotationRate( |
| 94 JNIEnv*, jobject, double alpha, double beta, double gamma) { | 115 JNIEnv*, jobject, double alpha, double beta, double gamma) { |
| 95 NOTIMPLEMENTED(); | 116 device_motion_buffer_->seqlock.WriteBegin(); |
| 117 device_motion_buffer_->data.rotationRateAlpha = alpha; |
| 118 device_motion_buffer_->data.hasRotationRateAlpha = true; |
| 119 device_motion_buffer_->data.rotationRateBeta = beta; |
| 120 device_motion_buffer_->data.hasRotationRateBeta = true; |
| 121 device_motion_buffer_->data.rotationRateGamma = gamma; |
| 122 device_motion_buffer_->data.hasRotationRateGamma = true; |
| 123 device_motion_buffer_->seqlock.WriteEnd(); |
| 124 |
| 125 if (!is_buffer_ready_) { |
| 126 received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] = 1; |
| 127 CheckBufferReadyToRead(); |
| 128 } |
| 96 } | 129 } |
| 97 | 130 |
| 98 bool DataFetcherImplAndroid::Start( | 131 bool DataFetcherImplAndroid::Start(DeviceData::Type event_type) { |
| 99 DeviceData::Type event_type, int rate_in_milliseconds) { | |
| 100 DCHECK(!device_orientation_.is_null()); | 132 DCHECK(!device_orientation_.is_null()); |
| 101 return Java_DeviceMotionAndOrientation_start( | 133 return Java_DeviceMotionAndOrientation_start( |
| 102 AttachCurrentThread(), device_orientation_.obj(), | 134 AttachCurrentThread(), device_orientation_.obj(), |
| 103 reinterpret_cast<jint>(this), static_cast<jint>(event_type), | 135 reinterpret_cast<jint>(this), static_cast<jint>(event_type), |
| 104 rate_in_milliseconds); | 136 kPeriodInMilliseconds); |
| 105 } | 137 } |
| 106 | 138 |
| 107 void DataFetcherImplAndroid::Stop(DeviceData::Type event_type) { | 139 void DataFetcherImplAndroid::Stop(DeviceData::Type event_type) { |
| 108 DCHECK(!device_orientation_.is_null()); | 140 DCHECK(!device_orientation_.is_null()); |
| 109 Java_DeviceMotionAndOrientation_stop( | 141 Java_DeviceMotionAndOrientation_stop( |
| 110 AttachCurrentThread(), device_orientation_.obj(), | 142 AttachCurrentThread(), device_orientation_.obj(), |
| 111 static_cast<jint>(event_type)); | 143 static_cast<jint>(event_type)); |
| 112 } | 144 } |
| 113 | 145 |
| 114 int DataFetcherImplAndroid::GetNumberActiveDeviceMotionSensors() { | 146 int DataFetcherImplAndroid::GetNumberActiveDeviceMotionSensors() { |
| 115 DCHECK(!device_orientation_.is_null()); | 147 DCHECK(!device_orientation_.is_null()); |
| 116 return Java_DeviceMotionAndOrientation_getNumberActiveDeviceMotionSensors( | 148 return Java_DeviceMotionAndOrientation_getNumberActiveDeviceMotionSensors( |
| 117 AttachCurrentThread(), device_orientation_.obj()); | 149 AttachCurrentThread(), device_orientation_.obj()); |
| 118 } | 150 } |
| 119 | 151 |
| 152 |
| 153 // ----- Shared memory API methods |
| 154 |
| 155 bool DataFetcherImplAndroid::StartFetchingDeviceMotionData( |
| 156 DeviceMotionHardwareBuffer* buffer) { |
| 157 device_motion_buffer_ = buffer; |
| 158 ClearInternalBuffers(); |
| 159 bool success = Start(DeviceData::kTypeMotion); |
| 160 |
| 161 // If no motion data can ever be provided, the number of active device motion |
| 162 // sensors will be zero. In that case flag the shared memory buffer |
| 163 // as ready to read, as it will not change anyway. |
| 164 number_active_device_motion_sensors_ = GetNumberActiveDeviceMotionSensors(); |
| 165 CheckBufferReadyToRead(); |
| 166 return success; |
| 167 } |
| 168 |
| 169 void DataFetcherImplAndroid::StopFetchingDeviceMotionData() { |
| 170 Stop(DeviceData::kTypeMotion); |
| 171 ClearInternalBuffers(); |
| 172 } |
| 173 |
| 174 void DataFetcherImplAndroid::CheckBufferReadyToRead() { |
| 175 if (received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION] + |
| 176 received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY] + |
| 177 received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] == |
| 178 number_active_device_motion_sensors_) { |
| 179 SetBufferReadyStatus(true); |
| 180 } |
| 181 } |
| 182 |
| 183 void DataFetcherImplAndroid::SetBufferReadyStatus(bool ready) { |
| 184 device_motion_buffer_->seqlock.WriteBegin(); |
| 185 device_motion_buffer_->data.allAvailableSensorsAreActive = ready; |
| 186 device_motion_buffer_->seqlock.WriteEnd(); |
| 187 is_buffer_ready_ = ready; |
| 188 } |
| 189 |
| 190 void DataFetcherImplAndroid::ClearInternalBuffers() { |
| 191 memset(received_motion_data_, 0, sizeof(received_motion_data_)); |
| 192 number_active_device_motion_sensors_ = 0; |
| 193 SetBufferReadyStatus(false); |
| 194 } |
| 195 |
| 120 } // namespace content | 196 } // namespace content |
| OLD | NEW |