1 // Copyright (c) 2012 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 CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_
6 #define CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_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/browser/device_orientation/device_data.h"
12 #include "content/common/content_export.h"
13 #include "content/common/device_orientation/device_motion_hardware_buffer.h"
14 #include "content/common/device_orientation/device_orientation_hardware_buffer.h"
16 template<typename T> struct DefaultSingletonTraits;
20 // Android implementation of Device Orientation 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.
27 // TODO(timvolodine): rename this class to SensorManagerAndroid.
28 class CONTENT_EXPORT DataFetcherImplAndroid {
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 DataFetcherImplAndroid* GetInstance();
36 // Called from Java via JNI.
37 void GotOrientation(JNIEnv*, jobject,
38 double alpha, double beta, double gamma);
39 void GotAcceleration(JNIEnv*, jobject,
40 double x, double y, double z);
41 void GotAccelerationIncludingGravity(JNIEnv*, jobject,
42 double x, double y, double z);
43 void GotRotationRate(JNIEnv*, jobject,
44 double alpha, double beta, double gamma);
46 virtual bool Start(DeviceData::Type event_type);
47 virtual void Stop(DeviceData::Type event_type);
49 // Shared memory related methods.
50 bool StartFetchingDeviceMotionData(DeviceMotionHardwareBuffer* buffer);
51 void StopFetchingDeviceMotionData();
53 bool StartFetchingDeviceOrientationData(
54 DeviceOrientationHardwareBuffer* buffer);
55 void StopFetchingDeviceOrientationData();
58 DataFetcherImplAndroid();
59 virtual ~DataFetcherImplAndroid();
61 virtual int GetNumberActiveDeviceMotionSensors();
64 friend struct DefaultSingletonTraits<DataFetcherImplAndroid>;
66 void CheckMotionBufferReadyToRead();
67 void SetMotionBufferReadyStatus(bool ready);
68 void ClearInternalMotionBuffers();
70 void SetOrientationBufferReadyStatus(bool ready);
73 RECEIVED_MOTION_DATA_ACCELERATION = 0,
74 RECEIVED_MOTION_DATA_ACCELERATION_INCL_GRAVITY = 1,
75 RECEIVED_MOTION_DATA_ROTATION_RATE = 2,
76 RECEIVED_MOTION_DATA_MAX = 3,
79 // The Java provider of orientation info.
80 base::android::ScopedJavaGlobalRef<jobject> device_orientation_;
81 int number_active_device_motion_sensors_;
82 int received_motion_data_[RECEIVED_MOTION_DATA_MAX];
83 DeviceMotionHardwareBuffer* device_motion_buffer_;
84 DeviceOrientationHardwareBuffer* device_orientation_buffer_;
85 bool is_motion_buffer_ready_;
86 bool is_orientation_buffer_ready_;
88 base::Lock motion_buffer_lock_;
89 base::Lock orientation_buffer_lock_;
91 DISALLOW_COPY_AND_ASSIGN(DataFetcherImplAndroid);
94 } // namespace content
96 #endif // CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_