package com.anyun.exam.lib; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.Signature; import android.os.Handler; import android.util.Log; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.nio.file.Path; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.List; import java.util.Locale; public class Upgrade { public static final String TAG = Upgrade.class.getCanonicalName(); private static final String newProgramIndUrl = "http://upgrade.safeluck.com/ftp/aykj4/aks3.json"; private static Upgrade instance = null; private Context context; private DownloadManagerUtil downloadManagerUtil = null; private InstallUtil installUtil = null; public static Upgrade getInstance(Context context) { if (instance == null) { synchronized (Upgrade.class) { if (instance == null) { instance = new Upgrade(context); instance.context = context; } } } return instance; } private Upgrade(Context context) { this.context = context; installUtil = InstallUtil.getInstance(context); downloadManagerUtil = DownloadManagerUtil.getInstance(context); downloadManagerUtil.SetDMCallback(new DMCB()); } private String CheckNewVersion(String urlStr) { HttpURLConnection uRLConnection = null; InputStream is = null; BufferedReader buffer = null; String result = null; try { URL url = new URL(urlStr); uRLConnection = (HttpURLConnection) url.openConnection(); uRLConnection.setConnectTimeout(8000); uRLConnection.setReadTimeout(8000); uRLConnection.setRequestMethod("GET"); is = uRLConnection.getInputStream(); buffer = new BufferedReader(new InputStreamReader(is)); StringBuilder strBuilder = new StringBuilder(); String line; while ((line = buffer.readLine()) != null) { strBuilder.append(line); } result = strBuilder.toString(); } catch (Exception e) { e.printStackTrace(); Log.e(TAG, "http post error"); } finally { if (buffer != null) { try { buffer.close(); } catch (IOException ignored) { } } if (is != null) { try { is.close(); } catch (IOException ignored) { } } if (uRLConnection != null) { uRLConnection.disconnect(); } } return result; } class DfuFileInfo { boolean valid = false; int appVerCode = 0; String appUrl = null; int systemVerCode = 0; String systemUrl = null; public void setValid(boolean en) { this.valid = en; } public void setApp(int code, String url) { this.appVerCode = code; this.appUrl = url; } public void setSystem(int code, String url) { this.systemVerCode = code; this.systemUrl = url; } public boolean getValid() { return valid; } public int getAppVerCode() { return appVerCode; } public String getAppUrl() { return appUrl; } public int getSystemVerCode() { return systemVerCode; } public String getSystemUrl() { return systemUrl; } } private DfuFileInfo parseJson(String result, int province, int city) { Log.d(TAG, "parseJson province " + province + " city " + city); DfuFileInfo dfuFileInfo = new DfuFileInfo(); try { JSONObject obj = new JSONObject(result); JSONArray jsonarray = obj.getJSONArray("province"); boolean marchProvince = false; for (int i = 0; !marchProvince && i < jsonarray.length(); i++) { JSONObject obj2 = jsonarray.getJSONObject(i); int pid = obj2.getInt("id"); Log.d(TAG, "pid = " + pid); if (pid == province) { JSONArray jsonarray2 = obj2.getJSONArray("city"); boolean marchCity = false; // 匹配省id成功 marchProvince = true; for (int j = 0; !marchCity && j < jsonarray2.length(); j++) { // 遍历市id组 JSONObject obj3 = jsonarray2.getJSONObject(j); JSONArray jsonarray3 = obj3.getJSONArray("id"); marchCity = false; for (int k = 0; k < jsonarray3.length(); k++) { // 遍历市id int cid = jsonarray3.getInt(k); Log.d(TAG, "cid = " + cid); if (cid == 0) { Log.d(TAG, "March GeneralCity"); dfuFileInfo.setValid(true); if (obj3.has("appVersionCode") && obj3.has("appUpgradeUrl")) { dfuFileInfo.setApp(obj3.getInt("appVersionCode"), obj3.getString("appUpgradeUrl")); } if (obj3.has("systemVersionCode") && obj3.has("systemUpgradeUrl")) { dfuFileInfo.setSystem(obj3.getInt("systemVersionCode"), obj3.getString("systemUpgradeUrl")); } } if (cid == city) { marchCity = true; Log.d(TAG, "March City"); dfuFileInfo.setValid(true); if (obj3.has("appVersionCode") && obj3.has("appUpgradeUrl")) { dfuFileInfo.setApp(obj3.getInt("appVersionCode"), obj3.getString("appUpgradeUrl")); } if (obj3.has("systemVersionCode") && obj3.has("systemUpgradeUrl")) { dfuFileInfo.setSystem(obj3.getInt("systemVersionCode"), obj3.getString("systemUpgradeUrl")); } break; } } } } } Log.d(TAG, "Parse End"); } catch (JSONException e) { Log.e(TAG, e.getMessage()); dfuFileInfo.setValid(false); } return dfuFileInfo; } class CheckNewVersionThread implements Runnable { @Override public void run() { Log.d(TAG, "检查升级站点"); String result = CheckNewVersion(newProgramIndUrl); if (result != null) { DfuFileInfo dfuFileInfo = parseJson(result, 53, 100); if (dfuFileInfo != null && dfuFileInfo.getValid()) { if (dfuFileInfo.getAppVerCode() > 0 && dfuFileInfo.getAppUrl() != null) { if (installUtil.getVerCode() < dfuFileInfo.getAppVerCode()) { Log.d(TAG, "下载地址 " + dfuFileInfo.getAppUrl()); downloadManagerUtil.DownloadFile(dfuFileInfo.getAppUrl(), "aks3.apk", "aks3", "驾考app升级"); // downloadManagerUtil.DownloadFile("https://ftp-idc.pconline.com.cn/pub/download/201010/AutoCAD2007.rar?md5=KeZ0iEmKKGEWJ5c7OVxk_w&expires=1592964150", "aks3.apk", "aks3", "驾考app升级"); } } if (dfuFileInfo.getSystemVerCode() > 0 && dfuFileInfo.getSystemUrl() != null) { } } } handlerCheckNewVersion.removeCallbacks(runnableCheckNewVersion); handlerCheckNewVersion.postDelayed(runnableCheckNewVersion, 30*60*1000); } } Handler handlerCheckNewVersion = new Handler(); Runnable runnableCheckNewVersion = new Runnable() { @Override public void run() { new Thread(new CheckNewVersionThread()).start(); } }; public void CheckUpgrade() { handlerCheckNewVersion.removeCallbacks(runnableCheckNewVersion); handlerCheckNewVersion.postDelayed(runnableCheckNewVersion, 100); } public void ManualUpgrade(String path) { installUtil.InstallAppNormal(context, path); } class DMCB implements DownloadManagerCallback { @Override public void DownloadComplete(String title, String path) { if (title != null && path != null) { if (title.equals("aks3")) { Signature[] sig = installUtil.getSignature(); Signature[] sig2 = installUtil.getSignature(path); if (sig != null && sig2 != null && installUtil.getFingerprint(sig[0], "SHA-1").equals(installUtil.getFingerprint(sig2[0], "SHA-1"))) { if (RemoteService.mAyDevice) { installUtil.InstallApp(path); } else { installUtil.InstallAppNormal(context, path); } } } } } } }