-
Notifications
You must be signed in to change notification settings - Fork 17
/
UpdateApp.java
344 lines (315 loc) · 9.72 KB
/
UpdateApp.java
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
package com.jingle;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import com.jingle.R;
public class UpdateApp extends CordovaPlugin {
/*版本号检查路径*/
private String checkPath;
/* 新版本号*/
private int newVerCode;
/* 新版本名称 */
private String newVerName;
/* APK 下载路径*/
private String downloadPath;
/* 下载中 */
private static final int DOWNLOAD = 1;
/* 下载结束 */
private static final int DOWNLOAD_FINISH = 2;
/* 下载保存路径 */
private String mSavePath;
/* 记录进度条数量 */
private int progress;
/* 是否取消更新 */
private boolean cancelUpdate = false;
/* 上下文*/
private Context mContext;
/* 更新进度条 */
private ProgressBar mProgress;
private Dialog mDownloadDialog;
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
this.mContext = cordova.getActivity();
if (action.equals("checkAndUpdate")) {
this.checkPath = args.getString(0);
checkAndUpdate();
}else if(action.equals("getCurrentVersion")){
//优化 缩短传输内容,减少流量
// JSONObject obj = new JSONObject();
// obj.put("versionCode", this.getCurrentVerCode());
// obj.put("versionName", this.getCurrentVerName());
callbackContext.success(this.getCurrentVerCode()+"");
}else if(action.equals("getServerVersion")){
this.checkPath = args.getString(0);
if(this.getServerVerInfo()){
//优化 缩短传输内容,减少流量
// JSONObject obj = new JSONObject();
// obj.put("serverVersionCode", newVerCode);
// obj.put("serverVersionName", newVerName);
callbackContext.success(newVerCode+"");
}else{
callbackContext.error("can't connect to the server!please check [checkpath]");
}
}
return false;
}
/**
* 检查更新
*/
private void checkAndUpdate(){
if(getServerVerInfo()){
int currentVerCode = getCurrentVerCode();
if(newVerCode>currentVerCode){
this.showNoticeDialog();
}
}
}
/**
* 获取应用当前版本代码
* @param context
* @return
*/
private int getCurrentVerCode(){
String packageName = this.mContext.getPackageName();
int currentVer = -1;
try {
currentVer = this.mContext.getPackageManager().getPackageInfo(packageName, 0).versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return currentVer;
}
/**
* 获取应用当前版本名称
* @param context
* @return
*/
private String getCurrentVerName(){
String packageName = this.mContext.getPackageName();
String currentVerName = "";
try {
currentVerName = this.mContext.getPackageManager().getPackageInfo(packageName, 0).versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return currentVerName;
}
/**
* 获取应用名称
* @param context
* @return
*/
private String getAppName(){
return this.mContext.getResources().getText(R.string.app_name).toString();
}
/**
* 获取服务器上的版本信息
* @param path
* @return
* @throws Exception
*/
private boolean getServerVerInfo(){
try {
StringBuilder verInfoStr = new StringBuilder();
URL url = new URL(checkPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"),8192);
String line = null;
while((line = reader.readLine()) != null){
verInfoStr.append(line+"\n");
}
reader.close();
JSONArray array = new JSONArray(verInfoStr.toString());
if(array.length()>0){
JSONObject obj = array.getJSONObject(0);
newVerCode = obj.getInt("verCode");
newVerName = obj.getString("verName");
downloadPath = obj.getString("apkPath");
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 显示软件更新对话框
*/
private void showNoticeDialog() {
// 构造对话框
AlertDialog.Builder builder = new Builder(mContext);
builder.setTitle(R.string.soft_update_title);
builder.setMessage(R.string.soft_update_info);
// 更新
builder.setPositiveButton(R.string.soft_update_updatebtn, new OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.dismiss();
// 显示下载对话框
showDownloadDialog();
}
});
// 稍后更新
builder.setNegativeButton(R.string.soft_update_later, new OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.dismiss();
}
});
Dialog noticeDialog = builder.create();
noticeDialog.show();
}
/**
* 显示软件下载对话框
*/
private void showDownloadDialog()
{
// 构造软件下载对话框
AlertDialog.Builder builder = new Builder(mContext);
builder.setTitle(R.string.soft_updating);
// 给下载对话框增加进度条
final LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.softupdate_progress, null);
mProgress = (ProgressBar) v.findViewById(R.id.update_progress);
builder.setView(v);
// 取消更新
builder.setNegativeButton(R.string.soft_update_cancel, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
// 设置取消状态
cancelUpdate = true;
}
});
mDownloadDialog = builder.create();
mDownloadDialog.show();
// 现在文件
downloadApk();
}
/**
* 下载apk文件
*/
private void downloadApk()
{
// 启动新线程下载软件
new downloadApkThread().start();
}
private Handler mHandler = new Handler()
{
public void handleMessage(Message msg)
{
switch (msg.what)
{
// 正在下载
case DOWNLOAD:
// 设置进度条位置
mProgress.setProgress(progress);
break;
case DOWNLOAD_FINISH:
// 安装文件
installApk();
break;
default:
break;
}
};
};
/**
* 下载文件线程
*/
private class downloadApkThread extends Thread {
@Override
public void run() {
try {
// 判断SD卡是否存在,并且是否具有读写权限
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
// 获得存储卡的路径
String sdpath = Environment.getExternalStorageDirectory()+ "/";
mSavePath = sdpath + "download";
URL url = new URL(downloadPath);
// 创建连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
// 获取文件大小
int length = conn.getContentLength();
// 创建输入流
InputStream is = conn.getInputStream();
File file = new File(mSavePath);
// 判断文件目录是否存在
if (!file.exists()) {
file.mkdir();
}
File apkFile = new File(mSavePath, newVerName);
FileOutputStream fos = new FileOutputStream(apkFile);
int count = 0;
// 缓存
byte buf[] = new byte[1024];
// 写入到文件中
do {
int numread = is.read(buf);
count += numread;
// 计算进度条位置
progress = (int) (((float) count / length) * 100);
// 更新进度
mHandler.sendEmptyMessage(DOWNLOAD);
if (numread <= 0) {
// 下载完成
mHandler.sendEmptyMessage(DOWNLOAD_FINISH);
break;
}
// 写入文件
fos.write(buf, 0, numread);
} while (!cancelUpdate);// 点击取消就停止下载.
fos.close();
is.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 取消下载对话框显示
mDownloadDialog.dismiss();
}
};
/**
* 安装APK文件
*/
private void installApk() {
File apkfile = new File(mSavePath, newVerName);
if (!apkfile.exists()) {
return;
}
// 通过Intent安装APK文件
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + apkfile.toString()),
"application/vnd.android.package-archive");
mContext.startActivity(i);
}
}