From cb388ce908d973466ea98f5d66167843eeb3c470 Mon Sep 17 00:00:00 2001 From: Yoann Lopes Date: Tue, 3 Sep 2013 14:20:28 +0200 Subject: Android: use QGuiApplication::applicationState() for releasing camera. We previously had a custom Java class to listen for Activity state changes but that was a bit hackish. Use the new QGuiApplication API instead. Change-Id: I658efc578dfcd281439a57905ca1fd4ae1b4ac2b Reviewed-by: Christian Stromme --- src/plugins/android/jar/jar.pri | 1 - .../multimedia/QtActivityStateListener.java | 100 ------------------- .../src/mediacapture/qandroidcamerasession.cpp | 45 ++++++--- .../src/mediacapture/qandroidcamerasession.h | 6 +- .../android/src/qandroidmediaserviceplugin.cpp | 2 - .../src/wrappers/jactivitystatelistener.cpp | 107 --------------------- .../android/src/wrappers/jactivitystatelistener.h | 69 ------------- src/plugins/android/src/wrappers/jcamera.cpp | 4 +- .../android/src/wrappers/jmediarecorder.cpp | 2 +- src/plugins/android/src/wrappers/wrappers.pri | 2 - 10 files changed, 34 insertions(+), 304 deletions(-) delete mode 100644 src/plugins/android/jar/src/org/qtproject/qt5/android/multimedia/QtActivityStateListener.java delete mode 100644 src/plugins/android/src/wrappers/jactivitystatelistener.cpp delete mode 100644 src/plugins/android/src/wrappers/jactivitystatelistener.h diff --git a/src/plugins/android/jar/jar.pri b/src/plugins/android/jar/jar.pri index 973046201..9e235144b 100644 --- a/src/plugins/android/jar/jar.pri +++ b/src/plugins/android/jar/jar.pri @@ -7,7 +7,6 @@ JAVACLASSPATH += $$PWD/src JAVASOURCES += $$PWD/src/org/qtproject/qt5/android/multimedia/QtAndroidMediaPlayer.java \ $$PWD/src/org/qtproject/qt5/android/multimedia/QtCamera.java \ - $$PWD/src/org/qtproject/qt5/android/multimedia/QtActivityStateListener.java \ $$PWD/src/org/qtproject/qt5/android/multimedia/QtSurfaceTexture.java \ $$PWD/src/org/qtproject/qt5/android/multimedia/QtSurfaceTextureHolder.java \ $$PWD/src/org/qtproject/qt5/android/multimedia/QtMultimediaUtils.java \ diff --git a/src/plugins/android/jar/src/org/qtproject/qt5/android/multimedia/QtActivityStateListener.java b/src/plugins/android/jar/src/org/qtproject/qt5/android/multimedia/QtActivityStateListener.java deleted file mode 100644 index 50f831e49..000000000 --- a/src/plugins/android/jar/src/org/qtproject/qt5/android/multimedia/QtActivityStateListener.java +++ /dev/null @@ -1,100 +0,0 @@ -/**************************************************************************** - ** - ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). - ** Contact: http://www.qt-project.org/legal - ** - ** This file is part of the QtMultimedia module of the Qt Toolkit. - ** - ** $QT_BEGIN_LICENSE:LGPL$ - ** Commercial License Usage - ** Licensees holding valid commercial Qt licenses may use this file in - ** accordance with the commercial license agreement provided with the - ** Software or, alternatively, in accordance with the terms contained in - ** a written agreement between you and Digia. For licensing terms and - ** conditions see http://qt.digia.com/licensing. For further information - ** use the contact form at http://qt.digia.com/contact-us. - ** - ** GNU Lesser General Public License Usage - ** Alternatively, this file may be used under the terms of the GNU Lesser - ** General Public License version 2.1 as published by the Free Software - ** Foundation and appearing in the file LICENSE.LGPL included in the - ** packaging of this file. Please review the following information to - ** ensure the GNU Lesser General Public License version 2.1 requirements - ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. - ** - ** In addition, as a special exception, Digia gives you certain additional - ** rights. These rights are described in the Digia Qt LGPL Exception - ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. - ** - ** GNU General Public License Usage - ** Alternatively, this file may be used under the terms of the GNU - ** General Public License version 3.0 as published by the Free Software - ** Foundation and appearing in the file LICENSE.GPL included in the - ** packaging of this file. Please review the following information to - ** ensure the GNU General Public License version 3.0 requirements will be - ** met: http://www.gnu.org/copyleft/gpl.html. - ** - ** - ** $QT_END_LICENSE$ - ** - ****************************************************************************/ - -package org.qtproject.qt5.android.multimedia; - -import android.app.Activity; -import android.app.Fragment; -import android.app.FragmentTransaction; -import android.app.FragmentManager; - -public class QtActivityStateListener extends Fragment -{ - static private FragmentManager m_fragmentManager = null; - private long m_Id = -1; - - static public void setActivity(Activity activity, Object activityDelegate) - { - m_fragmentManager = activity.getFragmentManager(); - } - - public QtActivityStateListener() - { - super(); - } - - public QtActivityStateListener(long id) - { - super(); - m_Id = id; - } - - public void create() - { - FragmentTransaction fragmentTransaction = m_fragmentManager.beginTransaction(); - fragmentTransaction.add(this, "QtActivityStateListener"); - fragmentTransaction.commit(); - } - - public void destroy() - { - FragmentTransaction fragmentTransaction = m_fragmentManager.beginTransaction(); - fragmentTransaction.remove(this); - fragmentTransaction.commit(); - } - - @Override - public void onPause() - { - notifyPause(m_Id); - super.onPause(); - } - - @Override - public void onResume() - { - notifyResume(m_Id); - super.onResume(); - } - - private static native void notifyPause(long id); - private static native void notifyResume(long id); -} diff --git a/src/plugins/android/src/mediacapture/qandroidcamerasession.cpp b/src/plugins/android/src/mediacapture/qandroidcamerasession.cpp index aefba22fe..761b716d1 100644 --- a/src/plugins/android/src/mediacapture/qandroidcamerasession.cpp +++ b/src/plugins/android/src/mediacapture/qandroidcamerasession.cpp @@ -42,12 +42,12 @@ #include "qandroidcamerasession.h" #include "jcamera.h" -#include "jactivitystatelistener.h" #include "jmultimediautils.h" #include "qandroidvideooutput.h" #include "qandroidmultimediautils.h" #include #include +#include #include QT_BEGIN_NAMESPACE @@ -77,15 +77,15 @@ QAndroidCameraSession::QAndroidCameraSession(QObject *parent) , m_captureCanceled(false) , m_currentImageCaptureId(-1) { - m_activityListener = new JActivityStateListener; - connect(m_activityListener, SIGNAL(paused()), this, SLOT(onActivityPaused())); - connect(m_activityListener, SIGNAL(resumed()), this, SLOT(onActivityResumed())); + if (qApp) { + connect(qApp, SIGNAL(applicationStateChanged(Qt::ApplicationState)), + this, SLOT(onApplicationStateChanged(Qt::ApplicationState))); + } } QAndroidCameraSession::~QAndroidCameraSession() { close(); - delete m_activityListener; } void QAndroidCameraSession::setCaptureMode(QCamera::CaptureModes mode) @@ -110,6 +110,13 @@ bool QAndroidCameraSession::isCaptureModeSupported(QCamera::CaptureModes mode) c void QAndroidCameraSession::setState(QCamera::State state) { + // If the application is inactive, the camera shouldn't be started. Save the desired state + // instead and it will be set when the application becomes active. + if (qApp->applicationState() != Qt::ApplicationActive) { + m_savedState = state; + return; + } + if (m_state == state) return; @@ -521,17 +528,25 @@ void QAndroidCameraSession::onSurfaceTextureReady() m_camera->setPreviewTexture(m_videoOutput->surfaceTexture()); } -void QAndroidCameraSession::onActivityPaused() -{ - m_savedState = m_state; - setState(QCamera::UnloadedState); -} - -void QAndroidCameraSession::onActivityResumed() +void QAndroidCameraSession::onApplicationStateChanged(Qt::ApplicationState state) { - if (m_savedState != -1) { - setState(QCamera::State(m_savedState)); - m_savedState = -1; + switch (state) { + case Qt::ApplicationInactive: + if (m_state != QCamera::UnloadedState) { + m_savedState = m_state; + close(); + m_state = QCamera::UnloadedState; + emit stateChanged(m_state); + } + break; + case Qt::ApplicationActive: + if (m_savedState != -1) { + setState(QCamera::State(m_savedState)); + m_savedState = -1; + } + break; + default: + break; } } diff --git a/src/plugins/android/src/mediacapture/qandroidcamerasession.h b/src/plugins/android/src/mediacapture/qandroidcamerasession.h index 3b9dd2672..f1cf44eec 100644 --- a/src/plugins/android/src/mediacapture/qandroidcamerasession.h +++ b/src/plugins/android/src/mediacapture/qandroidcamerasession.h @@ -50,7 +50,6 @@ QT_BEGIN_NAMESPACE class JCamera; -class JActivityStateListener; class QAndroidVideoOutput; class QAndroidCameraSession : public QObject @@ -111,8 +110,7 @@ Q_SIGNALS: void imageCaptureError(int id, int error, const QString &errorString); private Q_SLOTS: - void onActivityPaused(); - void onActivityResumed(); + void onApplicationStateChanged(Qt::ApplicationState state); void onCameraPictureExposed(); void onCameraPictureCaptured(const QByteArray &data); @@ -136,8 +134,6 @@ private: int m_nativeOrientation; QAndroidVideoOutput *m_videoOutput; - JActivityStateListener *m_activityListener; - QCamera::CaptureModes m_captureMode; QCamera::State m_state; int m_savedState; diff --git a/src/plugins/android/src/qandroidmediaserviceplugin.cpp b/src/plugins/android/src/qandroidmediaserviceplugin.cpp index 212d00726..d7a2cd29f 100644 --- a/src/plugins/android/src/qandroidmediaserviceplugin.cpp +++ b/src/plugins/android/src/qandroidmediaserviceplugin.cpp @@ -49,7 +49,6 @@ #include "jsurfacetexture.h" #include "jsurfacetextureholder.h" #include "jcamera.h" -#include "jactivitystatelistener.h" #include "jmultimediautils.h" #include "jmediarecorder.h" #include @@ -139,7 +138,6 @@ Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void * /*reserved*/) !JSurfaceTexture::initJNI(jniEnv) || !JSurfaceTextureHolder::initJNI(jniEnv) || !JCamera::initJNI(jniEnv) || - !JActivityStateListener::initJNI(jniEnv) || !JMultimediaUtils::initJNI(jniEnv) || !JMediaRecorder::initJNI(jniEnv)) { return JNI_ERR; diff --git a/src/plugins/android/src/wrappers/jactivitystatelistener.cpp b/src/plugins/android/src/wrappers/jactivitystatelistener.cpp deleted file mode 100644 index 607e604cf..000000000 --- a/src/plugins/android/src/wrappers/jactivitystatelistener.cpp +++ /dev/null @@ -1,107 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "jactivitystatelistener.h" - -#include - -QT_BEGIN_NAMESPACE - -static jclass g_qtActivityStateListenerClass = 0; -static QMap g_objectMap; - -// native methods for QtCameraFragment.java -static void notifyPause(JNIEnv*, jobject, jlong id) -{ - JActivityStateListener *obj = g_objectMap.value(id, 0); - if (obj) - emit obj->paused(); -} - -static void notifyResume(JNIEnv*, jobject, jlong id) -{ - JActivityStateListener *obj = g_objectMap.value(id, 0); - if (obj) - emit obj->resumed(); -} - -JActivityStateListener::JActivityStateListener() - : QObject() - , QJNIObject(g_qtActivityStateListenerClass, "(J)V", reinterpret_cast(this)) - , m_Id(reinterpret_cast(this)) -{ - if (m_jobject) { - g_objectMap.insert(m_Id, this); - callMethod("create"); - } -} - -JActivityStateListener::~JActivityStateListener() -{ - if (m_jobject) - g_objectMap.remove(m_Id); - callMethod("destroy"); -} - -static JNINativeMethod methods[] = { - {"notifyPause", "(J)V", (void *)notifyPause}, - {"notifyResume", "(J)V", (void *)notifyResume} -}; - -bool JActivityStateListener::initJNI(JNIEnv *env) -{ - jclass clazz = env->FindClass("org/qtproject/qt5/android/multimedia/QtActivityStateListener"); - if (env->ExceptionCheck()) - env->ExceptionClear(); - - if (clazz) { - g_qtActivityStateListenerClass = static_cast(env->NewGlobalRef(clazz)); - if (env->RegisterNatives(g_qtActivityStateListenerClass, - methods, - sizeof(methods) / sizeof(methods[0])) < 0) { - return false; - } - } - - return true; -} - -QT_END_NAMESPACE diff --git a/src/plugins/android/src/wrappers/jactivitystatelistener.h b/src/plugins/android/src/wrappers/jactivitystatelistener.h deleted file mode 100644 index 98abc5737..000000000 --- a/src/plugins/android/src/wrappers/jactivitystatelistener.h +++ /dev/null @@ -1,69 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef JACTIVITYSTATELISTENER_H -#define JACTIVITYSTATELISTENER_H - -#include -#include - -QT_BEGIN_NAMESPACE - -class JActivityStateListener : public QObject, public QJNIObject -{ - Q_OBJECT -public: - JActivityStateListener(); - ~JActivityStateListener(); - - static bool initJNI(JNIEnv *env); - -Q_SIGNALS: - void paused(); - void resumed(); - -private: - jlong m_Id; -}; - -QT_END_NAMESPACE - -#endif // JACTIVITYSTATELISTENER_H diff --git a/src/plugins/android/src/wrappers/jcamera.cpp b/src/plugins/android/src/wrappers/jcamera.cpp index 5734a2657..9005ac6e0 100644 --- a/src/plugins/android/src/wrappers/jcamera.cpp +++ b/src/plugins/android/src/wrappers/jcamera.cpp @@ -111,7 +111,7 @@ JCamera::JCamera(int cameraId, jobject cam) , m_parameters(0) , m_hasAPI14(false) { - if (m_jobject) { + if (isValid()) { g_objectMap.insert(cameraId, this); m_info = new QJNIObject("android/hardware/Camera$CameraInfo"); @@ -145,7 +145,7 @@ JCamera::JCamera(int cameraId, jobject cam) JCamera::~JCamera() { - if (m_jobject) + if (isValid()) g_objectMap.remove(m_cameraId); delete m_parameters; delete m_info; diff --git a/src/plugins/android/src/wrappers/jmediarecorder.cpp b/src/plugins/android/src/wrappers/jmediarecorder.cpp index 0deb493a5..b7cbe5724 100644 --- a/src/plugins/android/src/wrappers/jmediarecorder.cpp +++ b/src/plugins/android/src/wrappers/jmediarecorder.cpp @@ -69,7 +69,7 @@ JMediaRecorder::JMediaRecorder() , QJNIObject(g_qtMediaRecorderClass, "(J)V", reinterpret_cast(this)) , m_id(reinterpret_cast(this)) { - if (m_jobject) + if (isValid()) g_objectMap.insert(m_id, this); } diff --git a/src/plugins/android/src/wrappers/wrappers.pri b/src/plugins/android/src/wrappers/wrappers.pri index 153b507dc..b2faa5b79 100644 --- a/src/plugins/android/src/wrappers/wrappers.pri +++ b/src/plugins/android/src/wrappers/wrappers.pri @@ -8,7 +8,6 @@ HEADERS += \ $$PWD/jsurfacetextureholder.h \ $$PWD/jmediametadataretriever.h \ $$PWD/jcamera.h \ - $$PWD/jactivitystatelistener.h \ $$PWD/jmultimediautils.h \ $$PWD/jmediarecorder.h @@ -18,6 +17,5 @@ SOURCES += \ $$PWD/jsurfacetextureholder.cpp \ $$PWD/jmediametadataretriever.cpp \ $$PWD/jcamera.cpp \ - $$PWD/jactivitystatelistener.cpp \ $$PWD/jmultimediautils.cpp \ $$PWD/jmediarecorder.cpp -- cgit v1.2.1