Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(697)

Side by Side Diff: content/browser/device_orientation/data_fetcher_impl_android.cc

Issue 14678012: Implement the content/renderer and content/browser part of the Device Motion API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: factored out as much code as possible from the templated reader Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
25 DataFetcherImplAndroid::DataFetcherImplAndroid()
26 : device_motion_buffer_(0),
27 is_buffer_ready_(false) {
25 device_orientation_.Reset( 28 device_orientation_.Reset(
26 Java_DeviceMotionAndOrientation_getInstance(AttachCurrentThread())); 29 Java_DeviceMotionAndOrientation_getInstance(AttachCurrentThread()));
27 } 30 }
28 31
29 void DataFetcherImplAndroid::Init(JNIEnv* env) { 32 void DataFetcherImplAndroid::Init(JNIEnv* env) {
30 bool result = RegisterNativesImpl(env); 33 bool result = RegisterNativesImpl(env);
31 DCHECK(result); 34 DCHECK(result);
32 } 35 }
33 36
34 // TODO(timvolodine): Modify this method to be able to distinguish 37 DataFetcherImplAndroid* DataFetcherImplAndroid::instance() {
35 // device motion from orientation. 38 CR_DEFINE_STATIC_LOCAL(DataFetcherImplAndroid, s_data_fetcher, ());
39 return &s_data_fetcher;
40 }
41
42 // TODO(timvolodine): Remove this method once orientation switches to shared
43 // memory implementation..
36 DataFetcher* DataFetcherImplAndroid::Create() { 44 DataFetcher* DataFetcherImplAndroid::Create() {
37 scoped_ptr<DataFetcherImplAndroid> fetcher(new DataFetcherImplAndroid); 45 DataFetcherImplAndroid* fetcher = DataFetcherImplAndroid::instance();
38 if (fetcher->Start(DeviceData::kTypeOrientation, kPeriodInMilliseconds)) 46 if (fetcher->Start(DeviceData::kTypeOrientation, kPeriodInMilliseconds))
39 return fetcher.release(); 47 return fetcher;
40 48
41 LOG(ERROR) << "DataFetcherImplAndroid::Start failed!"; 49 DVLOG(2) << "DataFetcherImplAndroid::Start failed!";
42 return NULL; 50 return NULL;
43 } 51 }
44 52
45 DataFetcherImplAndroid::~DataFetcherImplAndroid() { 53 DataFetcherImplAndroid::~DataFetcherImplAndroid() {
46 // TODO(timvolodine): Support device motion as well. Only stop 54 // TODO(timvolodine): Support device motion as well. Only stop
47 // the active event type(s). 55 // the active event type(s).
48 Stop(DeviceData::kTypeOrientation); 56 Stop(DeviceData::kTypeOrientation);
57 Stop(DeviceData::kTypeMotion);
49 } 58 }
50 59
51 const DeviceData* DataFetcherImplAndroid::GetDeviceData( 60 const DeviceData* DataFetcherImplAndroid::GetDeviceData(
52 DeviceData::Type type) { 61 DeviceData::Type type) {
53 if (type != DeviceData::kTypeOrientation) 62 if (type != DeviceData::kTypeOrientation)
54 return NULL; 63 return NULL;
55 return GetOrientation(); 64 return GetOrientation();
56 } 65 }
57 66
58 const Orientation* DataFetcherImplAndroid::GetOrientation() { 67 const Orientation* DataFetcherImplAndroid::GetOrientation() {
(...skipping 14 matching lines...) Expand all
73 base::AutoLock autolock(next_orientation_lock_); 82 base::AutoLock autolock(next_orientation_lock_);
74 83
75 Orientation* orientation = new Orientation(); 84 Orientation* orientation = new Orientation();
76 orientation->set_alpha(alpha); 85 orientation->set_alpha(alpha);
77 orientation->set_beta(beta); 86 orientation->set_beta(beta);
78 orientation->set_gamma(gamma); 87 orientation->set_gamma(gamma);
79 orientation->set_absolute(true); 88 orientation->set_absolute(true);
80 next_orientation_ = orientation; 89 next_orientation_ = orientation;
81 } 90 }
82 91
83 void DataFetcherImplAndroid::GotAcceleration(
84 JNIEnv*, jobject, double x, double y, double z) {
85 NOTIMPLEMENTED();
86 }
87
88 void DataFetcherImplAndroid::GotAccelerationIncludingGravity(
89 JNIEnv*, jobject, double x, double y, double z) {
90 NOTIMPLEMENTED();
91 }
92
93 void DataFetcherImplAndroid::GotRotationRate(
94 JNIEnv*, jobject, double alpha, double beta, double gamma) {
95 NOTIMPLEMENTED();
96 }
97
98 bool DataFetcherImplAndroid::Start( 92 bool DataFetcherImplAndroid::Start(
99 DeviceData::Type event_type, int rate_in_milliseconds) { 93 DeviceData::Type event_type, int rate_in_milliseconds) {
100 DCHECK(!device_orientation_.is_null()); 94 DCHECK(!device_orientation_.is_null());
101 return Java_DeviceMotionAndOrientation_start( 95 return Java_DeviceMotionAndOrientation_start(
102 AttachCurrentThread(), device_orientation_.obj(), 96 AttachCurrentThread(), device_orientation_.obj(),
103 reinterpret_cast<jint>(this), static_cast<jint>(event_type), 97 reinterpret_cast<jint>(this), static_cast<jint>(event_type),
104 rate_in_milliseconds); 98 rate_in_milliseconds);
105 } 99 }
106 100
107 void DataFetcherImplAndroid::Stop(DeviceData::Type event_type) { 101 void DataFetcherImplAndroid::Stop(DeviceData::Type event_type) {
108 DCHECK(!device_orientation_.is_null()); 102 DCHECK(!device_orientation_.is_null());
109 Java_DeviceMotionAndOrientation_stop( 103 Java_DeviceMotionAndOrientation_stop(
110 AttachCurrentThread(), device_orientation_.obj(), 104 AttachCurrentThread(), device_orientation_.obj(),
111 static_cast<jint>(event_type)); 105 static_cast<jint>(event_type));
112 } 106 }
113 107
108 // ----- New shared memory API methods
109
110 void DataFetcherImplAndroid::GotAcceleration(
111 JNIEnv*, jobject, double x, double y, double z) {
112 device_motion_buffer_->seqlock.WriteBegin();
113 device_motion_buffer_->data.accelerationX = x;
114 device_motion_buffer_->data.hasAccelerationX = true;
115 device_motion_buffer_->data.accelerationY = y;
116 device_motion_buffer_->data.hasAccelerationY = true;
117 device_motion_buffer_->data.accelerationZ = z;
118 device_motion_buffer_->data.hasAccelerationZ = true;
119 device_motion_buffer_->seqlock.WriteEnd();
120
121 if (!is_buffer_ready_) {
122 receivedMotionData[0] = 1;
123 CheckBufferReadyToRead();
124 }
125 }
126
127 void DataFetcherImplAndroid::GotAccelerationIncludingGravity(
128 JNIEnv*, jobject, double x, double y, double z) {
129 device_motion_buffer_->seqlock.WriteBegin();
130 device_motion_buffer_->data.accelerationIncludingGravityX = x;
131 device_motion_buffer_->data.hasAccelerationIncludingGravityX = true;
132 device_motion_buffer_->data.accelerationIncludingGravityY = y;
133 device_motion_buffer_->data.hasAccelerationIncludingGravityY = true;
134 device_motion_buffer_->data.accelerationIncludingGravityZ = z;
135 device_motion_buffer_->data.hasAccelerationIncludingGravityZ = true;
136 device_motion_buffer_->seqlock.WriteEnd();
137
138 if (!is_buffer_ready_) {
139 receivedMotionData[1] = 1;
140 CheckBufferReadyToRead();
141 }
142 }
143
144 void DataFetcherImplAndroid::GotRotationRate(
145 JNIEnv*, jobject, double alpha, double beta, double gamma) {
146 device_motion_buffer_->seqlock.WriteBegin();
147 device_motion_buffer_->data.rotationRateAlpha = alpha;
148 device_motion_buffer_->data.hasRotationRateAlpha = true;
149 device_motion_buffer_->data.rotationRateBeta = beta;
150 device_motion_buffer_->data.hasRotationRateBeta = true;
151 device_motion_buffer_->data.rotationRateGamma = gamma;
152 device_motion_buffer_->data.hasRotationRateGamma = true;
153 device_motion_buffer_->seqlock.WriteEnd();
154
155 if (!is_buffer_ready_) {
156 receivedMotionData[2] = 1;
157 CheckBufferReadyToRead();
158 }
159 }
160
114 int DataFetcherImplAndroid::GetNumberActiveDeviceMotionSensors() { 161 int DataFetcherImplAndroid::GetNumberActiveDeviceMotionSensors() {
115 DCHECK(!device_orientation_.is_null()); 162 DCHECK(!device_orientation_.is_null());
116 return Java_DeviceMotionAndOrientation_getNumberActiveDeviceMotionSensors( 163 return Java_DeviceMotionAndOrientation_getNumberActiveDeviceMotionSensors(
117 AttachCurrentThread(), device_orientation_.obj()); 164 AttachCurrentThread(), device_orientation_.obj());
118 } 165 }
119 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_->is_ready_for_read = true;
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;
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;
203 is_buffer_ready_ = false;
204 }
205
120 } // namespace content 206 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698