7
7
import android .net .Uri ;
8
8
import android .os .Build ;
9
9
import android .support .v4 .content .FileProvider ;
10
+ import android .util .SparseArray ;
10
11
11
12
import com .facebook .react .bridge .ActivityEventListener ;
12
13
import com .facebook .react .bridge .Callback ;
38
39
39
40
public class RNFetchBlob extends ReactContextBaseJavaModule {
40
41
41
- // Cookies
42
- private final ForwardingCookieHandler mCookieHandler ;
43
- private final CookieJarContainer mCookieJarContainer ;
44
42
private final OkHttpClient mClient ;
45
43
46
44
static ReactApplicationContext RCTContext ;
47
- static LinkedBlockingQueue <Runnable > taskQueue = new LinkedBlockingQueue <>();
48
- static ThreadPoolExecutor threadPool = new ThreadPoolExecutor (5 , 10 , 5000 , TimeUnit .MILLISECONDS , taskQueue );
45
+ private static LinkedBlockingQueue <Runnable > taskQueue = new LinkedBlockingQueue <>();
46
+ private static ThreadPoolExecutor threadPool = new ThreadPoolExecutor (5 , 10 , 5000 , TimeUnit .MILLISECONDS , taskQueue );
49
47
static LinkedBlockingQueue <Runnable > fsTaskQueue = new LinkedBlockingQueue <>();
50
- static ThreadPoolExecutor fsThreadPool = new ThreadPoolExecutor (2 , 10 , 5000 , TimeUnit .MILLISECONDS , taskQueue );
51
- static public boolean ActionViewVisible = false ;
52
- static HashMap < Integer , Promise > promiseTable = new HashMap <>();
48
+ private static ThreadPoolExecutor fsThreadPool = new ThreadPoolExecutor (2 , 10 , 5000 , TimeUnit .MILLISECONDS , taskQueue );
49
+ private static boolean ActionViewVisible = false ;
50
+ private static SparseArray < Promise > promiseTable = new SparseArray <>();
53
51
54
52
public RNFetchBlob (ReactApplicationContext reactContext ) {
55
53
56
54
super (reactContext );
57
55
58
56
mClient = OkHttpClientProvider .getOkHttpClient ();
59
- mCookieHandler = new ForwardingCookieHandler (reactContext );
60
- mCookieJarContainer = (CookieJarContainer ) mClient .cookieJar ();
57
+ ForwardingCookieHandler mCookieHandler = new ForwardingCookieHandler (reactContext );
58
+ CookieJarContainer mCookieJarContainer = (CookieJarContainer ) mClient .cookieJar ();
61
59
mCookieJarContainer .setCookieJar (new JavaNetCookieJar (mCookieHandler ));
62
60
63
61
RCTContext = reactContext ;
@@ -89,14 +87,23 @@ public Map<String, Object> getConstants() {
89
87
}
90
88
91
89
@ ReactMethod
92
- public void createFile (final String path , final String content , final String encode , final Callback callback ) {
90
+ public void createFile (final String path , final String content , final String encode , final Promise promise ) {
93
91
threadPool .execute (new Runnable () {
94
92
@ Override
95
93
public void run () {
96
- RNFetchBlobFS .createFile (path , content , encode , callback );
94
+ RNFetchBlobFS .createFile (path , content , encode , promise );
97
95
}
98
96
});
97
+ }
99
98
99
+ @ ReactMethod
100
+ public void createFileASCII (final String path , final ReadableArray dataArray , final Promise promise ) {
101
+ threadPool .execute (new Runnable () {
102
+ @ Override
103
+ public void run () {
104
+ RNFetchBlobFS .createFileASCII (path , dataArray , promise );
105
+ }
106
+ });
100
107
}
101
108
102
109
@ ReactMethod
@@ -147,21 +154,10 @@ public void onHostDestroy() {
147
154
};
148
155
RCTContext .addLifecycleEventListener (listener );
149
156
} catch (Exception ex ) {
150
- promise .reject (ex .getLocalizedMessage ());
157
+ promise .reject ("EUNSPECIFIED" , ex .getLocalizedMessage ());
151
158
}
152
159
}
153
160
154
- @ ReactMethod
155
- public void createFileASCII (final String path , final ReadableArray dataArray , final Callback callback ) {
156
- threadPool .execute (new Runnable () {
157
- @ Override
158
- public void run () {
159
- RNFetchBlobFS .createFileASCII (path , dataArray , callback );
160
- }
161
- });
162
-
163
- }
164
-
165
161
@ ReactMethod
166
162
public void writeArrayChunk (final String streamId , final ReadableArray dataArray , final Callback callback ) {
167
163
RNFetchBlobFS .writeArrayChunk (streamId , dataArray , callback );
@@ -173,8 +169,8 @@ public void unlink(String path, Callback callback) {
173
169
}
174
170
175
171
@ ReactMethod
176
- public void mkdir (String path , Callback callback ) {
177
- RNFetchBlobFS .mkdir (path , callback );
172
+ public void mkdir (String path , Promise promise ) {
173
+ RNFetchBlobFS .mkdir (path , promise );
178
174
}
179
175
180
176
@ ReactMethod
@@ -190,7 +186,6 @@ public void run() {
190
186
RNFetchBlobFS .cp (path , dest , callback );
191
187
}
192
188
});
193
-
194
189
}
195
190
196
191
@ ReactMethod
@@ -199,8 +194,8 @@ public void mv(String path, String dest, Callback callback) {
199
194
}
200
195
201
196
@ ReactMethod
202
- public void ls (String path , Callback callback ) {
203
- RNFetchBlobFS .ls (path , callback );
197
+ public void ls (String path , Promise promise ) {
198
+ RNFetchBlobFS .ls (path , promise );
204
199
}
205
200
206
201
@ ReactMethod
@@ -251,7 +246,6 @@ public void run() {
251
246
RNFetchBlobFS .writeFile (path , encoding , data , append , promise );
252
247
}
253
248
});
254
-
255
249
}
256
250
257
251
@ ReactMethod
@@ -286,15 +280,24 @@ public void run() {
286
280
new RNFetchBlobFS (ctx ).scanFile (p , m , callback );
287
281
}
288
282
});
289
-
290
283
}
291
284
292
285
@ ReactMethod
286
+ public void hash (final String path , final String algorithm , final Promise promise ) {
287
+ threadPool .execute (new Runnable () {
288
+ @ Override
289
+ public void run () {
290
+ RNFetchBlobFS .hash (path , algorithm , promise );
291
+ }
292
+ });
293
+ }
294
+
293
295
/**
294
296
* @param path Stream file path
295
297
* @param encoding Stream encoding, should be one of `base64`, `ascii`, and `utf8`
296
298
* @param bufferSize Stream buffer size, default to 4096 or 4095(base64).
297
299
*/
300
+ @ ReactMethod
298
301
public void readStream (final String path , final String encoding , final int bufferSize , final int tick , final String streamId ) {
299
302
final ReactApplicationContext ctx = this .getReactApplicationContext ();
300
303
fsThreadPool .execute (new Runnable () {
@@ -368,10 +371,10 @@ public void getContentIntent(String mime, Promise promise) {
368
371
369
372
@ ReactMethod
370
373
public void addCompleteDownload (ReadableMap config , Promise promise ) {
371
- DownloadManager dm = (DownloadManager ) RNFetchBlob . RCTContext .getSystemService (RNFetchBlob . RCTContext .DOWNLOAD_SERVICE );
374
+ DownloadManager dm = (DownloadManager ) RCTContext .getSystemService (RCTContext .DOWNLOAD_SERVICE );
372
375
String path = RNFetchBlobFS .normalizePath (config .getString ("path" ));
373
376
if (path == null ) {
374
- promise .reject ("RNFetchblob.addCompleteDownload can not resolve URI:" + config .getString ("path" ), "RNFetchblob.addCompleteDownload can not resolve URI:" + path );
377
+ promise .reject ("EINVAL" , " RNFetchblob.addCompleteDownload can not resolve URI:" + config .getString ("path" ));
375
378
return ;
376
379
}
377
380
try {
@@ -388,9 +391,18 @@ public void addCompleteDownload (ReadableMap config, Promise promise) {
388
391
promise .resolve (null );
389
392
}
390
393
catch (Exception ex ) {
391
- promise .reject ("RNFetchblob.addCompleteDownload failed " , ex .getStackTrace (). toString ());
394
+ promise .reject ("EUNSPECIFIED " , ex .getLocalizedMessage ());
392
395
}
393
396
394
397
}
395
398
399
+ @ ReactMethod
400
+ public void getSDCardDir (Promise promise ) {
401
+ RNFetchBlobFS .getSDCardDir (promise );
402
+ }
403
+
404
+ @ ReactMethod
405
+ public void getSDCardApplicationDir (Promise promise ) {
406
+ RNFetchBlobFS .getSDCardApplicationDir (this .getReactApplicationContext (), promise );
407
+ }
396
408
}
0 commit comments