Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit cdee490

Browse files
committed
#22 android implementation complete
1 parent 1471ca1 commit cdee490

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ public class RNFetchBlobFS {
5252
this.emitter = ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
5353
}
5454

55+
static String getExternalFilePath(ReactApplicationContext ctx, String taskId, RNFetchBlobConfig config) {
56+
if(config.path != null)
57+
return config.path;
58+
else if(config.fileCache && config.appendExt != null)
59+
return RNFetchBlobFS.getTmpPath(ctx, taskId) + "." + config.appendExt;
60+
else
61+
return RNFetchBlobFS.getTmpPath(ctx, taskId);
62+
}
63+
5564
/**
5665
* Write string with encoding to file
5766
* @param path Destination file path.
@@ -128,6 +137,7 @@ static public void readFile(String path, String encoding, final Promise promise
128137
AsyncTask<String, Integer, Integer> task = new AsyncTask<String, Integer, Integer>() {
129138
@Override
130139
protected Integer doInBackground(String... strings) {
140+
131141
try {
132142
String path = strings[0];
133143
String encoding = strings[1];

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.RNFetchBlob;
22

33
import android.app.DownloadManager;
4+
import android.content.BroadcastReceiver;
45
import android.content.Context;
6+
import android.content.Intent;
7+
import android.content.IntentFilter;
8+
import android.database.Cursor;
59
import android.net.Uri;
610

711
import com.facebook.react.bridge.Callback;
@@ -15,11 +19,9 @@
1519
import com.loopj.android.http.MySSLSocketFactory;
1620

1721
import java.io.File;
18-
import java.nio.charset.Charset;
1922
import java.security.KeyStore;
2023

2124
import cz.msebera.android.httpclient.HttpEntity;
22-
import cz.msebera.android.httpclient.entity.AbstractHttpEntity;
2325
import cz.msebera.android.httpclient.entity.ByteArrayEntity;
2426
import cz.msebera.android.httpclient.entity.ContentType;
2527
import cz.msebera.android.httpclient.entity.FileEntity;
@@ -28,7 +30,7 @@
2830
/**
2931
* Created by wkh237 on 2016/6/21.
3032
*/
31-
public class RNFetchBlobReq implements Runnable{
33+
public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
3234

3335
final String filePathPrefix = "RNFetchBlob-file://";
3436
ReactApplicationContext ctx;
@@ -40,6 +42,7 @@ public class RNFetchBlobReq implements Runnable{
4042
ReadableMap headers;
4143
Callback callback;
4244
HttpEntity entity;
45+
long downloadManagerId;
4346
AsyncHttpClient req;
4447
String type;
4548

@@ -82,10 +85,13 @@ public void run() {
8285
if(options.addAndroidDownloads.getBoolean("useDownloadManager")) {
8386
Uri uri = Uri.parse(url);
8487
DownloadManager.Request req = new DownloadManager.Request(uri);
85-
if(options.path != null) {
86-
Uri dest = null;
87-
dest = Uri.parse(options.path);
88-
req.setDestinationUri(dest);
88+
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
89+
90+
if(options.addAndroidDownloads.hasKey("title")) {
91+
req.setTitle(options.addAndroidDownloads.getString("title"));
92+
}
93+
if(options.addAndroidDownloads.hasKey("description")) {
94+
req.setDescription(options.addAndroidDownloads.getString("description"));
8995
}
9096
// set headers
9197
ReadableMapKeySetIterator it = headers.keySetIterator();
@@ -94,7 +100,8 @@ public void run() {
94100
req.addRequestHeader(key, headers.getString(key));
95101
}
96102
DownloadManager dm = (DownloadManager) ctx.getSystemService(Context.DOWNLOAD_SERVICE);
97-
dm.enqueue(req);
103+
downloadManagerId = dm.enqueue(req);
104+
ctx.registerReceiver(this, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
98105
return;
99106
}
100107

@@ -230,4 +237,28 @@ void buildEntity(String body) {
230237
}
231238

232239
}
240+
241+
@Override
242+
public void onReceive(Context context, Intent intent) {
243+
String action = intent.getAction();
244+
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
245+
long id = intent.getExtras().getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
246+
if(id == this.downloadManagerId) {
247+
DownloadManager.Query query = new DownloadManager.Query();
248+
query.setFilterById(downloadManagerId);
249+
DownloadManager dm = (DownloadManager) ctx.getSystemService(Context.DOWNLOAD_SERVICE);
250+
dm.query(query);
251+
Cursor c = dm.query(query);
252+
if (c.moveToFirst()) {
253+
String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
254+
Uri uri = Uri.parse(contentUri);
255+
Cursor cursor = ctx.getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
256+
cursor.moveToFirst();
257+
String filePath = cursor.getString(0);
258+
cursor.close();
259+
this.callback.invoke(null, filePath);
260+
}
261+
}
262+
}
263+
}
233264
}

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const {
2828
mkdir,
2929
session,
3030
writeStream,
31+
readFile,
3132
ls,
3233
isDir,
3334
mv,
@@ -124,7 +125,7 @@ function fetch(...args:any):Promise {
124125
else {
125126
let respType = 'base64'
126127
// response data is saved to storage
127-
if(options.path || options.fileCache) {
128+
if(options.path || options.fileCache || options.addAndroidDownloads) {
128129
respType = 'path'
129130
if(options.session)
130131
session(options.session).add(data)
@@ -247,7 +248,7 @@ class FetchBlobResponse {
247248
this.readFile = (encode: 'base64' | 'utf8' | 'ascii') => {
248249
if(this.type === 'path') {
249250
encode = encode || 'utf8'
250-
return RNFetchBlob.fs.readFile(this.data, encode)
251+
return readFile(this.data, encode)
251252
}
252253
else {
253254
console.warn('RNFetchblob', 'this response does not contains a readable file')

src/scripts/prelink.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ fs.readFile(MANIFEST_PATH, function(err, data) {
66
if(err)
77
console.log('failed to locate AndroidManifest.xml file, you may have to add file access permission manually.');
88
else {
9-
9+
// append fs permission
1010
data = String(data).replace(
1111
'<uses-permission android:name="android.permission.INTERNET" />',
1212
'<uses-permission android:name="android.permission.INTERNET" />\n <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> '
1313
)
14+
// append DOWNLOAD_COMPLETE intent permission
15+
data = String(data).replace(
16+
'<category android:name="android.intent.category.LAUNCHER" />',
17+
'<category android:name="android.intent.category.LAUNCHER" />\n <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>'
18+
)
1419
fs.writeFileSync(MANIFEST_PATH, data);
1520

1621
}

0 commit comments

Comments
 (0)