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
     return Promise.reject('RNFetchBlob.android.addCompleteDownload only supports Android.')
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
 export default {
56
 export default {
43
   actionViewIntent,
57
   actionViewIntent,
44
   getContentIntent,
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
                 RNFetchBlobFS.createFile(path, content, encode, callback);
92
                 RNFetchBlobFS.createFile(path, content, encode, callback);
93
             }
93
             }
94
         });
94
         });
95
-
96
     }
95
     }
97
 
96
 
98
     @ReactMethod
97
     @ReactMethod
136
                 RNFetchBlobFS.createFileASCII(path, dataArray, callback);
135
                 RNFetchBlobFS.createFileASCII(path, dataArray, callback);
137
             }
136
             }
138
         });
137
         });
139
-
140
     }
138
     }
141
 
139
 
142
     @ReactMethod
140
     @ReactMethod
167
                 RNFetchBlobFS.cp(path, dest, callback);
165
                 RNFetchBlobFS.cp(path, dest, callback);
168
             }
166
             }
169
         });
167
         });
170
-
171
     }
168
     }
172
 
169
 
173
     @ReactMethod
170
     @ReactMethod
228
                 RNFetchBlobFS.writeFile(path, encoding, data, append, promise);
225
                 RNFetchBlobFS.writeFile(path, encoding, data, append, promise);
229
             }
226
             }
230
         });
227
         });
231
-
232
     }
228
     }
233
 
229
 
234
     @ReactMethod
230
     @ReactMethod
263
                 new RNFetchBlobFS(ctx).scanFile(p, m, callback);
259
                 new RNFetchBlobFS(ctx).scanFile(p, m, callback);
264
             }
260
             }
265
         });
261
         });
266
-
267
     }
262
     }
268
 
263
 
269
     @ReactMethod
264
     @ReactMethod
324
     @ReactMethod
319
     @ReactMethod
325
     public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
320
     public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
326
         new RNFetchBlobReq(options, taskId, method, url, headers, body, null, mClient, callback).run();
321
         new RNFetchBlobReq(options, taskId, method, url, headers, body, null, mClient, callback).run();
327
-}
322
+    }
328
 
323
 
329
     @ReactMethod
324
     @ReactMethod
330
     public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) {
325
     public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) {
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
         state = Environment.getExternalStorageState();
213
         state = Environment.getExternalStorageState();
214
         if (state.equals(Environment.MEDIA_MOUNTED)) {
214
         if (state.equals(Environment.MEDIA_MOUNTED)) {
215
             res.put("SDCardDir", Environment.getExternalStorageDirectory().getAbsolutePath());
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
         res.put("MainBundleDir", ctx.getApplicationInfo().dataDir);
222
         res.put("MainBundleDir", ctx.getApplicationInfo().dataDir);
219
         return res;
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
      * Static method that returns a temp file path
249
      * Static method that returns a temp file path
224
      * @param ctx   React Native application context
250
      * @param ctx   React Native application context

+ 11
- 2
fs.js View File

28
     MovieDir : RNFetchBlob.MovieDir,
28
     MovieDir : RNFetchBlob.MovieDir,
29
     DownloadDir : RNFetchBlob.DownloadDir,
29
     DownloadDir : RNFetchBlob.DownloadDir,
30
     DCIMDir : RNFetchBlob.DCIMDir,
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
     MainBundleDir : RNFetchBlob.MainBundleDir,
42
     MainBundleDir : RNFetchBlob.MainBundleDir,
34
     LibraryDir : RNFetchBlob.LibraryDir
43
     LibraryDir : RNFetchBlob.LibraryDir
35
 }
44
 }