VersionUpdateUtil.java 7.81 KB
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();
	}
}