From 7e465a7eb0f1dcf2410f061dd2cdab6de9637fd5 Mon Sep 17 00:00:00 2001 From: Daz Jones Date: Thu, 1 Aug 2013 22:13:18 +0100 Subject: [PATCH] Revert "Use newly-provided API level to compare builds." This reverts commit c118ceb80fe104ef938a31d49c9294b2946004a8. --- .../cyanogenmod/updater/UpdatesSettings.java | 4 +- .../cyanogenmod/updater/misc/UpdateInfo.java | 62 +++++++++++++++---- .../updater/service/UpdateCheckService.java | 3 +- src/com/cyanogenmod/updater/utils/Utils.java | 12 ++-- 4 files changed, 60 insertions(+), 21 deletions(-) diff --git a/src/com/cyanogenmod/updater/UpdatesSettings.java b/src/com/cyanogenmod/updater/UpdatesSettings.java index ce95c6627..883a11b6f 100644 --- a/src/com/cyanogenmod/updater/UpdatesSettings.java +++ b/src/com/cyanogenmod/updater/UpdatesSettings.java @@ -555,7 +555,7 @@ private void refreshPreferences(LinkedList updates) { mUpdatesList.removeAll(); // Convert the installed version name to the associated filename - String installedZip = "cm-" + Utils.getInstalledVersion() + ".zip"; + String installedZip = "cm-" + Utils.getInstalledVersion(true) + ".zip"; // Add the updates for (UpdateInfo ui : updates) { @@ -686,7 +686,7 @@ private void showSysInfo() { String time = DateFormat.getTimeFormat(this).format(lastCheck); String message = getString(R.string.sysinfo_device) + " " + Utils.getDeviceType() + "\n\n" - + getString(R.string.sysinfo_running) + " " + Utils.getInstalledVersion() + "\n\n" + + getString(R.string.sysinfo_running) + " " + Utils.getInstalledVersion(true) + "\n\n" + getString(R.string.sysinfo_last_check) + " " + date + " " + time; AlertDialog.Builder builder = new AlertDialog.Builder(this) diff --git a/src/com/cyanogenmod/updater/misc/UpdateInfo.java b/src/com/cyanogenmod/updater/misc/UpdateInfo.java index 03d10bfde..cdd03fbc5 100644 --- a/src/com/cyanogenmod/updater/misc/UpdateInfo.java +++ b/src/com/cyanogenmod/updater/misc/UpdateInfo.java @@ -31,8 +31,8 @@ public enum Type { private String mUiName; private String mFileName; + private String mVersion; private Type mType; - private int mApiLevel; private long mBuildDate; private String mDownloadUrl; private String mMd5Sum; @@ -40,11 +40,10 @@ public enum Type { private Boolean mIsNewerThanInstalled; - public UpdateInfo(String fileName, long date, int apiLevel, String url, + public UpdateInfo(String fileName, long date, String url, String md5, Type type, String changeLog) { initializeName(fileName); mBuildDate = date; - mApiLevel = apiLevel; mDownloadUrl = url; mMd5Sum = md5; mType = type; @@ -52,7 +51,7 @@ public UpdateInfo(String fileName, long date, int apiLevel, String url, } public UpdateInfo(String fileName, String changeLog) { - this(fileName, 0, 0, null, null, Type.UNKNOWN, changeLog); + this(fileName, 0, null, null, Type.UNKNOWN, changeLog); } private UpdateInfo(Parcel in) { @@ -73,6 +72,13 @@ public String getName() { return mUiName; } + /** + * Get version + */ + public String getVersion() { + return mVersion; + } + /** * Get file name */ @@ -117,23 +123,57 @@ public boolean isNewerThanInstalled() { return mIsNewerThanInstalled; } - int installedApiLevel = Utils.getInstalledApiLevel(); - if (installedApiLevel != mApiLevel && mApiLevel > 0) { - mIsNewerThanInstalled = mApiLevel > installedApiLevel; - } else { - // API levels match, so compare build dates. + int[] installedVersion = canonicalizeVersion(Utils.getInstalledVersion(false)); + int[] ourVersion = canonicalizeVersion(mVersion); + + if (installedVersion.length < ourVersion.length) { + installedVersion = Arrays.copyOf(installedVersion, ourVersion.length); + } else if (installedVersion.length > ourVersion.length) { + ourVersion = Arrays.copyOf(ourVersion, installedVersion.length); + } + + for (int i = 0; i < ourVersion.length; i++) { + if (ourVersion[i] > installedVersion[i]) { + mIsNewerThanInstalled = true; + break; + } + if (ourVersion[i] < installedVersion[i]) { + mIsNewerThanInstalled = false; + break; + } + } + + if (mIsNewerThanInstalled == null) { + // Version strings match, so compare build dates. mIsNewerThanInstalled = mBuildDate > Utils.getInstalledBuildDate(); } return mIsNewerThanInstalled; } + private int[] canonicalizeVersion(String versionString) { + String[] parts = versionString.split("\\."); + int[] version = new int[parts.length]; + + for (int i = 0; i < parts.length; i++) { + try { + version[i] = Integer.valueOf(parts[i]); + } catch (NumberFormatException e) { + version[i] = 0; + } + } + + return version; + } + private void initializeName(String fileName) { mFileName = fileName; if (!TextUtils.isEmpty(fileName)) { mUiName = extractUiName(fileName); + mVersion = fileName.replaceAll(".*?([0-9.]+?)-.+","$1"); } else { mUiName = null; + mVersion = null; } } @@ -185,8 +225,8 @@ public int describeContents() { public void writeToParcel(Parcel out, int flags) { out.writeString(mUiName); out.writeString(mFileName); + out.writeString(mVersion); out.writeString(mType.toString()); - out.writeInt(mApiLevel); out.writeLong(mBuildDate); out.writeString(mDownloadUrl); out.writeString(mMd5Sum); @@ -196,8 +236,8 @@ public void writeToParcel(Parcel out, int flags) { private void readFromParcel(Parcel in) { mUiName = in.readString(); mFileName = in.readString(); + mVersion = in.readString(); mType = Enum.valueOf(Type.class, in.readString()); - mApiLevel = in.readInt(); mBuildDate = in.readLong(); mDownloadUrl = in.readString(); mMd5Sum = in.readString(); diff --git a/src/com/cyanogenmod/updater/service/UpdateCheckService.java b/src/com/cyanogenmod/updater/service/UpdateCheckService.java index 93454970f..6fd061fbc 100644 --- a/src/com/cyanogenmod/updater/service/UpdateCheckService.java +++ b/src/com/cyanogenmod/updater/service/UpdateCheckService.java @@ -333,7 +333,6 @@ private UpdateInfo parseUpdateJSONObject(JSONObject obj, int updateType, String fileName = obj.getString("filename"); String url = obj.getString("url"); String md5 = obj.getString("md5sum"); - int apiLevel = obj.getInt("api_level"); long timestamp = obj.getLong("timestamp"); String typeString = obj.getString("channel"); UpdateInfo.Type type; @@ -350,7 +349,7 @@ private UpdateInfo parseUpdateJSONObject(JSONObject obj, int updateType, type = UpdateInfo.Type.UNKNOWN; } - UpdateInfo ui = new UpdateInfo(fileName, timestamp, apiLevel, url, md5, type, null); + UpdateInfo ui = new UpdateInfo(fileName, timestamp, url, md5, type, null); boolean includeAll = updateType == Constants.UPDATE_TYPE_ALL_STABLE || updateType == Constants.UPDATE_TYPE_ALL_NIGHTLY; diff --git a/src/com/cyanogenmod/updater/utils/Utils.java b/src/com/cyanogenmod/updater/utils/Utils.java index bca2c8a98..a15dcb217 100644 --- a/src/com/cyanogenmod/updater/utils/Utils.java +++ b/src/com/cyanogenmod/updater/utils/Utils.java @@ -57,12 +57,12 @@ public static String getDeviceType() { return SystemProperties.get("ro.cm.device"); } - public static String getInstalledVersion() { - return SystemProperties.get("ro.cm.version"); - } - - public static int getInstalledApiLevel() { - return SystemProperties.getInt("ro.build.version.sdk", 0); + public static String getInstalledVersion(boolean fullVersionString) { + String version = SystemProperties.get("ro.cm.version"); + if (!fullVersionString) { + version = version.replaceAll("([0-9.]+?)-.+","$1"); + } + return version; } public static long getInstalledBuildDate() {