Browse Source

Catch exceptions on sd card directories constants creation. Methods to get sd card directories. sd card directiories as constants deprecated warning

Artur Chrusciel 7 years ago
parent
commit
22fd32a863

+ 17
- 1
android.js View File

@@ -38,9 +38,25 @@ function addCompleteDownload(config) {
38 38
     return Promise.reject('RNFetchBlob.android.addCompleteDownload only supports Android.')
39 39
 }
40 40
 
41
+function getSDCardDir() {
42
+  if(Platform.OS === 'android')
43
+    return RNFetchBlob.getSDCardDir()
44
+  else
45
+    return Promise.reject('RNFetchBlob.android.getSDCardDir only supports Android.')
46
+}
47
+
48
+function getSDCardApplicationDir() {
49
+  if(Platform.OS === 'android')
50
+    return RNFetchBlob.getSDCardApplicationDir()
51
+  else
52
+    return Promise.reject('RNFetchBlob.android.getSDCardApplicationDir only supports Android.')
53
+}
54
+
41 55
 
42 56
 export default {
43 57
   actionViewIntent,
44 58
   getContentIntent,
45
-  addCompleteDownload
59
+  addCompleteDownload,
60
+  getSDCardDir,
61
+  getSDCardApplicationDir,
46 62
 }

+ 10
- 6
android/src/main/java/com/RNFetchBlob/RNFetchBlob.java View File

@@ -92,7 +92,6 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
92 92
                 RNFetchBlobFS.createFile(path, content, encode, callback);
93 93
             }
94 94
         });
95
-
96 95
     }
97 96
 
98 97
     @ReactMethod
@@ -136,7 +135,6 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
136 135
                 RNFetchBlobFS.createFileASCII(path, dataArray, callback);
137 136
             }
138 137
         });
139
-
140 138
     }
141 139
 
142 140
     @ReactMethod
@@ -167,7 +165,6 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
167 165
                 RNFetchBlobFS.cp(path, dest, callback);
168 166
             }
169 167
         });
170
-
171 168
     }
172 169
 
173 170
     @ReactMethod
@@ -228,7 +225,6 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
228 225
                 RNFetchBlobFS.writeFile(path, encoding, data, append, promise);
229 226
             }
230 227
         });
231
-
232 228
     }
233 229
 
234 230
     @ReactMethod
@@ -263,7 +259,6 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
263 259
                 new RNFetchBlobFS(ctx).scanFile(p, m, callback);
264 260
             }
265 261
         });
266
-
267 262
     }
268 263
 
269 264
     @ReactMethod
@@ -324,7 +319,7 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
324 319
     @ReactMethod
325 320
     public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
326 321
         new RNFetchBlobReq(options, taskId, method, url, headers, body, null, mClient, callback).run();
327
-}
322
+    }
328 323
 
329 324
     @ReactMethod
330 325
     public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) {
@@ -370,4 +365,13 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
370 365
 
371 366
     }
372 367
 
368
+    @ReactMethod
369
+    public void getSDCardDir(Promise promise) {
370
+        RNFetchBlobFS.getSDCardDir(promise);
371
+    }
372
+
373
+    @ReactMethod
374
+    public void getSDCardApplicationDir(Promise promise) {
375
+        RNFetchBlobFS.getSDCardApplicationDir(this.getReactApplicationContext(), promise);
376
+    }
373 377
 }

+ 27
- 1
android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

@@ -213,12 +213,38 @@ public class RNFetchBlobFS {
213 213
         state = Environment.getExternalStorageState();
214 214
         if (state.equals(Environment.MEDIA_MOUNTED)) {
215 215
             res.put("SDCardDir", Environment.getExternalStorageDirectory().getAbsolutePath());
216
-            res.put("SDCardApplicationDir", ctx.getExternalFilesDir(null).getParentFile().getAbsolutePath());
216
+            try {
217
+                res.put("SDCardApplicationDir", ctx.getExternalFilesDir(null).getParentFile().getAbsolutePath());
218
+            } catch(Exception e) {
219
+                res.put("SDCardApplicationDir", "");
220
+            }
217 221
         }
218 222
         res.put("MainBundleDir", ctx.getApplicationInfo().dataDir);
219 223
         return res;
220 224
     }
221 225
 
226
+    static public void getSDCardDir(Promise promise) {
227
+        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
228
+            promise.resolve(Environment.getExternalStorageDirectory().getAbsolutePath());
229
+        } else {
230
+            promise.reject("RNFetchBlob.getSDCardDir", "External storage not mounted");
231
+        }
232
+
233
+    }
234
+
235
+    static public void getSDCardApplicationDir(ReactApplicationContext ctx, Promise promise) {
236
+        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
237
+            try {
238
+                final String path = ctx.getExternalFilesDir(null).getParentFile().getAbsolutePath();
239
+                promise.resolve(path);
240
+            } catch (Exception e) {
241
+                promise.reject("RNFetchBlob.getSDCardApplicationDir", e.getLocalizedMessage());
242
+            }
243
+        } else {
244
+            promise.reject("RNFetchBlob.getSDCardApplicationDir", "External storage not mounted");
245
+        }
246
+    }
247
+
222 248
     /**
223 249
      * Static method that returns a temp file path
224 250
      * @param ctx   React Native application context

+ 11
- 2
fs.js View File

@@ -28,8 +28,17 @@ const dirs = {
28 28
     MovieDir : RNFetchBlob.MovieDir,
29 29
     DownloadDir : RNFetchBlob.DownloadDir,
30 30
     DCIMDir : RNFetchBlob.DCIMDir,
31
-    SDCardDir : RNFetchBlob.SDCardDir,
32
-    SDCardApplicationDir : RNFetchBlob.SDCardApplicationDir,
31
+    get SDCardDir() {
32
+      console.warn('SDCardDir as a constant is deprecated and will be removed in feature release. ' +
33
+                   'Use RNFetchBlob.android.getSDCardDir():Promise instead.');
34
+      return RNFetchBlob.SDCardDir;
35
+    },
36
+    get SDCardApplicationDir() {
37
+      console.warn('SDCardApplicationDir as a constant is deprecated and will be removed in feature release. ' +
38
+                   'Use RNFetchBlob.android.getSDCardApplicationDir():Promise instead. ' +
39
+                   'This variable can be empty on error in native code.');
40
+      return RNFetchBlob.SDCardApplicationDir;
41
+    },
33 42
     MainBundleDir : RNFetchBlob.MainBundleDir,
34 43
     LibraryDir : RNFetchBlob.LibraryDir
35 44
 }