-
-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathUpdateHelper.java
More file actions
383 lines (341 loc) · 17.9 KB
/
Copy pathUpdateHelper.java
File metadata and controls
383 lines (341 loc) · 17.9 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package org.qpython.qpy.utils;
import android.support.v7.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.quseit.base.QBaseApp;
import com.quseit.config.BASE_CONF;
import com.quseit.common.db.AppLog;
import com.quseit.common.db.CacheLog;
import com.quseit.common.db.UserLog;
import com.quseit.util.DateTimeHelper;
import com.quseit.util.FileHelper;
import com.quseit.util.NAction;
import com.quseit.util.NUtil;
import com.quseit.util.VeDate;
import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.qpython.qpy.R;
import org.qpython.qpy.main.app.App;
import org.qpython.qpy.main.server.model.SettingModel;
import org.qpython.qpysdk.QPyConstants;
import java.lang.reflect.Field;
import java.util.ArrayList;
public class UpdateHelper {
private static final String TAG = "UpdateHelper";
private static void checkConfUpdate(final Context context) {
if (NUtil.netCheckin(context.getApplicationContext())) {
// clear db cache
CacheLog cDB = new CacheLog(context);
cDB.cleanCache();
RequestParams myParam = new RequestParams();
String types = "11,12,13";
String limit = BASE_CONF.LOG_LIMIT;
UserLog userLog = new UserLog(context.getApplicationContext());
String xlogs = "";
try {
xlogs = userLog.getLogs(types, 0, limit, "ASC");
} catch (OutOfMemoryError e) {
Log.d(TAG, "err when getLogs:" + e.getMessage());
xlogs = "";
userLog.deleteAllStat_0_Log();
}
final String logs = xlogs;
myParam.put("time", DateTimeHelper.getDateMin());
myParam.put("logs", logs);
/* 手机客户信息 */
String collectInfos = BASE_CONF.COLLECT_INFO;
Field[] fields = Build.class.getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible(true);
String k = field.getName();
if (k != null) {
k = k.toLowerCase().trim();
}
if (collectInfos.contains("#" + k + "#")) {
myParam.put(k, field.get(null).toString());
}
} catch (Exception e) {
Log.e(TAG, "Error occured when collect crash info", e);
}
}
if (BASE_CONF.DEBUG)
Log.d(TAG, "checkUpdate:" + logs);
String updateUrl = NAction.getUpdateHost(context.getApplicationContext());
if (updateUrl.equals("")) {
updateUrl = confGetUpdateURL(context, 1);
}
if (!BASE_CONF.DEBUG)
Log.d(TAG, "checkUpdate:" + updateUrl + "?" + NAction.getUserUrl(context.getApplicationContext()));
QBaseApp.getInstance().getAsyncHttpClient().post(context.getApplicationContext(), updateUrl + "?" + NAction.getUserUrl(context.getApplicationContext()),
myParam, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject result) {
if (result == null) {
return;
}
if (BASE_CONF.DEBUG)
Log.d(TAG, "checkUpdate-result:" + result.toString());
UserLog log = new UserLog(context);
log.deleteAllStat_0_Log();
Gson gson = App.getGson();
SettingModel settings = gson.fromJson(result.toString(), SettingModel.class);
try {
NAction.setExtConf(context, gson.toJson(settings.getQpy().getExt2()));
NAction.setExtAdConf(context, gson.toJson(settings.getQpy().getExt_ad()));
NAction.setUpdateCheckTime(context);
} catch (NullPointerException e) {
//Toast.makeText(context, R.string.get_setting_data_fail+"(e)"+e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
// waitingWindow.dismiss();
Log.d(TAG, "Error in checkConfUpdate:" + throwable.getMessage());
}
});
}
}
private static String confGetUpdateURL(Context context, int flag) {
if (flag == 2) {
return QPyConstants.LOG_URL + context.getPackageName() + "/" + NUtil.getVersinoCode(context);
} else if (flag == 3) {
return QPyConstants.AD_URL + context.getPackageName() + "/" + NUtil.getVersinoCode(context) + "?"
+ NAction.getUserUrl(context.getApplicationContext());
} else if (flag == 5) {
return QPyConstants.IAP_LOG_URL + context.getPackageName() + "/" + NUtil.getVersinoCode(context) + "?"
+ NAction.getUserUrl(context.getApplicationContext());
} else {
return QPyConstants.UPDATE_URL + context.getPackageName() + "/" + NUtil.getVersinoCode(context);
}
}
public static void checkConfUpdate(Context context, String root) {
int now = VeDate.getStringDateHourAsInt();
int lastCheck = NAction.getUpdateCheckTime(context);
PackageInfo pInfo = null;
try {
pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if ((pInfo == null ? 0 : pInfo.versionCode) < BASE_CONF.UPDATE_VER) {
NAction.setUpdateHost(context, "");
checkConfUpdate(context.getApplicationContext());
} else {
if (!sendCrashLog(context.getApplicationContext())) {
int q = NAction.getUpdateQ(context.getApplicationContext());
if (q == 0) {
q = BASE_CONF.UPDATEQ;
}
if ((now - lastCheck) >= q) { // 每q小时检查一次更新/清空一下不必要的cache
checkUpdate(context, true);
checkConfUpdate(context.getApplicationContext());
// 清空图片目录的缓存
String cacheDir = Environment.getExternalStorageDirectory() + "/" + root + "/" + BASE_CONF.DCACHE + "/";
FileHelper.clearDir(cacheDir, 0, false);
}
}
}
}
private static boolean sendCrashLog(final Context context) {
if (NUtil.netCheckin(context.getApplicationContext())) {
Log.d(TAG, "notifyErr");
RequestParams myParam = new RequestParams();
//String updateUrl = NAction.getUpdateHost(context.getApplicationContext());
//if (updateUrl.equals("")) {
String updateUrl = confGetUpdateURL(context, 2);
//}
// else {
// updateUrl = NAction.getExtP(context, "conf_send_log_host");
// }
String collectInfos = NAction.getExtP(context, "conf_get_log_cls");
if (collectInfos.equals("")) {
collectInfos = BASE_CONF.COLLECT_INFO;
}
Field[] fields = Build.class.getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible(true);
String k = field.getName();
if (k != null) {
k = k.toLowerCase().trim();
}
if (collectInfos.contains("#" + k + "#")) {
myParam.put(k, field.get(null).toString());
}
// LogUtil.d(TAG, field.getName() + " : " + field.get(null));
} catch (Exception e) {
Log.e(TAG, "an error occured when collect crash info", e);
}
}
AppLog appDB = new AppLog(context);
final ArrayList<String[]> logs = appDB.getLogs(0);
if (logs.size() != 0) {
JSONArray descArray = new JSONArray();
// LogUtil.d(TAG, "notifyErr:"+logs);
for (int i = 0; i < logs.size(); i++) {
String[] items = logs.get(i);
myParam.put("act", "report_err");
descArray.put(items[5]);
/*
* myParam.put("report_id", items[0]); myParam.put("report_title", items[1]);
* myParam.put("report_ver", items[2]); myParam.put("report_time", items[3]);
* myParam.put("report_stat", items[4]); myParam.put("report_desc", items[5]);
* myParam.put("report_userno", items[6]); myParam.put("report_id", items[0]);
* myParam.put("report_title", items[1]); myParam.put("report_ver", items[2]);
* myParam.put("report_time", items[3]); myParam.put("report_stat", items[4]);
* myParam.put("report_desc", items[5]); myParam.put("report_userno", items[6]);
*/
appDB.deleteLog(Long.parseLong(items[0]));
}
myParam.put("report_desc", descArray.toString());
new AsyncHttpClient().post(context.getApplicationContext(), updateUrl + "?" + NAction.getUserUrl(context.getApplicationContext()),
myParam, new JsonHttpResponseHandler() { });
return true;
} else {
// LogUtil.d(TAG, "notifyErr no need");
return false;
}
} else {
return false;
}
}
private static void checkUpdate(final Context context, final boolean isAuto) {
QBaseApp.getInstance().getAsyncHttpClient().post(context, BASE_CONF.UPDATER_URL, null, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
int ver = 0;
String downloadUrl = "";
String verName = "";
String desc = "";
int lastPluginVer = 0;
int currentPluginVer = 0;
String jsonPlugin = NAction.getExtPluginsConf(context);
try {
JSONObject app = response.getJSONObject(BASE_CONF.APP_KEY);
ver = app.getInt(BASE_CONF.VERSION_KEY);
verName = app.getString(BASE_CONF.VERSION_NAME_KEY);
downloadUrl = app.getString(BASE_CONF.DOWNLOAD_LINK_KEY);
desc = app.getString(BASE_CONF.VERIOSN_DESC_KEY);
} catch (JSONException e) {
e.printStackTrace();
}
try {
if ("".equals(jsonPlugin)) {
lastPluginVer = BASE_CONF.plugin_ver;
} else {
lastPluginVer = new JSONObject(jsonPlugin).getJSONObject(BASE_CONF.APP_KEY).getInt(BASE_CONF.VERSION_PLUGIN_KEY);
}
currentPluginVer = response.getJSONObject(BASE_CONF.APP_KEY).getInt(BASE_CONF.VERSION_PLUGIN_KEY);
} catch (JSONException e) {
e.printStackTrace();
}
if (lastPluginVer < currentPluginVer || NUtil.getVersinoCode(context.getApplicationContext()) < ver) {
//弹出提示更新
AlertDialog.Builder alert = new AlertDialog.Builder(context);
final String finalDownloadUrl = downloadUrl;
final int finalVer = ver;
final int finalLastPluginVer = lastPluginVer;
final int finalCurrentPluginVer = currentPluginVer;
alert.setTitle(com.quseit.android.R.string.up_soft_state_found)
.setMessage(verName + "\n" + desc)
.setPositiveButton(context.getString(com.quseit.android.R.string.up_soft), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (NUtil.getVersinoCode(context.getApplicationContext()) < finalVer) {
//app更新
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse(finalDownloadUrl);
intent.setData(content_url);
context.startActivity(intent);
}
if (finalLastPluginVer < finalCurrentPluginVer) {
//插件更新
JSONArray plugins = null;
try {
plugins = response.getJSONArray(BASE_CONF.PLUGIN_KEY);
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < plugins.length(); i++) {
JSONObject plugin = null;
try {
plugin = plugins.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
if (!(plugin == null)) {
try {
final String dst = plugin.getString("dst");
final String link = plugin.getString("link");
new Thread(new Runnable() {
@Override
public void run() {
boolean ret = FileHelper.getUrlAsFile(link, context.getApplicationContext().getFilesDir() + "/" + dst);
if (ret) {
// runOnUiThread(new Runnable() {
// @Override
// public void run() {
// Toast.makeText(context, "插件已更新,请重启app", Toast.LENGTH_SHORT).show();
// }
// });
} else {
// runOnUiThread(new Runnable() {
// @Override
// public void run() {
// Toast.makeText(context, "插件更新失败", Toast.LENGTH_SHORT).show();
// }
// });
}
}
}).start();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
NAction.setExtPluginsConf(context, response.toString());
}
}
}).setNegativeButton(context.getString(com.quseit.android.R.string.promote_cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.create().show();
} else {
if (!isAuto) {
// closeWaitWindow();
// alertUpdateDialog2(verName);
}
}
}
});
}
/*
key: 16 bit string
param: 64 bit string
*/
public static void submitIAPLog(final Context context, String key, String param) {
RequestParams myParam = new RequestParams();
myParam.put("idt", key);
myParam.put("param", param);
new AsyncHttpClient().post(context, confGetUpdateURL(context, 5),
myParam, new JsonHttpResponseHandler() { }
);
}
}