-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathandroidpositionprovider.cpp
More file actions
283 lines (222 loc) · 9.43 KB
/
androidpositionprovider.cpp
File metadata and controls
283 lines (222 loc) · 9.43 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "androidpositionprovider.h"
#include "coreutils.h"
#include "qgis.h"
#include <QCoreApplication>
#include <QLocationPermission>
#include <android/log.h>
#include <QTimeZone>
#include "inpututils.h"
int AndroidPositionProvider::sLastInstanceId = 0;
QMap<int, AndroidPositionProvider *> AndroidPositionProvider::sInstances;
void jniOnPositionUpdated( JNIEnv *env, jclass clazz, jint instanceId, jobject locationObj, jobject gnssStatusObj )
{
AndroidPositionProvider *inst = AndroidPositionProvider::sInstances[instanceId];
if ( !inst )
{
__android_log_print( ANDROID_LOG_ERROR, "CPP", "[c++] unknown instance! %d", instanceId );
return;
}
QJniObject location( locationObj );
if ( !location.isValid() )
{
__android_log_print( ANDROID_LOG_ERROR, "CPP", "[c++] invalid location obj" );
return;
}
const jdouble latitude = location.callMethod<jdouble>( "getLatitude" );
const jdouble longitude = location.callMethod<jdouble>( "getLongitude" );
const jlong timestamp = location.callMethod<jlong>( "getTime" );
GeoPosition pos;
pos.latitude = latitude;
pos.longitude = longitude;
pos.utcDateTime = QDateTime::fromMSecsSinceEpoch( timestamp, QTimeZone::UTC );
if ( location.callMethod<jboolean>( "hasAltitude" ) )
{
const jdouble ellipsoidHeight = location.callMethod<jdouble>( "getAltitude" );
if ( !qFuzzyIsNull( ellipsoidHeight ) )
{
bool positionOutsideGeoidModelArea = false;
// transform the altitude from EPSG:4979 (WGS84 (EPSG:4326) + ellipsoidal height) to specified geoid model
const QgsPoint geoidPosition = InputUtils::transformPoint(
PositionKit::positionCrs3DEllipsoidHeight(),
PositionKit::positionCrs3D(),
QgsProject::instance()->transformContext(),
{longitude, latitude, ellipsoidHeight},
positionOutsideGeoidModelArea );
if ( !positionOutsideGeoidModelArea )
{
pos.elevation = geoidPosition.z();
const double geoidSeparation = ellipsoidHeight - geoidPosition.z();
pos.elevation_diff = geoidSeparation;
}
}
}
// horizontal accuracy
if ( location.callMethod<jboolean>( "hasAccuracy" ) )
{
const jfloat accuracy = location.callMethod<jfloat>( "getAccuracy" );
if ( !qFuzzyIsNull( accuracy ) )
pos.hacc = accuracy;
}
// vertical accuracy (available since API Level 26 (Android 8.0))
if ( QNativeInterface::QAndroidApplication::sdkVersion() >= 26 )
{
if ( location.callMethod<jboolean>( "hasVerticalAccuracy" ) )
{
const jfloat accuracy = location.callMethod<jfloat>( "getVerticalAccuracyMeters" );
if ( !qFuzzyIsNull( accuracy ) )
pos.vacc = accuracy;
}
}
// ground speed
if ( location.callMethod<jboolean>( "hasSpeed" ) )
{
const jfloat speed = location.callMethod<jfloat>( "getSpeed" );
if ( !qFuzzyIsNull( speed ) )
pos.speed = speed * 3.6; // convert from m/s to km/h
// could also use getSpeedAccuracyMetersPerSecond() since API level 26 (Android 8.0)
}
// bearing
if ( location.callMethod<jboolean>( "hasBearing" ) )
{
const jfloat bearing = location.callMethod<jfloat>( "getBearing" );
if ( !qFuzzyIsNull( bearing ) )
pos.direction = bearing;
// could also use getBearingAccuracyDegrees() since API level 26 (Android 8.0)
}
// detect if location is mocked (useful to check if 3rd party app is setting it for external GNSS receiver)
// we only use this to show users that the mock location is active
jboolean isMock = false;
if ( QtAndroidPrivate::androidSdkVersion() >= 31 )
{
isMock = location.callMethod<jboolean>( "isMock" );
}
else
{
isMock = location.callMethod<jboolean>( "isFromMockProvider" );
}
pos.isMock = isMock;
// could also use getExtras() to get further details from mocked location
// (the key/value pairs are vendor-specific, and could include things like DOP,
// info about corrections, geoid undulation, receiver model)
__android_log_print( ANDROID_LOG_INFO, "CPP", "[c++] pos %f %f", latitude, longitude );
QJniObject gnssStatus( gnssStatusObj );
if ( gnssStatus.isValid() )
{
int satellitesUsed = 0;
const int satellitesCount = gnssStatus.callMethod<jint>( "getSatelliteCount" );
for ( int i = 0; i < satellitesCount; ++i )
{
if ( gnssStatus.callMethod<jboolean>( "usedInFix", i ) )
++satellitesUsed;
// we could get more info here (ID, azimuth, elevation, signal strength, ...)
// but we are not using that anywhere
}
pos.satellitesVisible = satellitesCount;
pos.satellitesUsed = satellitesUsed;
}
QMetaObject::invokeMethod( inst, "positionChanged",
Qt::AutoConnection, Q_ARG( GeoPosition, pos ) );
}
AndroidPositionProvider::AndroidPositionProvider( bool fused, QObject *parent )
: AbstractPositionProvider( fused ? QStringLiteral( "android_fused" ) : QStringLiteral( "android_gps" ),
QStringLiteral( "internal" ),
fused ? tr( "Internal (fused)" ) : tr( "Internal (gps)" ), parent )
, mFused( fused )
, mInstanceId( ++sLastInstanceId )
{
__android_log_print( ANDROID_LOG_INFO, "CPP", "[c++] CONSTRUCT" );
Q_ASSERT( !sInstances.contains( mInstanceId ) );
sInstances[mInstanceId] = this;
// register the native methods
JNINativeMethod methods[]
{
{
"jniOnPositionUpdated",
"(ILandroid/location/Location;Landroid/location/GnssStatus;)V",
reinterpret_cast<void *>( jniOnPositionUpdated )
}
};
QJniEnvironment javaenv;
javaenv.registerNativeMethods( "uk/co/lutraconsulting/MMAndroidPosition", methods, 1 );
// create the Java object
__android_log_print( ANDROID_LOG_INFO, "CPP", "[c++] create Java object" );
QJniObject context = QNativeInterface::QAndroidApplication::context();
mAndroidPos = QJniObject::callStaticObjectMethod(
"uk/co/lutraconsulting/MMAndroidPosition",
"createWithJniCallback",
"(Landroid/content/Context;ZI)Luk/co/lutraconsulting/MMAndroidPosition;",
context.object(),
mFused,
mInstanceId );
AndroidPositionProvider::startUpdates();
}
AndroidPositionProvider::~AndroidPositionProvider()
{
__android_log_print( ANDROID_LOG_INFO, "CPP", "DESTRUCT" );
Q_ASSERT( sInstances[mInstanceId] == this );
sInstances.remove( mInstanceId );
}
bool AndroidPositionProvider::isFusedAvailable()
{
return QJniObject::callStaticMethod<jboolean>( "uk/co/lutraconsulting/MMAndroidPosition", "isFusedLocationProviderAvailable",
"(Landroid/content/Context;)Z", QNativeInterface::QAndroidApplication::context() );
}
QString AndroidPositionProvider::fusedErrorString()
{
QJniObject context = QNativeInterface::QAndroidApplication::context();
QJniObject str = QJniObject::callStaticObjectMethod(
"uk/co/lutraconsulting/MMAndroidPosition",
"fusedLocationProviderErrorString",
"(Landroid/content/Context;)Ljava/lang/String;",
context.object() );
return str.toString();
}
void AndroidPositionProvider::startUpdates()
{
__android_log_print( ANDROID_LOG_INFO, "CPP", "[c++] start updates" );
// permissions are currently being requested in main.qml, so here
// we only check that we have the permissions we need.
QLocationPermission perm;
perm.setAccuracy( QLocationPermission::Precise );
if ( qApp->checkPermission( perm ) != Qt::PermissionStatus::Granted )
{
__android_log_print( ANDROID_LOG_ERROR, "CPP", "[c++] no location permissions - not starting!" );
setState( tr( "No location permissions" ), State::NoConnection );
return;
}
jboolean res = mAndroidPos.callMethod<jboolean>( "start", "()Z" );
__android_log_print( ANDROID_LOG_INFO, "CPP", "[c++] start updates res: %d", res );
if ( !res )
{
QJniObject errMsgJni = mAndroidPos.callObjectMethod( "errorMessage", "()Ljava/lang/String;" );
QString errMsg = errMsgJni.toString();
__android_log_print( ANDROID_LOG_INFO, "CPP", "[c++] error: %s", errMsg.toUtf8().constData() );
if ( errMsg == "MISSING_PERMISSIONS" )
setState( tr( "No location permissions" ), State::NoConnection );
else if ( errMsg == "FUSED_NOT_AVAILABLE" )
setState( tr( "Fused location not available" ), State::NoConnection );
else
setState( errMsg, State::NoConnection );
return;
}
setState( tr( "Connected" ), State::Connected );
}
void AndroidPositionProvider::stopUpdates()
{
__android_log_print( ANDROID_LOG_INFO, "CPP", "[c++] stop updates" );
jboolean res = mAndroidPos.callMethod<jboolean>( "stop", "()Z" );
__android_log_print( ANDROID_LOG_INFO, "CPP", "[c++] stop updates res: %d", res );
}
void AndroidPositionProvider::closeProvider()
{
stopUpdates();
mAndroidPos = QJniObject();
}