summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorAssam Boudjelthia <assam.boudjelthia@qt.io>2020-02-22 20:26:56 +0200
committerAssam Boudjelthia <assam.boudjelthia@qt.io>2020-02-26 23:07:48 +0200
commit86f7d44089138b181642ee22989519f188c75bd5 (patch)
treee6abb16886545b5e6f7fcd6c9012ddf32b3d5fef /examples
parentbf377dd1af26a852d9c68f81be98bdf3c0c8ed69 (diff)
Examples: request Android storage permissions when using QStandardPaths
http example uses QStandardPaths, on Android permissions might need to be explicitly requested to write the downloaded file. Task-number: QTBUG-80717 Change-Id: Icd377254ad77cac661c5ae37e9081e0463493d8b Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
Diffstat (limited to 'examples')
-rw-r--r--examples/network/http/http.pro4
-rw-r--r--examples/network/http/main.cpp21
2 files changed, 24 insertions, 1 deletions
diff --git a/examples/network/http/http.pro b/examples/network/http/http.pro
index 2f2d3b00ae..f67cd01495 100644
--- a/examples/network/http/http.pro
+++ b/examples/network/http/http.pro
@@ -1,4 +1,8 @@
QT += network widgets
+android: qtHaveModule(androidextras) {
+ QT += androidextras
+ DEFINES += REQUEST_PERMISSIONS_ON_ANDROID
+}
HEADERS += httpwindow.h
SOURCES += httpwindow.cpp \
diff --git a/examples/network/http/main.cpp b/examples/network/http/main.cpp
index f126c7846a..1339f2f693 100644
--- a/examples/network/http/main.cpp
+++ b/examples/network/http/main.cpp
@@ -53,11 +53,30 @@
#include <QScreen>
#include "httpwindow.h"
+#ifdef REQUEST_PERMISSIONS_ON_ANDROID
+#include <QtAndroid>
+
+bool requestStoragePermission() {
+ using namespace QtAndroid;
+
+ QString permission = QStringLiteral("android.permission.WRITE_EXTERNAL_STORAGE");
+ const QHash<QString, PermissionResult> results = requestPermissionsSync(QStringList({permission}));
+ if (!results.contains(permission) || results[permission] == PermissionResult::Denied) {
+ qWarning() << "Couldn't get permission: " << permission;
+ return false;
+ }
+
+ return true;
+}
+#endif
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
-
+#ifdef REQUEST_PERMISSIONS_ON_ANDROID
+ if (!requestStoragePermission())
+ return -1;
+#endif
HttpWindow httpWin;
const QRect availableSize = httpWin.screen()->availableGeometry();
httpWin.resize(availableSize.width() / 5, availableSize.height() / 5);