summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Strømme <christian.stromme@digia.com>2014-01-08 14:06:38 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-01-16 02:38:11 +0100
commit48db373ce3cadab3a54cd443f9136d1e5d74ca50 (patch)
tree0ca70f21c297577eae769e210d5a85fe8f8cfbe4
parentfd22e89593002a4fd155e5da1aabbaefb72433db (diff)
Android: Add convenience function to get the API level.
It's common to check the Android API level at runtime to determine what functionality is available. This change provides a convenient way to get the Android SDK version (API level) from c++. Change-Id: I88f65ae87e0fa8ac0affefffbd1b1bba855c9f46 Reviewed-by: BogDan Vatra <bogdan@kde.org>
-rw-r--r--src/corelib/kernel/qjnihelpers.cpp21
-rw-r--r--src/corelib/kernel/qjnihelpers_p.h1
2 files changed, 22 insertions, 0 deletions
diff --git a/src/corelib/kernel/qjnihelpers.cpp b/src/corelib/kernel/qjnihelpers.cpp
index 9ec491f6a9..74eb6f5b27 100644
--- a/src/corelib/kernel/qjnihelpers.cpp
+++ b/src/corelib/kernel/qjnihelpers.cpp
@@ -46,6 +46,7 @@ QT_BEGIN_NAMESPACE
static JavaVM *g_javaVM = Q_NULLPTR;
static jobject g_jActivity = Q_NULLPTR;
static jobject g_jClassLoader = Q_NULLPTR;
+static jint g_androidSdkVersion = 0;
static inline bool exceptionCheck(JNIEnv *env)
{
@@ -60,6 +61,19 @@ static inline bool exceptionCheck(JNIEnv *env)
return false;
}
+static void setAndroidSdkVersion(JNIEnv *env)
+{
+ jclass androidVersionClass = env->FindClass("android/os/Build$VERSION");
+ if (exceptionCheck(env))
+ return;
+
+ jfieldID androidSDKFieldID = env->GetStaticFieldID(androidVersionClass, "SDK_INT", "I");
+ if (exceptionCheck(env))
+ return;
+
+ g_androidSdkVersion = env->GetStaticIntField(androidVersionClass, androidSDKFieldID);
+}
+
jint QtAndroidPrivate::initJNI(JavaVM *vm, JNIEnv *env)
{
jclass jQtNative = env->FindClass("org/qtproject/qt5/android/QtNative");
@@ -93,6 +107,8 @@ jint QtAndroidPrivate::initJNI(JavaVM *vm, JNIEnv *env)
if (exceptionCheck(env))
return JNI_ERR;
+ setAndroidSdkVersion(env);
+
g_jClassLoader = env->NewGlobalRef(classLoader);
env->DeleteLocalRef(classLoader);
g_jActivity = env->NewGlobalRef(activity);
@@ -118,4 +134,9 @@ jobject QtAndroidPrivate::classLoader()
return g_jClassLoader;
}
+jint QtAndroidPrivate::androidSdkVersion()
+{
+ return g_androidSdkVersion;
+}
+
QT_END_NAMESPACE
diff --git a/src/corelib/kernel/qjnihelpers_p.h b/src/corelib/kernel/qjnihelpers_p.h
index 39059db215..c92dbdde7f 100644
--- a/src/corelib/kernel/qjnihelpers_p.h
+++ b/src/corelib/kernel/qjnihelpers_p.h
@@ -64,6 +64,7 @@ namespace QtAndroidPrivate
Q_CORE_EXPORT JavaVM *javaVM();
Q_CORE_EXPORT jint initJNI(JavaVM *vm, JNIEnv *env);
jobject classLoader();
+ jint androidSdkVersion();
}
QT_END_NAMESPACE