package com.chiclam.android.updater;

import android.app.DownloadManager;
import android.net.Uri;
import android.widget.Toast;


/**
 * Description：
 * <br/>
 * Created by chiclaim on 2017/5/16.
 */

public class Updater {

    private static Updater instance;

    private Updater() {
        //
    }

    public synchronized static Updater get() {
        if (instance == null) {
            instance = new Updater();
        }
        return instance;
    }

    public Updater showLog(boolean log) {
        Logger.get().setShowLog(log);
        return this;
    }

    public void download(UpdaterConfig updaterConfig) {

        if (!UpdaterUtils.checkDownloadState(updaterConfig.getContext())) {
            Toast.makeText(updaterConfig.getContext(), R.string.system_download_component_disable, Toast.LENGTH_SHORT).show();
            UpdaterUtils.showDownloadSetting(updaterConfig.getContext());
            return;
        }

        long downloadId = UpdaterUtils.getLocalDownloadId(updaterConfig.getContext());
        Logger.get().d("local download id is " + downloadId);
        if (downloadId != -1L) {
            FileDownloadManager fdm = FileDownloadManager.get();
            //获取下载状态
            int status = fdm.getDownloadStatus(updaterConfig.getContext(), downloadId);
            switch (status) {
                //下载成功
                case DownloadManager.STATUS_SUCCESSFUL:
                    Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_SUCCESSFUL");
                    Uri uri = fdm.getDownloadUri(updaterConfig.getContext(), downloadId);
                    if (uri != null) {
                        //本地的版本大于当前程序的版本直接安装
                        if (UpdaterUtils.compare(updaterConfig.getContext(), uri.getPath())) {
                            Logger.get().d("start install UI");
                            UpdaterUtils.startInstall(updaterConfig.getContext(), uri);
                            return;
                        } else {
                            //从FileDownloadManager中移除这个任务
                            fdm.getDM(updaterConfig.getContext()).remove(downloadId);
                        }
                    }
                    //重新下载
                    startDownload(updaterConfig);
                    break;
                //下载失败
                case DownloadManager.STATUS_FAILED:
                    Logger.get().d("download failed " + downloadId);
                    startDownload(updaterConfig);
                    break;
                case DownloadManager.STATUS_RUNNING:
                    Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_RUNNING");
                    break;
                case DownloadManager.STATUS_PENDING:
                    Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_PENDING");
                    break;
                case DownloadManager.STATUS_PAUSED:
                    Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_PAUSED");
                    break;
                default:
                    Logger.get().d("downloadId=" + downloadId + " ,status = " + status);
                    break;
            }
        } else {
            startDownload(updaterConfig);
        }
    }

    private void startDownload(UpdaterConfig updaterConfig) {
        long id = FileDownloadManager.get().startDownload(updaterConfig);
        Logger.get().d("apk download start, downloadId is " + id);
    }

}

@@ -213,24 +213,146 @@
  Updater %7B%0A%0A
+    /**%0A     * FileDownloadManager.getDownloadStatus%E5%A6%82%E6%9E%9C%E6%B2%A1%E6%89%BE%E5%88%B0%E4%BC%9A%E8%BF%94%E5%9B%9E-1%0A     */%0A    private static final int STATUS_UN_FIND = -1;%0A%0A
     private 
@@ -3132,32 +3132,240 @@
          break;%0A
+                case STATUS_UN_FIND:%0A                    Logger.get().d(%22downloadId=%22 + downloadId + %22 ,status = STATUS_UN_FIND%22);%0A                    startDownload(updaterConfig);%0A                    break;%0A
                 

解决如果正在下载，如果用户点击Notification进入下载界面，把正在下载的任务删除，然后点击“Download”按钮无法下载的bug