Question

I am developing an app that downloads files and show 2 progress bars, the first one for the current downloading file, and the 2nd one for total progress based on the number of files.

I am using the DoubleProgressBar library in my app:

I succeeded to update the first ProgressBar, but stuck with the 2nd one.

Here is my code for the AsyncTask class:

private DoubleProgressDialog pDialog;

    class DownloadFileFromURL extends AsyncTask<String, Integer, String> {
        Context mContext;

        public DownloadFileFromURL(Context ctx) {
            // TODO Auto-generated constructor stub
            this.mContext = ctx;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(CUSTOM_PROGRESS_DIALOG);
        }

        /* Downloading file in background thread */
        @Override
        protected String doInBackground(String... f_url) {

            InputStream input = null;
            OutputStream output = null;
            HttpURLConnection connection = null;

            try {
                URL url = new URL(f_url[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();
                // getting file length
                int fileLength = connection.getContentLength();
                for (int i = 0; i <= ArrayOfFiles.length; i++){
                File f = new File(Environment.getExternalStorageDirectory() + "/Folder/", ArrayOfFiles[i]);

                // input stream to read file - with 8k buffer
                input = new BufferedInputStream(url.openStream(), 8192);
                // Output stream to write file
                output = new FileOutputStream(f); 

                byte data[] = new byte[8192];

                long total = 0;
                int count;
                int EntireProgress = 0;
                while ((count = input.read(data)) != -1) {
                    // allow canceling with back button
                    if (isCancelled()) {
                        input.close();
                        return null;
                    }
                    total += count;
                    // publishing the progress....
                    if (fileLength > 0) // only if total length is known
                    publishProgress((int)(total * 100 / fileLength));
                    output.write(data, 0, count);

                        /*Here is my trouble, the 2nd ProgressBar is updating as the same of the first one, I need the 2nd one to update itself slowly till all files get downloaded*/
                    int CurrentProgress = pDialog.getProgress();
                    pDialog.setSecondaryProgress(CurrentProgress );
                    publishProgress(CurrentProgress );

                }

            } catch (Exception e) {
                return e.toString();
            } finally {
                try {
                    if (output != null)
                        output.close();
                    if (input != null)
                        input.close();
            }
                } catch (IOException ignored) {
                }

                if (connection != null)
                    connection.disconnect();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // dismiss the dialog after the file was downloaded
            dismissDialog(CUSTOM_PROGRESS_DIALOG);

            if (result != null)
                Toast.makeText(mContext,"Download error: " + result, Toast.LENGTH_LONG).show();
            else
                Toast.makeText(mContext,"File downloaded", Toast.LENGTH_SHORT).show();  
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);
            // if we get here, length is known, now set indeterminate to false
            pDialog.setIndeterminate(false);
            pDialog.setMax(100);
            pDialog.setProgress(progress[0]);
        }

    }

I also used this method in my activity class:

    @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case CUSTOM_PROGRESS_DIALOG:
                pDialog = new DoubleProgressDialog(NetworkActivity.this);
                pDialog.setMessage("Downloading file. Please wait...");
                pDialog.setMax(100);
                pDialog.setIndeterminate(true);
                pDialog.setCancelable(false);
                pDialog.setButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.cancel();
                    }
                });
                pDialog.show();
                return pDialog;
            default:
                return null;
            }
        }

Any idea?

Was it helpful?

Solution

First part is to move pDialog.setSecondaryProgress to the onProgressUpdate(Integer... progress) method.
You are also resetting the secondary progress in each download task by setting it to CurrentProgress which is set to pDialog.getProgress();. Hence the second progress will always be reset after the download is finished.

Edit:

// publishing the progress....
if (fileLength > 0) // only if total length is known
    publishProgress((int)(total * 100 / fileLength), pDialog.getSecondaryProgress());

(...)

int CurrentProgress = pDialog.getProgress();

// do not update secondary progress here
// pDialog.setSecondaryProgress(CurrentProgress );
int secondaryProgress = (CurrentProgress + 100 * i)/ArrayOfFiles.length;
publishProgress(CurrentProgress, secondaryProgress);

And the onProgressUpdate

@Override
protected void onProgressUpdate(Integer... progress) {
   super.onProgressUpdate(progress);

   (...)       

   pDialog.setProgress(progress[0]);
   pDialog.setSecondaryProgress(progress[1]);

}

OTHER TIPS

If you are not setting your DownloadFileFromUrl outside your main class I would suggest something like this

int CurrentProgress = pDialog.getProgress();
int secondaryProgress = (CurrentProgress + 100 * id_treated_file)/number_of_files;
// id_treated_file - 0, 1, 2, ... , number_of_files - 1
pDialog.setSecondaryProgress(CurrentProgress);
// secondaryProgress will be progress[1] in your onProgressUpdate method
publishProgress(CurrentProgress, secondaryProgress);

your onProgressUpdate method should look like this :

@Override
protected void onProgressUpdate(Integer... progress) {
    super.onProgressUpdate(progress);
    // if we get here, length is known, now set indeterminate to false
    pDialog.setIndeterminate(false);
    pDialog.setMax(100);
    pDialog.setProgress(progress[0]);
    pDialog.setSecondaryProgress(progress[1]);
}

EDIT
or you can try

 pDialog.setSecondaryProgress((progress[0] + 100 * id_treated_file)/number_of_files);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top