Skip to content

Commit a6ff7ea

Browse files
authored
Merge pull request #501 from ath0mas/external-sdcards-no-permission
Android: direct getExternalSdCardDetails without permission request
2 parents e29cdeb + 6c23198 commit a6ff7ea

File tree

3 files changed

+4
-39
lines changed

3 files changed

+4
-39
lines changed

README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -3438,7 +3438,7 @@ Checks if the application is authorized to use external storage.
34383438

34393439
Notes for Android:
34403440
- 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.
3441-
- This checks for `READ_EXTERNAL_STORAGE` `CAMERA` run-time permission.
3441+
- This checks for `READ_EXTERNAL_STORAGE` run-time permission.
34423442

34433443
```
34443444
cordova.plugins.diagnostic.isExternalStorageAuthorized(successCallback, errorCallback);
@@ -3556,8 +3556,6 @@ Whereas this method returns:
35563556

35573557
which are on external removable storage.
35583558

3559-
- Requires permission for `READ_EXTERNAL_STORAGE` run-time permission which must be added to `AndroidManifest.xml`.
3560-
35613559
```
35623560
cordova.plugins.diagnostic.getExternalSdCardDetails(successCallback, errorCallback);
35633561
```
@@ -3572,7 +3570,7 @@ Each array entry is an object with the following keys:
35723570
- {String} path - absolute path to the storage location
35733571
- {String} filePath - absolute path prefixed with file protocol for use with cordova-plugin-file
35743572
- {Boolean} canWrite - true if the location is writable
3575-
- {Integer} freeSpace - number of bytes of free space on the device on which the storage locaiton is mounted.
3573+
- {Integer} freeSpace - number of bytes of free space on the device on which the storage location is mounted.
35763574
- {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.
35773575
- {Function} errorCallback - The callback which will be called when operation encounters an error.
35783576
The function is passed a single string parameter containing the error message.

src/android/Diagnostic.java

+1-14
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,6 @@ public class Diagnostic extends CordovaPlugin{
233233
public static final String CPU_ARCH_MIPS = "MIPS";
234234
public static final String CPU_ARCH_MIPS_64 = "MIPS_64";
235235

236-
protected static final String externalStorageClassName = "cordova.plugins.Diagnostic_External_Storage";
237-
protected static final Integer GET_EXTERNAL_SD_CARD_DETAILS_PERMISSION_REQUEST = 1000;
238-
239236
/*************
240237
* Variables *
241238
*************/
@@ -1056,17 +1053,7 @@ public void onRequestPermissionResult(int requestCode, String[] permissions, int
10561053
clearRequest(requestCode);
10571054
}
10581055

1059-
Class<?> externalStorageClass = null;
1060-
try {
1061-
externalStorageClass = Class.forName(externalStorageClassName);
1062-
} catch( ClassNotFoundException e ){}
1063-
1064-
if(requestCode == GET_EXTERNAL_SD_CARD_DETAILS_PERMISSION_REQUEST && externalStorageClass != null){
1065-
Method method = externalStorageClass.getMethod("onReceivePermissionResult");
1066-
method.invoke(null);
1067-
}else{
1068-
context.success(statuses);
1069-
}
1056+
context.success(statuses);
10701057
}catch(Exception e ) {
10711058
handleError("Exception occurred onRequestPermissionsResult: ".concat(e.getMessage()), requestCode);
10721059
}

src/android/Diagnostic_External_Storage.java

+1-21
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public class Diagnostic_External_Storage extends CordovaPlugin{
5151
* Constants *
5252
*************/
5353

54-
5554
/**
5655
* Tag for debug log messages
5756
*/
@@ -74,8 +73,6 @@ public class Diagnostic_External_Storage extends CordovaPlugin{
7473
*/
7574
protected CallbackContext currentContext;
7675

77-
protected static String externalStoragePermission = "READ_EXTERNAL_STORAGE";
78-
7976

8077
/*************
8178
* Public API
@@ -127,29 +124,12 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
127124
return true;
128125
}
129126

130-
public static void onReceivePermissionResult() throws JSONException{
131-
instance._getExternalSdCardDetails();
132-
}
133127

134128
/************
135129
* Internals
136130
***********/
137131

138-
protected void getExternalSdCardDetails() throws Exception{
139-
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
140-
_getExternalSdCardDetails();
141-
} else {
142-
String permission = diagnostic.permissionsMap.get(externalStoragePermission);
143-
if (diagnostic.hasRuntimePermission(permission)) {
144-
_getExternalSdCardDetails();
145-
} else {
146-
diagnostic.requestRuntimePermission(permission, Diagnostic.GET_EXTERNAL_SD_CARD_DETAILS_PERMISSION_REQUEST);
147-
}
148-
}
149-
}
150-
151-
152-
protected void _getExternalSdCardDetails() throws JSONException {
132+
protected void getExternalSdCardDetails() throws JSONException {
153133
String[] storageDirectories = getStorageDirectories();
154134

155135
JSONArray details = new JSONArray();

0 commit comments

Comments
 (0)