RFFileDownloadModule.java 3.61 KB
package com.sunvote.xpadcomm;

import android.os.Handler;
import android.os.Looper;

import com.sunvote.util.LogUtil;

import java.io.File;

/**
 * Created by Elvis on 2018/3/20 11:16
 * Email:Eluis@psunsky.com
 * 版权所有:长沙中天电子设计开发有限公司
 * Description: 人大通用版XPadAppRendaMac
 */
public class RFFileDownloadModule {

    private RFFileDownloadModule(){
        handler = new Handler(Looper.getMainLooper());
    }
    private Handler handler ;
    private long overtime = 15 * 1000 ;
    private static RFFileDownloadModule instance  = new RFFileDownloadModule();

    public static RFFileDownloadModule getInstance() {
        return instance;
    }

    private AbsDownloadProcess downloadProcess;
    private AbsDownloadProcess tdownloadProcess = new AbsDownloadProcess() {

        private boolean start = false;
        private double process = 0 ;
        private Runnable runnable = new Runnable() {
            @Override
            public void run() {
                onDownloadError(new Exception("overtime"));
            }
        };

        @Override
        public void onStartDownload(File file, long length) {
            start = true;
            process = 0;
            LogUtil.i("onStartDownload", file.getName() + "," + length);
            if(downloadProcess != null){
                downloadProcess.onStartDownload(file,length);
            }
            handler.removeCallbacks(runnable);
            handler.postDelayed(runnable,overtime);
        }

        @Override
        public void onDownloadProcess(double process) {
            this.process = process;
            LogUtil.i("onDownloadProcess", "process," + process);
            if(downloadProcess != null && start){
                downloadProcess.onDownloadProcess(process);
            }
            handler.removeCallbacks(runnable);
            handler.postDelayed(runnable,overtime);
        }

        @Override
        public void onDownloadProcess(String process) {
            LogUtil.i("onDownloadProcess", "process," + process);
            if(downloadProcess != null && start){
                downloadProcess.onDownloadProcess(process);
            }
            handler.removeCallbacks(runnable);
            handler.postDelayed(runnable,overtime);
        }

        @Override
        public void onStopDownload(File file) {
            LogUtil.i("onStopDownload", "process," + file.getName());
            if(downloadProcess != null && start){
                if(process >= 1.0){
                    downloadProcess.onStopDownload(file);
                }else{
                    downloadProcess.onDownloadError(new Exception("file download error"));
                }
            }
            start = false;
            handler.removeCallbacks(runnable);
            file.deleteOnExit();
        }

        @Override
        public void onDownloadError(Exception e) {
            LogUtil.i("onStopDownload", "error", e);
            if(downloadProcess != null && start){
                downloadProcess.onDownloadError(e);
            }
            start = false;
            handler.removeCallbacks(runnable);
        }
    };

    public void setOvertime(long overtime) {
        if(overtime > 0){
            this.overtime = overtime;
        }
    }

    public synchronized void registerDownloadProcess(AbsDownloadProcess downloadProcess){
        this.downloadProcess = downloadProcess ;
     }

    public synchronized void unRegisterDownloadProcess(AbsDownloadProcess downloadProcess){
        this.downloadProcess = null;
    }

    protected synchronized AbsDownloadProcess getDownloadProcess() {

        return tdownloadProcess;
    }


}