Skip to content
This repository was archived by the owner on Mar 4, 2023. It is now read-only.

Commit fe2923e

Browse files
committed
permission shit...
it's not really working thought...
1 parent dbb7c73 commit fe2923e

File tree

9 files changed

+110
-16
lines changed

9 files changed

+110
-16
lines changed

Demo/Demo.pro

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ include(../vendor/vendor.pri)
88
QPM_ROOT = $$_PRO_FILE_PWD_/../vendor
99
include(../de_skycoder42_androidutils.pri)
1010

11-
SOURCES += main.cpp
11+
SOURCES += main.cpp \
12+
opener.cpp
1213

1314
RESOURCES += qml.qrc
1415

@@ -40,3 +41,6 @@ DISTFILES += \
4041
android/build.gradle
4142

4243
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
44+
45+
HEADERS += \
46+
opener.h

Demo/android/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,5 @@
7676

7777
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
7878
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
79+
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
7980
</manifest>

Demo/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <QGuiApplication>
33
#include <QQmlApplicationEngine>
44
#include <QDebug>
5+
#include "opener.h"
56
#ifdef Q_OS_ANDROID
67
#include <contentdevice.h>
78
#endif
@@ -11,6 +12,8 @@ int main(int argc, char *argv[])
1112
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
1213
QGuiApplication app(argc, argv);
1314

15+
qmlRegisterType<Opener>("de.skycoder42.androidutils", 1, 0, "Opener");
16+
1417
#ifdef Q_OS_ANDROID
1518
QUrl url("file:///sdcard/temp/baum");
1619
ContentDevice device(url);

Demo/main.qml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ ApplicationWindow {
110110
text: "Open file chooser"
111111
onClicked: chooser.open()
112112
}
113+
114+
Button {
115+
id: editButton
116+
text: "Display/Edit File"
117+
118+
Opener {
119+
id: opener
120+
121+
onDataLoaded: console.log(data)
122+
}
123+
124+
onClicked: opener.openFile(chooser.contentUrl)
125+
}
113126
}
114127
}
115128
}

Demo/opener.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "opener.h"
2+
#include <QFile>
3+
#ifdef Q_OS_ANDROID
4+
#include <contentdevice.h>
5+
#include <QtAndroid>
6+
#endif
7+
8+
Opener::Opener(QObject *parent) :
9+
QObject(parent)
10+
{}
11+
12+
void Opener::openFile(const QUrl &file)
13+
{
14+
#ifdef Q_OS_ANDROID
15+
ContentDevice device(QtAndroid::androidActivity(), file);
16+
#else
17+
QFile device(file.toLocalFile());
18+
#endif
19+
device.open(QIODevice::ReadOnly);
20+
auto data = QString::fromUtf8(device.readAll());
21+
device.close();
22+
emit dataLoaded(data);
23+
}
24+
25+
void Opener::saveFile(const QUrl &file, const QString &data)
26+
{
27+
#ifdef Q_OS_ANDROID
28+
ContentDevice device(QtAndroid::androidActivity(), file);
29+
#else
30+
QFile device(file.toLocalFile());
31+
#endif
32+
device.open(QIODevice::WriteOnly);
33+
device.write(data.toUtf8());
34+
device.close();
35+
}

Demo/opener.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef OPENER_H
2+
#define OPENER_H
3+
4+
#include <QObject>
5+
6+
class Opener : public QObject
7+
{
8+
Q_OBJECT
9+
public:
10+
explicit Opener(QObject *parent = nullptr);
11+
12+
public slots:
13+
void openFile(const QUrl &file);
14+
void saveFile(const QUrl &file, const QString &data);
15+
16+
signals:
17+
void dataLoaded(const QString &data);
18+
};
19+
20+
#endif // OPENER_H

android/src/de/skycoder42/androidutils/FileChooser.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import android.net.Uri;
77
import android.content.Context;
88
import android.content.Intent;
9+
import android.content.ContentResolver;
910
import android.app.Activity;
1011
import org.qtproject.qt5.android.QtNative;
1112
import androidnative.SystemDispatcher;
1213

1314
public class FileChooser {
1415
public static final int CHOOSE_CONTENT_ACTION = 0x1091c657;
16+
public static final int CHOOSE_DOCUMENT_ACTION = 0x1091c658;
1517

1618
public static final String GET_CONTENT_MESSAGE = "AndroidUtils.FileChooser.getContent";
1719
public static final String OPEN_DOCUMENT_MESSAGE = "AndroidUtils.FileChooser.openDocument";
@@ -26,13 +28,13 @@ public void onDispatched(String type , Map message) {
2628
(String)message.get("mime"),
2729
(Boolean)message.get("openable"),
2830
(Boolean)message.get("localOnly"),
29-
(Boolean)message.get("allowMultiple"));
31+
(Boolean)message.get("grantWrite"));
3032
} else if (type.equals(OPEN_DOCUMENT_MESSAGE)) {
3133
openDocument((String)message.get("title"),
3234
(String)message.get("mime"),
3335
(String)message.get("url"),
3436
(Boolean)message.get("openable"),
35-
(Boolean)message.get("allowMultiple"));
37+
(Boolean)message.get("grantWrite"));
3638
} else if (type.equals(CREATE_DOCUMENT_MESSAGE)) {
3739
createDocument((String)message.get("title"),
3840
(String)message.get("mime"),
@@ -48,17 +50,18 @@ public void onDispatched(String type , Map message) {
4850
});
4951
}
5052

