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