morristech
3/29/2019 - 6:51 AM

Reflect DownloadManager progress of downloading multiple files in ProgressBar widget

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();
    }
    
    
}