UpdateApp.java
4.98 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
package com.cnlive.goldenline.util;
import android.app.Dialog;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.cnlive.goldenline.R;
import com.cnlive.goldenline.api.CmsAPI;
import com.cnlive.goldenline.model.mine.UpdateAppRequest;
import com.cnlive.goldenline.model.mine.UpdateAppResponse;
import com.cnlive.goldenline.model.mine.UpdateAppResponseBean;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
/**
* Created by gujun on 2017/4/19.
* 跟产品确认过这个版本更新app不区分渠道,要是更新的话统一更新成服务器上的apk
*/
public class UpdateApp {
/**
* 获取后台版本号进行对比
* @param context
* @param l
*/
public static void getVersion(final Context context, final UpdateDialogListener l) {
RestAdapterUtils.getRestAPI(CmsAPI.class).getVersion(new UpdateAppRequest(ChannelUtil.getChannelFromApk(context)), new Callback<UpdateAppResponse>() {
@Override
public void success(UpdateAppResponse updateAppResponse, Response response) {
try {
if (updateAppResponse != null && updateAppResponse.getCode() == 0) {
UpdateAppResponseBean bean = updateAppResponse.getData();
if (bean != null && bean.getVersionCode() > AppUtils.getVersionCode(context) && !TextUtils.isEmpty(bean.getUpdateUrl())) {
updateDialog(context, bean.getContent(), bean.getVersionName(), bean.getIsMust(),bean.getUpdateUrl(), l);
}else{
if(l != null) l.hasUpdate(false);
}
}
}catch (Exception e){
if(l != null) l.hasUpdate(false);
}
}
@Override
public void failure(RetrofitError error) {
if(l != null) l.hasUpdate(false);
}
});
}
/**
* 调用系统的下载
*
* @param context
* @param apkUrl 下载地址
*/
public static void installApk(Context context, String apkUrl) {
String APK_DOWNLOAD_PATH = "com.cnlive.goldenline/apk";
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
//创建下载请求
DownloadManager.Request down = new DownloadManager.Request(Uri.parse(apkUrl));
//设置允许使用的网络类型,这里是移动网络和wifi都可以
down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
//设置下载路径
down.setDestinationInExternalPublicDir(APK_DOWNLOAD_PATH, "goldenline.apk");
down.setTitle("正在上演");
//将下载请求放入队列
long downloadId = manager.enqueue(down);
SharedPreferencesHelper.getInstance(context).setValue("update_apk_download_id", downloadId);
}
/**
* 更新app的dialog
*
* @param context
* @param content 更新内容
* @param version 更细的版本
* @param l 监听回调
* @return
*/
public static Dialog updateDialog(final Context context, String content, String version, int isMust, final String apkUrl, final UpdateDialogListener l) {
final Dialog dialog = new Dialog(context, R.style.CustomDialogStyle);
View view = LayoutInflater.from(context).inflate(R.layout.layout_update_app, null);
((TextView) (view.findViewById(R.id.updateContent))).setText(content);
((TextView) (view.findViewById(R.id.updateVersion))).setText(version);
ViewGroup.LayoutParams vl = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
View cancel = view.findViewById(R.id.updateCancel);
//1-强制更新,0-非强制更新
cancel.setVisibility(isMust == 1 ? View.GONE : View.VISIBLE);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
view.findViewById(R.id.update).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
installApk(context,apkUrl);
dialog.dismiss();
}
});
if(l != null) l.getDialog(dialog);
dialog.addContentView(view, vl);
dialog.setCancelable(isMust == 1 ? false : true);
dialog.show();
return dialog;
}
public interface UpdateDialogListener {
void getDialog(Dialog dialog);
void hasUpdate(boolean hasUpdate);
}
}