Blame view

src/com/ectrip/cyt/version/VersionUpdateUtil.java 7.81 KB
3c2353cd   杜方   1、畅游通核销app源码提交;
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
  package com.ectrip.cyt.version;
  
  import java.util.concurrent.atomic.AtomicBoolean;
  
  import android.app.AlertDialog;
  import android.app.AlertDialog.Builder;
  import android.app.Dialog;
  import android.app.ProgressDialog;
  import android.content.DialogInterface;
  import android.content.DialogInterface.OnCancelListener;
  import android.content.pm.PackageInfo;
  import android.content.pm.PackageManager.NameNotFoundException;
  import android.os.Looper;
  import android.view.View;
  import android.view.WindowManager;
  import android.widget.Button;
  import android.widget.ProgressBar;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.ectrip.cyt.constant.DeviceType;
  import com.ectrip.cyt.response.DetectVersionResponse;
  import com.ectrip.cyt.ui.BaseActivity;
  import com.ectrip.cyt.utils.SharedPreferences2Obj;
  import com.ectrip.trips.check.R;
  
  /**
   * @author jigo 版本更新工具
   */
  public class VersionUpdateUtil {
  	// 通知最新版
  	private final int DIALOG_TYPE_LATEST = 0;
  	// 通知失败
  	private final int DIALOG_TYPE_FAIL = 1;
  
  	// 下载对话框
  	private Dialog downloadDialog;
  	// '已经是最新' 或者 '无法获取最新版本' 的对话框
  	private Dialog latestOrFailDialog;
  	// 进度条
  	private ProgressBar mProgress;
  	// 显示下载数值
  	private TextView mProgressText;
  	// 查询动画
  	private ProgressDialog mProDialog;
  	// 标题
  	private TextView titleText;
  	// 内容
  	private TextView contentText;
  	// 取消按钮
  	private Button cancleBtn;
  	// 确定按钮
  	private Button okBtn;
  	// 终止标记
  	private AtomicBoolean interceptFlag = new AtomicBoolean();
  	// 更新数据
  	private DetectVersionResponse mUpdate;
  	// 版本名称,如1.0.1
  	private String curVersionName;
  	// 包信息
  	private PackageInfo info = null;
  	// 通知对话框
  	private Dialog noticeDialog;
  	// 通知布局
  	private View updateNoticeView;
  	// 机子种类
  	private Integer type;
  
  	/**
  	 * @param mUpdate
  	 *            服务端返回的数据
  	 * @param activity
  	 */
  	public void showUpdateDialog(DetectVersionResponse mUpdate,
  								 BaseActivity activity) {
  		if (type == null) {
  			type = SharedPreferences2Obj.getInstance(activity)
  					.setName("MachineType").getObject("type", Integer.class);
  		}
  		this.mUpdate = mUpdate;
  		interceptFlag.set(false);
  		try {
  			if (info == null) {
  				info = activity.getPackageManager().getPackageInfo(
  						activity.getPackageName(), 0);
  			}
  			if (curVersionName == null) {
  				curVersionName = info.versionName;
  			}
  		} catch (NameNotFoundException e) {
  			e.printStackTrace();
  		}
  		// 关闭并释放释放进度条对话框
  		if (mProDialog != null) {
  			mProDialog.dismiss();
  			mProDialog = null;
  		}
  		isUpdate(activity);
  	}
  
  	/**
  	 * 是否更新,根据版本来判断
  	 */
  	private void isUpdate(BaseActivity activity) {
  		// 显示检测结果
  		if (mUpdate != null) {
  			if (VersionComparison.comparison(curVersionName, mUpdate.getVer())) {
  				try {
  					showNoticeDialog(activity);
  				} catch (Exception e) {
  					e.printStackTrace();
  					Looper.prepare();
  					activity.MToast(activity,
  							activity.getString(R.string.upgrade_failed),
  							Toast.LENGTH_LONG);
  					Looper.loop();
  				}
  			} else {
  				try {
  					showLatestOrFailDialog(DIALOG_TYPE_LATEST,activity);
  				} catch (Exception e) {
  					e.printStackTrace();
  				}
  			}
  		}
  	}
  
  	/**
  	 * 显示'已经是最新'或者'无法获取版本信息'对话框
  	 */
  	private void showLatestOrFailDialog(int dialogType, BaseActivity activity)
  			throws Exception {
  		if (latestOrFailDialog != null) {
  			// 关闭并释放之前的对话框
  			latestOrFailDialog.dismiss();
  			latestOrFailDialog = null;
  		}
  		AlertDialog.Builder builder = new Builder(activity);
  		builder.setTitle(R.string.system_tip);
  		if (dialogType == DIALOG_TYPE_LATEST) {
  			builder.setMessage(R.string.already_is_the_latest);
  			Toast.makeText(activity,
  					activity.getString(R.string.already_is_the_latest),
  					Toast.LENGTH_SHORT).show();
  		} else if (dialogType == DIALOG_TYPE_FAIL) {
  			builder.setMessage(R.string.unable_to_obtain_version_information);
  			Toast.makeText(
  					activity,
  					activity.getString(R.string.unable_to_obtain_version_information),
  					Toast.LENGTH_SHORT).show();
  		}
  		builder.setPositiveButton(R.string.btn_ok, null);
  		AlertDialog alertDialog = builder.create();
  		if (type!=null&&type == DeviceType.HANDSET.getValue()) {
  			alertDialog.getWindow().setType(
  					WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
  		}
  		alertDialog.show();
  	}
  
  	/**
  	 * 显示版本更新通知对话框
  	 */
  	private void showNoticeDialog(final BaseActivity activity) throws Exception {
  		if (noticeDialog == null) {
  			noticeDialog = new Dialog(activity, R.style.dialogTheme);
  		}
  		if (updateNoticeView == null) {
  			updateNoticeView = View.inflate(activity,
  					R.layout.app_update_notice, null);
  			titleText = (TextView) updateNoticeView
  					.findViewById(R.id.titleText);
  			contentText = (TextView) updateNoticeView
  					.findViewById(R.id.contentText);
  			cancleBtn = (Button) updateNoticeView.findViewById(R.id.cancleBtn);
  			okBtn = (Button) updateNoticeView.findViewById(R.id.okBtn);
  			titleText.setText(R.string.version_update);
  		}
  		if (mUpdate.getDesc() != null) {
  			contentText.setText(mUpdate.getDesc());
  		}
  		noticeDialog.setContentView(updateNoticeView);
  		okBtn.setOnClickListener(new View.OnClickListener() {
  			@Override
  			public void onClick(View arg0) {
  				noticeDialog.dismiss();
  				try {
  					showDownloadDialog(activity);
  				} catch (Exception e) {
  					e.printStackTrace();
  					Looper.prepare();
  					activity.MToast(activity,
  							activity.getString(R.string.upgrade_failed),
  							Toast.LENGTH_LONG);
  					Looper.loop();
  				}
  			}
  		});
  		cancleBtn.setOnClickListener(new View.OnClickListener() {
  			@Override
  			public void onClick(View arg0) {
  				noticeDialog.dismiss();
  			}
  		});
  		if (type!=null&&type == DeviceType.HANDSET.getValue()) {
  			noticeDialog.getWindow().setType(
  					WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
  		}
  		noticeDialog.setCanceledOnTouchOutside(false);
  		noticeDialog.setCancelable(true);
  		noticeDialog.show();
  	}
  
  	/**
  	 * 显示下载对话框
  	 */
  	private void showDownloadDialog(BaseActivity activity) throws Exception {
  		AlertDialog.Builder builder = new Builder(activity);
  		builder.setTitle(R.string.downloading_new_version);
  		View v = View.inflate(activity, R.layout.app_update_progress, null);
  		mProgress = (ProgressBar) v.findViewById(R.id.update_progress);
  		mProgressText = (TextView) v.findViewById(R.id.update_progress_text);
  		builder.setView(v);
  		builder.setNegativeButton(R.string.btn_cancel,
  				new DialogInterface.OnClickListener() {
  					@Override
  					public void onClick(DialogInterface dialog, int which) {
  						dialog.dismiss();
  						interceptFlag.set(true);
  					}
  				});
  		builder.setOnCancelListener(new OnCancelListener() {
  			@Override
  			public void onCancel(DialogInterface dialog) {
  				dialog.dismiss();
  				interceptFlag.set(true);
  			}
  		});
  		downloadDialog = builder.create();
  		if (type!=null&&type == DeviceType.HANDSET.getValue()) {
  			downloadDialog.getWindow().setType(
  					WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
  		}
  		downloadDialog.setCanceledOnTouchOutside(false);
  		downloadDialog.show();
  		// 下载
  		downloadApk(new VersionHandler(activity, new VersionInterface() {
  			@Override
  			public void tmpFileSize(String content) {
  				mProgressText.setText(content);
  			}
  
  			@Override
  			public void setProgress(int progress) {
  				mProgress.setProgress(progress);
  			}
  
  			@Override
  			public void dialogDismiss() {
  				downloadDialog.dismiss();
  			}
  		}),activity);
  	}
  
  	/**
  	 * 下载apk
  	 */
  	private void downloadApk(VersionHandler versionHandler,BaseActivity activity) {
  		// 下载线程
  		Thread downLoadThread = new Thread(new DownApkRunnable(activity,
  				mUpdate, versionHandler, interceptFlag, activity.getResources()
  				.getString(R.string.app_name), info));
  		downLoadThread.start();
  	}
  }