UpdateUtils.java
3.15 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
package com.polysoft.nafmii.utils;
import android.text.TextUtils;
import androidx.fragment.app.FragmentManager;
import com.polysoft.nafmii.bean.UpdateVersion;
import com.polysoft.nafmii.constants.ServerApi;
import com.polysoft.nafmii.enums.StateEnum;
import com.polysoft.nafmii.fragment.dialog.UpdateDialog;
import com.polysoft.nafmii.net.OkhttpApi;
import com.polysoft.nafmii.net.RequestBack;
import com.whaty.nafmii.BuildConfig;
import java.io.File;
public class UpdateUtils {
public static void checkVersion(FragmentManager manager, String h5Version) {
ServerApi.postAppVersion(new RequestBack<UpdateVersion>() {
@Override
protected void onResponse(StateEnum state, UpdateVersion data, String message) {
if (state != StateEnum.OK || null == data) {
if (!TextUtils.isEmpty(h5Version)) {
checkH5Version(h5Version);
}
return;
}
ExecutorUtils.main(() -> UpdateDialog.show(manager, data));
}
});
}
public static String checkVersion(FragmentManager manager, UpdateVersion update) {
if (null != update) {
String upgradeType = update.getUpgradeType();
if ("1".equals(upgradeType)) { // h5
downH5Version(update);
return "";
} else if ("2".equals(upgradeType)) {
ExecutorUtils.main(() -> UpdateDialog.show(manager, update));
return "";
}
}
return "参数错误";
}
public static void checkH5Version(String h5Version) {
if (BuildConfig.ONLINE) { // 线上版本不需要进行热更新
return;
}
ServerApi.postH5Version(h5Version, new RequestBack<UpdateVersion>() {
@Override
protected void onResponse(StateEnum state, UpdateVersion data, String message) {
if (state != StateEnum.OK || null == data) {
return;
}
downH5Version(data);
}
});
}
static void downH5Version(UpdateVersion update) {
String url = update.getLocation();
if (TextUtils.isEmpty(url)) {
return;
}
if (!url.startsWith("http")) {
url = BuildConfig.IMG_API + url;
}
File file = WebVersionUtils.getDistZip(update.getvNumber());
OkhttpApi.doDownload(url, file, new OkhttpApi.IDownListener() {
@Override
public void onProgress(int progress) {
if (progress == 100) {
try {
File oldDistDir = WebVersionUtils.getDistDir();
ZipUtils.unZip(file, file.getParentFile());
WebVersionUtils.setVersion(update.getvNumber());
FileUtils.deletes(oldDistDir.getParentFile());
} catch (Exception e) {
e.printStackTrace();
}
return;
}
}
@Override
public void onFail() {
}
});
}
}