AppUpdateUtils.java
12.8 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package com.gimi.common.cinema.utils;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import com.gimi.common.cinema.model.AdsProof;
import com.gimi.common.cinema.model.AgentInfo;
import com.gimi.common.cinema.model.ApkMessage;
import com.gimi.common.cinema.model.PosterMessage;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.squareup.okhttp.Request;
import com.xgimi.gimicinema.BuildConfig;
import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.text.ParseException;
/**
* Created by wugian on 2016/8/5
*/
public class AppUpdateUtils {
private static final int APP_VERSION_UPDATE = 0x01;
private static final int APP_VERSION_NEWEST = 0x04;
private static final int DOWNLOAD_POSTER = 0x05;
private static final int UPDATE_ADS_INFO = 0x06;
private static final String API_ROOT = "http://api.qnbar.com";
private static final String UPDATE_ADS_VIDEO_INFO = API_ROOT + "/Api/Agent/CheckAdVedio?checkCode=";
private String updateApkUrl = API_ROOT + "/Api/Agent/CheckAppVersion?machineType=";// + XgimiDevice.getDeviceType() + "&imei=" + SystemUtils.getPID();
private String updatePosterUrl = API_ROOT + "/Agent/GetPoster?imei=";// + SystemUtils.getPID();
private String GET_AGENT_INFO = API_ROOT + "/Api/Agent/GetInfo?imei=";
private Context context;
private boolean showNoUpdate = true;
private String adsVideoHash;
private String sambaRootPath;
private ApkMessage apkMessage;
private PosterMessage posterMessage;
private Handler handler;
private static class AppUpdateHandler extends Handler {
private final WeakReference<AppUpdateUtils> activity;
AppUpdateHandler(AppUpdateUtils activity) {
this.activity = new WeakReference<>(activity);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
AppUpdateUtils a = activity.get();
if (a != null) {
switch (msg.what) {
case APP_VERSION_UPDATE:
a.doNewVersionUpdate();
break;
case APP_VERSION_NEWEST:
if (a.showNoUpdate) {
a.notNewVersionShow();
}
break;
case UPDATE_ADS_INFO:
a.updateAdsInfo();
break;
case DOWNLOAD_POSTER:
a.downloadPoster();
break;
}
}
}
}
String pid;
public AppUpdateUtils(Context context) {
this.context = context;
handler = new AppUpdateHandler(this);
String deviceType = "8888";
pid = SystemUtils.getPid(context, BuildConfig.MACHINE_TYPE);
if (!BuildConfig.MACHINE_TYPE.equals("himedia")) {
deviceType = XgimiDevice.getDeviceType();
}
updateApkUrl = API_ROOT + "/Api/Agent/CheckAppVersion?machineType="
+ deviceType + "&imei=" + pid;
updatePosterUrl = API_ROOT + "/Agent/GetPoster?imei=" + pid;
}
//main false set true
public void setShowNoUpdate(boolean showNoUpdate) {
this.showNoUpdate = showNoUpdate;
}
public boolean updateAppVersion() {
String verJson = null;
try {
verJson = OkHttpClientManager.getAsString(updateApkUrl);
} catch (Exception e) {
e.printStackTrace();
}
if (TextUtils.isEmpty(verJson)) {
return false;
}
try {
Gson gson = new Gson();
apkMessage = gson.fromJson(verJson, new TypeToken<ApkMessage>() {
}.getType());
if (apkMessage != null && apkMessage.getCode() == 0
&& apkMessage.getData() != null) {
if (apkMessage.getData().getVerCode() >
Config.getVerCode(context)) {
handler.sendEmptyMessage(APP_VERSION_UPDATE);
} else {
handler.sendEmptyMessage(APP_VERSION_NEWEST);
}
} else {
handler.sendEmptyMessage(APP_VERSION_NEWEST);
}
} catch (JsonSyntaxException e) {
e.printStackTrace();
}
return true;
}
private void doNewVersionUpdate() {
String verName = Config.getVerName(context);
StringBuilder message = new StringBuilder();
message.append("当前版本");
message.append(verName);
message.append(", 发现新版本");
message.append(apkMessage.getData().getVerName());
message.append(", 是否更新?");
message.append("\n");
String updateMsg = apkMessage.getData().getDescription();
if (!TextUtils.isEmpty(updateMsg)) {
if (updateMsg.contains("#")) {
String[] split = updateMsg.split("#");
for (String s : split) {
message.append("\n").append(s);
}
} else {
message.append("\n").append(updateMsg);
}
}
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 = apkMessage.getData().getAppUrl();
if (!TextUtils.isEmpty(url)) {
UpdateManager updateManager =
new UpdateManager(context, url);
updateManager.showDownloadDialog();
} else {
dialog1.dismiss();
}
}
}).create();// 创建
// 显示对话框
dialog.show();
}
private void notNewVersionShow() {
String verName = Config.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();
}
public void updatePoster() {
String verJson = null;
try {
verJson = OkHttpClientManager.getAsString(updatePosterUrl);
} catch (Exception e) {
e.printStackTrace();
}
if (TextUtils.isEmpty(verJson)) {
return;
}
try {
Gson gson = new Gson();
posterMessage = gson.fromJson(
verJson, new TypeToken<PosterMessage>() {
}.getType());
} catch (JsonSyntaxException e) {
e.printStackTrace();
}
if (posterMessage != null) {
if (posterMessage.getCode() == 0) {
handler.sendEmptyMessage(DOWNLOAD_POSTER);
}
}
}
private void downloadPoster() {
File cacheDir = context.getCacheDir();
File file = new File(String.valueOf(cacheDir));
String[] child = file.list();
String currentName = "";
String oldPoster = "";
for (String aChild : child) {
if (aChild.endsWith("jpg")) {
currentName = aChild.substring(0, aChild.indexOf("."));
oldPoster = cacheDir + "/" + aChild;
}
}
if (!currentName.equals(posterMessage.getData().getVersionId())) {
//noinspection ResultOfMethodCallIgnored
new File(oldPoster).delete();
downImages(posterMessage.getData().getPosterUrl(), cacheDir
+ "/" + posterMessage.getData().getVersionId() + ".jpg");
}
}
private void downImages(String urlPath, String savePath) {
OkHttpClientManager.downloadAsyn(urlPath, savePath,
new OkHttpClientManager.ResultCallback<String>() {
@Override
public void onError(Request request, Exception e) {
Log.e("APpUpdateUtils", "success:" + e.getMessage());
}
@Override
public void onResponse(String response) {
Log.e("APpUpdateUtils", "success:" + response);
}
});
}
public void updateVideoInfo(String sambaRootPath, String adsVideoHash) {
this.sambaRootPath = sambaRootPath;
this.adsVideoHash = adsVideoHash;
if (TextUtils.isEmpty(adsVideoHash)) {
return;
}
String verJson = null;
try {
verJson = OkHttpClientManager.getAsString(
UPDATE_ADS_VIDEO_INFO + adsVideoHash);
} catch (Exception e) {
e.printStackTrace();
}
if (TextUtils.isEmpty(verJson)) {
return;
}
try {
Gson gson = new Gson();
AdsProof ads = gson.fromJson(verJson, new TypeToken<AdsProof>() {
}.getType());
if (ads != null) {
if (ads.getCode() == 0) {
handler.sendEmptyMessage(UPDATE_ADS_INFO);
}
}
} catch (JsonSyntaxException e) {
e.printStackTrace();
}
}
private void updateAdsInfo() {
AdsVideoUtils.writeAdsVideoInfo(sambaRootPath, adsVideoHash);
}
public void updateAgentInfo() {
String verJson = null;
try {
verJson = OkHttpClientManager.getAsString(GET_AGENT_INFO + pid);
} catch (Exception e) {
e.printStackTrace();
}
if (TextUtils.isEmpty(verJson)) {
return;
}
AgentInfo agentInfo = new AgentInfo();
agentInfo.setCode(-3);
try {
agentInfo = new Gson().fromJson(verJson, AgentInfo.class);
} catch (Exception e) {
e.printStackTrace();
}
if (agentInfo.getCode() == 0) {
AgentInfo.DataBean.AgentBean agentBean = agentInfo.getData().getAgent();
Utils.saveString(context, "agent-dist-time", agentBean.getDistTime());
Utils.saveString(context, "agent-add-time", agentBean.getAddTime());
String agentIdStr = agentBean.getAgentId();
String agentType = agentBean.getAgentType();
try {
int i = Integer.parseInt(agentIdStr);
Utils.saveInt(context, "agent-id", i);
int agent = Integer.parseInt(agentType);
Utils.saveInt(context, "agent-type", agent);
} catch (NumberFormatException e) {
e.printStackTrace();
}
long l = System.currentTimeMillis() / 1000;
try {
l = OkHttpClientManager.getSysTime(GET_AGENT_INFO + pid);
} catch (IOException | ParseException e) {
e.printStackTrace();
}
Utils.saveString(context, "agent-info-success", l + "");
// long distTime = Long.parseLong(agentInfo.getData().getAgent().getDistTime());
// long addTime = Long.parseLong(agentInfo.getData().getAgent().getAddTime());
// if (l < distTime && l > addTime) {
// AuthUtils.resetAuth(context);
// AuthUtils.resetOfflineAccessCount(context);
// }
}
}
public void updateTestGroup() {
String verJson = null;
try {
verJson = OkHttpClientManager.getAsString("https://raw.githubusercontent.com/wugian/abc/master/cinema/config");
} catch (Exception e) {
e.printStackTrace();
}
if (TextUtils.isEmpty(verJson)) {
return;
}
try {
int i = Integer.parseInt(verJson.trim());
Utils.saveInt(context, "test-limit", i);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}