Added new private API for Android and onNewIntent

On Android the foreground activity can get intents
with onNewIntent. Those intents can not be received
in any other way. This is especially true in Android nfc.

This patch adds a way to receive those intents in Qt.
This patch heavily leans on the implementation of onActivityResult.

Change-Id: Ic4dca301f34afe9a528149c3653e545ed3265a3c
Reviewed-by: BogDan Vatra <bogdan@kde.org>
Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
This commit is contained in:
Peter Rustler 2015-01-09 16:14:17 +01:00 committed by Alex Blasche
parent c87566bf9e
commit f93c04e44a
5 changed files with 57 additions and 1 deletions

View File

@ -883,6 +883,11 @@ public class QtActivityDelegate
} }
} }
public void onNewIntent(Intent data)
{
QtNative.onNewIntent(data);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) public void onActivityResult(int requestCode, int resultCode, Intent data)
{ {
try { try {

View File

@ -628,6 +628,7 @@ public class QtNative
// activity methods // activity methods
public static native void onActivityResult(int requestCode, int resultCode, Intent data); public static native void onActivityResult(int requestCode, int resultCode, Intent data);
public static native void onNewIntent(Intent data);
public static native void onAndroidUiThread(long id); public static native void onAndroidUiThread(long id);
} }

View File

@ -89,6 +89,39 @@ void QtAndroidPrivate::handleActivityResult(jint requestCode, jint resultCode, j
} }
} }
namespace {
class NewIntentListeners
{
public:
QMutex mutex;
QList<QtAndroidPrivate::NewIntentListener *> listeners;
};
}
Q_GLOBAL_STATIC(NewIntentListeners, g_newIntentListeners)
void QtAndroidPrivate::registerNewIntentListener(NewIntentListener *listener)
{
QMutexLocker locker(&g_newIntentListeners()->mutex);
g_newIntentListeners()->listeners.append(listener);
}
void QtAndroidPrivate::unregisterNewIntentListener(NewIntentListener *listener)
{
QMutexLocker locker(&g_newIntentListeners()->mutex);
g_newIntentListeners()->listeners.removeAll(listener);
}
void QtAndroidPrivate::handleNewIntent(JNIEnv *env, jobject intent)
{
QMutexLocker locker(&g_newIntentListeners()->mutex);
const QList<QtAndroidPrivate::NewIntentListener *> &listeners = g_newIntentListeners()->listeners;
for (int i=0; i<listeners.size(); ++i) {
if (listeners.at(i)->handleNewIntent(env, intent))
break;
}
}
static inline bool exceptionCheck(JNIEnv *env) static inline bool exceptionCheck(JNIEnv *env)
{ {
if (env->ExceptionCheck()) { if (env->ExceptionCheck()) {

View File

@ -61,6 +61,13 @@ namespace QtAndroidPrivate
virtual bool handleActivityResult(jint requestCode, jint resultCode, jobject data) = 0; virtual bool handleActivityResult(jint requestCode, jint resultCode, jobject data) = 0;
}; };
class Q_CORE_EXPORT NewIntentListener
{
public:
virtual ~NewIntentListener() {}
virtual bool handleNewIntent(JNIEnv *env, jobject intent) = 0;
};
Q_CORE_EXPORT jobject activity(); Q_CORE_EXPORT jobject activity();
Q_CORE_EXPORT JavaVM *javaVM(); Q_CORE_EXPORT JavaVM *javaVM();
Q_CORE_EXPORT jint initJNI(JavaVM *vm, JNIEnv *env); Q_CORE_EXPORT jint initJNI(JavaVM *vm, JNIEnv *env);
@ -71,6 +78,10 @@ namespace QtAndroidPrivate
Q_CORE_EXPORT void handleActivityResult(jint requestCode, jint resultCode, jobject data); Q_CORE_EXPORT void handleActivityResult(jint requestCode, jint resultCode, jobject data);
Q_CORE_EXPORT void registerActivityResultListener(ActivityResultListener *listener); Q_CORE_EXPORT void registerActivityResultListener(ActivityResultListener *listener);
Q_CORE_EXPORT void unregisterActivityResultListener(ActivityResultListener *listener); Q_CORE_EXPORT void unregisterActivityResultListener(ActivityResultListener *listener);
Q_CORE_EXPORT void handleNewIntent(JNIEnv *env, jobject intent);
Q_CORE_EXPORT void registerNewIntentListener(NewIntentListener *listener);
Q_CORE_EXPORT void unregisterNewIntentListener(NewIntentListener *listener);
} }
QT_END_NAMESPACE QT_END_NAMESPACE

View File

@ -648,6 +648,11 @@ static void onActivityResult(JNIEnv */*env*/, jclass /*cls*/,
QtAndroidPrivate::handleActivityResult(requestCode, resultCode, data); QtAndroidPrivate::handleActivityResult(requestCode, resultCode, data);
} }
static void onNewIntent(JNIEnv *env, jclass /*cls*/, jobject data)
{
QtAndroidPrivate::handleNewIntent(env, data);
}
static JNINativeMethod methods[] = { static JNINativeMethod methods[] = {
{"startQtAndroidPlugin", "()Z", (void *)startQtAndroidPlugin}, {"startQtAndroidPlugin", "()Z", (void *)startQtAndroidPlugin},
{"startQtApplication", "(Ljava/lang/String;Ljava/lang/String;)V", (void *)startQtApplication}, {"startQtApplication", "(Ljava/lang/String;Ljava/lang/String;)V", (void *)startQtApplication},
@ -658,7 +663,8 @@ static JNINativeMethod methods[] = {
{"updateWindow", "()V", (void *)updateWindow}, {"updateWindow", "()V", (void *)updateWindow},
{"updateApplicationState", "(I)V", (void *)updateApplicationState}, {"updateApplicationState", "(I)V", (void *)updateApplicationState},
{"handleOrientationChanged", "(II)V", (void *)handleOrientationChanged}, {"handleOrientationChanged", "(II)V", (void *)handleOrientationChanged},
{"onActivityResult", "(IILandroid/content/Intent;)V", (void *)onActivityResult} {"onActivityResult", "(IILandroid/content/Intent;)V", (void *)onActivityResult},
{"onNewIntent", "(Landroid/content/Intent;)V", (void *)onNewIntent}
}; };
#define FIND_AND_CHECK_CLASS(CLASS_NAME) \ #define FIND_AND_CHECK_CLASS(CLASS_NAME) \