Chromium Code Reviews| 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> | |
| 7 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
| 8 #include "base/logging.h" | 9 #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 : device_motion_buffer_(NULL), | |
| 28 is_buffer_ready_(false) { | |
| 29 memset(received_motion_data_, 0, sizeof(received_motion_data_)); | |
| 25 device_orientation_.Reset( | 30 device_orientation_.Reset( |
| 26 Java_DeviceMotionAndOrientation_getInstance(AttachCurrentThread())); | 31 Java_DeviceMotionAndOrientation_getInstance(AttachCurrentThread())); |
| 27 } | 32 } |
| 28 | 33 |
| 29 void DataFetcherImplAndroid::Init(JNIEnv* env) { | 34 void DataFetcherImplAndroid::Register(JNIEnv* env) { |
| 30 bool result = RegisterNativesImpl(env); | 35 bool result = RegisterNativesImpl(env); |
| 31 DCHECK(result); | 36 DCHECK(result); |
| 32 } | 37 } |
| 33 | 38 |
| 34 // TODO(timvolodine): Modify this method to be able to distinguish | 39 DataFetcherImplAndroid* DataFetcherImplAndroid::GetInstance() { |
| 35 // device motion from orientation. | 40 return Singleton<DataFetcherImplAndroid, |
| 41 LeakySingletonTraits<DataFetcherImplAndroid> >::get(); | |
| 42 } | |
| 43 | |
| 44 // TODO(timvolodine): Remove this method once orientation also switches to | |
| 45 // shared memory implementation. | |
| 36 DataFetcher* DataFetcherImplAndroid::Create() { | 46 DataFetcher* DataFetcherImplAndroid::Create() { |
| 37 scoped_ptr<DataFetcherImplAndroid> fetcher(new DataFetcherImplAndroid); | 47 DataFetcherImplAndroid* fetcher = DataFetcherImplAndroid::GetInstance(); |
| 38 if (fetcher->Start(DeviceData::kTypeOrientation, kPeriodInMilliseconds)) | 48 if (fetcher->Start(DeviceData::kTypeOrientation, kPeriodInMilliseconds)) |
| 39 return fetcher.release(); | 49 return fetcher; |
| 40 | 50 |
| 41 LOG(ERROR) << "DataFetcherImplAndroid::Start failed!"; | 51 DVLOG(2) << "DataFetcherImplAndroid::Start failed!"; |
| 42 return NULL; | 52 return NULL; |
| 43 } | 53 } |
| 44 | 54 |
| 45 DataFetcherImplAndroid::~DataFetcherImplAndroid() { | 55 DataFetcherImplAndroid::~DataFetcherImplAndroid() { |
| 46 // TODO(timvolodine): Support device motion as well. Only stop | 56 NOTREACHED(); |
| 47 // the active event type(s). | |
| 48 Stop(DeviceData::kTypeOrientation); | |
| 49 } | 57 } |
| 50 | 58 |
| 51 const DeviceData* DataFetcherImplAndroid::GetDeviceData( | 59 const DeviceData* DataFetcherImplAndroid::GetDeviceData( |
| 52 DeviceData::Type type) { | 60 DeviceData::Type type) { |
| 53 if (type != DeviceData::kTypeOrientation) | 61 if (type != DeviceData::kTypeOrientation) |
| 54 return NULL; | 62 return NULL; |
| 55 return GetOrientation(); | 63 return GetOrientation(); |
| 56 } | 64 } |
| 57 | 65 |
| 58 const Orientation* DataFetcherImplAndroid::GetOrientation() { | 66 const Orientation* DataFetcherImplAndroid::GetOrientation() { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 75 Orientation* orientation = new Orientation(); | 83 Orientation* orientation = new Orientation(); |
| 76 orientation->set_alpha(alpha); | 84 orientation->set_alpha(alpha); |
| 77 orientation->set_beta(beta); | 85 orientation->set_beta(beta); |
| 78 orientation->set_gamma(gamma); | 86 orientation->set_gamma(gamma); |
| 79 orientation->set_absolute(true); | 87 orientation->set_absolute(true); |
| 80 next_orientation_ = orientation; | 88 next_orientation_ = orientation; |
| 81 } | 89 } |
| 82 | 90 |
| 83 void DataFetcherImplAndroid::GotAcceleration( | 91 void DataFetcherImplAndroid::GotAcceleration( |
| 84 JNIEnv*, jobject, double x, double y, double z) { | 92 JNIEnv*, jobject, double x, double y, double z) { |
| 85 NOTIMPLEMENTED(); | 93 device_motion_buffer_->seqlock.WriteBegin(); |
| 94 device_motion_buffer_->data.accelerationX = x; | |
| 95 device_motion_buffer_->data.hasAccelerationX = true; | |
| 96 device_motion_buffer_->data.accelerationY = y; | |
| 97 device_motion_buffer_->data.hasAccelerationY = true; | |
| 98 device_motion_buffer_->data.accelerationZ = z; | |
| 99 device_motion_buffer_->data.hasAccelerationZ = true; | |
| 100 device_motion_buffer_->seqlock.WriteEnd(); | |
| 101 | |
| 102 if (!is_buffer_ready_) { | |
| 103 received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION] = 1; | |
| 104 CheckBufferReadyToRead(); | |
| 105 } | |
| 86 } | 106 } |
| 87 | 107 |
| 88 void DataFetcherImplAndroid::GotAccelerationIncludingGravity( | 108 void DataFetcherImplAndroid::GotAccelerationIncludingGravity( |
| 89 JNIEnv*, jobject, double x, double y, double z) { | 109 JNIEnv*, jobject, double x, double y, double z) { |
| 90 NOTIMPLEMENTED(); | 110 device_motion_buffer_->seqlock.WriteBegin(); |
| 111 device_motion_buffer_->data.accelerationIncludingGravityX = x; | |
| 112 device_motion_buffer_->data.hasAccelerationIncludingGravityX = true; | |
| 113 device_motion_buffer_->data.accelerationIncludingGravityY = y; | |
| 114 device_motion_buffer_->data.hasAccelerationIncludingGravityY = true; | |
| 115 device_motion_buffer_->data.accelerationIncludingGravityZ = z; | |
| 116 device_motion_buffer_->data.hasAccelerationIncludingGravityZ = true; | |
| 117 device_motion_buffer_->seqlock.WriteEnd(); | |
| 118 | |
| 119 if (!is_buffer_ready_) { | |
| 120 received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY] = 1; | |
| 121 CheckBufferReadyToRead(); | |
| 122 } | |
| 91 } | 123 } |
| 92 | 124 |
| 93 void DataFetcherImplAndroid::GotRotationRate( | 125 void DataFetcherImplAndroid::GotRotationRate( |
| 94 JNIEnv*, jobject, double alpha, double beta, double gamma) { | 126 JNIEnv*, jobject, double alpha, double beta, double gamma) { |
| 95 NOTIMPLEMENTED(); | 127 device_motion_buffer_->seqlock.WriteBegin(); |
| 128 device_motion_buffer_->data.rotationRateAlpha = alpha; | |
| 129 device_motion_buffer_->data.hasRotationRateAlpha = true; | |
| 130 device_motion_buffer_->data.rotationRateBeta = beta; | |
| 131 device_motion_buffer_->data.hasRotationRateBeta = true; | |
| 132 device_motion_buffer_->data.rotationRateGamma = gamma; | |
| 133 device_motion_buffer_->data.hasRotationRateGamma = true; | |
| 134 device_motion_buffer_->seqlock.WriteEnd(); | |
| 135 | |
| 136 if (!is_buffer_ready_) { | |
| 137 received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] = 1; | |
| 138 CheckBufferReadyToRead(); | |
| 139 } | |
| 96 } | 140 } |
| 97 | 141 |
| 98 bool DataFetcherImplAndroid::Start( | 142 bool DataFetcherImplAndroid::Start( |
| 99 DeviceData::Type event_type, int rate_in_milliseconds) { | 143 DeviceData::Type event_type, int rate_in_milliseconds) { |
| 100 DCHECK(!device_orientation_.is_null()); | 144 DCHECK(!device_orientation_.is_null()); |
| 101 return Java_DeviceMotionAndOrientation_start( | 145 return Java_DeviceMotionAndOrientation_start( |
| 102 AttachCurrentThread(), device_orientation_.obj(), | 146 AttachCurrentThread(), device_orientation_.obj(), |
| 103 reinterpret_cast<jint>(this), static_cast<jint>(event_type), | 147 reinterpret_cast<jint>(this), static_cast<jint>(event_type), |
| 104 rate_in_milliseconds); | 148 rate_in_milliseconds); |
| 105 } | 149 } |
| 106 | 150 |
| 107 void DataFetcherImplAndroid::Stop(DeviceData::Type event_type) { | 151 void DataFetcherImplAndroid::Stop(DeviceData::Type event_type) { |
| 108 DCHECK(!device_orientation_.is_null()); | 152 DCHECK(!device_orientation_.is_null()); |
| 109 Java_DeviceMotionAndOrientation_stop( | 153 Java_DeviceMotionAndOrientation_stop( |
| 110 AttachCurrentThread(), device_orientation_.obj(), | 154 AttachCurrentThread(), device_orientation_.obj(), |
| 111 static_cast<jint>(event_type)); | 155 static_cast<jint>(event_type)); |
| 112 } | 156 } |
| 113 | 157 |
| 114 int DataFetcherImplAndroid::GetNumberActiveDeviceMotionSensors() { | 158 int DataFetcherImplAndroid::GetNumberActiveDeviceMotionSensors() { |
| 115 DCHECK(!device_orientation_.is_null()); | 159 DCHECK(!device_orientation_.is_null()); |
| 116 return Java_DeviceMotionAndOrientation_getNumberActiveDeviceMotionSensors( | 160 return Java_DeviceMotionAndOrientation_getNumberActiveDeviceMotionSensors( |
| 117 AttachCurrentThread(), device_orientation_.obj()); | 161 AttachCurrentThread(), device_orientation_.obj()); |
| 118 } | 162 } |
| 119 | 163 |
| 164 | |
| 165 // ----- 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.
| |
| 166 | |
| 167 bool DataFetcherImplAndroid::NeedsPolling() { | |
| 168 return false; | |
| 169 } | |
| 170 | |
| 171 bool DataFetcherImplAndroid::FetchDeviceMotionDataIntoBuffer() { | |
| 172 // This method should not be called because it is a push based fetcher. | |
| 173 NOTREACHED(); | |
| 174 return false; | |
| 175 } | |
| 176 | |
| 177 void DataFetcherImplAndroid::CheckBufferReadyToRead() { | |
| 178 if (received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION] + | |
| 179 received_motion_data_[RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY] + | |
| 180 received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] == | |
| 181 number_active_device_motion_sensors_) { | |
| 182 SetBufferReadyStatus(true); | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 void DataFetcherImplAndroid::SetBufferReadyStatus(bool ready) { | |
| 187 device_motion_buffer_->seqlock.WriteBegin(); | |
| 188 device_motion_buffer_->data.allAvailableSensorsAreActive = ready; | |
| 189 device_motion_buffer_->seqlock.WriteEnd(); | |
| 190 is_buffer_ready_ = ready; | |
| 191 } | |
| 192 | |
| 193 bool DataFetcherImplAndroid::StartFetchingDeviceMotionData( | |
| 194 DeviceMotionHardwareBuffer* buffer) { | |
| 195 device_motion_buffer_ = buffer; | |
| 196 ClearInternalBuffers(); | |
| 197 bool success = Start(DeviceData::kTypeMotion, kPeriodInMilliseconds); | |
| 198 | |
| 199 // If no motion data can ever be provided, the number of active device motion | |
| 200 // sensors will be zero. In that case flag the shared memory buffer | |
| 201 // as ready to read, as it will not change anyway. | |
| 202 number_active_device_motion_sensors_ = GetNumberActiveDeviceMotionSensors(); | |
| 203 CheckBufferReadyToRead(); | |
| 204 return success; | |
| 205 } | |
| 206 | |
| 207 void DataFetcherImplAndroid::ClearInternalBuffers() { | |
| 208 memset(received_motion_data_, 0, sizeof(received_motion_data_)); | |
| 209 number_active_device_motion_sensors_ = 0; | |
| 210 SetBufferReadyStatus(false); | |
| 211 } | |
| 212 | |
| 213 void DataFetcherImplAndroid::StopFetchingDeviceMotionData() { | |
| 214 Stop(DeviceData::kTypeMotion); | |
| 215 ClearInternalBuffers(); | |
| 216 } | |
| 217 | |
| 120 } // namespace content | 218 } // namespace content |
| OLD | NEW |