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/common/content_export.h"
12 #include "content/common/device_orientation/device_motion_hardware_buffer.h"
13 #include "content/common/device_orientation/device_orientation_hardware_buffer.h"
15 template<typename T> struct DefaultSingletonTraits;
19 // Android implementation of Device Orientation API.
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.
26 // TODO(timvolodine): rename this class to SensorManagerAndroid.
27 class CONTENT_EXPORT DataFetcherImplAndroid {
29 // Must be called at startup, before GetInstance().
30 static bool Register(JNIEnv* env);
32 // Needs to be thread-safe, because accessed from different threads.
33 static DataFetcherImplAndroid* GetInstance();
35 // Called from Java via JNI.
36 void GotOrientation(JNIEnv*, jobject,
37 double alpha, double beta, double gamma);
38 void GotAcceleration(JNIEnv*, jobject,
39 double x, double y, double z);
40 void GotAccelerationIncludingGravity(JNIEnv*, jobject,
41 double x, double y, double z);
42 void GotRotationRate(JNIEnv*, jobject,
43 double alpha, double beta, double gamma);
45 // Shared memory related methods.
46 bool StartFetchingDeviceMotionData(DeviceMotionHardwareBuffer* buffer);
47 void StopFetchingDeviceMotionData();
49 bool StartFetchingDeviceOrientationData(
50 DeviceOrientationHardwareBuffer* buffer);
51 void StopFetchingDeviceOrientationData();
55 // These constants should match DEVICE_ORIENTATION and DEVICE_MOTION
56 // constants in content/public/android/java/src/org/chromium/content/
57 // browser/DeviceMotionAndOrientation.java
62 DataFetcherImplAndroid();
63 virtual ~DataFetcherImplAndroid();
65 virtual bool Start(EventType event_type);
66 virtual void Stop(EventType event_type);
67 virtual int GetNumberActiveDeviceMotionSensors();
70 friend struct DefaultSingletonTraits<DataFetcherImplAndroid>;
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 void CheckMotionBufferReadyToRead();
80 void SetMotionBufferReadyStatus(bool ready);
81 void ClearInternalMotionBuffers();
83 void SetOrientationBufferReadyStatus(bool ready);
85 // The Java provider of orientation info.
86 base::android::ScopedJavaGlobalRef<jobject> device_orientation_;
87 int number_active_device_motion_sensors_;
88 int received_motion_data_[RECEIVED_MOTION_DATA_MAX];
89 DeviceMotionHardwareBuffer* device_motion_buffer_;
90 DeviceOrientationHardwareBuffer* device_orientation_buffer_;
91 bool is_motion_buffer_ready_;
92 bool is_orientation_buffer_ready_;
94 base::Lock motion_buffer_lock_;
95 base::Lock orientation_buffer_lock_;
97 DISALLOW_COPY_AND_ASSIGN(DataFetcherImplAndroid);
100 } // namespace content
102 #endif // CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_