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 "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" 15 16 template<typename T> struct DefaultSingletonTraits; 17 18 namespace content { 19 20 // Android implementation of Device {Motion|Orientation|Light} API. 21 // 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 { 26 public: 27 // Must be called at startup, before GetInstance(). 28 static bool Register(JNIEnv* env); 29 30 // Needs to be thread-safe, because accessed from different threads. 31 static SensorManagerAndroid* GetInstance(); 32 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); 43 44 // Shared memory related methods. 45 bool StartFetchingDeviceLightData(DeviceLightHardwareBuffer* buffer); 46 void StopFetchingDeviceLightData(); 47 48 bool StartFetchingDeviceMotionData(DeviceMotionHardwareBuffer* buffer); 49 void StopFetchingDeviceMotionData(); 50 51 bool StartFetchingDeviceOrientationData( 52 DeviceOrientationHardwareBuffer* buffer); 53 void StopFetchingDeviceOrientationData(); 54 55 protected: 56 enum EventType { 57 // These constants should match DEVICE_ORIENTATION, DEVICE_MOTION and 58 // DEVICE_LIGHT constants in content/public/android/java/src/org/ 59 // chromium/content/browser/DeviceSensors.java 60 kTypeOrientation = 0, 61 kTypeMotion = 1, 62 kTypeLight = 2 63 }; 64 65 SensorManagerAndroid(); 66 virtual ~SensorManagerAndroid(); 67 68 virtual bool Start(EventType event_type); 69 virtual void Stop(EventType event_type); 70 virtual int GetNumberActiveDeviceMotionSensors(); 71 72 private: 73 friend struct DefaultSingletonTraits<SensorManagerAndroid>; 74 75 enum { 76 RECEIVED_MOTION_DATA_ACCELERATION = 0, 77 RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY = 1, 78 RECEIVED_MOTION_DATA_ROTATION_RATE = 2, 79 RECEIVED_MOTION_DATA_MAX = 3, 80 }; 81 82 void SetLightBufferValue(double lux); 83 84 void CheckMotionBufferReadyToRead(); 85 void SetMotionBufferReadyStatus(bool ready); 86 void ClearInternalMotionBuffers(); 87 88 void SetOrientationBufferReadyStatus(bool ready); 89 90 // The Java provider of sensors info. 91 base::android::ScopedJavaGlobalRef<jobject> device_sensors_; 92 int number_active_device_motion_sensors_; 93 int received_motion_data_[RECEIVED_MOTION_DATA_MAX]; 94 DeviceLightHardwareBuffer* device_light_buffer_; 95 DeviceMotionHardwareBuffer* device_motion_buffer_; 96 DeviceOrientationHardwareBuffer* device_orientation_buffer_; 97 bool is_light_buffer_ready_; 98 bool is_motion_buffer_ready_; 99 bool is_orientation_buffer_ready_; 100 101 base::Lock light_buffer_lock_; 102 base::Lock motion_buffer_lock_; 103 base::Lock orientation_buffer_lock_; 104 105 DISALLOW_COPY_AND_ASSIGN(SensorManagerAndroid); 106 }; 107 108 } // namespace content 109 110 #endif // CONTENT_BROWSER_DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_ 111