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