@@ -20,14 +20,12 @@ Licensed to the Apache Software Foundation (ASF) under one
2020
2121import android .Manifest ;
2222import android .app .Activity ;
23- import android .content .ContentResolver ;
2423import android .content .Context ;
2524import android .content .pm .PackageManager ;
2625import android .net .Uri ;
2726import android .os .Build ;
2827import android .os .Environment ;
2928import android .util .Base64 ;
30- import android .util .Log ;
3129import android .webkit .MimeTypeMap ;
3230import android .webkit .WebResourceResponse ;
3331
@@ -46,17 +44,13 @@ Licensed to the Apache Software Foundation (ASF) under one
4644import org .json .JSONException ;
4745import org .json .JSONObject ;
4846
49- import java .io .BufferedInputStream ;
5047import java .io .ByteArrayOutputStream ;
5148import java .io .File ;
5249import java .io .FileInputStream ;
5350import java .io .FileNotFoundException ;
5451import java .io .IOException ;
5552import java .io .InputStream ;
56- import java .net .HttpURLConnection ;
5753import java .net .MalformedURLException ;
58- import java .net .URL ;
59- import java .security .Permission ;
6054import java .util .ArrayList ;
6155import java .util .HashMap ;
6256import java .util .HashSet ;
@@ -575,9 +569,21 @@ private boolean needPermission(String nativeURL, int permissionType) throws JSON
575569 if (j .has ("externalApplicationStorageDirectory" )) {
576570 allowedStorageDirectories .add (j .getString ("externalApplicationStorageDirectory" ));
577571 }
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+ }
581587
582588 if (permissionType == READ && hasReadPermission ()) {
583589 return false ;
@@ -591,11 +597,6 @@ private boolean needPermission(String nativeURL, int permissionType) throws JSON
591597 return false ;
592598 }
593599 }
594- for (String extraPatternDirectory : allowedExtraPatternStorageDirectories ) {
595- if (nativeURL .contains (extraPatternDirectory )) {
596- return false ;
597- }
598- }
599600 return true ;
600601 }
601602
@@ -1000,16 +1001,56 @@ private JSONObject requestAllPaths() throws JSONException {
10001001 ret .put ("applicationStorageDirectory" , toDirUrl (context .getFilesDir ().getParentFile ()));
10011002 ret .put ("dataDirectory" , toDirUrl (context .getFilesDir ()));
10021003 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 )) {
10051006 ret .put ("externalApplicationStorageDirectory" , toDirUrl (context .getExternalFilesDir (null ).getParentFile ()));
10061007 ret .put ("externalDataDirectory" , toDirUrl (context .getExternalFilesDir (null )));
10071008 ret .put ("externalCacheDirectory" , toDirUrl (context .getExternalCacheDir ()));
10081009 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" );
10121010 }
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" );
10131054 }
10141055 return ret ;
10151056 }
@@ -1302,9 +1343,9 @@ public CordovaPluginPathHandler getPathHandler() {
13021343
13031344 return new WebResourceResponse (fileMimeType , null , fileIS );
13041345 } catch (FileNotFoundException e ) {
1305- Log .e (LOG_TAG , e .getMessage ());
1346+ LOG .e (LOG_TAG , e .getMessage ());
13061347 } catch (IOException e ) {
1307- Log .e (LOG_TAG , e .getMessage ());
1348+ LOG .e (LOG_TAG , e .getMessage ());
13081349 }
13091350 }
13101351 }
0 commit comments