RFFileDownloadModule.java
3.61 KB
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
115
116
117
118
119
120
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;
}
}