DownloadUtils.java
7.02 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.huaheng.download;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.v4.content.FileProvider;
import com.huaheng.common.Constant;
import com.huaheng.common.WMSLog;
import com.huaheng.common.WMSUtils;
import com.huaheng.httplib.HttpConstant;
import com.huaheng.httplib.R;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
/**
* Created by youjie on 2018/8/24
*/
public class DownloadUtils {
private Context mContext;
private static DownloadUtils downloadUtils;
private String path = Environment.getExternalStorageDirectory().toString() + "/data/";
private ProgressDialog progressDialog;
ProgressHandlerThread handlerThread;
Handler mHandler;
public DownloadUtils(Context context) {
mContext = context;
handlerThread = new ProgressHandlerThread("ProgressHandlerThread");
handlerThread.start();
mHandler = new Handler(handlerThread.getLooper(), handlerThread);
}
public static DownloadUtils as(Context context) {
if(downloadUtils == null) {
downloadUtils = new DownloadUtils(context);
}
return downloadUtils;
}
public void startDownload() {
String pkgName = WMSUtils.getPackageName(mContext);
int versionCode = WMSUtils.getVersionCode(mContext);
final JSONObject object = new JSONObject();
try {
object.put("pkgName", pkgName);
object.put("versionCode", versionCode);
} catch (JSONException e) {
e.printStackTrace();
}
OKHttpUtils.commonPost(HttpConstant.GET_APKINFO_URL, object.toString(), new OKHttpInterface() {
@Override
public void onSuccess(String info) {
WMSLog.d("onSuccess :" + info);
try {
JSONObject object1 = new JSONObject(info);
int code = (int)object1.get("code");
final String msg = WMSUtils.getJsonData(object1, "msg");
String data = WMSUtils.getJsonData(object1, "data");
if(code != Constant.RET_OK) {
Looper.prepare();
WMSLog.d("onSuccess@@ :" + msg);
Looper.loop();
} else {
JSONObject jsonObject = new JSONObject(data);
String url = WMSUtils.getJsonData(jsonObject, "url");
String md5 = WMSUtils.getJsonData(jsonObject, "md5");
int version = Integer.parseInt(WMSUtils.getJsonData(jsonObject, "versionCode"));
download(url, md5, version);
}
} catch (JSONException e) {
e.printStackTrace();
WMSUtils.showShort(mContext, mContext.getString(R.string.download_fail));
}
}
@Override
public void onFail(String msg) {
WMSLog.d("onFail :" + msg);
Looper.prepare();
// WMSUtils.showShort(mContext.getString(R.string.network_exception));
Looper.loop();
}
});
}
private void download(String url, final String md5, int version) {
showProgress();
FileInfo fileInfo = new FileInfo();
fileInfo.setFileName(WMSUtils.getPackageName(mContext) + ".apk");
fileInfo.setFileType(FileInfo.FILE_TYPE_APK);
fileInfo.setPath(path);
fileInfo.setPkgName(WMSUtils.getPackageName(mContext));
fileInfo.setUrl(url);
fileInfo.setVersion(version);
DownloadManager.getInstance(mContext).startDownload(fileInfo);
DownloadManager.getInstance(mContext).setOnFileDownloadListener(new OnFileDownloadListener() {
@Override
public void onProgress(String fileName, int progress) {
WMSLog.d("onProgress progress:" + progress);
progressDialog.setProgress(progress);
}
@Override
public void onError(String fileName, String msg) {
WMSLog.d("onError msg:" + msg);
// WMSUtils.showShort(msg);
progressDialog.dismiss();
}
@Override
public void onSuccess(String fileName) {
WMSLog.d("onSuccess fileName:" + fileName);
File apkFile = new File(path + fileName);
String fileMD5 = WMSUtils.md5(apkFile);
// WMSLog.d("fileMD5:" + fileMD5 + " md5:" + md5);
if(!fileMD5.equals(md5)) {
WMSUtils.showShort(mContext, mContext.getString(R.string.md5_fail));
progressDialog.dismiss();
return;
}
WMSUtils.showShort(mContext, mContext.getString(R.string.download_success));
Intent intent = new Intent(Intent.ACTION_VIEW);
if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.N) {
Uri contentUri = FileProvider.getUriForFile(mContext,"com.lijinji.hucai.fileProvider", apkFile);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(contentUri,"application/vnd.android.package-archive");
}else{
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(apkFile),"application/vnd.android.package-archive");
}
mContext.startActivity(intent);
progressDialog.dismiss();
}
@Override
public void onCanceled(String fileName) {
progressDialog.dismiss();
}
});
}
private void showProgress() {
mHandler.sendEmptyMessage(0);
}
private void innerShowProgress() {
progressDialog = new ProgressDialog(mContext);//实例化ProgressDialog
progressDialog.setMax(100);//设置最大值
progressDialog.setTitle(mContext.getString(R.string.progress_download));//设置标题
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置样式为横向显示进度的样式
progressDialog.setIndeterminate(false);//是否精确显示对话框,flase为是,反之为否
//是否可以通过返回按钮退出对话框
progressDialog.setCancelable(false);
progressDialog.show();//显示对话框
}
private class ProgressHandlerThread extends HandlerThread implements Handler.Callback {
public ProgressHandlerThread(String name) {
super(name);
}
@Override
public boolean handleMessage(Message msg) {
innerShowProgress();
return true;
}
}
}