Commit 8d43ae78 authored by Vyacheslav Koshcheyev's avatar Vyacheslav Koshcheyev

create QAndroidCompass

parent 21af8a15
/*
Offscreen Android Views library for Qt
Author:
Vyacheslav O. Koscheev <vok1980@gmail.com>
Distrbuted under The BSD License
Copyright (c) 2015, 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 "QAndroidCompass.h"
#include <QAndroidQPAPluginGap.h>
static const char * const c_full_class_name_ = "ru/dublgis/androidcompass/CompassProvider";
Q_DECL_EXPORT void JNICALL Java_setAzimut(JNIEnv * env, jobject, jlong inst, jfloat azimut)
{
Q_UNUSED(env);
try
{
if (inst)
{
void * vp = reinterpret_cast<void*>(inst);
QAndroidCompass * proxy = reinterpret_cast<QAndroidCompass*>(vp);
proxy->setAzimut(azimut);
return;
}
else
{
qWarning() << __FUNCTION__ << "Zero param!";
}
}
catch (std::exception & e)
{
qWarning() << __FUNCTION__ << " exception: " << e.what();
}
}
QAndroidCompass::QAndroidCompass(QObject * parent /*= 0*/) :
QObject(parent)
{
preloadJavaClasses();
// Creating Java object
handler_.reset(new QJniObject(c_full_class_name_, "J",
jlong(reinterpret_cast<void*>(this))));
}
QAndroidCompass::~QAndroidCompass()
{
if (handler_)
{
handler_->callVoid("cppDestroyed");
handler_.reset();
}
}
void QAndroidCompass::preloadJavaClasses()
{
static volatile bool preloaded_ = false;
if (!preloaded_)
{
try
{
preloaded_ = true;
QAndroidQPAPluginGap::preloadJavaClasses();
QAndroidQPAPluginGap::preloadJavaClass(c_full_class_name_);
qDebug() << "Pre-loading Java classes for QAndroidCompass...";
QJniClass ov(c_full_class_name_);
static const JNINativeMethod methods[] = {
{"getContext", "()Landroid/content/Context;", (void*)QAndroidQPAPluginGap::getCurrentContext},
{"setAzimut", "(JF)V", (void*)Java_setAzimut},
};
if (!ov.registerNativeMethods(methods, sizeof(methods)))
{
qWarning() << "Failed to register native methods";
return;
}
qDebug() << "Pre-loading Java classes for QAndroidCompass finished successfully";
}
catch(std::exception & e)
{
qWarning() << "Exception while registering native methods: " << e.what();
}
}
}
void QAndroidCompass::start(int32_t delayMks /*= -1*/)
{
if (handler_)
{
handler_->callParamVoid("start", "I", delayMks);
}
}
void QAndroidCompass::stop()
{
if (handler_)
{
handler_->callVoid("stop");
}
}
void QAndroidCompass::setAzimut(float azimut)
{
emit azimutChanged(azimut);
}
/*
Offscreen Android Views library for Qt
Author:
Vyacheslav O. Koscheev <vok1980@gmail.com>
Distrbuted under The BSD License
Copyright (c) 2015, 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 <QObject>
#include <QJniHelpers.h>
class QAndroidCompass : public QObject
{
Q_OBJECT
public:
QAndroidCompass(QObject * parent = 0);
virtual ~QAndroidCompass();
public slots:
void start(int32_t delayMks = -1);
void stop();
signals:
void azimutChanged(float azimut);
private:
void setAzimut(float azimut);
static void preloadJavaClasses();
private:
QScopedPointer<QJniObject> handler_;
friend void JNICALL Java_setAzimut(JNIEnv * env, jobject, jlong inst, jfloat azimut);
};
android-g++ {
INCLUDEPATH += $$PWD
QT += sensors
HEADERS += \
$$PWD/QAndroidCompass.h \
SOURCES += \
$$PWD/QAndroidCompass.cpp \
}
/*
Offscreen Android Views library for Qt
Author:
Vyacheslav O. Koscheev <vok1980@gmail.com>
Distrbuted under The BSD License
Copyright (c) 2015, 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.androidcompass;
import java.lang.Math;
import android.util.Log;
import android.content.Context;
import android.hardware.SensorManager;
import android.hardware.Sensor;
import android.hardware.SensorEventListener;
import android.hardware.SensorEvent;
public class CompassProvider implements SensorEventListener
{
private final String TAG = "Grym/CompassProvider";
private long mNativePtr = 0;
private SensorManager mSensorManager;
private Sensor mOrientation;
CompassProvider(long native_ptr)
{
mNativePtr = native_ptr;
// initialize your android device sensor capabilities
mSensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);
if (null == mSensorManager)
{
Log.e(TAG, "SensorManager is null");
return;
}
mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
}
//! Called from C++ to notify us that the associated C++ object is being destroyed.
public void cppDestroyed()
{
stop();
mNativePtr = 0;
}
public void start(int delayMksec)
{
if (null == mSensorManager || null == mOrientation)
{
Log.e(TAG, "Sensors are null");
return;
}
if (delayMksec < 0)
{
delayMksec = SensorManager.SENSOR_DELAY_NORMAL;
}
try
{
mSensorManager.registerListener(this, mOrientation, delayMksec);
}
catch(Exception e)
{
Log.e(TAG, e.getMessage());
}
}
public void stop()
{
if (null == mSensorManager)
{
Log.e(TAG, "SensorManager is null");
return;
}
try
{
// to stop the listener and save battery
mSensorManager.unregisterListener(this);
}
catch(Exception e)
{
Log.e(TAG, e.getMessage());
}
}
public void onSensorChanged(SensorEvent event)
{
if (event.sensor.getType() == mOrientation.getType())
{
setAzimut(mNativePtr, event.values[0]);
}
}
public void onAccuracyChanged (Sensor sensor, int accuracy)
{
}
private native Context getContext();
private native void setAzimut(long nativeptr, float azimut);
};
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