Android Open Source - HelloWebView Downloader






From Project

Back to project page HelloWebView.

License

The source code is released under:

Apache License

If you think the Android project HelloWebView 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 com.mengdd.download;
//from  www .  ja va  2  s  . c  o m
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.mengdd.hellowebview.utils.LogUtil;

public class Downloader {

    private static final String LOG_TAG = "Downloader";
    private static final int CONNECT_TIMEOUT = 1000 * 10;
    private static final int READ_TIMEOUT = 1000 * 30;

    private URL mDownloadUrl = null;
    private File mFile = null;

    public Downloader(String url) {

        try {
            mDownloadUrl = new URL(url);
            mFile = DownloadManager.getDownloadFile(url);

        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        }

    }

    private boolean isDownloading = false;

    public boolean download() {
        if (isDownloading) {

            LogUtil.i(LOG_TAG, "download running already!");
            onAlreadyStarted();

            return false;

        }

        isDownloading = true;

        new DownloadThread().start();
        return true;

    }

    private class DownloadThread extends Thread {

        @Override
        public void run() {

            HttpURLConnection connection = null;
            RandomAccessFile threadfile = null;
            InputStream inputStream = null;
            try {
                onStarted();
                // ??Get?????
                connection = (HttpURLConnection) mDownloadUrl.openConnection();
                connection.setConnectTimeout(CONNECT_TIMEOUT);
                connection.setReadTimeout(READ_TIMEOUT);
                connection.setRequestMethod("GET");
                connection.setRequestProperty("Referer", mDownloadUrl.toString());
                connection.setRequestProperty("Charset", "UTF-8");
                connection.setRequestProperty("Connection", "Keep-Alive");

                // ????
                long downloadedLength = mFile.length() - 1;
                if (downloadedLength < 0) {
                    downloadedLength = 0;
                }
                connection.setRequestProperty("Range", "bytes=" + downloadedLength + "-");// ???????????????

                // ???????????,?????mFile.length(), ?????????416
                connection.connect();

                LogUtil.i(LOG_TAG, "Range" + "bytes=" + mFile.length() + "-");
                if (HttpURLConnection.HTTP_OK == connection.getResponseCode()
                        || HttpURLConnection.HTTP_PARTIAL == connection.getResponseCode()) {
                    LogUtil.i(LOG_TAG, "response code: " + connection.getResponseCode());
                    LogUtil.i(LOG_TAG, "response content length: " + connection.getContentLength());
                    inputStream = connection.getInputStream();
                    byte[] buffer = new byte[1024];
                    int byteCount = 0;

                    threadfile = new RandomAccessFile(mFile, "rwd");

                    LogUtil.i(LOG_TAG, "random file length: " + threadfile.length());
                    threadfile.seek(downloadedLength);
                    while ((byteCount = inputStream.read(buffer, 0, 1024)) != -1) {
                        threadfile.write(buffer, 0, byteCount);
                    }
                    onFinished();

                }
                else {
                    LogUtil.e(LOG_TAG, "response code: " + connection.getResponseCode());
                    onFailed();
                }
            }
            catch (Exception e) {
                LogUtil.e(LOG_TAG, "exception in download");
                e.printStackTrace();
                onFailed();

            }
            finally {

                isDownloading = false;
                try {
                    if (null != inputStream) {
                        inputStream.close();
                    }

                    if (null != threadfile) {
                        threadfile.close();
                    }

                    if (null != connection) {
                        connection.disconnect();
                    }
                }
                catch (IOException exception) {
                    exception.printStackTrace();
                }

            }

        }

    }

    // status methods
    private void onStarted() {

        LogUtil.i(LOG_TAG, " ================= on Started ================= ");
        if (null != mDownloadChangedListeners && null != mFile) {
            for (OnDownloadChangedListener listener : mDownloadChangedListeners) {
                listener.onDownloadStarted(mFile);
            }
        }

    }

    private void onFinished() {
        LogUtil.i(LOG_TAG, " ================= on Finished ================= ");
        if (null != mDownloadChangedListeners && null != mFile) {
            for (OnDownloadChangedListener listener : mDownloadChangedListeners) {
                listener.onDownloadFinished(mFile);
            }
        }

    }

    private void onAlreadyStarted() {
        LogUtil.i(LOG_TAG, " ================= on Already Started ================= ");
        if (null != mDownloadChangedListeners && null != mFile) {
            for (OnDownloadChangedListener listener : mDownloadChangedListeners) {
                listener.onAlreadyStarted(mFile);
            }
        }

    }

    private void onFailed() {
        LogUtil.i(LOG_TAG, " ================= on Failed ================= ");
        if (null != mDownloadChangedListeners && null != mFile) {
            for (OnDownloadChangedListener listener : mDownloadChangedListeners) {
                listener.onDownloadFailed(mFile);
            }
        }

    }

    private List<OnDownloadChangedListener> mDownloadChangedListeners = null;

    public void addDownloadChangedListener(OnDownloadChangedListener listener) {
        if (null == mDownloadChangedListeners) {
            mDownloadChangedListeners = new ArrayList<OnDownloadChangedListener>();
        }

        if (null != listener) {
            mDownloadChangedListeners.add(listener);
        }
    }

}




Java Source Code List

com.mengdd.download.DownloadManager.java
com.mengdd.download.Downloader.java
com.mengdd.download.OnDownloadChangedAdapter.java
com.mengdd.download.OnDownloadChangedListener.java
com.mengdd.hellowebview.Constants.java
com.mengdd.hellowebview.MainActivity.java
com.mengdd.hellowebview.utils.BrowserUtils.java
com.mengdd.hellowebview.utils.DeviceUtils.java
com.mengdd.hellowebview.utils.FileUtils.java
com.mengdd.hellowebview.utils.IOUtils.java
com.mengdd.hellowebview.utils.LogUtil.java
com.mengdd.hellowebview.utils.MediaFile.java
com.mengdd.hellowebview.utils.MediaUtil.java
com.mengdd.hellowebview.utils.PreferencesUtils.java
com.mengdd.hellowebview.utils.ReleaseConfig.java