Skip to content

Commit 3e4f054

Browse files
committed
新增md5校验
通知栏进度可以取消
1 parent 68c5586 commit 3e4f054

8 files changed

Lines changed: 131 additions & 30 deletions

File tree

apk/app-debug.apk

184 KB
Binary file not shown.

app/src/main/java/com/vector/appupdatedemo/MainActivity.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public void updateApp4(View view) {
225225
//添加自定义参数
226226
.setParams(params)
227227
//设置头部
228-
.setTopPic(R.mipmap.top_4)
228+
.setTopPic(R.mipmap.top_8)
229229
//设置主题色
230230
// .setThemeColor(0xff034ea0)
231231
.build()
@@ -246,13 +246,13 @@ protected UpdateAppBean parseJson(String json) {
246246
//是否更新Yes,No
247247
.setUpdate(jsonObject.getString("update"))
248248
//新版本号
249-
.setNew_version(jsonObject.getString("new_version"))
249+
.setNewVersion(jsonObject.getString("new_version"))
250250
//下载地址
251-
.setApk_file_url(jsonObject.getString("apk_file_url"))
251+
.setApkFileUrl(jsonObject.getString("apk_file_url"))
252252
//大小
253-
.setTarget_size(jsonObject.getString("target_size"))
253+
.setTargetSize(jsonObject.getString("target_size"))
254254
//更新内容
255-
.setUpdate_log(jsonObject.getString("update_log"))
255+
.setUpdateLog(jsonObject.getString("update_log"))
256256
//是否强制更新
257257
.setConstraint(true);
258258
} catch (JSONException e) {
@@ -269,6 +269,11 @@ protected UpdateAppBean parseJson(String json) {
269269
*/
270270
@Override
271271
public void hasNewApp(UpdateAppBean updateApp, UpdateAppManager updateAppManager) {
272+
if (updateApp.isConstraint()) {
273+
274+
} else {
275+
276+
}
272277
updateAppManager.showDialog();
273278
}
274279

@@ -298,4 +303,10 @@ public void noNewApp() {
298303
});
299304

300305
}
306+
307+
@Override
308+
protected void onStart() {
309+
super.onStart();
310+
311+
}
301312
}

json/json.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"apk_file_url": "https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/apk/app-debug.apk",
55
"update_log": "1,添加删除信用卡接口\r\n2,添加vip认证\r\n3,区分自定义消费,一个小时不限制。\r\n4,添加放弃任务接口,小时内不生成。\r\n5,消费任务手动生成。",
66
"target_size": "5M",
7+
"new_md5":"A818AD325EACC199BC62C552A32C35F2",
78
"constraint": false
89
}

