Skip to content

Commit a24712a

Browse files
authored
Merge pull request RonRadtke#143 from austinried/android-downloadmanager-progress
Enable reporting of progress from Android DownloadManager
2 parents 79eb7f1 + aa7a1a2 commit a24712a

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import android.net.NetworkInfo;
1313
import android.net.Uri;
1414
import android.os.Build;
15+
import android.os.Bundle;
16+
import android.os.Handler;
17+
import android.os.Message;
1518
import android.util.Base64;
1619
import android.webkit.CookieManager;
1720

@@ -48,6 +51,9 @@
4851

4952
import java.util.List;
5053
import java.util.Locale;
54+
import java.util.concurrent.Executors;
55+
import java.util.concurrent.Future;
56+
import java.util.concurrent.ScheduledExecutorService;
5157
import java.util.concurrent.TimeUnit;
5258

5359
import javax.net.ssl.SSLContext;
@@ -161,6 +167,59 @@ public static void cancelTask(String taskId) {
161167
}
162168
}
163169

170+
private final int QUERY = 1314;
171+
private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
172+
private Future<?> future;
173+
private Handler mHandler = new Handler(new Handler.Callback() {
174+
public boolean handleMessage(Message msg) {
175+
switch (msg.what) {
176+
177+
case QUERY:
178+
179+
Bundle data = msg.getData();
180+
long id = data.getLong("downloadManagerId");
181+
if (id == downloadManagerId) {
182+
183+
Context appCtx = ReactNativeBlobUtil.RCTContext.getApplicationContext();
184+
185+
DownloadManager downloadManager = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
186+
187+
DownloadManager.Query query = new DownloadManager.Query();
188+
189+
Cursor cursor = downloadManager.query(query);
190+
191+
if (cursor != null && cursor.moveToFirst()) {
192+
193+
long written = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
194+
195+
long total = cursor.getLong(cursor.getColumnIndex(
196+
DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
197+
cursor.close();
198+
199+
ReactNativeBlobUtilProgressConfig reportConfig = getReportProgress(taskId);
200+
float progress = (total != -1) ? written / total : 0;
201+
202+
if (reportConfig != null && reportConfig.shouldReport(progress /* progress */)) {
203+
WritableMap args = Arguments.createMap();
204+
args.putString("taskId", String.valueOf(taskId));
205+
args.putString("written", String.valueOf(written));
206+
args.putString("total", String.valueOf(total));
207+
args.putString("chunk", "");
208+
ReactNativeBlobUtil.RCTContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
209+
.emit(ReactNativeBlobUtilConst.EVENT_PROGRESS, args);
210+
211+
}
212+
213+
if (total == written) {
214+
future.cancel(true);
215+
}
216+
}
217+
}
218+
}
219+
return true;
220+
}
221+
});
222+
164223
@Override
165224
public void run() {
166225

@@ -212,6 +271,17 @@ public void run() {
212271
downloadManagerId = dm.enqueue(req);
213272
androidDownloadManagerTaskTable.put(taskId, Long.valueOf(downloadManagerId));
214273
appCtx.registerReceiver(this, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
274+
future = scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
275+
@Override
276+
public void run() {
277+
Message msg = mHandler.obtainMessage();
278+
Bundle data = new Bundle();
279+
data.putLong("downloadManagerId", downloadManagerId);
280+
msg.setData(data);
281+
msg.what = QUERY;
282+
mHandler.sendMessage(msg);
283+
}
284+
}, 0, 100, TimeUnit.MILLISECONDS);
215285
return;
216286
}
217287

0 commit comments

Comments
 (0)