Commit 748338aa authored by Vyacheslav Koscheev's avatar Vyacheslav Koscheev

Create QGeoPositionInfoSourceAndroidPassive

parent 6ab527dd
/*
Offscreen Android Views library for Qt
Author:
Vyacheslav O. Koscheev <vok1980@gmail.com>
Distrbuted under The BSD License
Copyright (c) 2017, DoubleGIS, LLC.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the DoubleGIS, LLC nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "PositionInfoConvertor.h"
static void setPositionAttributeFloat(QGeoPositionInfo & info, QGeoPositionInfo::Attribute attr,
QJniObject & location, const char * szCheck, const char * szGet)
{
if (location.callBool(szCheck))
{
jfloat val = location.callFloat(szGet);
info.setAttribute(attr, val);
}
}
QGeoPositionInfo positionInfoFromJavaLocation(const jobject jlocation)
{
QGeoPositionInfo info;
QJniObject location(jlocation, false);
if (!location)
{
qWarning() << "null location";
return QGeoPositionInfo();
}
jdouble latitude = location.callDouble("getLatitude");
jdouble longitude = location.callDouble("getLongitude");
QGeoCoordinate coordinate(latitude, longitude);
if (location.callBool("hasAltitude"))
{
jdouble value = location.callDouble("getAltitude");
coordinate.setAltitude(value);
}
info.setCoordinate(coordinate);
jlong timestamp = location.callLong("getTime");
info.setTimestamp(QDateTime::fromMSecsSinceEpoch(timestamp));
setPositionAttributeFloat(info, QGeoPositionInfo::HorizontalAccuracy, location, "hasAccuracy", "getAccuracy");
setPositionAttributeFloat(info, QGeoPositionInfo::GroundSpeed, location, "hasSpeed", "getSpeed");
setPositionAttributeFloat(info, QGeoPositionInfo::Direction, location, "hasBearing", "getBearing");
return info;
}
/*
Offscreen Android Views library for Qt
Author:
Vyacheslav O. Koscheev <vok1980@gmail.com>
Distrbuted under The BSD License
Copyright (c) 2017, DoubleGIS, LLC.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the DoubleGIS, LLC nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <QtPositioning/QGeoPositionInfo>
#include <QJniHelpers.h>
QGeoPositionInfo positionInfoFromJavaLocation(const jobject jlocation);
......@@ -41,55 +41,12 @@
#include <QtPositioning/QGeoPositionInfo>
#include <QtGui/QGuiApplication>
#include <TJniObjectLinker.h>
#include "PositionInfoConvertor.h"
static const char * const c_full_class_name_ = "ru/dublgis/androidlocation/GmsLocationProvider";
static void setPositionAttributeFloat(QGeoPositionInfo & info, QGeoPositionInfo::Attribute attr,
QJniObject & location, const char * szCheck, const char * szGet)
{
if (location.callBool(szCheck))
{
jfloat val = location.callFloat(szGet);
info.setAttribute(attr, val);
}
}
static QGeoPositionInfo positionInfoFromJavaLocation(JNIEnv *, const jobject & jlocation)
{
QGeoPositionInfo info;
QJniObject location(jlocation, false);
if (!location)
{
qWarning() << "null location";
return QGeoPositionInfo();
}
jdouble latitude = location.callDouble("getLatitude");
jdouble longitude = location.callDouble("getLongitude");
QGeoCoordinate coordinate(latitude, longitude);
if (location.callBool("hasAltitude"))
{
jdouble value = location.callDouble("getAltitude");
coordinate.setAltitude(value);
}
info.setCoordinate(coordinate);
jlong timestamp = location.callLong("getTime");
info.setTimestamp(QDateTime::fromMSecsSinceEpoch(timestamp));
setPositionAttributeFloat(info, QGeoPositionInfo::HorizontalAccuracy, location, "hasAccuracy", "getAccuracy");
setPositionAttributeFloat(info, QGeoPositionInfo::GroundSpeed, location, "hasSpeed", "getSpeed");
setPositionAttributeFloat(info, QGeoPositionInfo::Direction, location, "hasBearing", "getBearing");
return info;
}
Q_DECL_EXPORT void JNICALL Java_GooglePlayServiceLocationProvider_locationStatus(JNIEnv *, jobject, jlong param, jint state)
......@@ -99,7 +56,7 @@ Q_DECL_EXPORT void JNICALL Java_GooglePlayServiceLocationProvider_locationStatus
}
Q_DECL_EXPORT void JNICALL Java_GooglePlayServiceLocationProvider_locationRecieved(JNIEnv * env, jobject, jlong param, jobject location, jboolean initial, jlong requestId)
Q_DECL_EXPORT void JNICALL Java_GooglePlayServiceLocationProvider_locationRecieved(JNIEnv *, jobject, jlong param, jobject location, jboolean initial, jlong requestId)
{
if (0x0 == location)
{
......@@ -107,7 +64,7 @@ Q_DECL_EXPORT void JNICALL Java_GooglePlayServiceLocationProvider_locationReciev
}
JNI_LINKER_OBJECT(QAndroidGmsLocationProvider, param, obj)
QGeoPositionInfo posInfo = positionInfoFromJavaLocation(env, location);
QGeoPositionInfo posInfo = positionInfoFromJavaLocation(location);
obj->onLocationRecieved(posInfo, initial, requestId);
}
......
/*
Offscreen Android Views library for Qt
Author:
Vyacheslav O. Koscheev <vok1980@gmail.com>
Distrbuted under The BSD License
Copyright (c) 2017, DoubleGIS, LLC.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the DoubleGIS, LLC nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "QGeoPositionInfoSourceAndroidPassive.h"
#include <QtPositioning/QGeoPositionInfo>
#include <TJniObjectLinker.h>
#include "PositionInfoConvertor.h"
Q_DECLARE_METATYPE(QGeoPositionInfo)
static const char * const c_full_class_name_ = "ru/dublgis/androidlocation/PassiveLocationProvider";
Q_DECL_EXPORT void JNICALL Java_GeoPositionInfoSourceAndroidPassive_onLocation(JNIEnv *, jobject, jlong param, jobject location)
{
if (0x0 == location)
{
return;
}
JNI_LINKER_OBJECT(QGeoPositionInfoSourceAndroidPassive, param, obj)
QGeoPositionInfo posInfo = positionInfoFromJavaLocation(location);
obj->onLocationRecieved(posInfo);
}
static const JNINativeMethod methods[] = {
{"getActivity", "()Landroid/app/Activity;", reinterpret_cast<void*>(QAndroidQPAPluginGap::getActivityNoThrow)},
{"onLocationRecieved", "(JLandroid/location/Location;)V", reinterpret_cast<void*>(Java_GeoPositionInfoSourceAndroidPassive_onLocation)},
};
JNI_LINKER_IMPL(QGeoPositionInfoSourceAndroidPassive, c_full_class_name_, methods)
QGeoPositionInfoSourceAndroidPassive::QGeoPositionInfoSourceAndroidPassive(QObject * parent /*= 0*/)
: QGeoPositionInfoSource(parent)
, jniLinker_(new JniObjectLinker(this))
, started_(false)
{
qRegisterMetaType<QGeoPositionInfo>();
setPreferredPositioningMethods(QGeoPositionInfoSource::NonSatellitePositioningMethods);
}
QGeoPositionInfoSourceAndroidPassive::~QGeoPositionInfoSourceAndroidPassive()
{
}
void QGeoPositionInfoSourceAndroidPassive::startUpdates()
{
if (!started_ && isJniReady())
{
if (jni()->callBool("startLocationUpdates"))
{
started_ = true;
}
}
}
void QGeoPositionInfoSourceAndroidPassive::stopUpdates()
{
if (isJniReady())
{
started_ = false;
jni()->callVoid("stopLocationUpdates");
}
}
void QGeoPositionInfoSourceAndroidPassive::requestUpdate(int timeout /*= 0*/)
{
Q_UNUSED(timeout);
}
void QGeoPositionInfoSourceAndroidPassive::setUpdateInterval(int msec)
{
Q_UNUSED(msec);
}
QGeoPositionInfo QGeoPositionInfoSourceAndroidPassive::lastKnownPosition(bool fromSatellitePositioningMethodsOnly /*= false*/) const
{
QGeoPositionInfo info;
try
{
if (isJniReady())
{
QScopedPointer<QJniObject> jlocation(
jni()->callStaticParamObject(
"lastKnownPosition",
"Landroid/location/Location;",
"Z",
jboolean(fromSatellitePositioningMethodsOnly)));
if (jlocation && jlocation->jObject())
{
info = positionInfoFromJavaLocation(jlocation->jObject());
}
}
}
catch (const std::exception & e)
{
qWarning() << "Failed to get lastKnownPosition: " << e.what();
}
return info;
}
QGeoPositionInfoSource::PositioningMethods QGeoPositionInfoSourceAndroidPassive::supportedPositioningMethods() const
{
return QGeoPositionInfoSource::NoPositioningMethods;
}
void QGeoPositionInfoSourceAndroidPassive::setPreferredPositioningMethods(const PositioningMethods methods)
{
QGeoPositionInfoSource::setPreferredPositioningMethods(methods);
}
int QGeoPositionInfoSourceAndroidPassive::minimumUpdateInterval() const
{
return 0;
}
QGeoPositionInfoSource::Error QGeoPositionInfoSourceAndroidPassive::error() const
{
return QGeoPositionInfoSource::NoError;
}
void QGeoPositionInfoSourceAndroidPassive::onLocationRecieved(QGeoPositionInfo location)
{
emit positionUpdated(location);
}
/*
Offscreen Android Views library for Qt
Author:
Vyacheslav O. Koscheev <vok1980@gmail.com>
Distrbuted under The BSD License
Copyright (c) 2017, DoubleGIS, LLC.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the DoubleGIS, LLC nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <QtPositioning/QGeoPositionInfoSource>
#include <IJniObjectLinker.h>
class QGeoPositionInfoSourceAndroidPassive : public QGeoPositionInfoSource
{
Q_OBJECT
JNI_LINKER_DECL(QGeoPositionInfoSourceAndroidPassive)
Q_DISABLE_COPY(QGeoPositionInfoSourceAndroidPassive)
public:
QGeoPositionInfoSourceAndroidPassive(QObject * parent = 0);
virtual ~QGeoPositionInfoSourceAndroidPassive();
public:
// From QGeoPositionInfoSource
virtual void setUpdateInterval(int msec);
virtual QGeoPositionInfo lastKnownPosition(bool fromSatellitePositioningMethodsOnly = false) const;
virtual PositioningMethods supportedPositioningMethods() const;
virtual void setPreferredPositioningMethods(const PositioningMethods methods);
virtual int minimumUpdateInterval() const;
virtual Error error() const;
public Q_SLOTS:
virtual void startUpdates();
virtual void stopUpdates();
virtual void requestUpdate(int timeout = 0);
private:
friend void JNICALL Java_GeoPositionInfoSourceAndroidPassive_onLocation(JNIEnv * env, jobject, jlong param, jobject location);
void onLocationRecieved(QGeoPositionInfo location);
private:
bool started_;
};
......@@ -7,12 +7,15 @@ HEADERS += \
$$PWD/QLocationManagerProvidersListener.h \
$$PWD/QAndroidGmsLocationProvider.h \
$$PWD/QGeoPositionInfoSourceAndroidGms.h \
$$PWD/QGeoPositionInfoSourceAndroidPassive.h \
$$PWD/PositionInfoConvertor.h \
$$PWD/QNmeaListener.h
SOURCES += \
$$PWD/QLocationManagerProvidersListener.cpp \
$$PWD/QAndroidGmsLocationProvider.cpp \
$$PWD/QGeoPositionInfoSourceAndroidGms.cpp \
$$PWD/QGeoPositionInfoSourceAndroidPassive.cpp \
$$PWD/PositionInfoConvertor.cpp \
$$PWD/QNmeaListener.cpp
}
/*
Offscreen Android Views library for Qt
Author:
Vyacheslav O. Koscheev <vok1980@gmail.com>
Distrbuted under The BSD License
Copyright (c) 2017, DoubleGIS, LLC.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the DoubleGIS, LLC nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
package ru.dublgis.androidlocation;
import android.app.PendingIntent;
import android.content.Context;
import android.app.Activity;
import android.location.Criteria;
import android.os.Bundle;
import android.os.Looper;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.util.Log;
public class PassiveLocationProvider implements LocationListener
{
private static final String TAG = "PassiveLocationProvider";
private volatile long native_ptr_ = 0;
static LocationManager mLocationManager = null;
private Looper mlocationUpdatesLooper = null;
final private Thread mlocationUpdatesThread = new Thread() {
public void run() {
Looper.prepare();
mlocationUpdatesLooper = Looper.myLooper();
Looper.loop();
}
};
public PassiveLocationProvider(long native_ptr)
{
Log.i(TAG, "PassiveLocationProvider");
native_ptr_ = native_ptr;
setContext(getActivity());
mlocationUpdatesThread.start();
}
//! Called from C++ to notify us that the associated C++ object is being destroyed.
public void cppDestroyed()
{
Log.i(TAG, "cppDestroyed");
native_ptr_ = 0;
if (null != mlocationUpdatesLooper)
{
mlocationUpdatesLooper.quit();
}
try {
if (mlocationUpdatesThread.isAlive()) {
mlocationUpdatesThread.join(300);
}
} catch (InterruptedException e) {
Log.e(TAG, "Exception in cppDestroyed: ", e);
}
}
static public void setContext(Context context) {
Log.i(TAG, "setContext");
if (null != context) {
try {
Log.i(TAG, "null != context");
mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
} catch(Exception e) {
Log.e(TAG, "Failed to get LocationManager", e);
}
}
}
static public Location lastKnownPosition(boolean fromSatelliteOnly) {
Log.i(TAG, "lastKnownPosition");
try {
if (null != mLocationManager) {
Log.i(TAG, "lastKnownPosition, mLocationManager not null");
return mLocationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
}
} catch(Throwable e) {
Log.e(TAG, "Failed to get last known position", e);
}
Log.i(TAG, "lastKnownPosition, return null");
return null;
}
public void onLocationChanged(Location location) {
onLocationRecieved(native_ptr_, location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
static public boolean requestSingleUpdate(PendingIntent intent) {
try {
if (null != mLocationManager) {
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_HIGH);
mLocationManager.requestSingleUpdate(criteria, intent);
return true;
}
} catch(Throwable e) {
Log.e(TAG, "Failed to start location updates", e);
}
return false;
}
public boolean startLocationUpdates() {
try {
if (null != mLocationManager) {
mLocationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, this, mlocationUpdatesLooper);
return true;
}
} catch(Throwable e) {
Log.e(TAG, "Failed to start location updates", e);
}
return false;
}
public void stopLocationUpdates() {
try {
if (null != mLocationManager) {
mLocationManager.removeUpdates(this);
}
} catch(Throwable e) {
Log.e(TAG, "Failed to remove updates", e);
}
}
public native Activity getActivity();
public native void onLocationRecieved(long nativeptr, Location location);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment