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

add TJniObjectLinker usage

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