@@ -20,14 +20,12 @@ Licensed to the Apache Software Foundation (ASF) under one
20
20
21
21
import android .Manifest ;
22
22
import android .app .Activity ;
23
- import android .content .ContentResolver ;
24
23
import android .content .Context ;
25
24
import android .content .pm .PackageManager ;
26
25
import android .net .Uri ;
27
26
import android .os .Build ;
28
27
import android .os .Environment ;
29
28
import android .util .Base64 ;
30
- import android .util .Log ;
31
29
import android .webkit .MimeTypeMap ;
32
30
import android .webkit .WebResourceResponse ;
33
31
@@ -46,17 +44,13 @@ Licensed to the Apache Software Foundation (ASF) under one
46
44
import org .json .JSONException ;
47
45
import org .json .JSONObject ;
48
46
49
- import java .io .BufferedInputStream ;
50
47
import java .io .ByteArrayOutputStream ;
51
48
import java .io .File ;
52
49
import java .io .FileInputStream ;
53
50
import java .io .FileNotFoundException ;
54
51
import java .io .IOException ;
55
52
import java .io .InputStream ;
56
- import java .net .HttpURLConnection ;
57
53
import java .net .MalformedURLException ;
58
- import java .net .URL ;
59
- import java .security .Permission ;
60
54
import java .util .ArrayList ;
61
55
import java .util .HashMap ;
62
56
import java .util .HashSet ;
@@ -575,9 +569,21 @@ private boolean needPermission(String nativeURL, int permissionType) throws JSON
575
569
if (j .has ("externalApplicationStorageDirectory" )) {
576
570
allowedStorageDirectories .add (j .getString ("externalApplicationStorageDirectory" ));
577
571
}
578
- ArrayList <String > allowedExtraPatternStorageDirectories = new ArrayList <String >();
579
- // basic pattern for usual application storage directory, to extend the allowed list to external SD cards for example
580
- allowedExtraPatternStorageDirectories .add ("/Android/data/" + cordova .getActivity ().getPackageName () + "/" );
572
+ if (j .has ("removableExternalApplicationStorageDirectories" )) {
573
+ JSONArray array = j .getJSONArray ("removableExternalApplicationStorageDirectories" );
574
+ for (int i = 0 ; i < array .length (); i ++) {
575
+ allowedStorageDirectories .add (array .getString (i ));
576
+ }
577
+ }
578
+ if (j .has ("removableExternalMediaDirectories" )) {
579
+ JSONArray array = j .getJSONArray ("removableExternalMediaDirectories" );
580
+ for (int i = 0 ; i < array .length (); i ++) {
581
+ allowedStorageDirectories .add (array .getString (i ));
582
+ }
583
+ }
584
+ if (j .has ("externalMediaDirectory" )) {
585
+ allowedStorageDirectories .add (j .getString ("externalMediaDirectory" ));
586
+ }
581
587
582
588
if (permissionType == READ && hasReadPermission ()) {
583
589
return false ;
@@ -591,11 +597,6 @@ private boolean needPermission(String nativeURL, int permissionType) throws JSON
591
597
return false ;
592
598
}
593
599
}
594
- for (String extraPatternDirectory : allowedExtraPatternStorageDirectories ) {
595
- if (nativeURL .contains (extraPatternDirectory )) {
596
- return false ;
597
- }
598
- }
599
600
return true ;
600
601
}
601
602
@@ -1000,16 +1001,56 @@ private JSONObject requestAllPaths() throws JSONException {
1000
1001
ret .put ("applicationStorageDirectory" , toDirUrl (context .getFilesDir ().getParentFile ()));
1001
1002
ret .put ("dataDirectory" , toDirUrl (context .getFilesDir ()));
1002
1003
ret .put ("cacheDirectory" , toDirUrl (context .getCacheDir ()));
1003
- if ( Environment . getExternalStorageState (). equals ( Environment . MEDIA_MOUNTED )) {
1004
- try {
1004
+ try {
1005
+ if ( Environment . getExternalStorageState (). equals ( Environment . MEDIA_MOUNTED )) {
1005
1006
ret .put ("externalApplicationStorageDirectory" , toDirUrl (context .getExternalFilesDir (null ).getParentFile ()));
1006
1007
ret .put ("externalDataDirectory" , toDirUrl (context .getExternalFilesDir (null )));
1007
1008
ret .put ("externalCacheDirectory" , toDirUrl (context .getExternalCacheDir ()));
1008
1009
ret .put ("externalRootDirectory" , toDirUrl (Environment .getExternalStorageDirectory ()));
1009
- } catch (NullPointerException e ) {
1010
- /* If external storage is unavailable, context.getExternal* returns null */
1011
- LOG .d (LOG_TAG , "Unable to access these paths, most liklely due to USB storage" );
1012
1010
}
1011
+
1012
+ JSONArray removableExternalApplicationStorageDirs = new JSONArray ();
1013
+ JSONArray removableExternalDataDirs = new JSONArray ();
1014
+ JSONArray removableExternalCacheDirs = new JSONArray ();
1015
+ JSONArray removableExternalMediaDirs = new JSONArray ();
1016
+ String externalMediaDir = null ;
1017
+ for (File filesDir : context .getExternalFilesDirs (null )) {
1018
+ if (filesDir != null ) {
1019
+ if (Environment .isExternalStorageRemovable (filesDir )) {
1020
+ removableExternalApplicationStorageDirs .put (toDirUrl (filesDir .getParentFile ()));
1021
+ removableExternalDataDirs .put (toDirUrl (filesDir ));
1022
+ }
1023
+ }
1024
+ }
1025
+ for (File cacheDir : context .getExternalCacheDirs ()) {
1026
+ if (cacheDir != null ) {
1027
+ if (Environment .isExternalStorageRemovable (cacheDir )) {
1028
+ removableExternalCacheDirs .put (toDirUrl (cacheDir ));
1029
+ }
1030
+ }
1031
+ }
1032
+ for (File mediaDir : context .getExternalMediaDirs ()) {
1033
+ if (mediaDir != null ) {
1034
+ String dirUrl = toDirUrl (mediaDir );
1035
+ if (Environment .isExternalStorageRemovable (mediaDir )) {
1036
+ removableExternalMediaDirs .put (dirUrl );
1037
+ } else {
1038
+ if (externalMediaDir != null ) {
1039
+ LOG .w (LOG_TAG , "External media directory already found ; skip other value " + dirUrl );
1040
+ continue ;
1041
+ }
1042
+ externalMediaDir = dirUrl ;
1043
+ }
1044
+ }
1045
+ }
1046
+ ret .put ("removableExternalApplicationStorageDirectories" , removableExternalApplicationStorageDirs );
1047
+ ret .put ("removableExternalDataDirectories" , removableExternalDataDirs );
1048
+ ret .put ("removableExternalCacheDirectories" , removableExternalCacheDirs );
1049
+ ret .put ("removableExternalMediaDirectories" , removableExternalMediaDirs );
1050
+ ret .put ("externalMediaDirectory" , externalMediaDir );
1051
+ } catch (NullPointerException e ) {
1052
+ /* If external storage is unavailable, context.getExternal* returns null */
1053
+ LOG .d (LOG_TAG , "Unable to access these paths, most likely due to USB storage" );
1013
1054
}
1014
1055
return ret ;
1015
1056
}
@@ -1302,9 +1343,9 @@ public CordovaPluginPathHandler getPathHandler() {
1302
1343
1303
1344
return new WebResourceResponse (fileMimeType , null , fileIS );
1304
1345
} catch (FileNotFoundException e ) {
1305
- Log .e (LOG_TAG , e .getMessage ());
1346
+ LOG .e (LOG_TAG , e .getMessage ());
1306
1347
} catch (IOException e ) {
1307
- Log .e (LOG_TAG , e .getMessage ());
1348
+ LOG .e (LOG_TAG , e .getMessage ());
1308
1349
}
1309
1350
}
1310
1351
}
0 commit comments