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" |
9 #include "content/browser/device_orientation/orientation.h" | 10 #include "content/browser/device_orientation/orientation.h" |
10 #include "jni/DeviceMotionAndOrientation_jni.h" | 11 #include "jni/DeviceMotionAndOrientation_jni.h" |
11 | 12 |
12 using base::android::AttachCurrentThread; | 13 using base::android::AttachCurrentThread; |
13 | 14 |
14 namespace content { | 15 namespace content { |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 // This should match ProviderImpl::kDesiredSamplingIntervalMs. | 19 // This should match ProviderImpl::kDesiredSamplingIntervalMs. |
19 // TODO(husky): Make that constant public so we can use it directly. | 20 // TODO(husky): Make that constant public so we can use it directly. |
20 const int kPeriodInMilliseconds = 100; | 21 const int kPeriodInMilliseconds = 100; |
21 | 22 |
22 } // namespace | 23 } // namespace |
23 | 24 |
24 DataFetcherImplAndroid::DataFetcherImplAndroid() { | 25 DataFetcherImplAndroid::DataFetcherImplAndroid() |
26 : device_motion_buffer_(NULL), | |
27 is_buffer_ready_(false) { | |
28 memset(received_motion_data_, 0, sizeof(received_motion_data_)); | |
25 device_orientation_.Reset( | 29 device_orientation_.Reset( |
26 Java_DeviceMotionAndOrientation_getInstance(AttachCurrentThread())); | 30 Java_DeviceMotionAndOrientation_getInstance(AttachCurrentThread())); |
27 } | 31 } |
28 | 32 |
29 void DataFetcherImplAndroid::Init(JNIEnv* env) { | 33 void DataFetcherImplAndroid::Register(JNIEnv* env) { |
30 bool result = RegisterNativesImpl(env); | 34 bool result = RegisterNativesImpl(env); |
31 DCHECK(result); | 35 DCHECK(result); |
32 } | 36 } |
33 | 37 |
34 // TODO(timvolodine): Modify this method to be able to distinguish | 38 DataFetcherImplAndroid* DataFetcherImplAndroid::instance() { |
35 // device motion from orientation. | 39 CR_DEFINE_STATIC_LOCAL(DataFetcherImplAndroid, s_data_fetcher, ()); |
40 return &s_data_fetcher; | |
41 } | |
42 | |
43 // TODO(timvolodine): Remove this method once orientation also switches to | |
44 // shared memory implementation. | |
36 DataFetcher* DataFetcherImplAndroid::Create() { | 45 DataFetcher* DataFetcherImplAndroid::Create() { |
37 scoped_ptr<DataFetcherImplAndroid> fetcher(new DataFetcherImplAndroid); | 46 DataFetcherImplAndroid* fetcher = DataFetcherImplAndroid::instance(); |
38 if (fetcher->Start(DeviceData::kTypeOrientation, kPeriodInMilliseconds)) | 47 if (fetcher->Start(DeviceData::kTypeOrientation, kPeriodInMilliseconds)) |
39 return fetcher.release(); | 48 return fetcher; |
40 | 49 |
41 LOG(ERROR) << "DataFetcherImplAndroid::Start failed!"; | 50 DVLOG(2) << "DataFetcherImplAndroid::Start failed!"; |
42 return NULL; | 51 return NULL; |
43 } | 52 } |
44 | 53 |
45 DataFetcherImplAndroid::~DataFetcherImplAndroid() { | 54 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
| |
46 // TODO(timvolodine): Support device motion as well. Only stop | |
47 // the active event type(s). | |
48 Stop(DeviceData::kTypeOrientation); | 55 Stop(DeviceData::kTypeOrientation); |
56 Stop(DeviceData::kTypeMotion); | |
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_[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_[DATA_ACCELERATION_INCLUDING_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_[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 | |
166 | |
167 bool DataFetcherImplAndroid::FetchDeviceMotionDataIntoBuffer() { | |
168 // This method should not be called because it is a push based fetcher. | |
169 DCHECK(NeedsPolling()); | |
bulach
2013/07/09 09:03:29
would NOTREACHED(); be better?
timvolodine
2013/07/09 18:59:56
Done.
| |
170 return false; | |
171 } | |
172 | |
173 void DataFetcherImplAndroid::CheckBufferReadyToRead() { | |
174 if (received_motion_data_[DATA_ACCELERATION] | |
175 + 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.
| |
176 + received_motion_data_[DATA_ROTATION_RATE] | |
177 == number_active_device_motion_sensors_) { | |
178 SetBufferReadyStatus(true); | |
179 } | |
180 } | |
181 | |
182 void DataFetcherImplAndroid::SetBufferReadyStatus(bool ready) { | |
183 device_motion_buffer_->seqlock.WriteBegin(); | |
184 device_motion_buffer_->data.allAvailableSensorsAreActive = ready; | |
185 device_motion_buffer_->seqlock.WriteEnd(); | |
186 is_buffer_ready_ = ready; | |
187 } | |
188 | |
189 bool DataFetcherImplAndroid::StartFetchingDeviceMotionData( | |
190 DeviceMotionHardwareBuffer* buffer) { | |
191 device_motion_buffer_ = buffer; | |
192 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.
| |
193 number_active_device_motion_sensors_ = 0; | |
194 SetBufferReadyStatus(false); | |
195 bool success = Start(DeviceData::kTypeMotion, kPeriodInMilliseconds); | |
196 | |
197 // If no motion data can ever be provided, the number of active device motion | |
198 // sensors will be zero. In that case flag the shared memory buffer | |
199 // as ready to read, as it will not change anyway. | |
200 number_active_device_motion_sensors_ = GetNumberActiveDeviceMotionSensors(); | |
201 CheckBufferReadyToRead(); | |
202 return success; | |
203 } | |
204 | |
205 void DataFetcherImplAndroid::StopFetchingDeviceMotionData() { | |
206 Stop(DeviceData::kTypeMotion); | |
207 number_active_device_motion_sensors_ = 0; | |
208 memset(received_motion_data_, 0, sizeof(received_motion_data_)); | |
209 SetBufferReadyStatus(false); | |
210 } | |
211 | |
120 } // namespace content | 212 } // namespace content |
OLD | NEW |