Browse Source

自定义更新下载框及下载模式

leijie 8 years ago
parent
commit
b36c9bc4e5

+ 17 - 1
display-client/src/com/ai/ipu/display/MainActivity.java

@ -8,12 +8,14 @@ import android.os.Bundle;
8 8
import android.util.DisplayMetrics;
9 9
import android.view.View;
10 10
11
import com.ai.ipu.mgr.downloadMgr.DownloadMgr;
11 12
import com.ai.ipu.mobile.app.AppInfoUtil;
12 13
import com.ai.ipu.mobile.app.AppRecord;
13 14
import com.ai.ipu.mobile.app.ApplicationManager;
14 15
import com.ai.ipu.mobile.app.MobileCheck;
15 16
import com.ai.ipu.mobile.app.MobileOperation;
16 17
import com.ai.ipu.mobile.frame.activity.TemplateMainActivity;
18
import com.ai.ipu.mobile.frame.config.MobileConfig;
17 19
import com.ai.ipu.mobile.frame.config.ServerConfig;
18 20
import com.ai.ipu.mobile.plugin.MobileUI;
19 21
import com.ai.ipu.mobile.res.assets.AssetsUtil;
@ -210,6 +212,20 @@ public class MainActivity extends TemplateMainActivity {
210 212
		} catch (Exception e) {
211 213
			e.printStackTrace();
212 214
		}
213
215
	}
216
	
217
	@Override
218
	protected void updateResource() {
219
		// TODO Auto-generated method stub
220
		super.updateResource();
221
	}
222
	
223
	@Override
224
	protected void updateClient() {
225
		String apkName = AppInfoUtil.getAppName();
226
		String downloadUrl = MobileConfig.getInstance().getUpdateUrl();
227
		String apkPath = AppInfoUtil.getSdcardPath() + "/ipu/apk/"+ apkName + ".apk";
228
		DownloadMgr downloadMgr = new DownloadMgr(this, downloadUrl, apkPath, true);
229
		downloadMgr.showUpateDialog(this);
214 230
	}
215 231
}

+ 8 - 0
ipu-alertdialog/src/cn/pedant/SweetAlert/SweetAlertDialog.java

@ -402,4 +402,12 @@ public class SweetAlertDialog extends Dialog implements View.OnClickListener {
402 402
    public NumberProgressBar getNumProgressBar(){
403 403
    	return mNumberProgressBar;
404 404
    }
405
    
406
    public void setConfirmButtonColor(int resId){
407
    	mConfirmButton.setBackgroundResource(resId);
408
    }
409
    
410
    public Button getConfirmButton(){
411
    	return mConfirmButton;
412
    }
405 413
}

+ 213 - 0
ipu-alertdialog/src/com/ai/ipu/mgr/downloadMgr/DownloadMgr.java