51-
static private void getContent(final String title, String mime, boolean openable, boolean localOnly, boolean allowMultiple) {
53+
static private void getContent(final String title, String mime, boolean openable, boolean localOnly, boolean grantWrite) {
5254
final Activity activity = QtNative.activity();
5355

5456
final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
5557
intent.setType(mime);
58+
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
59+
if(grantWrite)
60+
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
5661
if(openable)
5762
intent.addCategory(Intent.CATEGORY_OPENABLE);
5863
if(localOnly)
5964
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
60-
if(allowMultiple)
61-
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
6265

6366
Runnable runnable = new Runnable () {
6467
public void run() {
@@ -70,24 +73,26 @@ public void run() {
7073
activity.runOnUiThread(runnable);
7174
}
7275

73-
static private void openDocument(final String title, String mime, String url, boolean openable, boolean allowMultiple) {
76+
static private void openDocument(final String title, String mime, String url, boolean openable, boolean grantWrite) {
7477
final Activity activity = QtNative.activity();
7578

7679
final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
7780
intent.setType(mime);
81+
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
82+
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
83+
if(grantWrite)
84+
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
7885
if(openable)
7986
intent.addCategory(Intent.CATEGORY_OPENABLE);
8087
//TODO add in Android O
8188
// if(!url.isEmpty())
8289
// intent.putExtra(Intent.EXTRA_INITIAL_URI, Uri.parse(url));
83-
if(allowMultiple)
84-
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
8590

8691
Runnable runnable = new Runnable () {
8792
public void run() {
8893
activity.startActivityForResult(
8994
Intent.createChooser(intent, title),
90-
CHOOSE_CONTENT_ACTION);
95+
CHOOSE_DOCUMENT_ACTION);
9196
};
9297
};
9398
activity.runOnUiThread(runnable);
@@ -98,6 +103,9 @@ static private void createDocument(final String title, String mime, String url,
98103

99104
final Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
100105
intent.setType(mime);
106+
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
107+
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
108+
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
101109
if(openable)
102110
intent.addCategory(Intent.CATEGORY_OPENABLE);
103111
//TODO add in Android O
@@ -110,17 +118,27 @@ static private void createDocument(final String title, String mime, String url,
110118
public void run() {
111119
activity.startActivityForResult(
112120
Intent.createChooser(intent, title),
113-
CHOOSE_CONTENT_ACTION);
121+
CHOOSE_DOCUMENT_ACTION);
114122
};
115123
};
116124
activity.runOnUiThread(runnable);
117125
}
118126

119127
static private void onActivityResult(int requestCode, int resultCode, Intent data) {
120-
if(requestCode == CHOOSE_CONTENT_ACTION) {
128+
if(requestCode == CHOOSE_CONTENT_ACTION ||
129+
requestCode == CHOOSE_DOCUMENT_ACTION) {
121130
Map reply = new HashMap();
122131
if (resultCode == Activity.RESULT_OK) {
123132
Uri uri = data.getData();
133+
134+
if(requestCode == CHOOSE_DOCUMENT_ACTION) {
135+
Activity activity = QtNative.activity();
136+
int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
137+
activity.grantUriPermission(activity.getPackageName(), uri, takeFlags);
138+
ContentResolver resolver = activity.getContentResolver();
139+
resolver.takePersistableUriPermission(uri, takeFlags);
140+
}
141+
124142
reply.put("uri", uri.toString());
125143
reply.put("success", true);
126144
} else

filechooser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ FileChooser::FileChooser(QObject *parent) :
77
QObject(parent),
88
_title(),
99
_contentUrl(),
10-
_type(GetContent),
10+
_type(OpenDocument),
1111
_mimeType(QStringLiteral("*/*")),
1212
_flags(OpenableFlag)
1313
{
@@ -104,7 +104,7 @@ void FileChooser::open()
104104
{"mime", _mimeType},
105105
{"openable", _flags.testFlag(OpenableFlag)},
106106
{"localOnly", _flags.testFlag(LocalOnlyFlag)},
107-
{"allowMultiple", _flags.testFlag(AllowMultipleFlag)}
107+
{"grantWrite", _flags.testFlag(AlwaysGrantWrite)}
108108
};
109109
break;
110110
case FileChooser::OpenDocument:
@@ -114,7 +114,7 @@ void FileChooser::open()
114114
{"mime", _mimeType},
115115
{"url", _contentUrl.toString()},
116116
{"openable", _flags.testFlag(OpenableFlag)},
117-
{"allowMultiple", _flags.testFlag(AllowMultipleFlag)}
117+
{"grantWrite", _flags.testFlag(AlwaysGrantWrite)}
118118
};
119119
break;
120120
case FileChooser::CreateDocument:

filechooser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class FileChooser : public QObject
2626
enum ChooserFlag {
2727
OpenableFlag = 0x01,
2828
LocalOnlyFlag = 0x02,
29-
AllowMultipleFlag = 0x04
29+
AlwaysGrantWrite = 0x04
3030
};
3131
Q_DECLARE_FLAGS(ChooserFlags, ChooserFlag)
3232
Q_FLAG(ChooserFlags)

0 commit comments

Comments
 (0)