Android Open Source - HelloWebView Download Manager






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  w w w .  jav a2 s .  c om*/
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.mengdd.hellowebview.utils.FileUtils;
import com.mengdd.hellowebview.utils.LogUtil;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

public class DownloadManager {
    private static final String LOG_TAG = "DownloadManager";

    private static final String DOWNLOAD_DIRECTORY = "/Download/HelloWebView";

    private Map<String, Downloader> mDownloadMap = new HashMap<String, Downloader>();

    // Singleton
    private DownloadManager() {
    }

    // Thread may keep a private copy of object for better performance.
    // Key word volatile stops this kind of action.
    // Threads have to read this object from shared memory every time when they
    // use it.
    private static volatile DownloadManager mInstance = null;

    public static DownloadManager getInstance() {
        if (null == mInstance) {
            synchronized (DownloadManager.class) {
                if (null == mInstance) {// double check here
                    mInstance = new DownloadManager();
                }
            }
        }
        return mInstance;
    }

    public boolean addDownloadTask(final Context context, final String downloadUrl,
            final OnDownloadChangedListener onDownloadChangedListener) {

        if (TextUtils.isEmpty(downloadUrl)) {
            LogUtil.i(LOG_TAG, "download url in web is empty!");
            return false;
        }

        Downloader downloader = mDownloadMap.get(downloadUrl);
        if (null == downloader) {
            downloader = new Downloader(downloadUrl);
            mDownloadMap.put(downloadUrl, downloader);
        }
        downloader.addDownloadChangedListener(onDownloadChangedListener);
        downloader.download();

        return true;
    }

    public static File getDownloadFile(String downloadURL) {
        String filename = FileUtils.getFileName(downloadURL);

        if (TextUtils.isEmpty(filename)) {
            return null;
        }

        File dir = getDownloadDirectory();
        // ?????????????,?????????
        FileUtils.createDirForcely(dir.toString());

        // ?????????????????????????
        File file = new File(dir, filename);

        return file;

    }

    /**
     * ??????
     *
     * @return
     */
    private static File getDownloadDirectory() {

        // Saving to external storage (SD card).
        String root = Environment.getExternalStorageDirectory().getPath();
        // if (!new File(root).exists()) {
        // root = Environment.getLegacyExternalStorageDirectory().getPath();
        // }

        File file = new File(root + DOWNLOAD_DIRECTORY);
        // ?????????????????????????native?

        return file;
    }
}




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