OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CONTENT_BROWSER_DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_ | |
6 #define CONTENT_BROWSER_DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 #include "base/android/scoped_java_ref.h" | |
11 #include "base/macros.h" | |
12 #include "base/synchronization/lock.h" | |
13 #include "base/threading/thread_checker.h" | |
14 #include "content/browser/device_sensors/device_sensors_consts.h" | |
15 #include "content/common/content_export.h" | |
16 #include "device/sensors/public/cpp/device_light_hardware_buffer.h" | |
17 #include "device/sensors/public/cpp/device_motion_hardware_buffer.h" | |
18 #include "device/sensors/public/cpp/device_orientation_hardware_buffer.h" | |
19 | |
20 namespace base { | |
21 template <typename T> | |
22 struct DefaultSingletonTraits; | |
23 } | |
24 | |
25 namespace content { | |
26 | |
27 // Android implementation of Device {Motion|Orientation|Light} API. | |
28 // | |
29 // 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 | |
31 // memory buffer, which is read by the renderer processes. | |
32 class CONTENT_EXPORT SensorManagerAndroid { | |
33 public: | |
34 // Must be called at startup, before GetInstance(). | |
35 static bool Register(JNIEnv* env); | |
36 | |
37 // Should be called only on the UI thread. | |
38 static SensorManagerAndroid* GetInstance(); | |
39 | |
40 // Called from Java via JNI. | |
41 void GotLight(JNIEnv*, | |
42 const base::android::JavaParamRef<jobject>&, | |
43 double value); | |
44 void GotOrientation(JNIEnv*, | |
45 const base::android::JavaParamRef<jobject>&, | |
46 double alpha, | |
47 double beta, | |
48 double gamma); | |
49 void GotOrientationAbsolute(JNIEnv*, | |
50 const base::android::JavaParamRef<jobject>&, | |
51 double alpha, | |
52 double beta, | |
53 double gamma); | |
54 void GotAcceleration(JNIEnv*, | |
55 const base::android::JavaParamRef<jobject>&, | |
56 double x, | |
57 double y, | |
58 double z); | |
59 void GotAccelerationIncludingGravity( | |
60 JNIEnv*, | |
61 const base::android::JavaParamRef<jobject>&, | |
62 double x, | |
63 double y, | |
64 double z); | |
65 void GotRotationRate(JNIEnv*, | |
66 const base::android::JavaParamRef<jobject>&, | |
67 double alpha, | |
68 double beta, | |
69 double gamma); | |
70 | |
71 // Shared memory related methods. | |
72 void StartFetchingDeviceLightData(DeviceLightHardwareBuffer* buffer); | |
73 void StopFetchingDeviceLightData(); | |
74 | |
75 void StartFetchingDeviceMotionData(DeviceMotionHardwareBuffer* buffer); | |
76 void StopFetchingDeviceMotionData(); | |
77 | |
78 void StartFetchingDeviceOrientationData( | |
79 DeviceOrientationHardwareBuffer* buffer); | |
80 void StopFetchingDeviceOrientationData(); | |
81 | |
82 void StartFetchingDeviceOrientationAbsoluteData( | |
83 DeviceOrientationHardwareBuffer* buffer); | |
84 void StopFetchingDeviceOrientationAbsoluteData(); | |
85 | |
86 void Shutdown(); | |
87 | |
88 // A Java counterpart will be generated for this enum. | |
89 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.content.browser | |
90 // When adding new constants don't modify the order as they are used for UMA. | |
91 enum OrientationSensorType { | |
92 NOT_AVAILABLE = 0, | |
93 ROTATION_VECTOR = 1, | |
94 ACCELEROMETER_MAGNETIC = 2, | |
95 GAME_ROTATION_VECTOR = 3, | |
96 ORIENTATION_SENSOR_MAX = 4, | |
97 }; | |
98 | |
99 protected: | |
100 SensorManagerAndroid(); | |
101 virtual ~SensorManagerAndroid(); | |
102 | |
103 // Starts listening to the sensors corresponding to the consumer_type. | |
104 // Returns true if the registration with sensors was successful. | |
105 virtual bool Start(ConsumerType consumer_type); | |
106 // Stops listening to the sensors corresponding to the consumer_type. | |
107 virtual void Stop(ConsumerType consumer_type); | |
108 // Returns currently used sensor type for device orientation. | |
109 virtual OrientationSensorType GetOrientationSensorTypeUsed(); | |
110 // Returns the number of active sensors corresponding to | |
111 // ConsumerType.DEVICE_MOTION. | |
112 virtual int GetNumberActiveDeviceMotionSensors(); | |
113 | |
114 private: | |
115 friend struct base::DefaultSingletonTraits<SensorManagerAndroid>; | |
116 | |
117 enum { | |
118 RECEIVED_MOTION_DATA_ACCELERATION = 0, | |
119 RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY = 1, | |
120 RECEIVED_MOTION_DATA_ROTATION_RATE = 2, | |
121 RECEIVED_MOTION_DATA_MAX = 3, | |
122 }; | |
123 | |
124 void SetLightBufferValue(double lux); | |
125 | |
126 void CheckMotionBufferReadyToRead(); | |
127 void SetMotionBufferReadyStatus(bool ready); | |
128 void ClearInternalMotionBuffers(); | |
129 | |
130 // The Java provider of sensors info. | |
131 base::android::ScopedJavaGlobalRef<jobject> device_sensors_; | |
132 int number_active_device_motion_sensors_; | |
133 int received_motion_data_[RECEIVED_MOTION_DATA_MAX]; | |
134 | |
135 // Cached pointers to buffers, owned by DataFetcherSharedMemoryBase. | |
136 DeviceLightHardwareBuffer* device_light_buffer_; | |
137 DeviceMotionHardwareBuffer* device_motion_buffer_; | |
138 DeviceOrientationHardwareBuffer* device_orientation_buffer_; | |
139 DeviceOrientationHardwareBuffer* device_orientation_absolute_buffer_; | |
140 | |
141 bool motion_buffer_initialized_; | |
142 bool orientation_buffer_initialized_; | |
143 bool orientation_absolute_buffer_initialized_; | |
144 | |
145 base::Lock light_buffer_lock_; | |
146 base::Lock motion_buffer_lock_; | |
147 base::Lock orientation_buffer_lock_; | |
148 base::Lock orientation_absolute_buffer_lock_; | |
149 | |
150 bool is_shutdown_; | |
151 base::ThreadChecker thread_checker_; | |
152 | |
153 DISALLOW_COPY_AND_ASSIGN(SensorManagerAndroid); | |
154 }; | |
155 | |
156 } // namespace content | |
157 | |
158 #endif // CONTENT_BROWSER_DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_ | |
OLD | NEW |