From ca16c5c81524d72537e0062053177afa19add784 Mon Sep 17 00:00:00 2001 From: Viktor Kojouharov Date: Wed, 15 Jul 2015 20:24:12 +0300 Subject: [PATCH] Upgrade the version checker to handle -beta and -rc suffixes ++version --- gearshift/build.gradle | 4 +- .../sugr/gearshift/GearShiftApplication.java | 47 +++++++++++++++---- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/gearshift/build.gradle b/gearshift/build.gradle index 5625c757..247e7d23 100644 --- a/gearshift/build.gradle +++ b/gearshift/build.gradle @@ -7,8 +7,8 @@ android { defaultConfig { minSdkVersion 16 targetSdkVersion 22 - versionCode 82 - versionName "0.9.99" + versionCode 83 + versionName "1.0-rc1" applicationId "org.sugr.gearshift" } diff --git a/gearshift/src/main/java/org/sugr/gearshift/GearShiftApplication.java b/gearshift/src/main/java/org/sugr/gearshift/GearShiftApplication.java index 45bc6d8d..616988e3 100644 --- a/gearshift/src/main/java/org/sugr/gearshift/GearShiftApplication.java +++ b/gearshift/src/main/java/org/sugr/gearshift/GearShiftApplication.java @@ -7,14 +7,13 @@ import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonArrayRequest; -import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import org.json.JSONArray; import org.json.JSONObject; -import java.io.ByteArrayInputStream; -import java.util.List; +import java.util.ArrayList; +import java.util.Arrays; public class GearShiftApplication extends Application { private static boolean activityVisible; @@ -100,24 +99,56 @@ public void checkForUpdates(final OnUpdateCheck onUpdateCheck) { requestQueue.add(request); } - public Integer versionCompare(String str1, String str2) { + public static Integer versionCompare(String str1, String str2) { + if (str1.equals(str2)) { + return 0; + } + String[] vals1 = str1.split("\\."); String[] vals2 = str2.split("\\."); + ArrayList a1, a2; + a1 = new ArrayList<>(Arrays.asList(vals1)); + a2 = new ArrayList<>(Arrays.asList(vals2)); + + formatPrerelease(a1); + formatPrerelease(a2); + int i = 0; // set index to first non-equal ordinal or length of shortest version string - while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) { + while (i < a1.size() && i < a2.size() && a1.get(i).equals(a2.get(i))) { i++; } // compare first non-equal ordinal number - if (i < vals1.length && i < vals2.length) { - int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i])); + if (i < a1.size() && i < a2.size()) { + + int diff = Integer.valueOf(a1.get(i)).compareTo(Integer.valueOf(a2.get(i))); return Integer.signum(diff); } else { // the strings are equal or one string is a substring of the other // e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4" - return Integer.signum(vals1.length - vals2.length); + return Integer.signum(a1.size() - a2.size()); + } + } + + private static void formatPrerelease(ArrayList version) { + String last = version.get(version.size() - 1); + if (last.contains("-")) { + version.remove(version.size() - 1); + version.add(last.substring(0, last.indexOf("-"))); + + if (last.contains("-beta")) { + version.add("0"); + version.add(last.substring(last.indexOf("-") + 5)); + } else if (last.contains("-rc")) { + version.add("1"); + version.add(last.substring(last.indexOf("-") + 3)); + } else { + version.add("-1"); + } + } else { + version.add("2"); } }