Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android!: allow read-write to app dirs in media dirs and on external sdcards too #6

Merged
merged 4 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ Each URL is in the form _file:///path/to/spot/_, and can be converted to a
* `cordova.file.externalCacheDirectory` - Application cache on external storage.
(_Android_)

* `cordova.file.externalMediaDirectory` - Application media directory on external storage. (_Android_)

* `cordova.file.externalRootDirectory` - External storage (SD card) root. (_Android_, _BlackBerry 10_)

* `cordova.file.tempDirectory` - Temp directory that the OS can clear at will. Do not
Expand All @@ -117,6 +119,14 @@ Each URL is in the form _file:///path/to/spot/_, and can be converted to a

* `cordova.file.sharedDirectory` - Files globally available to all applications (_BlackBerry 10_)

* `cordova.file.removableExternalApplicationStorageDirectories` - Application space on removable external storages. (_Android_)

* `cordova.file.removableExternalDataDirectories` - Where to put app-specific data files on removable external storages. (_Android_)

* `cordova.file.removableExternalCacheDirectories` - Application cache on removable external storages. (_Android_)

* `cordova.file.removableExternalMediaDirectories` - Application media directory on removable external storages. (_Android_)

## File System Layouts

Although technically an implementation detail, it can be very useful to know how
Expand Down Expand Up @@ -156,14 +166,20 @@ the `cordova.file.*` properties map to physical paths on a real device.
| Device Path | `cordova.file.*` | `AndroidExtraFileSystems` | r/w? | persistent? | OS clears | private |
|:------------------------------------------------|:----------------------------|:--------------------------|:----:|:-----------:|:---------:|:-------:|
| `file:///android_asset/` | applicationDirectory | assets | r | N/A | N/A | Yes |
| `<internal>/Android/data/<app-id>/` | applicationStorageDirectory | - | r/w | N/A | N/A | Yes |
| `<internal>/<app-id>/` | applicationStorageDirectory | - | r/w | N/A | N/A | Yes |
| &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 |
| &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;`cache` | externalCacheDirectory | cache-external | r/w | Yes | No\*\*| No |
| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`files` | externalDataDirectory | files-external | r/w | Yes | No | No |
| &nbsp;&nbsp;&nbsp;`Android/media/<app-id>/` | externalMediaDirectory | - | r/w | Yes | No | No |
| `<removable sdcard>/` (0 or more) | | - | r/w\*\*\* | Yes | No | No |
| &nbsp;&nbsp;&nbsp;`Android/data/<app-id>/` | removableExternalApplicationStorageDirectories | - | r/w | Yes | No | No |
| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`cache` | removableExternalCacheDirectories | - | r/w | Yes | No\*\*| No |
| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`files` | removableExternalDataDirectories | - | r/w | Yes | No | No |
| &nbsp;&nbsp;&nbsp;`Android/media/<app-id>/` | removableExternalMediaDirectories | - | r/w | Yes | No | No |

\* The OS may periodically clear this directory, but do not rely on this behavior. Clear
the contents of this directory as appropriate for your application. Should a user
Expand Down
83 changes: 62 additions & 21 deletions src/android/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ Licensed to the Apache Software Foundation (ASF) under one

import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
import android.webkit.MimeTypeMap;
import android.webkit.WebResourceResponse;

Expand All @@ -46,17 +44,13 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.Permission;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -575,9 +569,21 @@ private boolean needPermission(String nativeURL, int permissionType) throws JSON
if (j.has("externalApplicationStorageDirectory")) {
allowedStorageDirectories.add(j.getString("externalApplicationStorageDirectory"));
}
ArrayList<String> allowedExtraPatternStorageDirectories = new ArrayList<String>();
// basic pattern for usual application storage directory, to extend the allowed list to external SD cards for example
allowedExtraPatternStorageDirectories.add("/Android/data/" + cordova.getActivity().getPackageName() + "/");
if (j.has("removableExternalApplicationStorageDirectories")) {
JSONArray array = j.getJSONArray("removableExternalApplicationStorageDirectories");
for (int i = 0; i < array.length(); i++) {
allowedStorageDirectories.add(array.getString(i));
}
}
if (j.has("removableExternalMediaDirectories")) {
JSONArray array = j.getJSONArray("removableExternalMediaDirectories");
for (int i = 0; i < array.length(); i++) {
allowedStorageDirectories.add(array.getString(i));
}
}
if (j.has("externalMediaDirectory")) {
allowedStorageDirectories.add(j.getString("externalMediaDirectory"));
}

