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 <jni.h> | 8 #include <jni.h> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
12 #include "content/browser/device_orientation/data_fetcher.h" | 12 #include "content/browser/device_orientation/data_fetcher.h" |
13 #include "content/browser/device_orientation/orientation.h" | 13 #include "content/browser/device_orientation/device_data.h" |
14 | 14 |
15 namespace device_orientation { | 15 namespace device_orientation { |
16 | 16 |
| 17 class Orientation; |
| 18 |
17 // Android implementation of DeviceOrientation API. | 19 // Android implementation of DeviceOrientation API. |
18 | 20 |
19 // 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. |
20 // 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. |
21 // SensorManager calls SetOrientation() which pushes a new value (discarding the | 23 // SensorManager calls SetOrientation() which pushes a new value (discarding the |
22 // previous value if any). Chrome calls GetOrientation() which reads the most | 24 // previous value if any). Chrome calls GetDeviceData() which reads the most |
23 // recent value. Repeated calls to GetOrientation() will return the same value. | 25 // recent value. Repeated calls to GetDeviceData() will return the same value. |
24 | 26 |
25 class DataFetcherImplAndroid : public DataFetcher { | 27 class DataFetcherImplAndroid : public DataFetcher { |
26 public: | 28 public: |
27 // Must be called at startup, before Create(). | 29 // Must be called at startup, before Create(). |
28 static void Init(JNIEnv* env); | 30 static void Init(JNIEnv* env); |
29 | 31 |
30 // Factory function. We'll listen for events for the lifetime of this object. | 32 // Factory function. We'll listen for events for the lifetime of this object. |
31 // Returns NULL on error. | 33 // Returns NULL on error. |
32 static DataFetcher* Create(); | 34 static DataFetcher* Create(); |
33 | 35 |
34 virtual ~DataFetcherImplAndroid(); | 36 virtual ~DataFetcherImplAndroid(); |
35 | 37 |
36 // Called from Java via JNI. | 38 // Called from Java via JNI. |
37 void GotOrientation(JNIEnv*, jobject, | 39 void GotOrientation(JNIEnv*, jobject, |
38 double alpha, double beta, double gamma); | 40 double alpha, double beta, double gamma); |
39 | 41 |
40 // Implementation of DataFetcher. | 42 // Implementation of DataFetcher. |
41 virtual bool GetOrientation(Orientation* orientation) OVERRIDE; | 43 virtual DeviceData* GetDeviceData( |
| 44 DeviceData::DeviceDataType device_data_type) OVERRIDE; |
42 | 45 |
43 private: | 46 private: |
44 DataFetcherImplAndroid(); | 47 DataFetcherImplAndroid(); |
| 48 Orientation* GetOrientation(); |
45 | 49 |
46 // Wrappers for JNI methods. | 50 // Wrappers for JNI methods. |
47 bool Start(int rate_in_milliseconds); | 51 bool Start(int rate_in_milliseconds); |
48 void Stop(); | 52 void Stop(); |
49 | 53 |
50 // Value returned by GetOrientation. | 54 // Value returned by GetDeviceData. |
51 scoped_ptr<Orientation> current_orientation_; | 55 scoped_ptr<Orientation> current_orientation_; |
52 | 56 |
53 // 1-element buffer, written by GotOrientation, read by GetOrientation. | 57 // 1-element buffer, written by GotOrientation, read by GetDeviceData. |
54 base::Lock next_orientation_lock_; | 58 base::Lock next_orientation_lock_; |
55 scoped_ptr<Orientation> next_orientation_; | 59 scoped_ptr<Orientation> next_orientation_; |
56 | 60 |
57 DISALLOW_COPY_AND_ASSIGN(DataFetcherImplAndroid); | 61 DISALLOW_COPY_AND_ASSIGN(DataFetcherImplAndroid); |
58 }; | 62 }; |
59 | 63 |
60 } // namespace device_orientation | 64 } // namespace device_orientation |
61 | 65 |
62 #endif // CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_ | 66 #endif // CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_ |
OLD | NEW |