Skip to content

Commit 77e10c3

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

5 files changed

Lines changed: 64 additions & 46 deletions

File tree

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,17 +244,19 @@ protected UpdateAppBean parseJson(String json) {
244244
JSONObject jsonObject = new JSONObject(json);
245245
updateAppBean
246246
//是否更新Yes,No
247-
.setUpdate(jsonObject.getString("update"))
247+
.setUpdate(jsonObject.optString("update"))
248248
//新版本号
249-
.setNewVersion(jsonObject.getString("new_version"))
249+
.setNewVersion(jsonObject.optString("new_version"))
250250
//下载地址
251-
.setApkFileUrl(jsonObject.getString("apk_file_url"))
251+
.setApkFileUrl(jsonObject.optString("apk_file_url"))
252252
//大小
253-
.setTargetSize(jsonObject.getString("target_size"))
253+
.setTargetSize(jsonObject.optString("target_size"))
254254
//更新内容
255-
.setUpdateLog(jsonObject.getString("update_log"))
255+
.setUpdateLog(jsonObject.optString("update_log"))
256256
//是否强制更新
257-
.setConstraint(true);
257+
.setConstraint(true)
258+
//设置md5
259+
.setNewMd5(jsonObject.optString("new_md5ddfdfdf"));
258260
} catch (JSONException e) {
259261
e.printStackTrace();
260262
}

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
import android.content.Intent;
55
import android.content.ServiceConnection;
66
import android.graphics.Color;
7+
import android.net.Uri;
8+
import android.os.Build;
79
import android.os.Bundle;
810
import android.os.IBinder;
911
import android.support.annotation.Nullable;
1012
import android.support.v4.app.FragmentActivity;
13+
import android.support.v4.content.FileProvider;
1114
import android.support.v7.graphics.Palette;
1215
import android.text.TextUtils;
1316
import android.view.View;
@@ -19,9 +22,12 @@
1922
import com.vector.update_app.service.DownloadService;
2023
import com.vector.update_app.utils.ColorUtil;
2124
import com.vector.update_app.utils.DrawableUtil;
25+
import com.vector.update_app.utils.Md5Util;
2226
import com.vector.update_app.utils.Utils;
2327
import com.vector.update_app.view.NumberProgressBar;
2428

29+
import java.io.File;
30+
2531
/**
2632
* 新版本提交对话框
2733
*/
@@ -178,7 +184,7 @@ private void initEvents() {
178184
public void onClick(View view) {
179185
int i = view.getId();
180186
if (i == R.id.btn_ok) {
181-
downloadApp();
187+
installApp();
182188
mUpdateOkButton.setVisibility(View.GONE);
183189
} else if (i == R.id.iv_close) {
184190
// if (mNumberProgressBar.getVisibility() == View.VISIBLE) {
@@ -188,6 +194,40 @@ public void onClick(View view) {
188194
}
189195
}
190196

197+
private void installApp() {
198+
String apkUrl = mUpdateApp.getApkFileUrl();
199+
final String appName = apkUrl.substring(apkUrl.lastIndexOf("/") + 1, apkUrl.length());
200+
File appFile = new File(mUpdateApp.getTargetPath()
201+
.concat(File.separator + mUpdateApp.getNewVersion())
202+
.concat(File.separator + appName));
203+
//md5不为空
204+
//文件存在
205+
//md5只一样
206+
if (!TextUtils.isEmpty(mUpdateApp.getNewMd5())
207+
&& appFile.exists()
208+
&& Md5Util.getFileMD5(appFile).equalsIgnoreCase(mUpdateApp.getNewMd5())) {
209+
210+
211+
Uri fileUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".fileProvider", appFile);
212+
Intent intent = new Intent(Intent.ACTION_VIEW);
213+
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
214+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
215+
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
216+
intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
217+
} else {
218+
intent.setDataAndType(Uri.fromFile(appFile), "application/vnd.android.package-archive");
219+
}
220+
if (getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
221+
startActivity(intent);
222+
}
223+
//安装完自杀
224+
onBackPressed();
225+
} else {
226+
downloadApp();
227+
}
228+
}
229+
230+
191231
/**
192232
* 开启后台服务下载
193233
*/

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ public class UpdateAppBean implements Serializable {
3131
private String target_size;
3232
//是否强制更新
3333
private boolean constraint;
34+
//md5
35+
private String new_md5;
3436

3537

36-
//md5暂时不用
37-
private String new_md5;
3838
//是否增量 暂时不用
3939
private boolean delta;
4040

@@ -68,8 +68,9 @@ public boolean isConstraint() {
6868
return constraint;
6969
}
7070

71-
public void setConstraint(boolean constraint) {
71+
public UpdateAppBean setConstraint(boolean constraint) {
7272
this.constraint = constraint;
73+
return this;
7374
}
7475

7576
public String getUpdate() {
@@ -121,8 +122,9 @@ public String getNewMd5() {
121122
return new_md5;
122123
}
123124

124-
public void setNewMd5(String new_md5) {
125+
public UpdateAppBean setNewMd5(String new_md5) {
125126
this.new_md5 = new_md5;
127+
return this;
126128
}
127129

128130
public String getTargetSize() {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.vector.update_app;
22

3-
import org.json.JSONException;
43
import org.json.JSONObject;
54

65
/**
@@ -18,13 +17,14 @@ protected UpdateAppBean parseJson(String json) {
1817
UpdateAppBean updateAppBean = new UpdateAppBean();
1918
try {
2019
JSONObject jsonObject = new JSONObject(json);
21-
updateAppBean.setUpdate(jsonObject.getString("update"))
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"))
26-
.setConstraint(jsonObject.getBoolean("constraint"));
27-
} catch (JSONException e) {
20+
updateAppBean.setUpdate(jsonObject.optString("update"))
21+
.setNewVersion(jsonObject.optString("new_version"))
22+
.setApkFileUrl(jsonObject.optString("apk_file_url"))
23+
.setTargetSize(jsonObject.optString("target_size"))
24+
.setUpdateLog(jsonObject.optString("update_log"))
25+
.setConstraint(jsonObject.optBoolean("constraint"))
26+
.setNewMd5(jsonObject.optString("new_md5"));
27+
} catch (Exception e) {
2828
e.printStackTrace();
2929
}
3030
return updateAppBean;

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

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
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;
2322
import com.vector.update_app.utils.Utils;
2423

2524
import java.io.File;
@@ -110,32 +109,7 @@ private void startDownload(UpdateAppBean updateApp, final DownloadCallback callb
110109

111110
String target = appDir + File.separator + updateApp.getNewVersion();
112111

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-
}
138-
112+
updateApp.getHttpManager().download(apkUrl, target, appName, new FileDownloadCallBack(callback));
139113
}
140114

141115
private void stop(String contentText) {

0 commit comments

Comments
 (0)