Added new private API for Android and onPause/onResume

On Android the foreground activity can get onPause/onResume
calls. In Nfc code in android we need to know if we are paused
or resumed. And we need to make sure to call disableForegroundDispatch
inside the onPause call and in the main Ui thread. The current
implementiton of applicationStateChanged was not sufficient to
acomplish that.

This patch adds a way to receive those onPause/onResume calls in Qt
with proper timing.

Change-Id: I3a8cec093fc02ec42cc8677dfe2d0d4f8a227f8b
Reviewed-by: BogDan Vatra <bogdan@kde.org>
This commit is contained in:
Peter Rustler 2015-02-10 13:31:24 +01:00
parent 3d8c86881c
commit 88e448a90b
3 changed files with 57 additions and 0 deletions

View File

@ -122,6 +122,45 @@ void QtAndroidPrivate::handleNewIntent(JNIEnv *env, jobject intent)
}
}
namespace {
class ResumePauseListeners
{
public:
QMutex mutex;
QList<QtAndroidPrivate::ResumePauseListener *> listeners;
};
}
Q_GLOBAL_STATIC(ResumePauseListeners, g_resumePauseListeners)
void QtAndroidPrivate::registerResumePauseListener(ResumePauseListener *listener)
{
QMutexLocker locker(&g_resumePauseListeners()->mutex);
g_resumePauseListeners()->listeners.append(listener);
}
void QtAndroidPrivate::unregisterResumePauseListener(ResumePauseListener *listener)
{
QMutexLocker locker(&g_resumePauseListeners()->mutex);
g_resumePauseListeners()->listeners.removeAll(listener);
}
void QtAndroidPrivate::handlePause()
{
QMutexLocker locker(&g_resumePauseListeners()->mutex);
const QList<QtAndroidPrivate::ResumePauseListener *> &listeners = g_resumePauseListeners()->listeners;
for (int i=0; i<listeners.size(); ++i)
listeners.at(i)->handlePause();
}
void QtAndroidPrivate::handleResume()
{
QMutexLocker locker(&g_resumePauseListeners()->mutex);
const QList<QtAndroidPrivate::ResumePauseListener *> &listeners = g_resumePauseListeners()->listeners;
for (int i=0; i<listeners.size(); ++i)
listeners.at(i)->handleResume();
}
static inline bool exceptionCheck(JNIEnv *env)
{
if (env->ExceptionCheck()) {

View File

@ -68,6 +68,14 @@ namespace QtAndroidPrivate
virtual bool handleNewIntent(JNIEnv *env, jobject intent) = 0;
};
class Q_CORE_EXPORT ResumePauseListener
{
public:
virtual ~ResumePauseListener() {}
virtual void handlePause() {};
virtual void handleResume() {};
};
Q_CORE_EXPORT jobject activity();
Q_CORE_EXPORT JavaVM *javaVM();
Q_CORE_EXPORT jint initJNI(JavaVM *vm, JNIEnv *env);
@ -82,6 +90,11 @@ namespace QtAndroidPrivate
Q_CORE_EXPORT void handleNewIntent(JNIEnv *env, jobject intent);
Q_CORE_EXPORT void registerNewIntentListener(NewIntentListener *listener);
Q_CORE_EXPORT void unregisterNewIntentListener(NewIntentListener *listener);
Q_CORE_EXPORT void handlePause();
Q_CORE_EXPORT void handleResume();
Q_CORE_EXPORT void registerResumePauseListener(ResumePauseListener *listener);
Q_CORE_EXPORT void unregisterResumePauseListener(ResumePauseListener *listener);
}
QT_END_NAMESPACE

View File

@ -594,6 +594,11 @@ static void updateApplicationState(JNIEnv */*env*/, jobject /*thiz*/, jint state
return;
}
if (state == Qt::ApplicationActive)
QtAndroidPrivate::handleResume();
else if (state == Qt::ApplicationInactive)
QtAndroidPrivate::handlePause();
if (state <= Qt::ApplicationInactive) {
// Don't send timers and sockets events anymore if we are going to hide all windows
QAndroidEventDispatcherStopper::instance()->goingToStop(true);