Commit 9fb4d9c3 authored by Vyacheslav Koscheev's avatar Vyacheslav Koscheev

add TJniObjectLinker usage

parent bc5e8b9f
......@@ -37,10 +37,19 @@
#include "QAndroidScreenLocker.h"
#include <QAndroidQPAPluginGap.h>
#include <TJniObjectLinker.h>
static const char * const c_full_class_name_ = "ru/dublgis/androidhelpers/ScreenLocker";
static const JNINativeMethod methods[] =
{
{"getContext", "()Landroid/content/Context;", (void*)QAndroidQPAPluginGap::getCurrentContext},
};
JNI_LINKER_IMPL(QAndroidScreenLocker, c_full_class_name_, methods)
QAndroidScreenLocker & QAndroidScreenLocker::instance()
{
......@@ -49,64 +58,43 @@ QAndroidScreenLocker & QAndroidScreenLocker::instance()
}
QAndroidScreenLocker::QAndroidScreenLocker() :
QLocks::QLockedObject(true)
QAndroidScreenLocker::QAndroidScreenLocker()
: QLocks::QLockedObject(true)
, jniLinker_(new JniObjectLinker(this))
{
preloadJavaClasses();
// Creating Java object
java_handler_.reset(new QJniObject(c_full_class_name_, "J",
jlong(reinterpret_cast<void *>(this))));
}
QAndroidScreenLocker::~QAndroidScreenLocker()
{
if (java_handler_)
{
java_handler_->callVoid("cppDestroyed");
java_handler_.reset();
}
}
void QAndroidScreenLocker::preloadJavaClasses()
void QAndroidScreenLocker::lock()
{
static volatile bool preloaded_ = false;
if (!preloaded_)
{
preloaded_ = true;
QAndroidQPAPluginGap::preloadJavaClasses();
QAndroidQPAPluginGap::preloadJavaClass(c_full_class_name_);
QJniClass ov(c_full_class_name_);
static const JNINativeMethod methods[] =
if (isJniReady())
{
{"getContext", "()Landroid/content/Context;", (void*)QAndroidQPAPluginGap::getCurrentContext},
};
ov.registerNativeMethods(methods, sizeof(methods));
jni()->callBool("Lock");
}
}
void QAndroidScreenLocker::lock()
{
java_handler_->callBool("Lock");
Q_ASSERT(isLocked());
}
void QAndroidScreenLocker::unlock()
{
java_handler_->callVoid("Unlock");
if (isJniReady())
{
jni()->callVoid("Unlock");
}
}
bool QAndroidScreenLocker::isLocked()
{
return java_handler_->callBool("IsLocked");
if (isJniReady())
{
return jni()->callBool("IsLocked");
}
return false;
}
......@@ -38,6 +38,7 @@
#include <QJniHelpers.h>
#include "QLocks/QLockedObject.h"
#include <IJniObjectLinker.h>
/*!
......@@ -47,6 +48,7 @@
class QAndroidScreenLocker: public QLocks::QLockedObject
{
Q_DISABLE_COPY(QAndroidScreenLocker)
JNI_LINKER_DECL(QAndroidScreenLocker)
public:
static QAndroidScreenLocker& instance();
......@@ -57,13 +59,9 @@ private:
public:
bool isLocked();
static void preloadJavaClasses();
private:
virtual void lock();
virtual void unlock();
private:
QScopedPointer<QJniObject> java_handler_;
};
......@@ -37,72 +37,56 @@
#include "QAndroidSharedPreferences.h"
#include <QAndroidQPAPluginGap.h>
#include <TJniObjectLinker.h>
static const char * const c_full_class_name_ = "ru/dublgis/androidhelpers/SharedPreferencesHelper";
QAndroidSharedPreferences::QAndroidSharedPreferences(QObject * parent /*= 0*/) :
QObject(parent)
static const JNINativeMethod methods[] =
{
preloadJavaClasses();
{"getContext", "()Landroid/content/Context;", (void*)QAndroidQPAPluginGap::getCurrentContext},
};
// Creating Java object
java_handler_.reset(new QJniObject(c_full_class_name_, "J",
jlong(reinterpret_cast<void*>(this))));
}
JNI_LINKER_IMPL(QAndroidSharedPreferences, c_full_class_name_, methods)
QAndroidSharedPreferences::~QAndroidSharedPreferences()
QAndroidSharedPreferences::QAndroidSharedPreferences(QObject * parent /*= 0*/)
: QObject(parent)
, jniLinker_(new JniObjectLinker(this))
{
if (java_handler_)
{
java_handler_->callVoid("cppDestroyed");
java_handler_.reset();
}
}
void QAndroidSharedPreferences::preloadJavaClasses()
QAndroidSharedPreferences::~QAndroidSharedPreferences()
{
static volatile bool preloaded_ = false;
if (!preloaded_)
{
preloaded_ = true;
QAndroidQPAPluginGap::preloadJavaClasses();
QAndroidQPAPluginGap::preloadJavaClass(c_full_class_name_);
QJniClass ov(c_full_class_name_);
static const JNINativeMethod methods[] =
{
{"getContext", "()Landroid/content/Context;", (void*)QAndroidQPAPluginGap::getCurrentContext},
};
ov.registerNativeMethods(methods, sizeof(methods));
}
}
void QAndroidSharedPreferences::writeString(const QString &key, const QString &value)
{
if (isJniReady())
{
QJniEnvPtr jep;
java_handler_->callParamVoid("WriteString", "Ljava/lang/String;Ljava/lang/String;",
jni()->callParamVoid("WriteString", "Ljava/lang/String;Ljava/lang/String;",
QJniLocalRef(jep, key).jObject(), QJniLocalRef(jep, value).jObject());
}
}
QString QAndroidSharedPreferences::readString(const QString & key, const QString & valueDefault)
{
QJniEnvPtr jep;
QString ret;
QString ret = valueDefault;
try
{
ret = java_handler_->callParamString("ReadString", "Ljava/lang/String;Ljava/lang/String;",
if (isJniReady())
{
ret = jni()->callParamString("ReadString", "Ljava/lang/String;Ljava/lang/String;",
QJniLocalRef(jep, key).jObject(), QJniLocalRef(jep, valueDefault).jObject());
}
}
catch (const std::exception & e)
......@@ -116,16 +100,24 @@ QString QAndroidSharedPreferences::readString(const QString & key, const QString
void QAndroidSharedPreferences::writeInt(const QString & key, int32_t value)
{
if (isJniReady())
{
QJniEnvPtr jep;
java_handler_->callParamVoid("WriteInt", "Ljava/lang/String;I",
jni()->callParamVoid("WriteInt", "Ljava/lang/String;I",
QJniLocalRef(jep, key).jObject(), value);
}
}
int32_t QAndroidSharedPreferences::readInt(const QString & key, int32_t valueDefault)
{
if (isJniReady())
{
QJniEnvPtr jep;
return java_handler_->callParamInt("ReadInt", "Ljava/lang/String;I",
return jni()->callParamInt("ReadInt", "Ljava/lang/String;I",
QJniLocalRef(jep, key).jObject(), valueDefault);
}
return valueDefault;
}
......@@ -38,11 +38,13 @@
#include <QtCore/QObject>
#include <QJniHelpers.h>
#include <IJniObjectLinker.h>
class QAndroidSharedPreferences : public QObject
{
Q_OBJECT
JNI_LINKER_DECL(QAndroidSharedPreferences)
public:
QAndroidSharedPreferences(QObject * parent = 0);
......@@ -54,11 +56,5 @@ public:
void writeInt(const QString & key, int32_t value);
int32_t readInt(const QString & key, int32_t valueDefault);
private:
static void preloadJavaClasses();
private:
QScopedPointer<QJniObject> java_handler_;
};
......@@ -37,11 +37,21 @@
#include "QAndroidWiFiLocker.h"
#include <QAndroidQPAPluginGap.h>
#include <TJniObjectLinker.h>
static const char * const c_full_class_name_ = "ru/dublgis/androidhelpers/WifiLocker";
static const JNINativeMethod methods[] =
{
{"getContext", "()Landroid/content/Context;", (void*)QAndroidQPAPluginGap::getCurrentContext},
};
JNI_LINKER_IMPL(QAndroidWiFiLocker, c_full_class_name_, methods)
QAndroidWiFiLocker & QAndroidWiFiLocker::instance()
{
static QAndroidWiFiLocker obj;
......@@ -49,73 +59,53 @@ QAndroidWiFiLocker & QAndroidWiFiLocker::instance()
}
QAndroidWiFiLocker::QAndroidWiFiLocker() :
QLocks::QLockedObject(false)
QAndroidWiFiLocker::QAndroidWiFiLocker()
: QLocks::QLockedObject(false)
, jniLinker_(new JniObjectLinker(this))
{
preloadJavaClasses();
// Creating Java object
java_handler_.reset(new QJniObject(c_full_class_name_, "J",
jlong(reinterpret_cast<void *>(this))));
}
QAndroidWiFiLocker::~QAndroidWiFiLocker()
{
if (java_handler_)
{
java_handler_->callVoid("cppDestroyed");
java_handler_.reset();
}
}
void QAndroidWiFiLocker::preloadJavaClasses()
void QAndroidWiFiLocker::lock()
{
static volatile bool preloaded_ = false;
if (!preloaded_)
{
preloaded_ = true;
QAndroidQPAPluginGap::preloadJavaClasses();
QAndroidQPAPluginGap::preloadJavaClass(c_full_class_name_);
QJniClass ov(c_full_class_name_);
static const JNINativeMethod methods[] =
if (isJniReady())
{
{"getContext", "()Landroid/content/Context;", (void*)QAndroidQPAPluginGap::getCurrentContext},
};
ov.registerNativeMethods(methods, sizeof(methods));
jni()->callBool("Lock");
}
}
void QAndroidWiFiLocker::lock()
{
java_handler_->callBool("Lock");
Q_ASSERT(isLocked());
}
void QAndroidWiFiLocker::unlock()
{
java_handler_->callVoid("Unlock");
if (isJniReady())
{
jni()->callVoid("Unlock");
}
}
bool QAndroidWiFiLocker::isLocked()
{
return java_handler_->callBool("IsLocked");
if (isJniReady())
{
return jni()->callBool("IsLocked");
}
return false;
}
bool QAndroidWiFiLocker::isWifiEnabled()
{
if (java_handler_)
if (isJniReady())
{
return java_handler_->callBool("IsWifiEnabled");
return jni()->callBool("IsWifiEnabled");
}
return false;
......
......@@ -38,6 +38,7 @@
#include <QJniHelpers.h>
#include "QLocks/QLockedObject.h"
#include <IJniObjectLinker.h>
/*!
......@@ -47,6 +48,7 @@
class QAndroidWiFiLocker: public QLocks::QLockedObject
{
Q_DISABLE_COPY(QAndroidWiFiLocker)
JNI_LINKER_DECL(QAndroidWiFiLocker)
public:
static QAndroidWiFiLocker& instance();
......@@ -58,13 +60,9 @@ private:
public:
bool isLocked();
bool isWifiEnabled();
static void preloadJavaClasses();
private:
virtual void lock();
virtual void unlock();
private:
QScopedPointer<QJniObject> java_handler_;
};
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