update-app/src/main/java/com/vector/update_app/DialogActivity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ private void initData() {
8989

9090
if (mUpdateApp != null) {
9191
//弹出对话框
92-
String newVersion = mUpdateApp.getNew_version();
93-
String targetSize = mUpdateApp.getTarget_size();
94-
String updateLog = mUpdateApp.getUpdate_log();
92+
String newVersion = mUpdateApp.getNewVersion();
93+
String targetSize = mUpdateApp.getTargetSize();
94+
String updateLog = mUpdateApp.getUpdateLog();
9595

9696
String msg = "";
9797

update-app/src/main/java/com/vector/update_app/UpdateAppBean.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,29 +81,30 @@ public UpdateAppBean setUpdate(String update) {
8181
return this;
8282
}
8383

84-
public String getNew_version() {
84+
public String getNewVersion() {
8585
return new_version;
8686
}
8787

88-
public UpdateAppBean setNew_version(String new_version) {
88+
public UpdateAppBean setNewVersion(String new_version) {
8989
this.new_version = new_version;
9090
return this;
9191
}
9292

93-
public String getApk_file_url() {
93+
public String getApkFileUrl() {
9494
return apk_file_url;
9595
}
9696

97-
public UpdateAppBean setApk_file_url(String apk_file_url) {
97+
98+
public UpdateAppBean setApkFileUrl(String apk_file_url) {
9899
this.apk_file_url = apk_file_url;
99100
return this;
100101
}
101102

102-
public String getUpdate_log() {
103+
public String getUpdateLog() {
103104
return update_log;
104105
}
105106

106-
public UpdateAppBean setUpdate_log(String update_log) {
107+
public UpdateAppBean setUpdateLog(String update_log) {
107108
this.update_log = update_log;
108109
return this;
109110
}
@@ -116,19 +117,19 @@ public void setDelta(boolean delta) {
116117
this.delta = delta;
117118
}
118119

119-
public String getNew_md5() {
120+
public String getNewMd5() {
120121
return new_md5;
121122
}
122123

123-
public void setNew_md5(String new_md5) {
124+
public void setNewMd5(String new_md5) {
124125
this.new_md5 = new_md5;
125126
}
126127

127-
public String getTarget_size() {
128+
public String getTargetSize() {
128129
return target_size;
129130
}
130131

131-
public UpdateAppBean setTarget_size(String target_size) {
132+
public UpdateAppBean setTargetSize(String target_size) {
132133
this.target_size = target_size;
133134
return this;
134135
}

update-app/src/main/java/com/vector/update_app/UpdateCallback.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ protected UpdateAppBean parseJson(String json) {
1919
try {
2020
JSONObject jsonObject = new JSONObject(json);
2121
updateAppBean.setUpdate(jsonObject.getString("update"))
22-
.setNew_version(jsonObject.getString("new_version"))
23-
.setApk_file_url(jsonObject.getString("apk_file_url"))
24-
.setTarget_size(jsonObject.getString("target_size"))
25-
.setUpdate_log(jsonObject.getString("update_log"))
22+
.setNewVersion(jsonObject.getString("new_version"))
23+
.setApkFileUrl(jsonObject.getString("apk_file_url"))
24+
.setTargetSize(jsonObject.getString("target_size"))
25+
.setUpdateLog(jsonObject.getString("update_log"))
2626
.setConstraint(jsonObject.getBoolean("constraint"));
2727
} catch (JSONException e) {
2828
e.printStackTrace();

update-app/src/main/java/com/vector/update_app/service/DownloadService.java

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.vector.update_app.HttpManager;
2020
import com.vector.update_app.R;
2121
import com.vector.update_app.UpdateAppBean;
22+
import com.vector.update_app.utils.Md5Util;
2223
import com.vector.update_app.utils.Utils;
2324

2425
import java.io.File;
@@ -80,6 +81,7 @@ private void setUpNotification() {
8081
.setSmallIcon(R.mipmap.lib_update_app_update_icon)
8182
.setLargeIcon(Utils.drawableToBitmap(Utils.getAppIcon(DownloadService.this)))
8283
.setOngoing(true)
84+
.setAutoCancel(true)
8385
.setWhen(System.currentTimeMillis());
8486
mNotificationManager.notify(NOTIFY_ID, mBuilder.build());
8587
}
@@ -88,7 +90,7 @@ private void setUpNotification() {
8890
* 下载模块
8991
*/
9092
private void startDownload(UpdateAppBean updateApp, final DownloadCallback callback) {
91-
String apkUrl = updateApp.getApk_file_url();
93+
String apkUrl = updateApp.getApkFileUrl();
9294
if (TextUtils.isEmpty(apkUrl)) {
9395
String contentText = "新版本下载路径错误";
9496
stop(contentText);
@@ -106,10 +108,33 @@ private void startDownload(UpdateAppBean updateApp, final DownloadCallback callb
106108
appDir.mkdirs();
107109
}
108110

109-
String target = appDir + File.separator + updateApp.getNew_version();
111+
String target = appDir + File.separator + updateApp.getNewVersion();
110112

111-
// //可以利用md5验证是否重复下载
112-
updateApp.getHttpManager().download(apkUrl, target, appName, new FileDownloadCallBack(callback));
113+
File appFile = new File(target.concat(File.separator + appName));
114+
//有md5值
115+
//已下载
116+
//并且md5正确
117+
if (!TextUtils.isEmpty(updateApp.getNewMd5())
118+
&& appFile.exists()
119+
&& Md5Util.getFileMD5(appFile).equals(updateApp.getNewMd5().toLowerCase())) {
120+
121+
Uri fileUri = FileProvider.getUriForFile(DownloadService.this, getApplicationContext().getPackageName() + ".fileProvider", appFile);
122+
Intent intent = new Intent(Intent.ACTION_VIEW);
123+
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
124+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
125+
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
126+
intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
127+
} else {
128+
intent.setDataAndType(Uri.fromFile(appFile), "application/vnd.android.package-archive");
129+
}
130+
if (getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
131+
startActivity(intent);
132+
}
133+
//安装完自杀
134+
close();
135+
} else {
136+
updateApp.getHttpManager().download(apkUrl, target, appName, new FileDownloadCallBack(callback));
137+
}
113138

114139
}
115140

@@ -149,8 +174,9 @@ public interface DownloadCallback {
149174
public class DownloadBinder extends Binder {
150175
/**
151176
* 开始下载
177+
*
152178
* @param updateApp 新app信息
153-
* @param callback 下载回调
179+
* @param callback 下载回调
154180
*/
155181
public void start(UpdateAppBean updateApp, DownloadCallback callback) {
156182
//初始化通知栏
@@ -186,7 +212,9 @@ public void onProgress(float progress, long total) {
186212
.setContentText(rate + "%")
187213
.setProgress(100, rate, false)
188214
.setWhen(System.currentTimeMillis());
189-
mNotificationManager.notify(NOTIFY_ID, mBuilder.build());
215+
Notification notification = mBuilder.build();
216+
notification.flags = Notification.FLAG_AUTO_CANCEL;
217+
mNotificationManager.notify(NOTIFY_ID, notification);
190218
}
191219

192220
@Override
@@ -240,9 +268,11 @@ public void onResponse(File file) {
240268
.setContentTitle(Utils.getAppName(DownloadService.this))
241269
.setContentText("下载完成,请点击安装")
242270
.setProgress(0, 0, false)
243-
.setAutoCancel(true)
271+
// .setAutoCancel(true)
244272
.setDefaults((Notification.DEFAULT_ALL));
245-
mNotificationManager.notify(NOTIFY_ID, mBuilder.build());
273+
Notification notification = mBuilder.build();
274+
notification.flags = Notification.FLAG_AUTO_CANCEL;
275+
mNotificationManager.notify(NOTIFY_ID, notification);
246276
}
247277
//下载完自杀
248278
close();
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.vector.update_app.utils;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.nio.MappedByteBuffer;
7+
import java.nio.channels.FileChannel;
8+
import java.security.MessageDigest;
9+
import java.security.NoSuchAlgorithmException;
10+
11+
/**
12+
* Created by Vector
13+
* on 2017/7/11 0011.
14+
*/
15+
16+
public class Md5Util {
17+
public static String getFileMD5(File file) {
18+
if (!file.exists()) {
19+
return "";
20+
}
21+
FileInputStream in = null;
22+
try {
23+
in = new FileInputStream(file);
24+
FileChannel channel = in.getChannel();
25+
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
26+
MessageDigest md = MessageDigest.getInstance("MD5");
27+
md.update(buffer);
28+
return bytes2Hex(md.digest());
29+
} catch (NoSuchAlgorithmException | IOException e) {
30+
e.printStackTrace();
31+
} finally {
32+
if (in != null) {
33+
try {
34+
in.close();
35+
} catch (IOException ignored) {
36+
}
37+
}
38+
}
39+
return "";
40+
}
41+
42+
/**
43+
* 一个byte转为2个hex字符
44+
*
45+
* @param src byte数组
46+
* @return 16进制大写字符串
47+
*/
48+
public static String bytes2Hex(byte[] src) {
49+
char[] res = new char[src.length << 1];
50+
final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
51+
for (int i = 0, j = 0; i < src.length; i++) {
52+
res[j++] = hexDigits[src[i] >>> 4 & 0x0f];
53+
res[j++] = hexDigits[src[i] & 0x0f];
54+
}
55+
return new String(res);
56+
}
57+
58+
}

0 commit comments

Comments
 (0)