| OLD | NEW |
| 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 Loading... |
| 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; |
| 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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 return false; | 113 return false; |
| 114 } | 114 } |
| 115 if (success) { | 115 if (success) { |
| 116 mNativePtr = nativePtr; | 116 mNativePtr = nativePtr; |
| 117 setEventTypeActive(eventType, true); | 117 setEventTypeActive(eventType, true); |
| 118 } | 118 } |
| 119 return success; | 119 return success; |
| 120 } | 120 } |
| 121 } | 121 } |
| 122 | 122 |
| 123 @CalledByNative |
| 124 public int getNumberActiveDeviceMotionSensors() { |
| 125 Set<Integer> deviceMotionSensors = Sets.newHashSet(DEVICE_MOTION_SENSORS
); |
| 126 deviceMotionSensors.removeAll(mActiveSensors); |
| 127 return DEVICE_MOTION_SENSORS.size() - deviceMotionSensors.size(); |
| 128 } |
| 129 |
| 123 /** | 130 /** |
| 124 * Stop listening to sensors for a given event type. Ensures that sensors ar
e not disabled | 131 * 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. | 132 * if they are still in use by a different event type. |
| 126 * | 133 * |
| 127 * @param eventType Type of event to listen to, can be either DEVICE_ORIENTA
TION or | 134 * @param eventType Type of event to listen to, can be either DEVICE_ORIENTA
TION or |
| 128 * DEVICE_MOTION. | 135 * DEVICE_MOTION. |
| 129 * We strictly guarantee that the corresponding native*() methods will not b
e called | 136 * We strictly guarantee that the corresponding native*() methods will not b
e called |
| 130 * after this method returns. | 137 * after this method returns. |
| 131 */ | 138 */ |
| 132 @CalledByNative | 139 @CalledByNative |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 @Override | 174 @Override |
| 168 public void onSensorChanged(SensorEvent event) { | 175 public void onSensorChanged(SensorEvent event) { |
| 169 sensorChanged(event.sensor.getType(), event.values); | 176 sensorChanged(event.sensor.getType(), event.values); |
| 170 } | 177 } |
| 171 | 178 |
| 172 @VisibleForTesting | 179 @VisibleForTesting |
| 173 void sensorChanged(int type, float[] values) { | 180 void sensorChanged(int type, float[] values) { |
| 174 | 181 |
| 175 switch (type) { | 182 switch (type) { |
| 176 case Sensor.TYPE_ACCELEROMETER: | 183 case Sensor.TYPE_ACCELEROMETER: |
| 177 if (mAccelerationVector == null) { | 184 if (mAccelerationIncludingGravityVector == null) { |
| 178 mAccelerationVector = new float[3]; | 185 mAccelerationIncludingGravityVector = new float[3]; |
| 179 } | 186 } |
| 180 System.arraycopy(values, 0, mAccelerationVector, 0, | 187 System.arraycopy(values, 0, mAccelerationIncludingGravityVector, |
| 181 mAccelerationVector.length); | 188 0, mAccelerationIncludingGravityVector.length); |
| 182 if (mDeviceMotionIsActive) { | 189 if (mDeviceMotionIsActive) { |
| 183 gotAccelerationIncludingGravity(mAccelerationVector[0], mAcc
elerationVector[1], | 190 gotAccelerationIncludingGravity( |
| 184 mAccelerationVector[2]); | 191 mAccelerationIncludingGravityVector[0], |
| 192 mAccelerationIncludingGravityVector[1], |
| 193 mAccelerationIncludingGravityVector[2]); |
| 194 } |
| 195 if (mDeviceOrientationIsActive) { |
| 196 getOrientationUsingGetRotationMatrix(); |
| 185 } | 197 } |
| 186 break; | 198 break; |
| 187 case Sensor.TYPE_LINEAR_ACCELERATION: | 199 case Sensor.TYPE_LINEAR_ACCELERATION: |
| 188 if (mDeviceMotionIsActive) { | 200 if (mDeviceMotionIsActive) { |
| 189 gotAcceleration(values[0], values[1], values[2]); | 201 gotAcceleration(values[0], values[1], values[2]); |
| 190 } | 202 } |
| 191 break; | 203 break; |
| 192 case Sensor.TYPE_GYROSCOPE: | 204 case Sensor.TYPE_GYROSCOPE: |
| 193 if (mDeviceMotionIsActive) { | 205 if (mDeviceMotionIsActive) { |
| 194 gotRotationRate(values[0], values[1], values[2]); | 206 gotRotationRate(values[0], values[1], values[2]); |
| 195 } | 207 } |
| 196 break; | 208 break; |
| 197 case Sensor.TYPE_MAGNETIC_FIELD: | 209 case Sensor.TYPE_MAGNETIC_FIELD: |
| 198 if (mMagneticFieldVector == null) { | 210 if (mMagneticFieldVector == null) { |
| 199 mMagneticFieldVector = new float[3]; | 211 mMagneticFieldVector = new float[3]; |
| 200 } | 212 } |
| 201 System.arraycopy(values, 0, mMagneticFieldVector, 0, | 213 System.arraycopy(values, 0, mMagneticFieldVector, 0, |
| 202 mMagneticFieldVector.length); | 214 mMagneticFieldVector.length); |
| 215 if (mDeviceOrientationIsActive) { |
| 216 getOrientationUsingGetRotationMatrix(); |
| 217 } |
| 203 break; | 218 break; |
| 204 default: | 219 default: |
| 205 // Unexpected | 220 // Unexpected |
| 206 return; | 221 return; |
| 207 } | 222 } |
| 208 | |
| 209 if (mDeviceOrientationIsActive) { | |
| 210 getOrientationUsingGetRotationMatrix(); | |
| 211 } | |
| 212 } | 223 } |
| 213 | 224 |
| 214 private void getOrientationUsingGetRotationMatrix() { | 225 private void getOrientationUsingGetRotationMatrix() { |
| 215 if (mAccelerationVector == null || mMagneticFieldVector == null) { | 226 if (mAccelerationIncludingGravityVector == null || mMagneticFieldVector
== null) { |
| 216 return; | 227 return; |
| 217 } | 228 } |
| 218 | 229 |
| 219 // Get the rotation matrix. | 230 // Get the rotation matrix. |
| 220 // The rotation matrix that transforms from the body frame to the earth | 231 // The rotation matrix that transforms from the body frame to the earth |
| 221 // frame. | 232 // frame. |
| 222 float[] deviceRotationMatrix = new float[9]; | 233 float[] deviceRotationMatrix = new float[9]; |
| 223 if (!SensorManager.getRotationMatrix(deviceRotationMatrix, null, mAccele
rationVector, | 234 if (!SensorManager.getRotationMatrix(deviceRotationMatrix, null, |
| 224 mMagneticFieldVector)) { | 235 mAccelerationIncludingGravityVector, mMagneticFieldVector)) { |
| 225 return; | 236 return; |
| 226 } | 237 } |
| 227 | 238 |
| 228 // Convert rotation matrix to rotation angles. | 239 // Convert rotation matrix to rotation angles. |
| 229 // Assuming that the rotations are appied in the order listed at | 240 // Assuming that the rotations are appied in the order listed at |
| 230 // http://developer.android.com/reference/android/hardware/SensorEvent.h
tml#values | 241 // 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 | 242 // 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 | 243 // API. The only conversions are sign changes as follows. The angles ar
e in radians |
| 233 | 244 |
| 234 float[] rotationAngles = new float[3]; | 245 float[] rotationAngles = new float[3]; |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 | 453 |
| 443 public void unregisterListener(SensorEventListener listener, int sensorT
ype) { | 454 public void unregisterListener(SensorEventListener listener, int sensorT
ype) { |
| 444 List<Sensor> sensors = mSensorManager.getSensorList(sensorType); | 455 List<Sensor> sensors = mSensorManager.getSensorList(sensorType); |
| 445 if (!sensors.isEmpty()) { | 456 if (!sensors.isEmpty()) { |
| 446 mSensorManager.unregisterListener(listener, sensors.get(0)); | 457 mSensorManager.unregisterListener(listener, sensors.get(0)); |
| 447 } | 458 } |
| 448 } | 459 } |
| 449 } | 460 } |
| 450 | 461 |
| 451 } | 462 } |
| OLD | NEW |