Android Open Source - Rashr Downloader






From Project

Back to project page Rashr.

License

The source code is released under:

GNU General Public License

If you think the Android project Rashr listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.mkrtchyan.utils;
//  www  .j  a  va 2s . c o m
/**
 * Copyright (c) 2014 Ashot Mkrtchyan
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights 
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Downloader extends AsyncTask<Void, Integer, Boolean> {

    private static final String TAG = "Downloader";

    private Context mContext;
    private ProgressDialog downloadDialog;
    private boolean mFirstStart = true;
    private URL mURL;
    private File mOutputFile;
    private boolean mCheckSHA1 = false;
    private boolean mOverrideFile = false;
    private boolean mHide = false;
    private boolean mRetry = false;
    private boolean mCancelable = true;
    private File ChecksumFile = null;
    private Downloader thisDownloader = this;
    private boolean askBeforeDownload = false;
    private OnDownloadListener onDownloadListener = null;
    private boolean mErrorOccurred = false;

    private IOException ioException;
    private MalformedURLException urlException;

    public Downloader(Context context, URL url, File outputFile) {
        mContext = context;
        mURL = url;
        mOutputFile = outputFile;
    }

    public void ask() {
        final Downloader thisDownloader = this;
        if (askBeforeDownload) {
            showDownloadNowDialog();
        } else {
            thisDownloader.execute();
        }
    }

    protected void onPreExecute() {
        if (mOverrideFile)
            mErrorOccurred = mErrorOccurred || !mOutputFile.delete();
        if (!mOutputFile.getParentFile().exists()) {
            mErrorOccurred = mErrorOccurred || !mOutputFile.getParentFile().mkdir();
        }

        if (!mHide) {
            downloadDialog = new ProgressDialog(mContext);
            downloadDialog.setTitle(mContext.getResources().getString(R.string.connecting));
            downloadDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            downloadDialog.setCancelable(false);
            downloadDialog.setMessage(mURL.toString());
            if (mCancelable)
                downloadDialog.setButton(DialogInterface.BUTTON_NEGATIVE, mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        thisDownloader.cancel(false);
                        if (mOutputFile.exists())
                            mErrorOccurred = mErrorOccurred || !mOutputFile.delete();
                    }
                });
            downloadDialog.show();
        }
    }

    protected Boolean doInBackground(Void... params) {

        if (!mOutputFile.exists() || mOverrideFile) {
            ConnectivityManager connMgr = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

            if (networkInfo != null
                    && networkInfo.isConnected()) {
                try {

                    Log.i(TAG, "Connecting to " + mURL.getHost());
                    URLConnection connection = mURL.openConnection();

                    connection.setDoOutput(true);
                    connection.connect();

                    FileOutputStream fileOutput = new FileOutputStream(mOutputFile);

                    InputStream inputStream = connection.getInputStream();

                    byte[] buffer = new byte[1024];
                    int fullLength = connection.getContentLength();

                    int bufferLength;
                    int downloaded = 0;

                    Log.i(TAG, "Downloading " + mOutputFile.getName());

                    while ((bufferLength = inputStream.read(buffer)) > 0) {
                        fileOutput.write(buffer, 0, bufferLength);
                        downloaded += bufferLength;
                        if (!mHide)
                            publishProgress(downloaded, fullLength);
                    }

                    fileOutput.close();

                    Log.i(TAG, "Download finished!");

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    urlException = e;
                    Log.i(TAG, e.getMessage());
                    return false;
                } catch (IOException e) {
                    e.printStackTrace();
                    ioException = e;
                    Log.i(TAG, e.getMessage());
                    return false;
                }
            } else {
                return false;
            }
        }
        return !mCheckSHA1 || !isDownloadCorrupt();
    }

    @Override
    protected void onProgressUpdate(final Integer... progress) {
        super.onProgressUpdate(progress);
        if (mFirstStart) {
            downloadDialog.dismiss();
            downloadDialog = new ProgressDialog(mContext);
            downloadDialog.setTitle(R.string.Downloading);
            downloadDialog.setMessage(mOutputFile.getName());
            downloadDialog.setCancelable(false);
            if (mCancelable)
                downloadDialog.setButton(DialogInterface.BUTTON_NEGATIVE, mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        thisDownloader.cancel(false);
                        if (mOutputFile.exists())
                            mOutputFile.delete();
                    }
                });
            if (progress[1] >= 0) {
                downloadDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            } else {
                downloadDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            }
            downloadDialog.show();
            downloadDialog.setMax(progress[1]);
            downloadDialog.setCancelable(false);
            mFirstStart = false;
        }
        downloadDialog.setProgress(progress[0]);
    }

    protected void onPostExecute(Boolean success) {
        if (!mHide) {
            if (downloadDialog.isShowing()) {
                downloadDialog.dismiss();
            }
        }
        if (success) {
            if (onDownloadListener != null) {
                onDownloadListener.success(mOutputFile);
            }
        } else {
            if (!mHide) {
                if (onDownloadListener != null) {
                    if (ioException != null)
                        onDownloadListener.failed(ioException);
                    if (urlException != null)
                        onDownloadListener.failed(urlException);
                }

            }
            if (mOutputFile.delete() || mRetry) {
                loop();
            }
        }

    }

    public boolean isDownloadCorrupt() {
        try {
            return !SHA1.verifyChecksum(mOutputFile, ChecksumFile);
        } catch (IOException e) {
            Log.d(TAG, e.getMessage());
            e.printStackTrace();
        } catch (SHA1.SHA1SumNotFound sha1SumNotFound) {
            Log.d(TAG, sha1SumNotFound.getMessage());
            sha1SumNotFound.printStackTrace();
        }
        return true;
    }

    private void loop() {
        final Downloader newInstance = new Downloader(mContext, mURL, mOutputFile);
        newInstance.setOnDownloadListener(onDownloadListener);
        newInstance.setOverrideFile(mOverrideFile);
        newInstance.setCancelable(mCancelable);
        newInstance.setHidden(mHide);
        newInstance.setAskBeforeDownload(askBeforeDownload);
        newInstance.setRetry(mRetry);
        if (mCheckSHA1)
            newInstance.setChecksumFile(ChecksumFile);
        if (!mHide) {
            final AlertDialog.Builder tryAgain = new AlertDialog.Builder(mContext);
            tryAgain
                    .setMessage(String.format(mContext.getString(R.string.failed_download), mOutputFile.getName()))
                    .setPositiveButton(R.string.try_again, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            newInstance.execute();
                        }
                    })
                    .setNegativeButton(R.string.later, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                        }
                    })
                    .setTitle(R.string.warning)
                    .show();
        } else {
            newInstance.execute();
        }
    }

    public void setChecksumFile(File checksumFile) {
        ChecksumFile = checksumFile;
        mCheckSHA1 = ChecksumFile != null;
    }

    public void setOverrideFile(boolean overrideFile) {
        this.mOverrideFile = overrideFile;
    }

    public void setHidden(boolean hide) {
        this.mHide = hide;
    }

    public void setRetry(boolean retry) {
        this.mRetry = retry;
    }

    public void setCancelable(boolean cancelable) {
        mCancelable = cancelable;
    }

    public void setAskBeforeDownload(boolean askBeforeDownload) {
        this.askBeforeDownload = askBeforeDownload;
    }

    public void setOnDownloadListener(OnDownloadListener onDownloadListener) {
        this.onDownloadListener = onDownloadListener;
    }

    private void showDownloadNowDialog() {
        new AlertDialog.Builder(mContext)
                .setTitle(R.string.info)
                .setMessage(String.format(mContext.getString(R.string.download_now), mOutputFile.getName()))
                .setPositiveButton(R.string.positive, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        thisDownloader.execute();
                    }
                })
                .show();
    }

    public abstract interface OnDownloadListener {
        void success(File file);
        void failed(Exception e);
    }
}




Java Source Code List

com.fima.cardsui.StackAdapter.java
com.fima.cardsui.SwipeDismissTouchListener.java
com.fima.cardsui.Utils.java
com.fima.cardsui.objects.AbstractCard.java
com.fima.cardsui.objects.CardFactory.java
com.fima.cardsui.objects.CardModel.java
com.fima.cardsui.objects.CardStack.java
com.fima.cardsui.objects.Card.java
com.fima.cardsui.objects.RecyclableCard.java
com.fima.cardsui.views.CardUI.java
com.fima.cardsui.views.MyCard.java
com.fima.cardsui.views.MyImageCard.java
com.fima.cardsui.views.MyPlayCard.java
com.fima.cardsui.views.QuickReturnListView.java
de.mkrtchyan.recoverytools.BackupRestoreFragment.java
de.mkrtchyan.recoverytools.Constants.java
de.mkrtchyan.recoverytools.Device.java
de.mkrtchyan.recoverytools.FlashAsFragment.java
de.mkrtchyan.recoverytools.FlashFragment.java
de.mkrtchyan.recoverytools.FlashUtil.java
de.mkrtchyan.recoverytools.NavigationDrawerFragment.java
de.mkrtchyan.recoverytools.RashrActivity.java
de.mkrtchyan.recoverytools.ReportDialog.java
de.mkrtchyan.recoverytools.ScriptManagerFragment.java
de.mkrtchyan.recoverytools.SettingsFragment.java
de.mkrtchyan.utils.Common.java
de.mkrtchyan.utils.Downloader.java
de.mkrtchyan.utils.FileChooserDialog.java
de.mkrtchyan.utils.FileListView.java
de.mkrtchyan.utils.Notifyer.java
de.mkrtchyan.utils.SHA1.java
de.mkrtchyan.utils.Unzipper.java
donations.DonationsFragment.java
donations.google.util.Base64DecoderException.java
donations.google.util.Base64.java
donations.google.util.IabException.java
donations.google.util.IabHelper.java
donations.google.util.IabResult.java
donations.google.util.Inventory.java
donations.google.util.Purchase.java
donations.google.util.Security.java
donations.google.util.SkuDetails.java
org.sufficientlysecure.rootcommands.Mount.java
org.sufficientlysecure.rootcommands.Remounter.java
org.sufficientlysecure.rootcommands.RootCommands.java
org.sufficientlysecure.rootcommands.Shell.java
org.sufficientlysecure.rootcommands.SystemCommands.java
org.sufficientlysecure.rootcommands.Toolbox.java
org.sufficientlysecure.rootcommands.command.BinaryCommand.java
org.sufficientlysecure.rootcommands.command.Command.java
org.sufficientlysecure.rootcommands.command.SimpleBinaryCommand.java
org.sufficientlysecure.rootcommands.command.SimpleCommand.java
org.sufficientlysecure.rootcommands.util.BrokenBusyboxException.java
org.sufficientlysecure.rootcommands.util.FailedExecuteCommand.java
org.sufficientlysecure.rootcommands.util.Log.java
org.sufficientlysecure.rootcommands.util.RootAccessDeniedException.java
org.sufficientlysecure.rootcommands.util.UnsupportedArchitectureException.java
org.sufficientlysecure.rootcommands.util.Utils.java