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 CONTENT_BROWSER_DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_ | 5 #ifndef CONTENT_BROWSER_DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_ |
6 #define CONTENT_BROWSER_DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_ | 6 #define CONTENT_BROWSER_DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_ |
7 | 7 |
8 #include "base/android/scoped_java_ref.h" | 8 #include "base/android/scoped_java_ref.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
11 #include "content/common/content_export.h" | 11 #include "content/common/content_export.h" |
| 12 #include "content/common/device_sensors/device_light_hardware_buffer.h" |
12 #include "content/common/device_sensors/device_motion_hardware_buffer.h" | 13 #include "content/common/device_sensors/device_motion_hardware_buffer.h" |
13 #include "content/common/device_sensors/device_orientation_hardware_buffer.h" | 14 #include "content/common/device_sensors/device_orientation_hardware_buffer.h" |
14 | 15 |
15 template<typename T> struct DefaultSingletonTraits; | 16 template<typename T> struct DefaultSingletonTraits; |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 | 19 |
19 // Android implementation of Device Orientation API. | 20 // Android implementation of Device {Motion|Orientation|Light} API. |
20 // | 21 // |
21 // Android's SensorManager has a push API, so when Got*() methods are called | 22 // Android's SensorManager has a push API, so when Got*() methods are called |
22 // by the system the browser process puts the received data into a shared | 23 // by the system the browser process puts the received data into a shared |
23 // memory buffer, which is read by the renderer processes. | 24 // memory buffer, which is read by the renderer processes. |
24 class CONTENT_EXPORT SensorManagerAndroid { | 25 class CONTENT_EXPORT SensorManagerAndroid { |
25 public: | 26 public: |
26 // Must be called at startup, before GetInstance(). | 27 // Must be called at startup, before GetInstance(). |
27 static bool Register(JNIEnv* env); | 28 static bool Register(JNIEnv* env); |
28 | 29 |
29 // Needs to be thread-safe, because accessed from different threads. | 30 // Needs to be thread-safe, because accessed from different threads. |
30 static SensorManagerAndroid* GetInstance(); | 31 static SensorManagerAndroid* GetInstance(); |
31 | 32 |
32 // Called from Java via JNI. | 33 // Called from Java via JNI. |
| 34 void GotLight(JNIEnv*, jobject, double value); |
33 void GotOrientation(JNIEnv*, jobject, | 35 void GotOrientation(JNIEnv*, jobject, |
34 double alpha, double beta, double gamma); | 36 double alpha, double beta, double gamma); |
35 void GotAcceleration(JNIEnv*, jobject, | 37 void GotAcceleration(JNIEnv*, jobject, |
36 double x, double y, double z); | 38 double x, double y, double z); |
37 void GotAccelerationIncludingGravity(JNIEnv*, jobject, | 39 void GotAccelerationIncludingGravity(JNIEnv*, jobject, |
38 double x, double y, double z); | 40 double x, double y, double z); |
39 void GotRotationRate(JNIEnv*, jobject, | 41 void GotRotationRate(JNIEnv*, jobject, |
40 double alpha, double beta, double gamma); | 42 double alpha, double beta, double gamma); |
41 | 43 |
42 // Shared memory related methods. | 44 // Shared memory related methods. |
| 45 bool StartFetchingDeviceLightData(DeviceLightHardwareBuffer* buffer); |
| 46 void StopFetchingDeviceLightData(); |
| 47 |
43 bool StartFetchingDeviceMotionData(DeviceMotionHardwareBuffer* buffer); | 48 bool StartFetchingDeviceMotionData(DeviceMotionHardwareBuffer* buffer); |
44 void StopFetchingDeviceMotionData(); | 49 void StopFetchingDeviceMotionData(); |
45 | 50 |
46 bool StartFetchingDeviceOrientationData( | 51 bool StartFetchingDeviceOrientationData( |
47 DeviceOrientationHardwareBuffer* buffer); | 52 DeviceOrientationHardwareBuffer* buffer); |
48 void StopFetchingDeviceOrientationData(); | 53 void StopFetchingDeviceOrientationData(); |
49 | 54 |
50 protected: | 55 protected: |
51 enum EventType { | 56 enum EventType { |
52 // These constants should match DEVICE_ORIENTATION and DEVICE_MOTION | 57 // These constants should match DEVICE_ORIENTATION, DEVICE_MOTION and |
53 // constants in content/public/android/java/src/org/chromium/content/ | 58 // DEVICE_LIGHT constants in content/public/android/java/src/org/ |
54 // browser/DeviceMotionAndOrientation.java | 59 // chromium/content/browser/DeviceSensors.java |
55 kTypeOrientation = 0, | 60 kTypeOrientation = 0, |
56 kTypeMotion = 1 | 61 kTypeMotion = 1, |
| 62 kTypeLight = 2 |
57 }; | 63 }; |
58 | 64 |
59 SensorManagerAndroid(); | 65 SensorManagerAndroid(); |
60 virtual ~SensorManagerAndroid(); | 66 virtual ~SensorManagerAndroid(); |
61 | 67 |
62 virtual bool Start(EventType event_type); | 68 virtual bool Start(EventType event_type); |
63 virtual void Stop(EventType event_type); | 69 virtual void Stop(EventType event_type); |
64 virtual int GetNumberActiveDeviceMotionSensors(); | 70 virtual int GetNumberActiveDeviceMotionSensors(); |
65 | 71 |
66 private: | 72 private: |
67 friend struct DefaultSingletonTraits<SensorManagerAndroid>; | 73 friend struct DefaultSingletonTraits<SensorManagerAndroid>; |
68 | 74 |
69 enum { | 75 enum { |
70 RECEIVED_MOTION_DATA_ACCELERATION = 0, | 76 RECEIVED_MOTION_DATA_ACCELERATION = 0, |
71 RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY = 1, | 77 RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY = 1, |
72 RECEIVED_MOTION_DATA_ROTATION_RATE = 2, | 78 RECEIVED_MOTION_DATA_ROTATION_RATE = 2, |
73 RECEIVED_MOTION_DATA_MAX = 3, | 79 RECEIVED_MOTION_DATA_MAX = 3, |
74 }; | 80 }; |
75 | 81 |
| 82 void SetLightBufferValue(double lux); |
| 83 |
76 void CheckMotionBufferReadyToRead(); | 84 void CheckMotionBufferReadyToRead(); |
77 void SetMotionBufferReadyStatus(bool ready); | 85 void SetMotionBufferReadyStatus(bool ready); |
78 void ClearInternalMotionBuffers(); | 86 void ClearInternalMotionBuffers(); |
79 | 87 |
80 void SetOrientationBufferReadyStatus(bool ready); | 88 void SetOrientationBufferReadyStatus(bool ready); |
81 | 89 |
82 // The Java provider of orientation info. | 90 // The Java provider of sensors info. |
83 base::android::ScopedJavaGlobalRef<jobject> device_orientation_; | 91 base::android::ScopedJavaGlobalRef<jobject> device_sensors_; |
84 int number_active_device_motion_sensors_; | 92 int number_active_device_motion_sensors_; |
85 int received_motion_data_[RECEIVED_MOTION_DATA_MAX]; | 93 int received_motion_data_[RECEIVED_MOTION_DATA_MAX]; |
| 94 DeviceLightHardwareBuffer* device_light_buffer_; |
86 DeviceMotionHardwareBuffer* device_motion_buffer_; | 95 DeviceMotionHardwareBuffer* device_motion_buffer_; |
87 DeviceOrientationHardwareBuffer* device_orientation_buffer_; | 96 DeviceOrientationHardwareBuffer* device_orientation_buffer_; |
| 97 bool is_light_buffer_ready_; |
88 bool is_motion_buffer_ready_; | 98 bool is_motion_buffer_ready_; |
89 bool is_orientation_buffer_ready_; | 99 bool is_orientation_buffer_ready_; |
90 | 100 |
| 101 base::Lock light_buffer_lock_; |
91 base::Lock motion_buffer_lock_; | 102 base::Lock motion_buffer_lock_; |
92 base::Lock orientation_buffer_lock_; | 103 base::Lock orientation_buffer_lock_; |
93 | 104 |
94 DISALLOW_COPY_AND_ASSIGN(SensorManagerAndroid); | 105 DISALLOW_COPY_AND_ASSIGN(SensorManagerAndroid); |
95 }; | 106 }; |
96 | 107 |
97 } // namespace content | 108 } // namespace content |
98 | 109 |
99 #endif // CONTENT_BROWSER_DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_ | 110 #endif // CONTENT_BROWSER_DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_ |
OLD | NEW |