|
12 | 12 | import android.net.NetworkInfo;
|
13 | 13 | import android.net.Uri;
|
14 | 14 | import android.os.Build;
|
| 15 | +import android.os.Bundle; |
| 16 | +import android.os.Handler; |
| 17 | +import android.os.Message; |
15 | 18 | import android.util.Base64;
|
16 | 19 | import android.webkit.CookieManager;
|
17 | 20 |
|
|
48 | 51 |
|
49 | 52 | import java.util.List;
|
50 | 53 | import java.util.Locale;
|
| 54 | +import java.util.concurrent.Executors; |
| 55 | +import java.util.concurrent.Future; |
| 56 | +import java.util.concurrent.ScheduledExecutorService; |
51 | 57 | import java.util.concurrent.TimeUnit;
|
52 | 58 |
|
53 | 59 | import javax.net.ssl.SSLContext;
|
@@ -161,6 +167,59 @@ public static void cancelTask(String taskId) {
|
161 | 167 | }
|
162 | 168 | }
|
163 | 169 |
|
| 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 | + |
164 | 223 | @Override
|
165 | 224 | public void run() {
|
166 | 225 |
|
@@ -212,6 +271,17 @@ public void run() {
|
212 | 271 | downloadManagerId = dm.enqueue(req);
|
213 | 272 | androidDownloadManagerTaskTable.put(taskId, Long.valueOf(downloadManagerId));
|
214 | 273 | 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); |
215 | 285 | return;
|
216 | 286 | }
|
217 | 287 |
|
|
0 commit comments