You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by no...@apache.org on 2022/03/17 13:08:40 UTC

[cordova-plugin-file] branch master updated: fix(android): Request external read permission when listing external directories (#487)

This is an automated email from the ASF dual-hosted git repository.

normanbreau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-plugin-file.git


The following commit(s) were added to refs/heads/master by this push:
     new a56ab1e  fix(android): Request external read permission when listing external directories (#487)
a56ab1e is described below

commit a56ab1eb890e4a6a7f0ff5aac42165593f1a5a4c
Author: Norman Breau <no...@nbsolutions.ca>
AuthorDate: Thu Mar 17 10:06:58 2022 -0300

    fix(android): Request external read permission when listing external directories (#487)
---
 README.md                  |  4 +++-
 plugin.xml                 |  3 ---
 src/android/FileUtils.java | 25 +++++++++++++++++++++----
 3 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/README.md b/README.md
index ae47fa3..3bf3ebd 100644
--- a/README.md
+++ b/README.md
@@ -160,7 +160,7 @@ the `cordova.file.*` properties map to physical paths on a real device.
 | &nbsp;&nbsp;&nbsp;`cache`                       | cacheDirectory              | cache                     | r/w  |     Yes     |     Yes\* |   Yes   |
 | &nbsp;&nbsp;&nbsp;`files`                       | dataDirectory               | files                     | r/w  |     Yes     |     No    |   Yes   |
 | &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`Documents` |                             | documents                 | r/w  |     Yes     |     No    |   Yes   |
-| `<sdcard>/`                                     | externalRootDirectory       | sdcard                    | r/w  |     Yes     |     No    |   No    |
+| `<sdcard>/`                                     | externalRootDirectory       | sdcard                    | r/w\*\*\*  |     Yes     |     No    |   No    |
 | &nbsp;&nbsp;&nbsp;`Android/data/<app-id>/`      | externalApplicationStorageDirectory | -                 | r/w  |     Yes     |     No    |   No    |
 | &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`cache`     | externalCacheDirectory       | cache-external            | r/w  |     Yes     |     No\*\*|   No    |
 | &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.
      the contents yourself. Should the user purge the cache manually, the contents of the
      directory are removed.
 
+\*\*\* As of API 30, these directories are no longer writable.
+
 **Note**: If external storage can't be mounted, the `cordova.file.external*`
 properties are `null`.
 
diff --git a/plugin.xml b/plugin.xml
index 2eec461..0a2ee72 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -132,9 +132,6 @@ to config.xml in order for the application to find previously stored files.
             </feature>
             <allow-navigation href="cdvfile:*" />
         </config-file>
-        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
-            <application android:requestLegacyExternalStorage="true" />
-        </edit-config>
         <config-file target="AndroidManifest.xml" parent="/*">
             <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
         </config-file>
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index cd2a338..c2b1ca0 100644
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -88,6 +88,7 @@ public class FileUtils extends CordovaPlugin {
     public static final int ACTION_GET_FILE = 0;
     public static final int ACTION_WRITE = 1;
     public static final int ACTION_GET_DIRECTORY = 2;
+    public static final int ACTION_READ_ENTRIES = 3;
 
     public static final int WRITE = 3;
     public static final int READ = 4;
@@ -285,6 +286,7 @@ public class FileUtils extends CordovaPlugin {
         if (action.equals("testSaveLocationExists")) {
             threadhelper(new FileOp() {
                 public void run(JSONArray args) {
+                    
                     boolean b = DirectoryManager.testSaveLocationExists();
                     callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
                 }
@@ -543,10 +545,16 @@ public class FileUtils extends CordovaPlugin {
         }
         else if (action.equals("readEntries")) {
             threadhelper( new FileOp( ){
-                public void run(JSONArray args) throws FileNotFoundException, JSONException, MalformedURLException {
-                    String fname=args.getString(0);
-                    JSONArray entries = readEntries(fname);
-                    callbackContext.success(entries);
+                public void run(JSONArray args) throws FileNotFoundException, JSONException, MalformedURLException, IOException {
+                    String directory = args.getString(0);
+                    String nativeURL = resolveLocalFileSystemURI(directory).getString("nativeURL");
+                    if (needPermission(nativeURL, READ)) {
+                        getReadPermission(rawArgs, ACTION_READ_ENTRIES, callbackContext);
+                    }
+                    else {
+                        JSONArray entries = readEntries(directory);
+                        callbackContext.success(entries);
+                    }
                 }
             }, rawArgs, callbackContext);
         }
@@ -1236,6 +1244,15 @@ public class FileUtils extends CordovaPlugin {
                         }
                     }, req.getRawArgs(), req.getCallbackContext());
                     break;
+                case ACTION_READ_ENTRIES:
+                    threadhelper( new FileOp( ){
+                        public void run(JSONArray args) throws FileNotFoundException, JSONException, MalformedURLException {
+                            String fname=args.getString(0);
+                            JSONArray entries = readEntries(fname);
+                            req.getCallbackContext().success(entries);
+                        }
+                    }, req.getRawArgs(), req.getCallbackContext());
+                    break;
             }
         } else {
            LOG.d(LOG_TAG, "Received permission callback for unknown request code");

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org