Skip to content

Instantly share code, notes, and snippets.

@tolmachevroman
Last active December 14, 2023 17:39
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tolmachevroman/2c3358faad90aa802dfb to your computer and use it in GitHub Desktop.
Save tolmachevroman/2c3358faad90aa802dfb to your computer and use it in GitHub Desktop.
Reflect DownloadManager progress of downloading multiple files in ProgressBar widget
/**
* Fetches how many bytes have been downloaded so far and updates ProgressBar
*/
class DownloadProgressCounter extends Thread {
private final long downloadId;
private final DownloadManager.Query query;
private Cursor cursor;
private int lastBytesDownloadedSoFar;
private int totalBytes;
public DownloadProgressCounter(long downloadId) {
this.downloadId = downloadId;
this.query = new DownloadManager.Query();
query.setFilterById(this.downloadId);
}
@Override
public void run() {
while (downloadId > 0) {
try {
Thread.sleep(300);
cursor = manager.query(query);
if (cursor.moveToFirst()) {
//get total bytes of the file
if (totalBytes <= 0) {
totalBytes = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
}
final int bytesDownloadedSoFar = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
if (bytesDownloadedSoFar == totalBytes && totalBytes > 0) {
this.interrupt();
} else {
//update progress bar
runOnUiThread(new Runnable() {
@Override
public void run() {
mProgressBar.setProgress(mProgressBar.getProgress() + (bytesDownloadedSoFar - lastBytesDownloadedSoFar));
lastBytesDownloadedSoFar = bytesDownloadedSoFar;
}
});
}
}
cursor.close();
} catch (Exception e) {
return;
}
}
}
}
public class LessonDetailsActivity extends Activity {
private ProgressBar mProgressBar;
/**
* Some code here...
* */
//download and save file in SD
private void download(String url) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
if (Build.VERSION.SDK_INT >= 11) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
}
//expected url ends like /uploads/content/15/image/23_FAJE457F4A5NQBJ.MEDIUM.jpg
String[] paths = url.split("/");
String fileName = paths[paths.length - 1];
request.setDestinationInExternalPublicDir(Utils.getLessonFolderPath(mBundle.getString(Lesson._ID)), fileName);
registerReceiver(mDownloadReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
mIsReceiverRegistered = true;
// get download service and enqueue file
manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
new DownloadProgressCounter(manager.enqueue(request)).start();
}
}
@abiemann
Copy link

this is broken; manager is left undefined in DownloadProgressCounter

@lune856
Copy link

lune856 commented Jun 23, 2021

Can you double-check if this works for multiple downloads?

@Vivaan7
Copy link

Vivaan7 commented May 10, 2022

Can you double-check if this works for multiple downloads?

@lune856 have you completed multiple downloads from download manager ?

@KhadijaHameed
Copy link

great !!

@sadik-fattah
Copy link

@abiemann manager is variable of DownloadManager manager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);

@kesha-antonov
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment