6
6
import android .net .Uri ;
7
7
import android .content .Context ;
8
8
import android .content .Intent ;
9
+ import android .content .ContentResolver ;
9
10
import android .app .Activity ;
10
11
import org .qtproject .qt5 .android .QtNative ;
11
12
import androidnative .SystemDispatcher ;
12
13
13
14
public class FileChooser {
14
15
public static final int CHOOSE_CONTENT_ACTION = 0x1091c657 ;
16
+ public static final int CHOOSE_DOCUMENT_ACTION = 0x1091c658 ;
15
17
16
18
public static final String GET_CONTENT_MESSAGE = "AndroidUtils.FileChooser.getContent" ;
17
19
public static final String OPEN_DOCUMENT_MESSAGE = "AndroidUtils.FileChooser.openDocument" ;
@@ -26,13 +28,13 @@ public void onDispatched(String type , Map message) {
26
28
(String )message .get ("mime" ),
27
29
(Boolean )message .get ("openable" ),
28
30
(Boolean )message .get ("localOnly" ),
29
- (Boolean )message .get ("allowMultiple " ));
31
+ (Boolean )message .get ("grantWrite " ));
30
32
} else if (type .equals (OPEN_DOCUMENT_MESSAGE )) {
31
33
openDocument ((String )message .get ("title" ),
32
34
(String )message .get ("mime" ),
33
35
(String )message .get ("url" ),
34
36
(Boolean )message .get ("openable" ),
35
- (Boolean )message .get ("allowMultiple " ));
37
+ (Boolean )message .get ("grantWrite " ));
36
38
} else if (type .equals (CREATE_DOCUMENT_MESSAGE )) {
37
39
createDocument ((String )message .get ("title" ),
38
40
(String )message .get ("mime" ),
@@ -48,17 +50,18 @@ public void onDispatched(String type , Map message) {
48
50
});
49
51
}
50
52
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 ) {
52
54
final Activity activity = QtNative .activity ();
53
55
54
56
final Intent intent = new Intent (Intent .ACTION_GET_CONTENT );
55
57
intent .setType (mime );
58
+ intent .addFlags (Intent .FLAG_GRANT_READ_URI_PERMISSION );
59
+ if (grantWrite )
60
+ intent .addFlags (Intent .FLAG_GRANT_WRITE_URI_PERMISSION );
56
61
if (openable )
57
62
intent .addCategory (Intent .CATEGORY_OPENABLE );
58
63
if (localOnly )
59
64
intent .putExtra (Intent .EXTRA_LOCAL_ONLY , true );
60
- if (allowMultiple )
61
- intent .putExtra (Intent .EXTRA_ALLOW_MULTIPLE , true );
62
65
63
66
Runnable runnable = new Runnable () {
64
67
public void run () {
@@ -70,24 +73,26 @@ public void run() {
70
73
activity .runOnUiThread (runnable );
71
74
}
72
75
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 ) {
74
77
final Activity activity = QtNative .activity ();
75
78
76
79
final Intent intent = new Intent (Intent .ACTION_OPEN_DOCUMENT );
77
80
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 );
78
85
if (openable )
79
86
intent .addCategory (Intent .CATEGORY_OPENABLE );
80
87
//TODO add in Android O
81
88
// if(!url.isEmpty())
82
89
// intent.putExtra(Intent.EXTRA_INITIAL_URI, Uri.parse(url));
83
- if (allowMultiple )
84
- intent .putExtra (Intent .EXTRA_ALLOW_MULTIPLE , true );
85
90
86
91
Runnable runnable = new Runnable () {
87
92
public void run () {
88
93
activity .startActivityForResult (
89
94
Intent .createChooser (intent , title ),
90
- CHOOSE_CONTENT_ACTION );
95
+ CHOOSE_DOCUMENT_ACTION );
91
96
};
92
97
};
93
98
activity .runOnUiThread (runnable );
@@ -98,6 +103,9 @@ static private void createDocument(final String title, String mime, String url,
98
103
99
104
final Intent intent = new Intent (Intent .ACTION_CREATE_DOCUMENT );
100
105
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 );
101
109
if (openable )
102
110
intent .addCategory (Intent .CATEGORY_OPENABLE );
103
111
//TODO add in Android O
@@ -110,17 +118,27 @@ static private void createDocument(final String title, String mime, String url,
110
118
public void run () {
111
119
activity .startActivityForResult (
112
120
Intent .createChooser (intent , title ),
113
- CHOOSE_CONTENT_ACTION );
121
+ CHOOSE_DOCUMENT_ACTION );
114
122
};
115
123
};
116
124
activity .runOnUiThread (runnable );
117
125
}
118
126
119
127
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 ) {
121
130
Map reply = new HashMap ();
122
131
if (resultCode == Activity .RESULT_OK ) {
123
132
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
+
124
142
reply .put ("uri" , uri .toString ());
125
143
reply .put ("success" , true );
126
144
} else
0 commit comments