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

Side by Side Diff: content/public/android/java/src/org/chromium/content/browser/DeviceMotionAndOrientation.java

Issue 15817019: Additions to the Android java-side Device Motion/Orientation fetching (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 package org.chromium.content.browser; 5 package org.chromium.content.browser;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 import android.hardware.Sensor; 8 import android.hardware.Sensor;
9 import android.hardware.SensorEvent; 9 import android.hardware.SensorEvent;
10 import android.hardware.SensorEventListener; 10 import android.hardware.SensorEventListener;
(...skipping 30 matching lines...) Expand all
41 private Object mHandlerLock = new Object(); 41 private Object mHandlerLock = new Object();
42 42
43 // Non-zero if and only if we're listening for events. 43 // Non-zero if and only if we're listening for events.
44 // To avoid race conditions on the C++ side, access must be synchronized. 44 // To avoid race conditions on the C++ side, access must be synchronized.
45 private int mNativePtr; 45 private int mNativePtr;
46 46
47 // The lock to access the mNativePtr. 47 // The lock to access the mNativePtr.
48 private Object mNativePtrLock = new Object(); 48 private Object mNativePtrLock = new Object();
49 49
50 // The acceleration vector including gravity expressed in the body frame. 50 // The acceleration vector including gravity expressed in the body frame.
51 private float[] mAccelerationVector; 51 private float[] mAccelerationIncludingGravityVector;;
bulach 2013/06/13 09:49:35 nit: double ;;
timvolodine 2013/06/13 14:00:40 Done.
52 52
53 // The geomagnetic vector expressed in the body frame. 53 // The geomagnetic vector expressed in the body frame.
54 private float[] mMagneticFieldVector; 54 private float[] mMagneticFieldVector;
55 55
56 // Lazily initialized when registering for notifications. 56 // Lazily initialized when registering for notifications.
57 private SensorManagerProxy mSensorManagerProxy; 57 private SensorManagerProxy mSensorManagerProxy;
58 58
59 // The only instance of that class and its associated lock. 59 // The only instance of that class and its associated lock.
60 private static DeviceMotionAndOrientation sSingleton; 60 private static DeviceMotionAndOrientation sSingleton;
61 private static Object sSingletonLock = new Object(); 61 private static Object sSingletonLock = new Object();
(...skipping 11 matching lines...) Expand all
73 73
74 static final ImmutableSet<Integer> DEVICE_MOTION_SENSORS = ImmutableSet.of( 74 static final ImmutableSet<Integer> DEVICE_MOTION_SENSORS = ImmutableSet.of(
75 Sensor.TYPE_ACCELEROMETER, 75 Sensor.TYPE_ACCELEROMETER,
76 Sensor.TYPE_LINEAR_ACCELERATION, 76 Sensor.TYPE_LINEAR_ACCELERATION,
77 Sensor.TYPE_GYROSCOPE); 77 Sensor.TYPE_GYROSCOPE);
78 78
79 @VisibleForTesting 79 @VisibleForTesting
80 final Set<Integer> mActiveSensors = Sets.newHashSet(); 80 final Set<Integer> mActiveSensors = Sets.newHashSet();
81 boolean mDeviceMotionIsActive = false; 81 boolean mDeviceMotionIsActive = false;
82 boolean mDeviceOrientationIsActive = false; 82 boolean mDeviceOrientationIsActive = false;
83 int mNumberActiveDeviceMotionSensors = 0;
bulach 2013/06/13 09:49:35 nit: seems unused
timvolodine 2013/06/13 14:00:40 Done.
83 84
84 protected DeviceMotionAndOrientation() { 85 protected DeviceMotionAndOrientation() {
85 } 86 }
86 87
87 /** 88 /**
88 * Start listening for sensor events. If this object is already listening 89 * Start listening for sensor events. If this object is already listening
89 * for events, the old callback is unregistered first. 90 * for events, the old callback is unregistered first.
90 * 91 *
91 * @param nativePtr Value to pass to nativeGotOrientation() for each event. 92 * @param nativePtr Value to pass to nativeGotOrientation() for each event.
92 * @param rateInMilliseconds Requested callback rate in milliseconds. The 93 * @param rateInMilliseconds Requested callback rate in milliseconds. The
(...skipping 20 matching lines...) Expand all
113 return false; 114 return false;
114 } 115 }
115 if (success) { 116 if (success) {
116 mNativePtr = nativePtr; 117 mNativePtr = nativePtr;
117 setEventTypeActive(eventType, true); 118 setEventTypeActive(eventType, true);
118 } 119 }
119 return success; 120 return success;
120 } 121 }
121 } 122 }
122 123
124 @CalledByNative
125 public int getNumberActiveDeviceMotionSensors() {
126 Set<Integer> deviceMotionSensors = Sets.newHashSet(DEVICE_MOTION_SENSORS );
127 deviceMotionSensors.removeAll(mActiveSensors);
128 return DEVICE_MOTION_SENSORS.size() - deviceMotionSensors.size();
129 }
130
123 /** 131 /**
124 * Stop listening to sensors for a given event type. Ensures that sensors ar e not disabled 132 * Stop listening to sensors for a given event type. Ensures that sensors ar e not disabled
125 * if they are still in use by a different event type. 133 * if they are still in use by a different event type.
126 * 134 *
127 * @param eventType Type of event to listen to, can be either DEVICE_ORIENTA TION or 135 * @param eventType Type of event to listen to, can be either DEVICE_ORIENTA TION or
128 * DEVICE_MOTION. 136 * DEVICE_MOTION.
129 * We strictly guarantee that the corresponding native*() methods will not b e called 137 * We strictly guarantee that the corresponding native*() methods will not b e called
130 * after this method returns. 138 * after this method returns.
131 */ 139 */
132 @CalledByNative 140 @CalledByNative
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 @Override 175 @Override
168 public void onSensorChanged(SensorEvent event) { 176 public void onSensorChanged(SensorEvent event) {
169 sensorChanged(event.sensor.getType(), event.values); 177 sensorChanged(event.sensor.getType(), event.values);
170 } 178 }
171 179
172 @VisibleForTesting 180 @VisibleForTesting
173 void sensorChanged(int type, float[] values) { 181 void sensorChanged(int type, float[] values) {
174 182
175 switch (type) { 183 switch (type) {
176 case Sensor.TYPE_ACCELEROMETER: 184 case Sensor.TYPE_ACCELEROMETER:
177 if (mAccelerationVector == null) { 185 if (mAccelerationIncludingGravityVector == null) {
178 mAccelerationVector = new float[3]; 186 mAccelerationIncludingGravityVector = new float[3];
179 } 187 }
180 System.arraycopy(values, 0, mAccelerationVector, 0, 188 System.arraycopy(values, 0, mAccelerationIncludingGravityVector,
181 mAccelerationVector.length); 189 0, mAccelerationIncludingGravityVector.length);
182 if (mDeviceMotionIsActive) { 190 if (mDeviceMotionIsActive) {
183 gotAccelerationIncludingGravity(mAccelerationVector[0], mAcc elerationVector[1], 191 gotAccelerationIncludingGravity(
184 mAccelerationVector[2]); 192 mAccelerationIncludingGravityVector[0],
193 mAccelerationIncludingGravityVector[1],
194 mAccelerationIncludingGravityVector[2]);
195 }
196 if (mDeviceOrientationIsActive) {
197 getOrientationUsingGetRotationMatrix();
185 } 198 }
186 break; 199 break;
187 case Sensor.TYPE_LINEAR_ACCELERATION: 200 case Sensor.TYPE_LINEAR_ACCELERATION:
188 if (mDeviceMotionIsActive) { 201 if (mDeviceMotionIsActive) {
189 gotAcceleration(values[0], values[1], values[2]); 202 gotAcceleration(values[0], values[1], values[2]);
190 } 203 }
191 break; 204 break;
192 case Sensor.TYPE_GYROSCOPE: 205 case Sensor.TYPE_GYROSCOPE:
193 if (mDeviceMotionIsActive) { 206 if (mDeviceMotionIsActive) {
194 gotRotationRate(values[0], values[1], values[2]); 207 gotRotationRate(values[0], values[1], values[2]);
195 } 208 }
196 break; 209 break;
197 case Sensor.TYPE_MAGNETIC_FIELD: 210 case Sensor.TYPE_MAGNETIC_FIELD:
198 if (mMagneticFieldVector == null) { 211 if (mMagneticFieldVector == null) {
199 mMagneticFieldVector = new float[3]; 212 mMagneticFieldVector = new float[3];
200 } 213 }
201 System.arraycopy(values, 0, mMagneticFieldVector, 0, 214 System.arraycopy(values, 0, mMagneticFieldVector, 0,
202 mMagneticFieldVector.length); 215 mMagneticFieldVector.length);
216 if (mDeviceOrientationIsActive) {
217 getOrientationUsingGetRotationMatrix();
218 }
203 break; 219 break;
204 default: 220 default:
205 // Unexpected 221 // Unexpected
206 return; 222 return;
207 } 223 }
208
209 if (mDeviceOrientationIsActive) {
210 getOrientationUsingGetRotationMatrix();
211 }
212 } 224 }
213 225
214 private void getOrientationUsingGetRotationMatrix() { 226 private void getOrientationUsingGetRotationMatrix() {
215 if (mAccelerationVector == null || mMagneticFieldVector == null) { 227 if (mAccelerationIncludingGravityVector == null || mMagneticFieldVector == null) {
216 return; 228 return;
217 } 229 }
218 230
219 // Get the rotation matrix. 231 // Get the rotation matrix.
220 // The rotation matrix that transforms from the body frame to the earth 232 // The rotation matrix that transforms from the body frame to the earth
221 // frame. 233 // frame.
222 float[] deviceRotationMatrix = new float[9]; 234 float[] deviceRotationMatrix = new float[9];
223 if (!SensorManager.getRotationMatrix(deviceRotationMatrix, null, mAccele rationVector, 235 if (!SensorManager.getRotationMatrix(deviceRotationMatrix, null,
224 mMagneticFieldVector)) { 236 mAccelerationIncludingGravityVector, mMagneticFieldVector)) {
225 return; 237 return;
226 } 238 }
227 239
228 // Convert rotation matrix to rotation angles. 240 // Convert rotation matrix to rotation angles.
229 // Assuming that the rotations are appied in the order listed at 241 // Assuming that the rotations are appied in the order listed at
230 // http://developer.android.com/reference/android/hardware/SensorEvent.h tml#values 242 // http://developer.android.com/reference/android/hardware/SensorEvent.h tml#values
231 // the rotations are applied about the same axes and in the same order a s required by the 243 // the rotations are applied about the same axes and in the same order a s required by the
232 // API. The only conversions are sign changes as follows. The angles ar e in radians 244 // API. The only conversions are sign changes as follows. The angles ar e in radians
233 245
234 float[] rotationAngles = new float[3]; 246 float[] rotationAngles = new float[3];
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 454
443 public void unregisterListener(SensorEventListener listener, int sensorT ype) { 455 public void unregisterListener(SensorEventListener listener, int sensorT ype) {
444 List<Sensor> sensors = mSensorManager.getSensorList(sensorType); 456 List<Sensor> sensors = mSensorManager.getSensorList(sensorType);
445 if (!sensors.isEmpty()) { 457 if (!sensors.isEmpty()) {
446 mSensorManager.unregisterListener(listener, sensors.get(0)); 458 mSensorManager.unregisterListener(listener, sensors.get(0));
447 } 459 }
448 } 460 }
449 } 461 }
450 462
451 } 463 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698