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 "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "content/browser/device_orientation/orientation.h" | 9 #include "content/browser/device_orientation/orientation.h" |
10 #include "jni/DeviceMotionAndOrientation_jni.h" | 10 #include "jni/DeviceMotionAndOrientation_jni.h" |
11 | 11 |
12 using base::android::AttachCurrentThread; | 12 using base::android::AttachCurrentThread; |
13 | 13 |
14 namespace content { | 14 namespace content { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // This should match ProviderImpl::kDesiredSamplingIntervalMs. | 18 // This should match ProviderImpl::kDesiredSamplingIntervalMs. |
19 // TODO(husky): Make that constant public so we can use it directly. | 19 // TODO(husky): Make that constant public so we can use it directly. |
20 const int kPeriodInMilliseconds = 100; | 20 const int kPeriodInMilliseconds = 100; |
21 | 21 |
22 } // namespace | 22 } // namespace |
23 | 23 |
24 DataFetcherImplAndroid::DataFetcherImplAndroid() { | 24 DataFetcherImplAndroid::DataFetcherImplAndroid() |
25 : device_motion_buffer_(0), | |
bulach
2013/07/04 08:23:36
nit: s/0/NULL/
timvolodine
2013/07/04 12:02:52
Done.
| |
26 is_buffer_ready_(false) { | |
25 device_orientation_.Reset( | 27 device_orientation_.Reset( |
26 Java_DeviceMotionAndOrientation_getInstance(AttachCurrentThread())); | 28 Java_DeviceMotionAndOrientation_getInstance(AttachCurrentThread())); |
27 } | 29 } |
28 | 30 |
29 void DataFetcherImplAndroid::Init(JNIEnv* env) { | 31 void DataFetcherImplAndroid::Init(JNIEnv* env) { |
bulach
2013/07/04 08:23:36
nit: not related to your patch, but rather than In
timvolodine
2013/07/04 12:02:52
Done.
| |
30 bool result = RegisterNativesImpl(env); | 32 bool result = RegisterNativesImpl(env); |
31 DCHECK(result); | 33 DCHECK(result); |
32 } | 34 } |
33 | 35 |
34 // TODO(timvolodine): Modify this method to be able to distinguish | 36 DataFetcherImplAndroid* DataFetcherImplAndroid::instance() { |
35 // device motion from orientation. | 37 CR_DEFINE_STATIC_LOCAL(DataFetcherImplAndroid, s_data_fetcher, ()); |
38 return &s_data_fetcher; | |
39 } | |
40 | |
41 // TODO(timvolodine): Remove this method once orientation also switches to | |
42 // shared memory implementation. | |
36 DataFetcher* DataFetcherImplAndroid::Create() { | 43 DataFetcher* DataFetcherImplAndroid::Create() { |
37 scoped_ptr<DataFetcherImplAndroid> fetcher(new DataFetcherImplAndroid); | 44 DataFetcherImplAndroid* fetcher = DataFetcherImplAndroid::instance(); |
38 if (fetcher->Start(DeviceData::kTypeOrientation, kPeriodInMilliseconds)) | 45 if (fetcher->Start(DeviceData::kTypeOrientation, kPeriodInMilliseconds)) |
39 return fetcher.release(); | 46 return fetcher; |
40 | 47 |
41 LOG(ERROR) << "DataFetcherImplAndroid::Start failed!"; | 48 DVLOG(2) << "DataFetcherImplAndroid::Start failed!"; |
42 return NULL; | 49 return NULL; |
43 } | 50 } |
44 | 51 |
45 DataFetcherImplAndroid::~DataFetcherImplAndroid() { | 52 DataFetcherImplAndroid::~DataFetcherImplAndroid() { |
46 // TODO(timvolodine): Support device motion as well. Only stop | 53 // TODO(timvolodine): Support device motion as well. Only stop |
47 // the active event type(s). | 54 // the active event type(s). |
bulach
2013/07/04 08:23:36
the first part of the TODO seem done :)
timvolodine
2013/07/04 12:02:52
Done.
| |
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 receivedMotionData[0] = 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 receivedMotionData[1] = 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 receivedMotionData[2] = 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()); | |
170 return false; | |
171 } | |
172 | |
173 void DataFetcherImplAndroid::CheckBufferReadyToRead() { | |
174 if (receivedMotionData[0] + receivedMotionData[1] + receivedMotionData[2] == | |
175 number_active_device_motion_sensors_) { | |
176 device_motion_buffer_->seqlock.WriteBegin(); | |
177 device_motion_buffer_->data.allAvailableSensorsAreActive = true; | |
bulach
2013/07/04 08:23:36
just checking:
once it has all three datatypes, th
timvolodine
2013/07/04 12:02:52
This flag is actually intended for the cases where
| |
178 device_motion_buffer_->seqlock.WriteEnd(); | |
179 is_buffer_ready_ = true; | |
180 } | |
181 } | |
182 | |
183 bool DataFetcherImplAndroid::StartFetchingDeviceMotionData( | |
184 DeviceMotionHardwareBuffer* buffer) { | |
185 device_motion_buffer_ = buffer; | |
186 receivedMotionData[0] = receivedMotionData[1] = receivedMotionData[2] = 0; | |
bulach
2013/07/04 08:23:36
nit:
memset(received_motion_data, 0, sizeof(receiv
timvolodine
2013/07/04 12:02:52
Done.
| |
187 number_active_device_motion_sensors_ = 0; | |
188 is_buffer_ready_ = false; | |
189 bool success = Start(DeviceData::kTypeMotion, kPeriodInMilliseconds); | |
190 | |
191 // If no motion data can ever be provided, the number of active device motion | |
192 // sensors will be zero. In that case flag the shared memory buffer | |
193 // as ready to read, as it will not change anyway. | |
194 number_active_device_motion_sensors_ = GetNumberActiveDeviceMotionSensors(); | |
195 CheckBufferReadyToRead(); | |
196 return success; | |
197 } | |
198 | |
199 void DataFetcherImplAndroid::StopFetchingDeviceMotionData() { | |
200 Stop(DeviceData::kTypeMotion); | |
201 number_active_device_motion_sensors_ = 0; | |
202 receivedMotionData[0] = receivedMotionData[1] = receivedMotionData[2] = 0; | |
bulach
2013/07/04 08:23:36
nit: memset
timvolodine
2013/07/04 12:02:52
Done.
| |
203 is_buffer_ready_ = false; | |
204 } | |
205 | |
120 } // namespace content | 206 } // namespace content |
OLD | NEW |