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 { 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); } } }