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"
16 template<typename T
> struct DefaultSingletonTraits
;
20 // Android implementation of Device {Motion|Orientation|Light} API.
22 // Android's SensorManager has a push API, so when Got*() methods are called
23 // by the system the browser process puts the received data into a shared
24 // memory buffer, which is read by the renderer processes.
25 class CONTENT_EXPORT SensorManagerAndroid
{
27 // Must be called at startup, before GetInstance().
28 static bool Register(JNIEnv
* env
);
30 // Needs to be thread-safe, because accessed from different threads.
31 static SensorManagerAndroid
* GetInstance();
33 // Called from Java via JNI.
34 void GotLight(JNIEnv
*, jobject
, double value
);
35 void GotOrientation(JNIEnv
*, jobject
,
36 double alpha
, double beta
, double gamma
);
37 void GotAcceleration(JNIEnv
*, jobject
,
38 double x
, double y
, double z
);
39 void GotAccelerationIncludingGravity(JNIEnv
*, jobject
,
40 double x
, double y
, double z
);
41 void GotRotationRate(JNIEnv
*, jobject
,
42 double alpha
, double beta
, double gamma
);
44 // Shared memory related methods.
45 bool StartFetchingDeviceLightData(DeviceLightHardwareBuffer
* buffer
);
46 void StopFetchingDeviceLightData();
48 bool StartFetchingDeviceMotionData(DeviceMotionHardwareBuffer
* buffer
);
49 void StopFetchingDeviceMotionData();
51 bool StartFetchingDeviceOrientationData(
52 DeviceOrientationHardwareBuffer
* buffer
);
53 void StopFetchingDeviceOrientationData();
59 // These constants should match DEVICE_ORIENTATION, DEVICE_MOTION and
60 // DEVICE_LIGHT constants in content/public/android/java/src/org/
61 // chromium/content/browser/DeviceSensors.java
67 SensorManagerAndroid();
68 virtual ~SensorManagerAndroid();
70 virtual bool Start(EventType event_type
);
71 virtual void Stop(EventType event_type
);
72 virtual int GetNumberActiveDeviceMotionSensors();
74 void StartFetchingLightDataOnUI(DeviceLightHardwareBuffer
* buffer
);
75 void StopFetchingLightDataOnUI();
77 void StartFetchingMotionDataOnUI(DeviceMotionHardwareBuffer
* buffer
);
78 void StopFetchingMotionDataOnUI();
80 void StartFetchingOrientationDataOnUI(
81 DeviceOrientationHardwareBuffer
* buffer
);
82 void StopFetchingOrientationDataOnUI();
85 friend struct DefaultSingletonTraits
<SensorManagerAndroid
>;
88 RECEIVED_MOTION_DATA_ACCELERATION
= 0,
89 RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY
= 1,
90 RECEIVED_MOTION_DATA_ROTATION_RATE
= 2,
91 RECEIVED_MOTION_DATA_MAX
= 3,
94 void SetLightBufferValue(double lux
);
96 void CheckMotionBufferReadyToRead();
97 void SetMotionBufferReadyStatus(bool ready
);
98 void ClearInternalMotionBuffers();
100 void SetOrientationBufferReadyStatus(bool ready
);
101 bool isUsingBackupSensorsForOrientation();
103 // The Java provider of sensors info.
104 base::android::ScopedJavaGlobalRef
<jobject
> device_sensors_
;
105 int number_active_device_motion_sensors_
;
106 int received_motion_data_
[RECEIVED_MOTION_DATA_MAX
];
107 DeviceLightHardwareBuffer
* device_light_buffer_
;
108 DeviceMotionHardwareBuffer
* device_motion_buffer_
;
109 DeviceOrientationHardwareBuffer
* device_orientation_buffer_
;
110 bool is_light_buffer_ready_
;
111 bool is_motion_buffer_ready_
;
112 bool is_orientation_buffer_ready_
;
114 base::Lock light_buffer_lock_
;
115 base::Lock motion_buffer_lock_
;
116 base::Lock orientation_buffer_lock_
;
118 bool is_using_backup_sensors_for_orientation_
;
121 DISALLOW_COPY_AND_ASSIGN(SensorManagerAndroid
);
124 } // namespace content
126 #endif // CONTENT_BROWSER_DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_