diff options
author | Christian Strømme <christian.stromme@qt.io> | 2016-12-12 18:06:54 +0100 |
---|---|---|
committer | Christian Stromme <christian.stromme@qt.io> | 2017-04-23 23:11:32 +0000 |
commit | 64e84dab2673020e4384ae36d9d1e9e4f0d8052d (patch) | |
tree | b3dc96fb555cefba3559746a0b3f9828d0c536a5 | |
parent | 6b20032d91067e06f6ab5d4acf553812134236c7 (diff) | |
download | qtmultimedia-64e84dab2673020e4384ae36d9d1e9e4f0d8052d.tar.gz |
Android: Run-time permission checks for camera
The camera should ask for permission before being used. Normally this
should be handle by the user, which can provide proper reasons for why
the camera is used. To verify that we are allowed to open the camera,
and as a good default, we'll check the permission and request access
if needed.
[ChangeLog][Android] Added run-time permission checks for accessing
the camera.
Task-number: QTBUG-55992
Change-Id: Ifb4e176728aab3076fdba292e17e17d33b1260a2
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
3 files changed, 16 insertions, 1 deletions
diff --git a/src/plugins/android/src/mediacapture/qandroidcamerasession.cpp b/src/plugins/android/src/mediacapture/qandroidcamerasession.cpp index b0dd7d900..a7f0254ee 100644 --- a/src/plugins/android/src/mediacapture/qandroidcamerasession.cpp +++ b/src/plugins/android/src/mediacapture/qandroidcamerasession.cpp @@ -176,7 +176,7 @@ bool QAndroidCameraSession::open() m_status = QCamera::LoadingStatus; emit statusChanged(m_status); - m_camera = AndroidCamera::open(m_selectedCamera); + m_camera = AndroidCamera::requestCameraPermission() ? AndroidCamera::open(m_selectedCamera) : nullptr; if (m_camera) { connect(m_camera, SIGNAL(pictureExposed()), this, SLOT(onCameraPictureExposed())); diff --git a/src/plugins/android/src/wrappers/jni/androidcamera.cpp b/src/plugins/android/src/wrappers/jni/androidcamera.cpp index 0f2a43531..e3afddd59 100644 --- a/src/plugins/android/src/wrappers/jni/androidcamera.cpp +++ b/src/plugins/android/src/wrappers/jni/androidcamera.cpp @@ -42,6 +42,7 @@ #include "androidsurfacetexture.h" #include "androidsurfaceview.h" #include "qandroidmultimediautils.h" +#include "qandroidglobal.h" #include <qstringlist.h> #include <qdebug.h> @@ -55,6 +56,11 @@ QT_BEGIN_NAMESPACE static const char QtCameraListenerClassName[] = "org/qtproject/qt5/android/multimedia/QtCameraListener"; +static QString cameraPermissionKey() +{ + return QStringLiteral("android.permission.CAMERA"); +} + typedef QHash<int, AndroidCamera *> CameraMap; Q_GLOBAL_STATIC(CameraMap, cameras) Q_GLOBAL_STATIC(QReadWriteLock, rwLock) @@ -756,6 +762,9 @@ QJNIObjectPrivate AndroidCamera::getCameraObject() int AndroidCamera::getNumberOfCameras() { + if (!requestCameraPermission()) + return 0; + return QJNIObjectPrivate::callStaticMethod<jint>("android/hardware/Camera", "getNumberOfCameras"); } @@ -790,6 +799,11 @@ void AndroidCamera::getCameraInfo(int id, AndroidCameraInfo *info) } } +bool AndroidCamera::requestCameraPermission() +{ + return qt_androidRequestPermission(cameraPermissionKey()); +} + void AndroidCamera::startPreview() { Q_D(AndroidCamera); diff --git a/src/plugins/android/src/wrappers/jni/androidcamera.h b/src/plugins/android/src/wrappers/jni/androidcamera.h index e58a81f8e..5ae141f01 100644 --- a/src/plugins/android/src/wrappers/jni/androidcamera.h +++ b/src/plugins/android/src/wrappers/jni/androidcamera.h @@ -201,6 +201,7 @@ public: static int getNumberOfCameras(); static void getCameraInfo(int id, AndroidCameraInfo *info); + static bool requestCameraPermission(); static bool initJNI(JNIEnv *env); |