Home | History | Annotate | Download | only in device_sensors
      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_motion_hardware_buffer.h"
     13 #include "content/common/device_sensors/device_orientation_hardware_buffer.h"
     14 
     15 template<typename T> struct DefaultSingletonTraits;
     16 
     17 namespace content {
     18 
     19 // Android implementation of Device Orientation API.
     20 //
     21 // Android's SensorManager has a push API, so when Got*() methods are called
     22 // by the system the browser process puts the received data into a shared
     23 // memory buffer, which is read by the renderer processes.
     24 class CONTENT_EXPORT SensorManagerAndroid {
     25  public:
     26   // Must be called at startup, before GetInstance().
     27   static bool Register(JNIEnv* env);
     28 
     29   // Needs to be thread-safe, because accessed from different threads.
     30   static SensorManagerAndroid* GetInstance();
     31 
     32   // Called from Java via JNI.
     33   void GotOrientation(JNIEnv*, jobject,
     34                       double alpha, double beta, double gamma);
     35   void GotAcceleration(JNIEnv*, jobject,
     36                        double x, double y, double z);
     37   void GotAccelerationIncludingGravity(JNIEnv*, jobject,
     38                                        double x, double y, double z);
     39   void GotRotationRate(JNIEnv*, jobject,
     40                        double alpha, double beta, double gamma);
     41 
     42   // Shared memory related methods.
     43   bool StartFetchingDeviceMotionData(DeviceMotionHardwareBuffer* buffer);
     44   void StopFetchingDeviceMotionData();
     45 
     46   bool StartFetchingDeviceOrientationData(
     47       DeviceOrientationHardwareBuffer* buffer);
     48   void StopFetchingDeviceOrientationData();
     49 
     50  protected:
     51   enum EventType {
     52     // These constants should match DEVICE_ORIENTATION and DEVICE_MOTION
     53     // constants in content/public/android/java/src/org/chromium/content/
     54     // browser/DeviceMotionAndOrientation.java
     55     kTypeOrientation = 0,
     56     kTypeMotion = 1
     57   };
     58 
     59   SensorManagerAndroid();
     60   virtual ~SensorManagerAndroid();
     61 
     62   virtual bool Start(EventType event_type);
     63   virtual void Stop(EventType event_type);
     64   virtual int GetNumberActiveDeviceMotionSensors();
     65 
     66  private:
     67   friend struct DefaultSingletonTraits<SensorManagerAndroid>;
     68 
     69   enum {
     70     RECEIVED_MOTION_DATA_ACCELERATION = 0,
     71     RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY = 1,
     72     RECEIVED_MOTION_DATA_ROTATION_RATE = 2,
     73     RECEIVED_MOTION_DATA_MAX = 3,
     74   };
     75 
     76   void CheckMotionBufferReadyToRead();
     77   void SetMotionBufferReadyStatus(bool ready);
     78   void ClearInternalMotionBuffers();
     79 
     80   void SetOrientationBufferReadyStatus(bool ready);
     81 
     82   // The Java provider of orientation info.
     83   base::android::ScopedJavaGlobalRef<jobject> device_orientation_;
     84   int number_active_device_motion_sensors_;
     85   int received_motion_data_[RECEIVED_MOTION_DATA_MAX];
     86   DeviceMotionHardwareBuffer* device_motion_buffer_;
     87   DeviceOrientationHardwareBuffer* device_orientation_buffer_;
     88   bool is_motion_buffer_ready_;
     89   bool is_orientation_buffer_ready_;
     90 
     91   base::Lock motion_buffer_lock_;
     92   base::Lock orientation_buffer_lock_;
     93 
     94   DISALLOW_COPY_AND_ASSIGN(SensorManagerAndroid);
     95 };
     96 
     97 }  // namespace content
     98 
     99 #endif  // CONTENT_BROWSER_DEVICE_SENSORS_SENSOR_MANAGER_ANDROID_H_
    100