Skip to content

Android: direct getExternalSdCardDetails without permission request #501

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

Merged
merged 2 commits into from
Nov 10, 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
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3535,7 +3535,7 @@ Checks if the application is authorized to use external storage.

Notes for Android:
- This is intended for Android 6 / API 23 and above. Calling on Android 5.1 / API 22 and below will always return TRUE as permissions are already granted at installation time.
- This checks for `READ_EXTERNAL_STORAGE` `CAMERA` run-time permission.
- This checks for `READ_EXTERNAL_STORAGE` run-time permission.

```
cordova.plugins.diagnostic.isExternalStorageAuthorized(successCallback, errorCallback);
Expand Down Expand Up @@ -3653,8 +3653,6 @@ Whereas this method returns:

which are on external removable storage.

- Requires permission for `READ_EXTERNAL_STORAGE` run-time permission which must be added to `AndroidManifest.xml`.

```
cordova.plugins.diagnostic.getExternalSdCardDetails(successCallback, errorCallback);
```
Expand All @@ -3669,7 +3667,7 @@ Each array entry is an object with the following keys:
- {String} path - absolute path to the storage location
- {String} filePath - absolute path prefixed with file protocol for use with cordova-plugin-file
- {Boolean} canWrite - true if the location is writable
- {Integer} freeSpace - number of bytes of free space on the device on which the storage locaiton is mounted.
- {Integer} freeSpace - number of bytes of free space on the device on which the storage location is mounted.
- {String} type - indicates the type of storage location: either "application" if the path is an Android application sandbox path or "root" if the path is the device root.
- {Function} errorCallback - The callback which will be called when operation encounters an error.
The function is passed a single string parameter containing the error message.
Expand Down
15 changes: 1 addition & 14 deletions src/android/Diagnostic.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,6 @@ public class Diagnostic extends CordovaPlugin{
public static final String CPU_ARCH_MIPS = "MIPS";
public static final String CPU_ARCH_MIPS_64 = "MIPS_64";

protected static final String externalStorageClassName = "cordova.plugins.Diagnostic_External_Storage";
protected static final Integer GET_EXTERNAL_SD_CARD_DETAILS_PERMISSION_REQUEST = 1000;

/*************
* Variables *
*************/
Expand Down Expand Up @@ -965,17 +962,7 @@ public void onRequestPermissionResult(int requestCode, String[] permissions, int
clearRequest(requestCode);
}

Class<?> externalStorageClass = null;
try {
externalStorageClass = Class.forName(externalStorageClassName);
} catch( ClassNotFoundException e ){}

if(requestCode == GET_EXTERNAL_SD_CARD_DETAILS_PERMISSION_REQUEST && externalStorageClass != null){
Method method = externalStorageClass.getMethod("onReceivePermissionResult");
method.invoke(null);
}else{
context.success(statuses);
}
context.success(statuses);
}catch(Exception e ) {
handleError("Exception occurred onRequestPermissionsResult: ".concat(e.getMessage()), requestCode);
}
Expand Down
22 changes: 1 addition & 21 deletions src/android/Diagnostic_External_Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public class Diagnostic_External_Storage extends CordovaPlugin{
* Constants *
*************/


/**
* Tag for debug log messages
*/
Expand All @@ -74,8 +73,6 @@ public class Diagnostic_External_Storage extends CordovaPlugin{
*/
protected CallbackContext currentContext;

protected static String externalStoragePermission = "READ_EXTERNAL_STORAGE";


/*************
* Public API
Expand Down Expand Up @@ -127,29 +124,12 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
return true;
}

public static void onReceivePermissionResult() throws JSONException{
instance._getExternalSdCardDetails();
}

/************
* Internals
***********/

protected void getExternalSdCardDetails() throws Exception{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
_getExternalSdCardDetails();
} else {
String permission = diagnostic.permissionsMap.get(externalStoragePermission);
if (diagnostic.hasRuntimePermission(permission)) {
_getExternalSdCardDetails();
} else {
diagnostic.requestRuntimePermission(permission, Diagnostic.GET_EXTERNAL_SD_CARD_DETAILS_PERMISSION_REQUEST);
}
}
}


protected void _getExternalSdCardDetails() throws JSONException {
protected void getExternalSdCardDetails() throws JSONException {
String[] storageDirectories = getStorageDirectories();

JSONArray details = new JSONArray();
Expand Down