1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/geolocation/location_api_adapter_android.h"
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/bind.h"
#include "base/location.h"
#include "content/browser/geolocation/location_provider_android.h"
#include "jni/location_provider_jni.h"
using base::android::AttachCurrentThread;
using base::android::CheckException;
using base::android::ClearException;
using base::android::GetMethodID;
static void NewLocationAvailable(JNIEnv* env, jclass,
jdouble latitude,
jdouble longitude,
jlong time_stamp,
jboolean has_altitude, jdouble altitude,
jboolean has_accuracy, jdouble accuracy,
jboolean has_heading, jdouble heading,
jboolean has_speed, jdouble speed) {
AndroidLocationApiAdapter::OnNewLocationAvailable(latitude, longitude,
time_stamp, has_altitude, altitude, has_accuracy, accuracy,
has_heading, heading, has_speed, speed);
}
static void NewErrorAvailable(JNIEnv* env, jclass, jstring message) {
AndroidLocationApiAdapter::OnNewErrorAvailable(env, message);
}
AndroidLocationApiAdapter::AndroidLocationApiAdapter()
: location_provider_(NULL) {
}
AndroidLocationApiAdapter::~AndroidLocationApiAdapter() {
CHECK(!location_provider_);
CHECK(!message_loop_);
CHECK(java_location_provider_android_object_.is_null());
}
bool AndroidLocationApiAdapter::Start(
LocationProviderAndroid* location_provider, bool high_accuracy) {
JNIEnv* env = AttachCurrentThread();
if (!location_provider_) {
location_provider_ = location_provider;
CHECK(java_location_provider_android_object_.is_null());
CreateJavaObject(env);
{
base::AutoLock lock(lock_);
CHECK(!message_loop_.get());
message_loop_ = base::MessageLoopProxy::current();
}
}
// At this point we should have all our pre-conditions ready, and they'd only
// change in Stop() which must be called on the same thread as here.
CHECK(location_provider_);
CHECK(message_loop_.get());
CHECK(!java_location_provider_android_object_.is_null());
// We'll start receiving notifications from java in the main thread looper
// until Stop() is called.
return Java_LocationProvider_start(env,
java_location_provider_android_object_.obj(), high_accuracy);
}
void AndroidLocationApiAdapter::Stop() {
if (!location_provider_) {
CHECK(!message_loop_.get());
CHECK(java_location_provider_android_object_.is_null());
return;
}
{
base::AutoLock lock(lock_);
message_loop_ = NULL;
}
location_provider_ = NULL;
JNIEnv* env = AttachCurrentThread();
Java_LocationProvider_stop(env, java_location_provider_android_object_.obj());
java_location_provider_android_object_.Reset();
}
// static
void AndroidLocationApiAdapter::NotifyProviderNewGeoposition(
const content::Geoposition& geoposition) {
// Called on the geolocation thread, safe to access location_provider_ here.
if (GetInstance()->location_provider_) {
CHECK(GetInstance()->message_loop_->BelongsToCurrentThread());
GetInstance()->location_provider_->NotifyNewGeoposition(geoposition);
}
}
// static
void AndroidLocationApiAdapter::OnNewLocationAvailable(
double latitude, double longitude, long time_stamp,
bool has_altitude, double altitude,
bool has_accuracy, double accuracy,
bool has_heading, double heading,
bool has_speed, double speed) {
content::Geoposition position;
position.latitude = latitude;
position.longitude = longitude;
position.timestamp = base::Time::FromDoubleT(time_stamp / 1000.0);
if (has_altitude)
position.altitude = altitude;
if (has_accuracy)
position.accuracy = accuracy;
if (has_heading)
position.heading = heading;
if (has_speed)
position.speed = speed;
GetInstance()->OnNewGeopositionInternal(position);
}
// static
void AndroidLocationApiAdapter::OnNewErrorAvailable(JNIEnv* env,
jstring message) {
content::Geoposition position_error;
position_error.error_code =
content::Geoposition::ERROR_CODE_POSITION_UNAVAILABLE;
position_error.error_message =
base::android::ConvertJavaStringToUTF8(env, message);
GetInstance()->OnNewGeopositionInternal(position_error);
}
// static
AndroidLocationApiAdapter* AndroidLocationApiAdapter::GetInstance() {
return Singleton<AndroidLocationApiAdapter>::get();
}
// static
bool AndroidLocationApiAdapter::RegisterGeolocationService(JNIEnv* env) {
return RegisterNativesImpl(env);
}
void AndroidLocationApiAdapter::CreateJavaObject(JNIEnv* env) {
// Create the Java AndroidLocationProvider object.
java_location_provider_android_object_.Reset(
Java_LocationProvider_create(env,
base::android::GetApplicationContext()));
CHECK(!java_location_provider_android_object_.is_null());
}
void AndroidLocationApiAdapter::OnNewGeopositionInternal(
const content::Geoposition& geoposition) {
base::AutoLock lock(lock_);
if (!message_loop_)
return;
message_loop_->PostTask(
FROM_HERE,
base::Bind(
&AndroidLocationApiAdapter::NotifyProviderNewGeoposition,
geoposition));
}
|