Skip to content

Commit a56ab1e

Browse files
authored
fix(android): Request external read permission when listing external directories (apache#487)
1 parent 3366ea6 commit a56ab1e

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ the `cordova.file.*` properties map to physical paths on a real device.
160160
|    `cache` | cacheDirectory | cache | r/w | Yes | Yes\* | Yes |
161161
|    `files` | dataDirectory | files | r/w | Yes | No | Yes |
162162
|       `Documents` | | documents | r/w | Yes | No | Yes |
163-
| `<sdcard>/` | externalRootDirectory | sdcard | r/w | Yes | No | No |
163+
| `<sdcard>/` | externalRootDirectory | sdcard | r/w\*\*\* | Yes | No | No |
164164
| &nbsp;&nbsp;&nbsp;`Android/data/<app-id>/` | externalApplicationStorageDirectory | - | r/w | Yes | No | No |
165165
| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`cache` | externalCacheDirectory | cache-external | r/w | Yes | No\*\*| No |
166166
| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`files` | externalDataDirectory | files-external | r/w | Yes | No | No |
@@ -173,6 +173,8 @@ the `cordova.file.*` properties map to physical paths on a real device.
173173
the contents yourself. Should the user purge the cache manually, the contents of the
174174
directory are removed.
175175

176+
\*\*\* As of API 30, these directories are no longer writable.
177+
176178
**Note**: If external storage can't be mounted, the `cordova.file.external*`
177179
properties are `null`.
178180

plugin.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,6 @@ to config.xml in order for the application to find previously stored files.
132132
</feature>
133133
<allow-navigation href="cdvfile:*" />
134134
</config-file>
135-
<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
136-
<application android:requestLegacyExternalStorage="true" />
137-
</edit-config>
138135
<config-file target="AndroidManifest.xml" parent="/*">
139136
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
140137
</config-file>

src/android/FileUtils.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public class FileUtils extends CordovaPlugin {
8888
public static final int ACTION_GET_FILE = 0;
8989
public static final int ACTION_WRITE = 1;
9090
public static final int ACTION_GET_DIRECTORY = 2;
91+
public static final int ACTION_READ_ENTRIES = 3;
9192

9293
public static final int WRITE = 3;
9394
public static final int READ = 4;
@@ -285,6 +286,7 @@ public boolean execute(String action, final String rawArgs, final CallbackContex
285286
if (action.equals("testSaveLocationExists")) {
286287
threadhelper(new FileOp() {
287288
public void run(JSONArray args) {
289+
288290
boolean b = DirectoryManager.testSaveLocationExists();
289291
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
290292
}
@@ -543,10 +545,16 @@ public void run(JSONArray args) throws JSONException, NoModificationAllowedExcep
543545
}
544546
else if (action.equals("readEntries")) {
545547
threadhelper( new FileOp( ){
546-
public void run(JSONArray args) throws FileNotFoundException, JSONException, MalformedURLException {
547-
String fname=args.getString(0);
548-
JSONArray entries = readEntries(fname);
549-
callbackContext.success(entries);
548+
public void run(JSONArray args) throws FileNotFoundException, JSONException, MalformedURLException, IOException {
549+
String directory = args.getString(0);
550+
String nativeURL = resolveLocalFileSystemURI(directory).getString("nativeURL");
551+
if (needPermission(nativeURL, READ)) {
552+
getReadPermission(rawArgs, ACTION_READ_ENTRIES, callbackContext);
553+
}
554+
else {
555+
JSONArray entries = readEntries(directory);
556+
callbackContext.success(entries);
557+
}
550558
}
551559
}, rawArgs, callbackContext);
552560
}
@@ -1236,6 +1244,15 @@ public void run(JSONArray args) throws JSONException, FileNotFoundException, IOE
12361244
}
12371245
}, req.getRawArgs(), req.getCallbackContext());
12381246
break;
1247+
case ACTION_READ_ENTRIES:
1248+
threadhelper( new FileOp( ){
1249+
public void run(JSONArray args) throws FileNotFoundException, JSONException, MalformedURLException {
1250+
String fname=args.getString(0);
1251+
JSONArray entries = readEntries(fname);
1252+
req.getCallbackContext().success(entries);
1253+
}
1254+
}, req.getRawArgs(), req.getCallbackContext());
1255+
break;
12391256
}
12401257
} else {
12411258
LOG.d(LOG_TAG, "Received permission callback for unknown request code");

0 commit comments

Comments
 (0)