AppUpgradeManager.java
9.5 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Copyright (c) 2017. wugian
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.xgimi.gimicinema.appupdate;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import com.gimi.common.cinema.utils.Config;
import com.gimi.common.cinema.utils.OkHttpClientManager;
import com.gimi.common.cinema.utils.SystemUtils;
import com.gimi.common.cinema.utils.Utils;
import com.gimi.common.cinema.utils.XgimiDevice;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.xgimi.gimicinema.BuildConfig;
import java.lang.ref.WeakReference;
/**
* Created by wugian on 2017/4/24
*/
public class AppUpgradeManager {
private static final String TAG = "AppUpgradeManager";
private static final int SHOW_DIALOG_UPDATE = 0x91;
private static final int SHOW_DIALOG_NEWEST = 0x92;
private String API_ROOT = "http://api.qnbar.com";
private static final String API_ROOT_TEST = "http://api.t.qnbar.com";
private static final String API_GET_APP_VERSION = "/Api/Agent/CheckAppVersion1";
// private static final String API_GET_APP_VERSION = "/Agent/CheckAppVersion1";
private String updateApkUrl;// = API_ROOT + "/Api/Agent/CheckAppVersion1";// + XgimiDevice.getDeviceType() + "&imei=" + SystemUtils.getPID();
private UpdateMsg mUpdateMsg;
private Context context;
private String packageName = "";
private boolean showUpdate;
private Handler mHandler;
public AppUpgradeManager(Context context, String packageName, boolean showUpdates) {
boolean test = Utils.getBool(context, "test");
if (test) {
API_ROOT = API_ROOT_TEST;
}
this.context = context;
this.packageName = packageName;
this.showUpdate = showUpdates;
this.mHandler = new AppUpdateHandler(this);
}
private static class AppUpdateHandler extends Handler {
private final WeakReference<AppUpgradeManager> activity;
AppUpdateHandler(AppUpgradeManager activity) {
this.activity = new WeakReference<>(activity);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
AppUpgradeManager a = activity.get();
if (a != null) {
switch (msg.what) {
case SHOW_DIALOG_UPDATE:
a.doNewVersionUpdate();
break;
case SHOW_DIALOG_NEWEST:
a.notNewVersionShow();
break;
}
}
}
}
public boolean updateAppVersion() {
String verJson = null;
String deviceType = "8888";
String pid = SystemUtils.getPid(context, BuildConfig.MACHINE_TYPE);
if (!BuildConfig.MACHINE_TYPE.equals("himedia")) {
deviceType = XgimiDevice.getDeviceType();
}
String pkgName = packageName;
if ("com.xgimi.gimicinema".equals(packageName)) {
pkgName = pkgName + ".ft";
}
updateApkUrl = API_ROOT + API_GET_APP_VERSION + "?machineType=" + deviceType + "&imei=" + pid + "&packageName=" + pkgName;
Log.d("install", "request:" + updateApkUrl);
try {
verJson = OkHttpClientManager.getAsString(updateApkUrl);
Log.d("install", "result:" + verJson);
} catch (Exception e) {
e.printStackTrace();
}
if (TextUtils.isEmpty(verJson)) {
Log.d("install", "verJson charge empty");
return false;
}
try {
UpdateMsg updateMsg = new Gson().fromJson(verJson, new TypeToken<UpdateMsg>() {
}.getType());
if (updateMsg != null && updateMsg.getCode() == 0
&& updateMsg.getData() != null) {
Log.d("install", "charge version");
if (updateMsg.getData().getVerCode() > getVerCode(context)) {
this.mUpdateMsg = updateMsg;
mHandler.sendEmptyMessage(SHOW_DIALOG_UPDATE);
// doNewVersionUpdate();
} else {
Log.d("install", "charge empty");
if (showUpdate) {
// notNewVersionShow();
mHandler.sendEmptyMessage(SHOW_DIALOG_NEWEST);
}
}
} else {
Log.d("install", "charge 123");
if (showUpdate) {
// notNewVersionShow();
mHandler.sendEmptyMessage(SHOW_DIALOG_NEWEST);
}
}
} catch (JsonSyntaxException e) {
e.printStackTrace();
}
return true;
}
private void doNewVersionUpdate() {
Log.d("install", "doNewVersionUpdate");
if (!showUpdate && mUpdateMsg.getData().isForceType()) {
String url = mUpdateMsg.getData().getAppUrl();
Log.d("install", "doNewVersionUpdate:" + url);
if (!TextUtils.isEmpty(url)) {
UpgradeDownloadManager updateManager =
new UpgradeDownloadManager(context, url, mUpdateMsg.getData().isForceType());
updateManager.downloadApk(showUpdate | !mUpdateMsg.getData().isForceType());
}
return;
}
String verName = Config.getVerName(context);
StringBuilder message = new StringBuilder();
message.append("当前版本");
message.append(verName);
message.append(", 发现新版本");
message.append(mUpdateMsg.getData().getVerName());
message.append(", 是否更新?");
message.append("\n");
String description = mUpdateMsg.getData().getDescription();
if (!TextUtils.isEmpty(description)) {
if (description.contains("#")) {
String[] split = description.split("#");
for (String s : split) {
message.append("\n").append(s);
}
} else {
message.append("\n").append(mUpdateMsg);
}
}
Dialog dialog = new AlertDialog.Builder(context)
.setTitle("软件更新")
.setMessage(message.toString())
.setCancelable(false)
// 设置内容
.setPositiveButton("更新",// 设置确定按钮
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog1, int which) {
String url = mUpdateMsg.getData().getAppUrl();
if (!TextUtils.isEmpty(url)) {
UpgradeDownloadManager updateManager =
new UpgradeDownloadManager(context, url, showUpdate & mUpdateMsg.getData().isForceType());
updateManager.downloadApk(showUpdate | !mUpdateMsg.getData().isForceType());
} else {
dialog1.dismiss();
}
}
}).create();// 创建
// 显示对话框
dialog.show();
}
private void notNewVersionShow() {
String verName = getVerName(context);
String string = "当前版本" +
verName +
"已是最新版本";
Dialog dialog = new AlertDialog.Builder(context)
.setTitle("软件更新")
.setMessage(string)// 设置内容
.setPositiveButton("确定",// 设置确定按钮
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog1, int which) {
dialog1.dismiss();
}
}).create();// 创建
// 显示对话框
dialog.show();
}
private int getVerCode(Context context) {
int verCode = -1;
try {
verCode = context.getPackageManager().getPackageInfo(
packageName.equals("com.xgimi.gimicinema.ft") ? "com.xgimi.gimicinema" : packageName, 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, e.getMessage());
}
return verCode;
}
private String getVerName(Context context) {
String verName = "";
try {
verName = context.getPackageManager().getPackageInfo(
packageName.equals("com.xgimi.gimicinema.ft") ? "com.xgimi.gimicinema" : packageName, 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, e.getMessage());
}
return verName;
}
}