Blame view

C5/app/src/main/java/com/sunvote/xpadcomm/AbsDownloadProcess.java 2.73 KB
c46db941   孙向锦   添加新文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  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);
  
  }