if (permissionType == READ && hasReadPermission()) {
return false;
Expand All @@ -591,11 +597,6 @@ private boolean needPermission(String nativeURL, int permissionType) throws JSON
return false;
}
}
for (String extraPatternDirectory : allowedExtraPatternStorageDirectories) {
if (nativeURL.contains(extraPatternDirectory)) {
return false;
}
}
return true;
}

Expand Down Expand Up @@ -1000,16 +1001,56 @@ private JSONObject requestAllPaths() throws JSONException {
ret.put("applicationStorageDirectory", toDirUrl(context.getFilesDir().getParentFile()));
ret.put("dataDirectory", toDirUrl(context.getFilesDir()));
ret.put("cacheDirectory", toDirUrl(context.getCacheDir()));
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
try {
try {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
ret.put("externalApplicationStorageDirectory", toDirUrl(context.getExternalFilesDir(null).getParentFile()));
ret.put("externalDataDirectory", toDirUrl(context.getExternalFilesDir(null)));
ret.put("externalCacheDirectory", toDirUrl(context.getExternalCacheDir()));
ret.put("externalRootDirectory", toDirUrl(Environment.getExternalStorageDirectory()));
} catch (NullPointerException e) {
/* If external storage is unavailable, context.getExternal* returns null */
LOG.d(LOG_TAG, "Unable to access these paths, most liklely due to USB storage");
}

JSONArray removableExternalApplicationStorageDirs = new JSONArray();
JSONArray removableExternalDataDirs = new JSONArray();
JSONArray removableExternalCacheDirs = new JSONArray();
JSONArray removableExternalMediaDirs = new JSONArray();
String externalMediaDir = null;
for (File filesDir : context.getExternalFilesDirs(null)) {
if (filesDir != null) {
if (Environment.isExternalStorageRemovable(filesDir)) {
removableExternalApplicationStorageDirs.put(toDirUrl(filesDir.getParentFile()));
removableExternalDataDirs.put(toDirUrl(filesDir));
}
}
}
for (File cacheDir : context.getExternalCacheDirs()) {
if (cacheDir != null) {
if (Environment.isExternalStorageRemovable(cacheDir)) {
removableExternalCacheDirs.put(toDirUrl(cacheDir));
}
}
}
for (File mediaDir : context.getExternalMediaDirs()) {
if (mediaDir != null) {
String dirUrl = toDirUrl(mediaDir);
if (Environment.isExternalStorageRemovable(mediaDir)) {
removableExternalMediaDirs.put(dirUrl);
} else {
if (externalMediaDir != null) {
LOG.w(LOG_TAG, "External media directory already found ; skip other value " + dirUrl);
continue;
}
externalMediaDir = dirUrl;
}
}
}
ret.put("removableExternalApplicationStorageDirectories", removableExternalApplicationStorageDirs);
ret.put("removableExternalDataDirectories", removableExternalDataDirs);
ret.put("removableExternalCacheDirectories", removableExternalCacheDirs);
ret.put("removableExternalMediaDirectories", removableExternalMediaDirs);
ret.put("externalMediaDirectory", externalMediaDir);
} catch (NullPointerException e) {
/* If external storage is unavailable, context.getExternal* returns null */
LOG.d(LOG_TAG, "Unable to access these paths, most likely due to USB storage");
}
return ret;
}
Expand Down Expand Up @@ -1302,9 +1343,9 @@ public CordovaPluginPathHandler getPathHandler() {

return new WebResourceResponse(fileMimeType, null, fileIS);
} catch (FileNotFoundException e) {
Log.e(LOG_TAG, e.getMessage());
LOG.e(LOG_TAG, e.getMessage());
} catch (IOException e) {
Log.e(LOG_TAG, e.getMessage());
LOG.e(LOG_TAG, e.getMessage());
}
}
}
Expand Down