Blame view

src/com/ectrip/cyt/version/UpdateManager.java 5.28 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
  package com.ectrip.cyt.version;
  
  import android.app.ProgressDialog;
  import android.content.pm.PackageInfo;
  import android.content.pm.PackageManager.NameNotFoundException;
  import android.view.WindowManager;
  import android.widget.Toast;
  
  import com.alibaba.fastjson.JSON;
  import com.ectrip.cyt.base.DataTrans;
  import com.ectrip.cyt.callback.HttpCallback;
  import com.ectrip.cyt.config.MyApp;
  import com.ectrip.cyt.constant.DeviceType;
  import com.ectrip.cyt.constant.OrderCode;
  import com.ectrip.cyt.response.DetectVersionResponse;
  import com.ectrip.cyt.ui.BaseActivity;
  import com.ectrip.cyt.utils.SharedPreferences2Obj;
  import com.ectrip.trips.check.R;
  import com.ectrip.trips.net.DataTool;
  import com.ectrip.trips.net.HttpHelper;
  
  /**
   * @author jigo 在线升级
   */
  public class UpdateManager {
  	private final int DIALOG_TYPE_LATEST = 0;
  	private final int DIALOG_TYPE_FAIL = 1; // 获取失败的情况
  
  	private static UpdateManager updateManager;
  	private Integer type; // 机子种类
  
  	public static UpdateManager getUpdateManager() {
  		if (updateManager == null) {
  			updateManager = new UpdateManager();
  		}
  		return updateManager;
  	}
  
  	/**
  	 * @param activity
  	 * @param url
  	 * @param signkey
  	 *            标识
  	 * @param identity
  	 *            编码
  	 */
  	public void checkAppUpdate(BaseActivity activity, String url,
  							   String signkey, String identity) {
  		if (type == null) {
  			type = SharedPreferences2Obj.getInstance(activity)
  					.setName("MachineType").getObject("type", Integer.class);
  		}
  		try {
  			PackageInfo info = activity.getPackageManager().getPackageInfo(
  					activity.getPackageName(), 0);
  			HttpHelper.getInstance(activity).versionUpdate(info.versionName,
  					signkey, identity, url,
  					new GetVersionHttpCallback(activity));
  		} catch (NameNotFoundException e) {
  			e.printStackTrace(System.err);
  		} catch (Exception e) {
  			e.printStackTrace(System.err);
  		}
  	}
  
  	/**
  	 * @author jigo 获取在线升级信息
  	 */
  	public class GetVersionHttpCallback extends HttpCallback<DataTrans> {
  		private String signed;
  		private String data;
  		private BaseActivity activity;
  		private ProgressDialog dialog = null;
  
  		public GetVersionHttpCallback(BaseActivity activity) {
  			this.activity = activity;
  		}
  
  		@Override
  		public void onPreCallback() {
  			if (dialog == null) {
  				// 显示ProgressDialog
  				dialog = new ProgressDialog(activity);
  				dialog.setMessage("正在检查更新...");
  				dialog.setCanceledOnTouchOutside(false);
  				dialog.setCancelable(true);
  				if (type!=null&&type == DeviceType.HANDSET.getValue()) {
  					dialog.getWindow().setType(
  							WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
  				}
  				try {
  					if (dialog != null && !dialog.isShowing()) {
  						dialog.show();
  					}
  				} catch (Exception e) {
  					e.printStackTrace();
  				}
  			} else {
  				dialog.setMessage(activity.getString(R.string.wait));
  			}
  		}
  
  		@Override
  		public void onCompletedCallback(DataTrans result) {
  
  		}
  
  		@Override
  		public void afterCompletedCallback(DataTrans result) {
  			if (dialog != null && dialog.isShowing()) {
  				dialog.dismiss();
  			}
  			if (result != null) {
  				signed = result.getSigned();
  				data = result.getData();
  				if (result.getData() != null && result.getData() != null) {
  					DetectVersionResponse versionResponse = JSON.parseObject(
  							result.getData(), DetectVersionResponse.class);
  					String code = versionResponse.getCode();
  					if (code != null
  							&& OrderCode.SUCESS.getValue().equals(code)) {
  						if (signed != null
  								&& (MyApp.getInstance().getSignkey().equals("") || signed
  								.equals(DataTool.getSign(MyApp
  												.getInstance().getSignkey(),
  										data)))) {
  							if (versionResponse != null
  									&& versionResponse.getUrl() != null
  									&& versionResponse.getVer() != null) {
  								VersionUpdateUtil versionUpdateUtil = new VersionUpdateUtil();
  								versionUpdateUtil.showUpdateDialog(
  										versionResponse, activity);
  							}
  						} else {
  							activity.MToast(activity,
  									activity.getString(R.string.sign_not_pass),
  									Toast.LENGTH_SHORT);
  						}
  					} else {
  						if (versionResponse.getDescribe() != null) {
  							activity.MToast(activity,
  									versionResponse.getDescribe(),
  									Toast.LENGTH_SHORT);
  						}
  					}
  				}
  			} else {
  				showLatestOrFailDialog(DIALOG_TYPE_FAIL, activity);
  			}
  		}
  
  		@Override
  		public void onFailureCallback(String FailureStr) {
  			if (dialog != null && dialog.isShowing()) {
  				dialog.dismiss();
  			}
  
  			if (activity.getString(R.string.server_error).equals(FailureStr)) {
  				activity.MToast(activity,
  						activity.getString(R.string.server_error),
  						Toast.LENGTH_SHORT);
  			} else {
  				activity.MToast(activity, FailureStr, Toast.LENGTH_SHORT);
  			}
  		}
  
  		@Override
  		public void onStopCallback() {
  
  		}
  
  		@Override
  		public void onAgainParseCallback(Object parse) {
  
  		}
  	}
  
  	/**
  	 * 显示'已经是最新'或者'无法获取版本信息'
  	 */
  	private void showLatestOrFailDialog(int dialogType, BaseActivity activity) {
  		if (dialogType == DIALOG_TYPE_LATEST) {
  			activity.MToast(activity,
  					activity.getString(R.string.already_is_the_latest),
  					Toast.LENGTH_SHORT);
  		} else if (dialogType == DIALOG_TYPE_FAIL) {
  			activity.MToast(activity, activity
  							.getString(R.string.unable_to_obtain_version_information),
  					Toast.LENGTH_SHORT);
  		}
  	}
  }