AbsDownloadProcess.java 2.73 KB
package com.sunvote.xpadcomm;

import java.io.File;
import java.io.IOException;

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

    private static String foldPath = "/sdcard/Sunvote/download/";

    protected static String fileName = "default.obj";

    private long length ;
    private long curComplete = 0 ;

    private static File file ;

    public File getFile() {
        if(file == null){
            curComplete = 0 ;
            File folder = new File(foldPath);
            if(!folder.exists()){
                folder.mkdirs();
            }
            if(fileName == null || "".equals(fileName.trim())){
                fileName = "default.obj" ;
            }
            file = new File(foldPath , fileName);
            file.deleteOnExit();
            if(!file.exists()){
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    onDownloadError(e);
                }
            }
        }
        return file;
    }

    protected long getLength() {
        return length;
    }

    public String getFoldPath() {
        return foldPath;
    }

    protected void setLength(long length) {
        this.length = length;
        curComplete = 0 ;
    }

    public void setFileName(String fileName) {
        if(!(fileName == null || "".equals(fileName.trim()))) {
            this.fileName = fileName;
            file = null;
        }
    }

    protected String getFileName() {
        return fileName;
    }

    public void setFoldPath(String foldPath) {
        this.foldPath = foldPath;
    }

    protected  void onStartDownload(){
        onStartDownload(getFile(),getLength());
    }

    public abstract void onStartDownload(File file,long length);

    protected void onDownloadAddProcess(int curPack) {
        curComplete += curPack;
        double result = 0;
        if(curComplete <= length){
            result = (double)curComplete / length ;
        }else{
            result = 1;
        }

        try{
            onDownloadProcess(result);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        try{
            int ri = (int)(result * 100);
            onDownloadProcess(ri + "%");
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    public abstract void onDownloadProcess(double process);

    public abstract void onDownloadProcess(String process);

    protected void onStopDownload(){
        onStopDownload(getFile());
    }

    public  abstract void onStopDownload(File file);

    public abstract void onDownloadError(Exception e);

}