@ -0,0 +1,213 @@
1
package com.ai.ipu.mgr.downloadMgr;
2
3
import java.io.File;
4
import java.net.HttpURLConnection;
5
import java.net.URL;
6
7
import com.ai.ipu.basic.net.http.HttpTool;
8
import com.ai.ipu.mobile.util.IpuMobileUtility;
9
import com.litesuits.android.log.Log;
10
11
import android.content.Context;
12
import android.content.Intent;
13
import android.content.pm.PackageManager.NameNotFoundException;
14
import android.net.Uri;
15
import android.os.AsyncTask;
16
import android.os.Bundle;
17
import android.os.Handler;
18
import android.os.Message;
19
import android.widget.Toast;
20
import cn.pedant.SweetAlert.SweetAlertDialog;
21
import cn.pedant.SweetAlert.SweetAlertDialog.OnSweetClickListener;
22
23
public class DownloadMgr {
24
	private Context context;
25
	private SweetAlertDialog updateDialog;
26
	private SweetAlertDialog progressDialog;
27
	private Handler progressHandler;
28
	private String downloadUrl, apkPath;
29
30
	private static final String MAX_PROGRESS = "max";
31
	private static final String CUR_PROGRESS = "progress";
32
	private static final int TAG_MSG_MAX = 0;
33
	private static final int TAG_MSG_PROGRESS = 1;
34
	private boolean needInstall = true;
35
36
	public DownloadMgr(Context context) {
37
		this.context = context;
38
	}
39
	
40
	public DownloadMgr(Context context,String downloadUrl,String apkPath){
41
		this(context);
42
		this.downloadUrl = downloadUrl;
43
		this.apkPath = apkPath;
44
	}
45
	public DownloadMgr(Context context,String downloadUrl,String apkPath,boolean needInstall){	
46
		this(context,downloadUrl,apkPath);
47
		this.needInstall = needInstall;
48
	}
49
50
	public void downloadFile(String downloadUrl, final String apkPath) {
51
		new AsyncTask<String, Integer, Integer>() {
52
			protected void onPreExecute() {
53
				showNumberProgressDialog(context);
54
				progressHandler = new Handler() {
55
					public void handleMessage(Message msg) {
56
						int tag = msg.what;
57
						Bundle data = msg.getData();
58
						switch (tag) {
59
						case TAG_MSG_MAX:
60
							int max = data.getInt(MAX_PROGRESS);
61
							progressDialog.setNumberMax(max);
62
							break;
63
						case TAG_MSG_PROGRESS:
64
							int progress = data.getInt(CUR_PROGRESS);
65
							progressDialog.setNumberProgress(progress);
66
							break;
67
						}
68
					};
69
				};
70
			};
71
72
			@Override
73
			protected Integer doInBackground(String... params) {
74
				final String downloadUrl = params[0];
75
				final String apkPath = params[1];
76
77
				File file = new File(apkPath).getParentFile();
78
				if(!file.exists() && !file.mkdirs()){
79
					IpuMobileUtility.error("创建下载文件夹失败!");
80
				}
81
				File apkFile = new File(apkPath);
82
				if(apkFile.exists()){
83
					apkFile.delete();
84
				}
85
				new Thread(new Runnable() {
86
					
87
					@Override
88
					public void run() {
89
						try {
90
							URL url = new URL(downloadUrl);
91
							HttpURLConnection conn = (HttpURLConnection) url.openConnection();
92
							int remoteLength = conn.getContentLength();
93
							Message msgMax = new Message();
94
							Bundle dataMax = new Bundle();
95
							dataMax.putInt(MAX_PROGRESS, remoteLength);
96
							msgMax.what = TAG_MSG_MAX;
97
							msgMax.setData(dataMax);
98
							progressHandler.sendMessage(msgMax);
99
							
100
							int localLength = 0;
101
							File apkFile = new File(apkPath);
102
							while(localLength < remoteLength){
103
								if(apkFile.exists()){
104
									Message msgCur = new Message();
105
									Bundle dataCur = new Bundle();
106
									localLength = (int) apkFile.length();
107
									msgCur.what = TAG_MSG_PROGRESS;
108
									dataCur.putInt(CUR_PROGRESS, localLength);
109
									msgCur.setData(dataCur);
110
									progressHandler.sendMessage(msgCur);
111
								}
112
							}
113
						} catch (Exception e) {
114
							e.printStackTrace();
115
						} 
116
					}
117
				}).start();
118
				try {
119
					HttpTool.httpDownload(downloadUrl, apkPath);
120
				} catch (Exception e) {
121
					e.printStackTrace();
122
					return 1;
123
				}
124
				return 0;
125
			}
126
127
			protected void onPostExecute(Integer result) {
128
				if(result == 0){
129
					Toast.makeText(context, "下载成功", 2000).show();
130
					progressDialog.dismiss();
131
					if(needInstall){						
132
						installApk();
133
					}
134
				}else {
135
					Toast.makeText(context, "下载失败", 3000).show();
136
				}
137
			};
138
		}.execute(downloadUrl, apkPath);
139
	}
140
141
	public void showNumberProgressDialog(Context context) {
142
		progressDialog = new SweetAlertDialog(context, SweetAlertDialog.NUMBERPROGRESSBAR_TYPE);
143
		progressDialog.setTitleText("");
144
		progressDialog.setContentText("下载中...");
145
//		progressDialog.setCancelable(true);
146
		progressDialog.show();
147
	}
148
149
	public void showUpateDialog(Context context) {
150
		updateDialog = new SweetAlertDialog(context, SweetAlertDialog.NORMAL_TYPE);
151
		updateDialog.setTitleText("提示");
152
		updateDialog.setContentText("确认是否需要更新");
153
		updateDialog.setCancelable(true);
154
		updateDialog.setConfirmText("确认");
155
		updateDialog.setCancelText("取消");
156
		updateDialog.setConfirmClickListener(new OnSweetClickListener() {
157
158
			@Override
159
			public void onClick(SweetAlertDialog sweetAlertDialog) {
160
				sweetAlertDialog.dismiss();
161
				downloadFile(downloadUrl, apkPath);
162
			}
163
		});
164
		updateDialog.setCancelClickListener(new OnSweetClickListener() {
165
166
			@Override
167
			public void onClick(SweetAlertDialog sweetAlertDialog) {
168
				sweetAlertDialog.dismiss();
169
			}
170
		});
171
		updateDialog.show();
172
	}
173
174
	// 通过版本检测是否需要下载
175
	public boolean checkVersion(int newVersionCode) throws NameNotFoundException {
176
		boolean needDownload = false;
177
		int currentVersionCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
178
		needDownload = currentVersionCode == newVersionCode ? false : true;
179
		return needDownload;
180
	}
181
182
	public void installApk(){
183
		File apkFile = new File(apkPath);
184
		if(!apkFile.exists()){
185
			return;
186
		}
187
		Uri uri = Uri.parse("file://"+ apkFile.toString());
188
		Intent intent = new Intent(Intent.ACTION_VIEW);
189
		intent.setDataAndType(uri, "application/vnd.android.package-archive");
190
		context.startActivity(intent);
191
	}
192
193
	public String getDownloadUrl() {
194
		return downloadUrl;
195
	}
196
197
	public void setDownloadUrl(String downloadUrl) {
198
		this.downloadUrl = downloadUrl;
199
	}
200
201
	public String getApkPath() {
202
		return apkPath;
203
	}
204
205
	public void setApkPath(String apkPath) {
206
		this.apkPath = apkPath;
207
	}
208
	
209
	public void setConfirmButtonColor(int colorResId){
210
		updateDialog.getConfirmButton().setBackgroundResource(colorResId);
211
	}
212
	
213
}

+ 4 - 4
ipu-alertdialog/src/com/daimajia/numberprogressbar/NumberProgressBar.java

@ -287,10 +287,10 @@ public class NumberProgressBar extends View {
287 287
    private void calculateDrawRectF() {
288 288
289 289
        mCurrentDrawText = String.format("%d", (int)(new Double(getProgress())* 100/ new Double(getMax())));
290
        Log.d("Drawtext", mCurrentDrawText);
291
        Log.d("Drawtext", "getProgress--->" + getProgress());
292
        Log.d("Drawtext", "getMax--->"+getMax());
293
        Log.d("Drawtext", "getProgress*100/max ---->" + getProgress() * 100 / getMax());
290
//        Log.d("Drawtext", mCurrentDrawText);
291
//        Log.d("Drawtext", "getProgress--->" + getProgress());
292
//        Log.d("Drawtext", "getMax--->"+getMax());
293
//        Log.d("Drawtext", "getProgress*100/max ---->" + getProgress() * 100 / getMax());
294 294
        mCurrentDrawText = mPrefix + mCurrentDrawText + mSuffix;
295 295
        mDrawTextWidth = mTextPaint.measureText(mCurrentDrawText);
296 296