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 #include "device/geolocation/location_api_adapter_android.h" | 5 #include "device/geolocation/location_api_adapter_android.h" |
6 | 6 |
7 #include "base/android/context_utils.h" | 7 #include "base/android/context_utils.h" |
8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 jdouble longitude, | 24 jdouble longitude, |
25 jdouble time_stamp, | 25 jdouble time_stamp, |
26 jboolean has_altitude, | 26 jboolean has_altitude, |
27 jdouble altitude, | 27 jdouble altitude, |
28 jboolean has_accuracy, | 28 jboolean has_accuracy, |
29 jdouble accuracy, | 29 jdouble accuracy, |
30 jboolean has_heading, | 30 jboolean has_heading, |
31 jdouble heading, | 31 jdouble heading, |
32 jboolean has_speed, | 32 jboolean has_speed, |
33 jdouble speed) { | 33 jdouble speed) { |
34 AndroidLocationApiAdapter::OnNewLocationAvailable(latitude, longitude, | 34 AndroidLocationApiAdapter::OnNewLocationAvailable( |
35 time_stamp, has_altitude, altitude, has_accuracy, accuracy, | 35 latitude, longitude, time_stamp, has_altitude, altitude, has_accuracy, |
36 has_heading, heading, has_speed, speed); | 36 accuracy, has_heading, heading, has_speed, speed); |
37 } | 37 } |
38 | 38 |
39 static void NewErrorAvailable(JNIEnv* env, | 39 static void NewErrorAvailable(JNIEnv* env, |
40 const JavaParamRef<jclass>&, | 40 const JavaParamRef<jclass>&, |
41 const JavaParamRef<jstring>& message) { | 41 const JavaParamRef<jstring>& message) { |
42 AndroidLocationApiAdapter::OnNewErrorAvailable(env, message); | 42 AndroidLocationApiAdapter::OnNewErrorAvailable(env, message); |
43 } | 43 } |
44 | 44 |
45 namespace device { | 45 namespace device { |
46 | 46 |
47 AndroidLocationApiAdapter::AndroidLocationApiAdapter() | 47 AndroidLocationApiAdapter::AndroidLocationApiAdapter() |
48 : location_provider_(NULL) { | 48 : location_provider_(NULL) {} |
49 } | |
50 | 49 |
51 AndroidLocationApiAdapter::~AndroidLocationApiAdapter() { | 50 AndroidLocationApiAdapter::~AndroidLocationApiAdapter() { |
52 CHECK(!location_provider_); | 51 CHECK(!location_provider_); |
53 CHECK(!task_runner_.get()); | 52 CHECK(!task_runner_.get()); |
54 CHECK(java_location_provider_android_object_.is_null()); | 53 CHECK(java_location_provider_android_object_.is_null()); |
55 } | 54 } |
56 | 55 |
57 bool AndroidLocationApiAdapter::Start( | 56 bool AndroidLocationApiAdapter::Start( |
58 LocationProviderAndroid* location_provider, bool high_accuracy) { | 57 LocationProviderAndroid* location_provider, |
| 58 bool high_accuracy) { |
59 JNIEnv* env = AttachCurrentThread(); | 59 JNIEnv* env = AttachCurrentThread(); |
60 if (!location_provider_) { | 60 if (!location_provider_) { |
61 location_provider_ = location_provider; | 61 location_provider_ = location_provider; |
62 CHECK(java_location_provider_android_object_.is_null()); | 62 CHECK(java_location_provider_android_object_.is_null()); |
63 CreateJavaObject(env); | 63 CreateJavaObject(env); |
64 { | 64 { |
65 base::AutoLock lock(lock_); | 65 base::AutoLock lock(lock_); |
66 CHECK(!task_runner_.get()); | 66 CHECK(!task_runner_.get()); |
67 task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 67 task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
68 } | 68 } |
69 } | 69 } |
70 // At this point we should have all our pre-conditions ready, and they'd only | 70 // At this point we should have all our pre-conditions ready, and they'd only |
71 // change in Stop() which must be called on the same thread as here. | 71 // change in Stop() which must be called on the same thread as here. |
72 CHECK(location_provider_); | 72 CHECK(location_provider_); |
73 CHECK(task_runner_.get()); | 73 CHECK(task_runner_.get()); |
74 CHECK(!java_location_provider_android_object_.is_null()); | 74 CHECK(!java_location_provider_android_object_.is_null()); |
75 // We'll start receiving notifications from java in the main thread looper | 75 // We'll start receiving notifications from java in the main thread looper |
76 // until Stop() is called. | 76 // until Stop() is called. |
77 return Java_LocationProviderAdapter_start(env, | 77 return Java_LocationProviderAdapter_start( |
78 java_location_provider_android_object_.obj(), high_accuracy); | 78 env, java_location_provider_android_object_.obj(), high_accuracy); |
79 } | 79 } |
80 | 80 |
81 void AndroidLocationApiAdapter::Stop() { | 81 void AndroidLocationApiAdapter::Stop() { |
82 if (!location_provider_) { | 82 if (!location_provider_) { |
83 CHECK(!task_runner_.get()); | 83 CHECK(!task_runner_.get()); |
84 CHECK(java_location_provider_android_object_.is_null()); | 84 CHECK(java_location_provider_android_object_.is_null()); |
85 return; | 85 return; |
86 } | 86 } |
87 | 87 |
88 { | 88 { |
(...skipping 13 matching lines...) Expand all Loading... |
102 void AndroidLocationApiAdapter::NotifyProviderNewGeoposition( | 102 void AndroidLocationApiAdapter::NotifyProviderNewGeoposition( |
103 const Geoposition& geoposition) { | 103 const Geoposition& geoposition) { |
104 // Called on the geolocation thread, safe to access location_provider_ here. | 104 // Called on the geolocation thread, safe to access location_provider_ here. |
105 if (GetInstance()->location_provider_) { | 105 if (GetInstance()->location_provider_) { |
106 CHECK(GetInstance()->task_runner_->BelongsToCurrentThread()); | 106 CHECK(GetInstance()->task_runner_->BelongsToCurrentThread()); |
107 GetInstance()->location_provider_->NotifyNewGeoposition(geoposition); | 107 GetInstance()->location_provider_->NotifyNewGeoposition(geoposition); |
108 } | 108 } |
109 } | 109 } |
110 | 110 |
111 // static | 111 // static |
112 void AndroidLocationApiAdapter::OnNewLocationAvailable( | 112 void AndroidLocationApiAdapter::OnNewLocationAvailable(double latitude, |
113 double latitude, double longitude, double time_stamp, | 113 double longitude, |
114 bool has_altitude, double altitude, | 114 double time_stamp, |
115 bool has_accuracy, double accuracy, | 115 bool has_altitude, |
116 bool has_heading, double heading, | 116 double altitude, |
117 bool has_speed, double speed) { | 117 bool has_accuracy, |
| 118 double accuracy, |
| 119 bool has_heading, |
| 120 double heading, |
| 121 bool has_speed, |
| 122 double speed) { |
118 Geoposition position; | 123 Geoposition position; |
119 position.latitude = latitude; | 124 position.latitude = latitude; |
120 position.longitude = longitude; | 125 position.longitude = longitude; |
121 position.timestamp = base::Time::FromDoubleT(time_stamp); | 126 position.timestamp = base::Time::FromDoubleT(time_stamp); |
122 if (has_altitude) | 127 if (has_altitude) |
123 position.altitude = altitude; | 128 position.altitude = altitude; |
124 if (has_accuracy) | 129 if (has_accuracy) |
125 position.accuracy = accuracy; | 130 position.accuracy = accuracy; |
126 if (has_heading) | 131 if (has_heading) |
127 position.heading = heading; | 132 position.heading = heading; |
(...skipping 18 matching lines...) Expand all Loading... |
146 } | 151 } |
147 | 152 |
148 // static | 153 // static |
149 bool AndroidLocationApiAdapter::RegisterGeolocationService(JNIEnv* env) { | 154 bool AndroidLocationApiAdapter::RegisterGeolocationService(JNIEnv* env) { |
150 return RegisterNativesImpl(env); | 155 return RegisterNativesImpl(env); |
151 } | 156 } |
152 | 157 |
153 void AndroidLocationApiAdapter::CreateJavaObject(JNIEnv* env) { | 158 void AndroidLocationApiAdapter::CreateJavaObject(JNIEnv* env) { |
154 // Create the Java LocationProviderAdapter object. | 159 // Create the Java LocationProviderAdapter object. |
155 java_location_provider_android_object_.Reset( | 160 java_location_provider_android_object_.Reset( |
156 Java_LocationProviderAdapter_create(env, | 161 Java_LocationProviderAdapter_create( |
157 base::android::GetApplicationContext())); | 162 env, base::android::GetApplicationContext())); |
158 CHECK(!java_location_provider_android_object_.is_null()); | 163 CHECK(!java_location_provider_android_object_.is_null()); |
159 } | 164 } |
160 | 165 |
161 void AndroidLocationApiAdapter::OnNewGeopositionInternal( | 166 void AndroidLocationApiAdapter::OnNewGeopositionInternal( |
162 const Geoposition& geoposition) { | 167 const Geoposition& geoposition) { |
163 base::AutoLock lock(lock_); | 168 base::AutoLock lock(lock_); |
164 if (!task_runner_.get()) | 169 if (!task_runner_.get()) |
165 return; | 170 return; |
166 task_runner_->PostTask( | 171 task_runner_->PostTask( |
167 FROM_HERE, | 172 FROM_HERE, |
168 base::Bind(&AndroidLocationApiAdapter::NotifyProviderNewGeoposition, | 173 base::Bind(&AndroidLocationApiAdapter::NotifyProviderNewGeoposition, |
169 geoposition)); | 174 geoposition)); |
170 } | 175 } |
171 | 176 |
172 } // namespace device | 177 } // namespace device |
OLD | NEW |