OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_ | 5 #ifndef DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_ |
6 #define DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_ | 6 #define DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/android/scoped_java_ref.h" | 10 #include "base/android/scoped_java_ref.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
13 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
14 #include "device/sensors/device_sensor_export.h" | 14 #include "device/sensors/device_sensor_export.h" |
15 #include "device/sensors/device_sensors_consts.h" | 15 #include "device/sensors/device_sensors_consts.h" |
16 #include "device/sensors/public/cpp/device_light_hardware_buffer.h" | |
17 #include "device/sensors/public/cpp/device_motion_hardware_buffer.h" | 16 #include "device/sensors/public/cpp/device_motion_hardware_buffer.h" |
18 #include "device/sensors/public/cpp/device_orientation_hardware_buffer.h" | 17 #include "device/sensors/public/cpp/device_orientation_hardware_buffer.h" |
19 | 18 |
20 namespace base { | 19 namespace base { |
21 template <typename T> | 20 template <typename T> |
22 struct DefaultSingletonTraits; | 21 struct DefaultSingletonTraits; |
23 } | 22 } |
24 | 23 |
25 namespace device { | 24 namespace device { |
26 | 25 |
27 // Android implementation of Device {Motion|Orientation|Light} API. | 26 // Android implementation of Device {Motion|Orientation} API. |
28 // | 27 // |
29 // Android's SensorManager has a push API, so when Got*() methods are called | 28 // Android's SensorManager has a push API, so when Got*() methods are called |
30 // by the system the browser process puts the received data into a shared | 29 // by the system the browser process puts the received data into a shared |
31 // memory buffer, which is read by the renderer processes. | 30 // memory buffer, which is read by the renderer processes. |
32 class DEVICE_SENSOR_EXPORT SensorManagerAndroid { | 31 class DEVICE_SENSOR_EXPORT SensorManagerAndroid { |
33 public: | 32 public: |
34 // Must be called at startup, before GetInstance(). | 33 // Must be called at startup, before GetInstance(). |
35 static bool Register(JNIEnv* env); | 34 static bool Register(JNIEnv* env); |
36 | 35 |
37 // Should be called only on the UI thread. | 36 // Should be called only on the UI thread. |
38 static SensorManagerAndroid* GetInstance(); | 37 static SensorManagerAndroid* GetInstance(); |
39 | 38 |
40 // Called from Java via JNI. | 39 // Called from Java via JNI. |
41 void GotLight(JNIEnv*, | |
42 const base::android::JavaParamRef<jobject>&, | |
43 double value); | |
44 void GotOrientation(JNIEnv*, | 40 void GotOrientation(JNIEnv*, |
45 const base::android::JavaParamRef<jobject>&, | 41 const base::android::JavaParamRef<jobject>&, |
46 double alpha, | 42 double alpha, |
47 double beta, | 43 double beta, |
48 double gamma); | 44 double gamma); |
49 void GotOrientationAbsolute(JNIEnv*, | 45 void GotOrientationAbsolute(JNIEnv*, |
50 const base::android::JavaParamRef<jobject>&, | 46 const base::android::JavaParamRef<jobject>&, |
51 double alpha, | 47 double alpha, |
52 double beta, | 48 double beta, |
53 double gamma); | 49 double gamma); |
54 void GotAcceleration(JNIEnv*, | 50 void GotAcceleration(JNIEnv*, |
55 const base::android::JavaParamRef<jobject>&, | 51 const base::android::JavaParamRef<jobject>&, |
56 double x, | 52 double x, |
57 double y, | 53 double y, |
58 double z); | 54 double z); |
59 void GotAccelerationIncludingGravity( | 55 void GotAccelerationIncludingGravity( |
60 JNIEnv*, | 56 JNIEnv*, |
61 const base::android::JavaParamRef<jobject>&, | 57 const base::android::JavaParamRef<jobject>&, |
62 double x, | 58 double x, |
63 double y, | 59 double y, |
64 double z); | 60 double z); |
65 void GotRotationRate(JNIEnv*, | 61 void GotRotationRate(JNIEnv*, |
66 const base::android::JavaParamRef<jobject>&, | 62 const base::android::JavaParamRef<jobject>&, |
67 double alpha, | 63 double alpha, |
68 double beta, | 64 double beta, |
69 double gamma); | 65 double gamma); |
70 | 66 |
71 // Shared memory related methods. | 67 // Shared memory related methods. |
72 void StartFetchingDeviceLightData(DeviceLightHardwareBuffer* buffer); | |
73 void StopFetchingDeviceLightData(); | |
74 | |
75 void StartFetchingDeviceMotionData(DeviceMotionHardwareBuffer* buffer); | 68 void StartFetchingDeviceMotionData(DeviceMotionHardwareBuffer* buffer); |
76 void StopFetchingDeviceMotionData(); | 69 void StopFetchingDeviceMotionData(); |
77 | 70 |
78 void StartFetchingDeviceOrientationData( | 71 void StartFetchingDeviceOrientationData( |
79 DeviceOrientationHardwareBuffer* buffer); | 72 DeviceOrientationHardwareBuffer* buffer); |
80 void StopFetchingDeviceOrientationData(); | 73 void StopFetchingDeviceOrientationData(); |
81 | 74 |
82 void StartFetchingDeviceOrientationAbsoluteData( | 75 void StartFetchingDeviceOrientationAbsoluteData( |
83 DeviceOrientationHardwareBuffer* buffer); | 76 DeviceOrientationHardwareBuffer* buffer); |
84 void StopFetchingDeviceOrientationAbsoluteData(); | 77 void StopFetchingDeviceOrientationAbsoluteData(); |
(...skipping 29 matching lines...) Expand all Loading... |
114 private: | 107 private: |
115 friend struct base::DefaultSingletonTraits<SensorManagerAndroid>; | 108 friend struct base::DefaultSingletonTraits<SensorManagerAndroid>; |
116 | 109 |
117 enum { | 110 enum { |
118 RECEIVED_MOTION_DATA_ACCELERATION = 0, | 111 RECEIVED_MOTION_DATA_ACCELERATION = 0, |
119 RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY = 1, | 112 RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY = 1, |
120 RECEIVED_MOTION_DATA_ROTATION_RATE = 2, | 113 RECEIVED_MOTION_DATA_ROTATION_RATE = 2, |
121 RECEIVED_MOTION_DATA_MAX = 3, | 114 RECEIVED_MOTION_DATA_MAX = 3, |
122 }; | 115 }; |
123 | 116 |
124 void SetLightBufferValue(double lux); | |
125 | |
126 void CheckMotionBufferReadyToRead(); | 117 void CheckMotionBufferReadyToRead(); |
127 void SetMotionBufferReadyStatus(bool ready); | 118 void SetMotionBufferReadyStatus(bool ready); |
128 void ClearInternalMotionBuffers(); | 119 void ClearInternalMotionBuffers(); |
129 | 120 |
130 // The Java provider of sensors info. | 121 // The Java provider of sensors info. |
131 base::android::ScopedJavaGlobalRef<jobject> device_sensors_; | 122 base::android::ScopedJavaGlobalRef<jobject> device_sensors_; |
132 int number_active_device_motion_sensors_; | 123 int number_active_device_motion_sensors_; |
133 int received_motion_data_[RECEIVED_MOTION_DATA_MAX]; | 124 int received_motion_data_[RECEIVED_MOTION_DATA_MAX]; |
134 | 125 |
135 // Cached pointers to buffers, owned by DataFetcherSharedMemoryBase. | 126 // Cached pointers to buffers, owned by DataFetcherSharedMemoryBase. |
136 DeviceLightHardwareBuffer* device_light_buffer_; | |
137 DeviceMotionHardwareBuffer* device_motion_buffer_; | 127 DeviceMotionHardwareBuffer* device_motion_buffer_; |
138 DeviceOrientationHardwareBuffer* device_orientation_buffer_; | 128 DeviceOrientationHardwareBuffer* device_orientation_buffer_; |
139 DeviceOrientationHardwareBuffer* device_orientation_absolute_buffer_; | 129 DeviceOrientationHardwareBuffer* device_orientation_absolute_buffer_; |
140 | 130 |
141 bool motion_buffer_initialized_; | 131 bool motion_buffer_initialized_; |
142 bool orientation_buffer_initialized_; | 132 bool orientation_buffer_initialized_; |
143 bool orientation_absolute_buffer_initialized_; | 133 bool orientation_absolute_buffer_initialized_; |
144 | 134 |
145 base::Lock light_buffer_lock_; | |
146 base::Lock motion_buffer_lock_; | 135 base::Lock motion_buffer_lock_; |
147 base::Lock orientation_buffer_lock_; | 136 base::Lock orientation_buffer_lock_; |
148 base::Lock orientation_absolute_buffer_lock_; | 137 base::Lock orientation_absolute_buffer_lock_; |
149 | 138 |
150 bool is_shutdown_; | 139 bool is_shutdown_; |
151 base::ThreadChecker thread_checker_; | 140 base::ThreadChecker thread_checker_; |
152 | 141 |
153 DISALLOW_COPY_AND_ASSIGN(SensorManagerAndroid); | 142 DISALLOW_COPY_AND_ASSIGN(SensorManagerAndroid); |
154 }; | 143 }; |
155 | 144 |
156 } // namespace device | 145 } // namespace device |
157 | 146 |
158 #endif // DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_ | 147 #endif // DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_ |
OLD | NEW |