package cn.com.magnity.magnitycx.upgrade; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.res.Configuration; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.support.v4.content.FileProvider; import cn.com.magnity.magnitycx.MagApplication; import cn.com.magnity.magnitycx.MainActivity; import cn.com.magnity.magnitycx.R; import cn.com.magnity.magnitycx.log.Logging; import java.io.File; import java.util.Locale; import org.apache.log4j.Priority; import org.json.JSONArray; import org.json.JSONObject; /* loaded from: classes.dex */ public class CheckVersionTask implements Runnable { private static final int CHECK_ERROR = -1; private static final int CHECK_OK = 0; private static final String UPGRADEINFO_DESC = "description"; private static final String UPGRADEINFO_FORCE = "forceUpgrade"; private static final String UPGRADEINFO_URL = "url"; private static final String UPGRADEINFO_VERSIONCODE = "versionCode"; private static final String UPGRADEINFO_VERSIONNAME = "versionName"; public static final int UPGRADE_BEGIN = 1; private static final int UPGRADE_ERROR = -2; public static final int UPGRADE_FINISHED = 3; public static final int UPGRADE_RECEIVED_SIZE = 2; private Context ctx_; private boolean isForceUpgrade_; private int localVersion_; Handler handler_ = new Handler() { // from class: cn.com.magnity.magnitycx.upgrade.CheckVersionTask.1 @Override // android.os.Handler public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case -2: if (CheckVersionTask.this.progressDialog_ != null) { CheckVersionTask.this.progressDialog_.dismiss(); CheckVersionTask.this.progressDialog_ = null; } AlertDialog.Builder builder = new AlertDialog.Builder(CheckVersionTask.this.ctx_); builder.setTitle(R.string.upgradeError); builder.setPositiveButton(R.string.labelOK, new DialogInterface.OnClickListener() { // from class: cn.com.magnity.magnitycx.upgrade.CheckVersionTask.1.1 @Override // android.content.DialogInterface.OnClickListener public void onClick(DialogInterface dialog, int which) { CheckVersionTask.this.unlockScreen(); } }); AlertDialog dialog = builder.create(); dialog.setCanceledOnTouchOutside(false); dialog.show(); break; case -1: CheckVersionTask.this.unlockScreen(); break; case 0: CheckVersionTask.this.lockScreen(); Bundle bundle = msg.getData(); CheckVersionTask.this.isForceUpgrade_ = bundle.getBoolean(CheckVersionTask.UPGRADEINFO_FORCE); CheckVersionTask.this.showUpgradeDialog(bundle.getString(CheckVersionTask.UPGRADEINFO_DESC), bundle.getString(CheckVersionTask.UPGRADEINFO_URL), bundle.getString(CheckVersionTask.UPGRADEINFO_VERSIONNAME), bundle.getInt(CheckVersionTask.UPGRADEINFO_VERSIONCODE), CheckVersionTask.this.isForceUpgrade_); break; case 1: CheckVersionTask.this.progressDialog_ = new ProgressDialog(CheckVersionTask.this.ctx_); CheckVersionTask.this.progressDialog_.setOnCancelListener(new DialogInterface.OnCancelListener() { // from class: cn.com.magnity.magnitycx.upgrade.CheckVersionTask.1.2 @Override // android.content.DialogInterface.OnCancelListener public void onCancel(DialogInterface dialog2) { CheckVersionTask.this.stopDownloadApk(); CheckVersionTask.this.unlockScreen(); if (CheckVersionTask.this.isForceUpgrade_ && (CheckVersionTask.this.ctx_ instanceof MainActivity)) { ((MainActivity) CheckVersionTask.this.ctx_).onBackPressed(); } } }); CheckVersionTask.this.progressDialog_.setProgressStyle(1); CheckVersionTask.this.progressDialog_.setMessage(CheckVersionTask.this.ctx_.getResources().getString(R.string.upgrading)); CheckVersionTask.this.progressDialog_.setProgressNumberFormat("%1d KB/%2d KB"); CheckVersionTask.this.progressDialog_.setProgress(0); CheckVersionTask.this.progressDialog_.setMax(msg.arg1 / 1000); CheckVersionTask.this.progressDialog_.setCanceledOnTouchOutside(false); CheckVersionTask.this.progressDialog_.show(); break; case 2: if (CheckVersionTask.this.progressDialog_ != null) { CheckVersionTask.this.progressDialog_.setProgress(msg.arg1 / 1000); break; } break; case 3: if (CheckVersionTask.this.progressDialog_ != null) { CheckVersionTask.this.progressDialog_.dismiss(); CheckVersionTask.this.progressDialog_ = null; } CheckVersionTask.this.unlockScreen(); if (msg.obj != null && (msg.obj instanceof File)) { CheckVersionTask.this.installApk((File) msg.obj); break; } break; } } }; private ProgressDialog progressDialog_ = null; private Thread threadUpgrading_ = null; public CheckVersionTask(Context ctx, int localVersion) { this.ctx_ = ctx; this.localVersion_ = localVersion; } public void stopDownload() { stopDownloadApk(); } public UpgradeInfo parseVersionInfo(String str) throws Exception { UpgradeInfo info = null; JSONObject object = new JSONObject(str); if (object != null && object.length() >= 1) { String productType = this.ctx_.getString(R.string.upgradeID); if (object.has(productType)) { JSONArray array = object.getJSONArray(productType); if (array.length() > 0) { JSONObject obj = array.getJSONObject(array.length() - 1); info = new UpgradeInfo(); if (obj.has("verName")) { info.setVersionName(obj.getString("verName")); } if (obj.has("verCode")) { info.setVersionCode(obj.getInt("verCode")); } if (obj.has("apkUrl")) { info.setApkUrl(obj.getString("apkUrl")); } try { Configuration config = this.ctx_.getResources().getConfiguration(); if (!config.locale.getLanguage().equals(Locale.CHINA.getLanguage()) || !config.locale.getCountry().equals(Locale.CHINA.getCountry())) { if (obj.has("description-en")) { info.setDescription(obj.getString("description-en")); } } else if (obj.has(UPGRADEINFO_DESC)) { info.setDescription(obj.getString(UPGRADEINFO_DESC)); } } catch (Exception e) { } if (obj.has(UPGRADEINFO_FORCE)) { info.setForceUpgrade(obj.getBoolean(UPGRADEINFO_FORCE)); } } } } return info; } /* JADX INFO: Access modifiers changed from: private */ public void lockScreen() { if (this.ctx_ instanceof Activity) { ((Activity) this.ctx_).setRequestedOrientation(14); } } /* JADX INFO: Access modifiers changed from: private */ public void unlockScreen() { if (this.ctx_ instanceof Activity) { ((Activity) this.ctx_).setRequestedOrientation(10); } } @Override // java.lang.Runnable public void run() { String path = this.ctx_.getResources().getString(R.string.remote_url); try { String str = HttpHelper.GetRemoteVersionInfo(path, 10000, Priority.INFO_INT); UpgradeInfo upgradeInfo = parseVersionInfo(str); if (upgradeInfo == null) { Logging.info("Parse version info fail."); this.handler_.sendEmptyMessage(-1); } else if (upgradeInfo.getVersionCode() > this.localVersion_) { Logging.info("New app version is detected(" + upgradeInfo.getVersionCode() + ")"); Message msg = new Message(); msg.what = 0; Bundle bundle = new Bundle(); bundle.putBoolean(UPGRADEINFO_FORCE, upgradeInfo.isForceUpgrade()); bundle.putString(UPGRADEINFO_DESC, upgradeInfo.getDescription()); bundle.putString(UPGRADEINFO_URL, upgradeInfo.getApkUrl()); bundle.putString(UPGRADEINFO_VERSIONNAME, upgradeInfo.getVersionName()); bundle.putInt(UPGRADEINFO_VERSIONCODE, upgradeInfo.getVersionCode()); msg.setData(bundle); this.handler_.sendMessage(msg); } } catch (Exception ex) { this.handler_.sendEmptyMessage(-1); Logging.error("Check upgrade error(" + ex.getMessage() + ")"); } } /* JADX INFO: Access modifiers changed from: private */ public void showUpgradeDialog(String description, final String url, final String versionName, final int versionCode, boolean isForceUpgrade) { AlertDialog.Builder builer = new AlertDialog.Builder(this.ctx_); builer.setTitle(this.ctx_.getResources().getString(R.string.upgrade) + "(V" + versionName + ")"); builer.setMessage(description); builer.setPositiveButton(R.string.labelOK, new DialogInterface.OnClickListener() { // from class: cn.com.magnity.magnitycx.upgrade.CheckVersionTask.2 @Override // android.content.DialogInterface.OnClickListener public void onClick(DialogInterface dialog, int which) { String path = MagApplication.magParameter.otherDir + File.separator + "MAG-Cx." + versionName + ".apk"; File file = new File(path); if (file.exists()) { PackageManager pm = CheckVersionTask.this.ctx_.getPackageManager(); PackageInfo packInfo = pm.getPackageArchiveInfo(path, 1); if (packInfo == null || packInfo.versionCode != versionCode) { CheckVersionTask.this.startDownloadApk(url, versionName); return; } else { CheckVersionTask.this.installApk(file); return; } } CheckVersionTask.this.startDownloadApk(url, versionName); } }); if (!isForceUpgrade) { builer.setNegativeButton(R.string.labelCancel, new DialogInterface.OnClickListener() { // from class: cn.com.magnity.magnitycx.upgrade.CheckVersionTask.3 @Override // android.content.DialogInterface.OnClickListener public void onClick(DialogInterface dialog, int which) { CheckVersionTask.this.unlockScreen(); } }); } else { builer.setOnCancelListener(new DialogInterface.OnCancelListener() { // from class: cn.com.magnity.magnitycx.upgrade.CheckVersionTask.4 @Override // android.content.DialogInterface.OnCancelListener public void onCancel(DialogInterface dialog) { if (CheckVersionTask.this.ctx_ instanceof MainActivity) { ((MainActivity) CheckVersionTask.this.ctx_).onBackPressed(); } } }); } AlertDialog dialog = builer.create(); dialog.setCanceledOnTouchOutside(false); dialog.show(); } /* JADX INFO: Access modifiers changed from: private */ public void startDownloadApk(final String url, final String version) { HttpHelper.isCancelled = false; this.threadUpgrading_ = new Thread() { // from class: cn.com.magnity.magnitycx.upgrade.CheckVersionTask.5 @Override // java.lang.Thread, java.lang.Runnable public void run() { try { File file = HttpHelper.getRemoteApk(url, version, 10000, 30000, CheckVersionTask.this.handler_); if (file == null) { CheckVersionTask.this.handler_.sendEmptyMessage(-2); } else { CheckVersionTask.this.handler_.sendMessage(CheckVersionTask.this.handler_.obtainMessage(3, file)); } } catch (Exception ex) { Logging.error("Upgrading error(" + ex.getMessage() + ")"); CheckVersionTask.this.handler_.sendEmptyMessage(-2); } } }; this.threadUpgrading_.start(); } /* JADX INFO: Access modifiers changed from: private */ public void stopDownloadApk() { HttpHelper.isCancelled = true; try { this.threadUpgrading_.join(500L); this.threadUpgrading_ = null; } catch (Exception e) { this.threadUpgrading_ = null; } } private File getOthersStoragePath(Context context) { File file; String state = Environment.getExternalStorageState(); if ("mounted".equals(state) && (file = new File(Environment.getExternalStorageDirectory(), "magnity/Cx/others")) != null) { if (!file.exists()) { file.mkdirs(); return file; } return file; } return null; } /* JADX INFO: Access modifiers changed from: private */ public void installApk(File file) { Intent installIntent = new Intent("android.intent.action.VIEW"); installIntent.setFlags(268435456); File usedFile = new File(getOthersStoragePath(this.ctx_), file.getName()); if (Build.VERSION.SDK_INT >= 24) { installIntent.setFlags(1); Uri contentUri = FileProvider.getUriForFile(this.ctx_, "cn.com.magnity.magnitycx.fileprovider", usedFile); installIntent.setDataAndType(contentUri, "application/vnd.android.package-archive"); } else { installIntent.setDataAndType(Uri.fromFile(usedFile), "application/vnd.android.package-archive"); } this.ctx_.startActivity(installIntent); } }