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.
5 #ifndef CONTENT_BROWSER_DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_
6 #define CONTENT_BROWSER_DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_
8 #include "base/android/scoped_java_ref.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/synchronization/lock.h"
11 #include "content/common/content_export.h"
12 #include "content/common/device_sensors/device_light_hardware_buffer.h"
13 #include "content/common/device_sensors/device_motion_hardware_buffer.h"
14 #include "content/common/device_sensors/device_orientation_hardware_buffer.h"
18 struct DefaultSingletonTraits
;
23 // Android implementation of Device {Motion|Orientation|Light} API.
25 // Android's SensorManager has a push API, so when Got*() methods are called
26 // by the system the browser process puts the received data into a shared
27 // memory buffer, which is read by the renderer processes.
28 class CONTENT_EXPORT SensorManagerAndroid
{
30 // Must be called at startup, before GetInstance().
31 static bool Register(JNIEnv
* env
);
33 // Needs to be thread-safe, because accessed from different threads.
34 static SensorManagerAndroid
* GetInstance();
36 // Called from Java via JNI.
37 void GotLight(JNIEnv
*, jobject
, double value
);
38 void GotOrientation(JNIEnv
*, jobject
,
39 double alpha
, double beta
, double gamma
);
40 void GotAcceleration(JNIEnv
*, jobject
,
41 double x
, double y
, double z
);
42 void GotAccelerationIncludingGravity(JNIEnv
*, jobject
,
43 double x
, double y
, double z
);
44 void GotRotationRate(JNIEnv
*, jobject
,
45 double alpha
, double beta
, double gamma
);
47 // Shared memory related methods.
48 bool StartFetchingDeviceLightData(DeviceLightHardwareBuffer
* buffer
);
49 void StopFetchingDeviceLightData();
51 bool StartFetchingDeviceMotionData(DeviceMotionHardwareBuffer
* buffer
);
52 void StopFetchingDeviceMotionData();
54 bool StartFetchingDeviceOrientationData(
55 DeviceOrientationHardwareBuffer
* buffer
);
56 void StopFetchingDeviceOrientationData();
62 // These constants should match DEVICE_ORIENTATION, DEVICE_MOTION and
63 // DEVICE_LIGHT constants in content/public/android/java/src/org/
64 // chromium/content/browser/DeviceSensors.java
70 SensorManagerAndroid();
71 virtual ~SensorManagerAndroid();
73 virtual bool Start(EventType event_type
);
74 virtual void Stop(EventType event_type
);
75 virtual int GetOrientationSensorTypeUsed();
76 virtual int GetNumberActiveDeviceMotionSensors();
78 void StartFetchingLightDataOnUI(DeviceLightHardwareBuffer
* buffer
);
79 void StopFetchingLightDataOnUI();
81 void StartFetchingMotionDataOnUI(DeviceMotionHardwareBuffer
* buffer
);
82 void StopFetchingMotionDataOnUI();
84 void StartFetchingOrientationDataOnUI(
85 DeviceOrientationHardwareBuffer
* buffer
);
86 void StopFetchingOrientationDataOnUI();
89 friend struct base::DefaultSingletonTraits
<SensorManagerAndroid
>;
92 RECEIVED_MOTION_DATA_ACCELERATION
= 0,
93 RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY
= 1,
94 RECEIVED_MOTION_DATA_ROTATION_RATE
= 2,
95 RECEIVED_MOTION_DATA_MAX
= 3,
98 void SetLightBufferValue(double lux
);
100 void CheckMotionBufferReadyToRead();
101 void SetMotionBufferReadyStatus(bool ready
);
102 void ClearInternalMotionBuffers();
104 void SetOrientationBufferStatus(bool ready
, bool absolute
);
106 // The Java provider of sensors info.
107 base::android::ScopedJavaGlobalRef
<jobject
> device_sensors_
;
108 int number_active_device_motion_sensors_
;
109 int received_motion_data_
[RECEIVED_MOTION_DATA_MAX
];
110 DeviceLightHardwareBuffer
* device_light_buffer_
;
111 DeviceMotionHardwareBuffer
* device_motion_buffer_
;
112 DeviceOrientationHardwareBuffer
* device_orientation_buffer_
;
114 bool motion_buffer_initialized_
;
115 bool orientation_buffer_initialized_
;
117 base::Lock light_buffer_lock_
;
118 base::Lock motion_buffer_lock_
;
119 base::Lock orientation_buffer_lock_
;
123 DISALLOW_COPY_AND_ASSIGN(SensorManagerAndroid
);
126 } // namespace content
128 #endif // CONTENT_BROWSER_DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_