OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_ | 5 #ifndef CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_ |
6 #define CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_ | 6 #define CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_ |
7 | 7 |
8 #include "base/android/scoped_java_ref.h" | 8 #include "base/android/scoped_java_ref.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
11 #include "content/browser/device_orientation/data_fetcher.h" | |
12 #include "content/browser/device_orientation/device_data.h" | 11 #include "content/browser/device_orientation/device_data.h" |
12 #include "content/common/device_motion_hardware_buffer.h" | |
13 | |
14 template<typename T> struct DefaultSingletonTraits; | |
13 | 15 |
14 namespace content { | 16 namespace content { |
15 | |
16 class Orientation; | 17 class Orientation; |
17 | 18 |
18 // Android implementation of DeviceOrientation API. | 19 // Android implementation of DeviceOrientation API. |
19 | 20 |
20 // Android's SensorManager has a push API, whereas Chrome wants to pull data. | 21 // Android's SensorManager has a push API, whereas Chrome wants to pull data. |
21 // To fit them together, we store incoming sensor events in a 1-element buffer. | 22 // To fit them together, we store incoming sensor events in a 1-element buffer. |
22 // SensorManager calls SetOrientation() which pushes a new value (discarding the | 23 // SensorManager calls SetOrientation() which pushes a new value (discarding the |
23 // previous value if any). Chrome calls GetDeviceData() which reads the most | 24 // previous value if any). Chrome calls GetDeviceData() which reads the most |
24 // recent value. Repeated calls to GetDeviceData() will return the same value. | 25 // recent value. Repeated calls to GetDeviceData() will return the same value. |
25 | 26 |
26 class DataFetcherImplAndroid : public DataFetcher { | 27 // TODO(timvolodine): Simplify this class and remove GetDeviceData() method, |
28 // once Device Orientation switches to shared memory implementation. | |
29 // Also rename this class to SensorManagerAndroid. | |
30 class DataFetcherImplAndroid { | |
27 public: | 31 public: |
28 // Must be called at startup, before Create(). | 32 // Must be called at startup, before GetInstance(). |
29 static void Init(JNIEnv* env); | 33 static void Init(JNIEnv* env); |
30 | 34 |
31 // Factory function. We'll listen for events for the lifetime of this object. | 35 // Needs to be thread-safe, because accessed from different threads. |
32 // Returns NULL on error. | 36 static DataFetcherImplAndroid* GetInstance(); |
33 static DataFetcher* Create(); | |
34 | |
35 virtual ~DataFetcherImplAndroid(); | |
36 | 37 |
37 // Called from Java via JNI. | 38 // Called from Java via JNI. |
38 void GotOrientation(JNIEnv*, jobject, | 39 void GotOrientation(JNIEnv*, jobject, |
39 double alpha, double beta, double gamma); | 40 double alpha, double beta, double gamma); |
40 void GotAcceleration(JNIEnv*, jobject, | 41 void GotAcceleration(JNIEnv*, jobject, |
41 double x, double y, double z); | 42 double x, double y, double z); |
42 void GotAccelerationIncludingGravity(JNIEnv*, jobject, | 43 void GotAccelerationIncludingGravity(JNIEnv*, jobject, |
43 double x, double y, double z); | 44 double x, double y, double z); |
44 void GotRotationRate(JNIEnv*, jobject, | 45 void GotRotationRate(JNIEnv*, jobject, |
45 double alpha, double beta, double gamma); | 46 double alpha, double beta, double gamma); |
46 | 47 |
47 // Implementation of DataFetcher. | 48 const DeviceData* GetDeviceData(DeviceData::Type type); |
48 virtual const DeviceData* GetDeviceData(DeviceData::Type type) OVERRIDE; | |
49 | 49 |
50 private: | 50 virtual bool Start(DeviceData::Type event_type); |
51 virtual void Stop(DeviceData::Type event_type); | |
52 | |
53 // Shared memory related methods. | |
54 bool StartFetchingDeviceMotionData(DeviceMotionHardwareBuffer* buffer); | |
55 void StopFetchingDeviceMotionData(); | |
56 | |
57 protected: | |
51 DataFetcherImplAndroid(); | 58 DataFetcherImplAndroid(); |
52 const Orientation* GetOrientation(); | 59 virtual ~DataFetcherImplAndroid(); |
53 | |
54 // Wrappers for JNI methods. | |
55 // TODO(timvolodine): move the DeviceData::Type enum to the service class | |
56 // once it is implemented. | |
57 bool Start(DeviceData::Type event_type, int rate_in_milliseconds); | |
58 void Stop(DeviceData::Type event_type); | |
59 | 60 |
60 virtual int GetNumberActiveDeviceMotionSensors(); | 61 virtual int GetNumberActiveDeviceMotionSensors(); |
61 | 62 |
63 private: | |
64 friend struct DefaultSingletonTraits<DataFetcherImplAndroid>; | |
65 | |
66 const Orientation* GetOrientation(); | |
67 | |
68 void CheckBufferReadyToRead(); | |
69 void SetBufferReadyStatus(bool ready); | |
70 void ClearInternalBuffers(); | |
71 | |
72 enum { | |
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, | |
77 }; | |
62 // Value returned by GetDeviceData. | 78 // Value returned by GetDeviceData. |
63 scoped_refptr<Orientation> current_orientation_; | 79 scoped_refptr<Orientation> current_orientation_; |
64 | 80 |
65 // 1-element buffer, written by GotOrientation, read by GetDeviceData. | 81 // 1-element buffer, written by GotOrientation, read by GetDeviceData. |
66 base::Lock next_orientation_lock_; | 82 base::Lock next_orientation_lock_; |
67 scoped_refptr<Orientation> next_orientation_; | 83 scoped_refptr<Orientation> next_orientation_; |
68 | 84 |
69 // The Java provider of orientation info. | 85 // The Java provider of orientation info. |
70 base::android::ScopedJavaGlobalRef<jobject> device_orientation_; | 86 base::android::ScopedJavaGlobalRef<jobject> device_orientation_; |
87 int number_active_device_motion_sensors_; | |
piman
2013/07/12 18:06:45
new field needs initialization.
timvolodine
2013/07/12 18:33:45
Done.
| |
88 int received_motion_data_[RECEIVED_MOTION_DATA_MAX]; | |
89 DeviceMotionHardwareBuffer* device_motion_buffer_; | |
90 bool is_buffer_ready_; | |
71 | 91 |
72 DISALLOW_COPY_AND_ASSIGN(DataFetcherImplAndroid); | 92 DISALLOW_COPY_AND_ASSIGN(DataFetcherImplAndroid); |
73 }; | 93 }; |
74 | 94 |
75 } // namespace content | 95 } // namespace content |
76 | 96 |
77 #endif // CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_ | 97 #endif // CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_ |
OLD | NEW |