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 "content/common/device_motion_hardware_buffer.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_(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 // TODO(timvolodine): Modify this method to be able to distinguish |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 base::AutoLock autolock(next_orientation_lock_); | 76 base::AutoLock autolock(next_orientation_lock_); |
74 | 77 |
75 Orientation* orientation = new Orientation(); | 78 Orientation* orientation = new Orientation(); |
76 orientation->set_alpha(alpha); | 79 orientation->set_alpha(alpha); |
77 orientation->set_beta(beta); | 80 orientation->set_beta(beta); |
78 orientation->set_gamma(gamma); | 81 orientation->set_gamma(gamma); |
79 orientation->set_absolute(true); | 82 orientation->set_absolute(true); |
80 next_orientation_ = orientation; | 83 next_orientation_ = orientation; |
81 } | 84 } |
82 | 85 |
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( | 86 bool DataFetcherImplAndroid::Start( |
99 DeviceData::Type event_type, int rate_in_milliseconds) { | 87 DeviceData::Type event_type, int rate_in_milliseconds) { |
100 DCHECK(!device_orientation_.is_null()); | 88 DCHECK(!device_orientation_.is_null()); |
101 return Java_DeviceMotionAndOrientation_start( | 89 return Java_DeviceMotionAndOrientation_start( |
102 AttachCurrentThread(), device_orientation_.obj(), | 90 AttachCurrentThread(), device_orientation_.obj(), |
103 reinterpret_cast<jint>(this), static_cast<jint>(event_type), | 91 reinterpret_cast<jint>(this), static_cast<jint>(event_type), |
104 rate_in_milliseconds); | 92 rate_in_milliseconds); |
105 } | 93 } |
106 | 94 |
107 void DataFetcherImplAndroid::Stop(DeviceData::Type event_type) { | 95 void DataFetcherImplAndroid::Stop(DeviceData::Type event_type) { |
108 DCHECK(!device_orientation_.is_null()); | 96 DCHECK(!device_orientation_.is_null()); |
109 Java_DeviceMotionAndOrientation_stop( | 97 Java_DeviceMotionAndOrientation_stop( |
110 AttachCurrentThread(), device_orientation_.obj(), | 98 AttachCurrentThread(), device_orientation_.obj(), |
111 static_cast<jint>(event_type)); | 99 static_cast<jint>(event_type)); |
112 } | 100 } |
113 | 101 |
| 102 // ----- New shared memory API methods |
| 103 |
| 104 void DataFetcherImplAndroid::GotAcceleration( |
| 105 JNIEnv*, jobject, double x, double y, double z) { |
| 106 device_motion_buffer_->sequence.WriteBegin(); |
| 107 device_motion_buffer_->buffer.accelerationX = x; |
| 108 device_motion_buffer_->buffer.hasAccelerationX = true; |
| 109 device_motion_buffer_->buffer.accelerationY = y; |
| 110 device_motion_buffer_->buffer.hasAccelerationY = true; |
| 111 device_motion_buffer_->buffer.accelerationZ = z; |
| 112 device_motion_buffer_->buffer.hasAccelerationZ = true; |
| 113 device_motion_buffer_->sequence.WriteEnd(); |
| 114 LOG(INFO) << "ACTIVE GotAcceleration" << x << "," << y << "," << z; |
| 115 |
| 116 if (!is_buffer_ready_) { |
| 117 receivedMotionData[0] = 1; |
| 118 CheckBufferReadyToRead(); |
| 119 } |
| 120 } |
| 121 |
| 122 void DataFetcherImplAndroid::GotAccelerationIncludingGravity( |
| 123 JNIEnv*, jobject, double x, double y, double z) { |
| 124 device_motion_buffer_->sequence.WriteBegin(); |
| 125 device_motion_buffer_->buffer.accelerationIncludingGravityX = x; |
| 126 device_motion_buffer_->buffer.hasAccelerationIncludingGravityX = true; |
| 127 device_motion_buffer_->buffer.accelerationIncludingGravityY = y; |
| 128 device_motion_buffer_->buffer.hasAccelerationIncludingGravityY = true; |
| 129 device_motion_buffer_->buffer.accelerationIncludingGravityZ = z; |
| 130 device_motion_buffer_->buffer.hasAccelerationIncludingGravityZ = true; |
| 131 device_motion_buffer_->sequence.WriteEnd(); |
| 132 |
| 133 if (!is_buffer_ready_) { |
| 134 receivedMotionData[1] = 1; |
| 135 CheckBufferReadyToRead(); |
| 136 } |
| 137 } |
| 138 |
| 139 void DataFetcherImplAndroid::GotRotationRate( |
| 140 JNIEnv*, jobject, double alpha, double beta, double gamma) { |
| 141 device_motion_buffer_->sequence.WriteBegin(); |
| 142 device_motion_buffer_->buffer.rotationRateAlpha = alpha; |
| 143 device_motion_buffer_->buffer.hasRotationRateAlpha = true; |
| 144 device_motion_buffer_->buffer.rotationRateBeta = beta; |
| 145 device_motion_buffer_->buffer.hasRotationRateBeta = true; |
| 146 device_motion_buffer_->buffer.rotationRateGamma = gamma; |
| 147 device_motion_buffer_->buffer.hasRotationRateGamma = true; |
| 148 device_motion_buffer_->sequence.WriteEnd(); |
| 149 |
| 150 if (!is_buffer_ready_) { |
| 151 receivedMotionData[2] = 1; |
| 152 CheckBufferReadyToRead(); |
| 153 } |
| 154 } |
| 155 |
| 156 int DataFetcherImplAndroid::GetNumberActiveDeviceMotionSensors() { |
| 157 DCHECK(!device_orientation_.is_null()); |
| 158 return Java_DeviceMotionAndOrientation_getNumberActiveDeviceMotionSensors( |
| 159 AttachCurrentThread(), device_orientation_.obj()); |
| 160 } |
| 161 |
| 162 bool DataFetcherImplAndroid::FetchDeviceMotionDataIntoBuffer() { |
| 163 // This method should not be called because it is a push based fetcher. |
| 164 DCHECK(needsPolling()); |
| 165 return false; |
| 166 } |
| 167 |
| 168 void DataFetcherImplAndroid::CheckBufferReadyToRead() { |
| 169 if (receivedMotionData[0] + receivedMotionData[1] + receivedMotionData[2] == |
| 170 number_active_device_motion_sensors_) { |
| 171 device_motion_buffer_->sequence.WriteBegin(); |
| 172 device_motion_buffer_->is_ready_for_read = true; |
| 173 device_motion_buffer_->sequence.WriteEnd(); |
| 174 is_buffer_ready_ = true; |
| 175 LOG(INFO)<<"ACTIVE BUFFER READY TO READ"; |
| 176 } |
| 177 } |
| 178 |
| 179 bool DataFetcherImplAndroid::StartFetchingDeviceMotionData( |
| 180 DeviceMotionHardwareBuffer* buffer) { |
| 181 device_motion_buffer_ = buffer; |
| 182 receivedMotionData[0] = receivedMotionData[1] = receivedMotionData[2] = 0; |
| 183 number_active_device_motion_sensors_ = 0; |
| 184 is_buffer_ready_ = false; |
| 185 bool success = Start(DeviceData::kTypeMotion, kPeriodInMilliseconds); |
| 186 |
| 187 // If no motion data can ever be provided, the number of active device motion |
| 188 // sensors will be zero. In that case flag the shared memory buffer |
| 189 // as ready to read, as it will not change anyway. |
| 190 number_active_device_motion_sensors_ = GetNumberActiveDeviceMotionSensors(); |
| 191 LOG(INFO)<< "ACTIVE SENSORS :"<<number_active_device_motion_sensors_; |
| 192 CheckBufferReadyToRead(); |
| 193 return success; |
| 194 } |
| 195 |
| 196 void DataFetcherImplAndroid::StopFetchingDeviceMotionData() { |
| 197 Stop(DeviceData::kTypeMotion); |
| 198 number_active_device_motion_sensors_ = 0; |
| 199 receivedMotionData[0] = receivedMotionData[1] = receivedMotionData[2] = 0; |
| 200 is_buffer_ready_ = false; |
| 201 } |
| 202 |
114 } // namespace content | 203 } // namespace content |
OLD | NEW |