Skip to content

Commit

Permalink
Merge pull request #136 from dhebbeker/feature/version-number-tolerance
Browse files Browse the repository at this point in the history
render version string countability more imperturbable
  • Loading branch information
javiersantos authored Jul 5, 2018
2 parents 8977181 + 6e43d72 commit ed54068
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,22 @@ static Integer getAppInstalledVersionCode(Context context) {
}

static Boolean isUpdateAvailable(Update installedVersion, Update latestVersion) {
Boolean res = false;

if (latestVersion.getLatestVersionCode() != null && latestVersion.getLatestVersionCode() > 0) {
return latestVersion.getLatestVersionCode() > installedVersion.getLatestVersionCode();
} else {
if (!TextUtils.equals(installedVersion.getLatestVersion(), "0.0.0.0") && !TextUtils.equals(latestVersion.getLatestVersion(), "0.0.0.0")) {
Version installed = new Version(installedVersion.getLatestVersion());
Version latest = new Version(latestVersion.getLatestVersion());
res = installed.compareTo(latest) < 0;
}
try
{
final Version installed = new Version(installedVersion.getLatestVersion());
final Version latest = new Version(latestVersion.getLatestVersion());
return installed.compareTo(latest) < 0;
} catch (Exception e)
{
e.printStackTrace();
return false;
}
} else return false;
}

return res;
}

static Boolean isStringAVersion(String version) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.javiersantos.appupdater.objects;

import android.support.annotation.NonNull;
import android.util.Log;

public class Version implements Comparable<Version> {
private String version;
Expand All @@ -10,14 +9,14 @@ public final String get() {
return this.version;
}

public Version(String version) {
final String TAG = "AppUpdater";
if (version == null)
Log.e(TAG, "Version can not be null");
version = version.replaceAll("[^0-9?!\\.]", "");
if (!version.matches("[0-9]+(\\.[0-9]+)*"))
Log.e(TAG, "Invalid version format");
this.version = version;
public Version(@NonNull final String version) throws Exception
{
String trimmedVersion = version.replaceAll("[^0-9?!\\.]", "");
// replace all empty version number-parts with zeros
trimmedVersion = trimmedVersion.replaceAll("\\.(\\.|$)", "\\.0$1");
if (!trimmedVersion.matches("[0-9]+(\\.[0-9]+)*"))
throw new Exception("Invalid version format. Original: `" + version + "` trimmed: `" + trimmedVersion + "`");
this.version = trimmedVersion;
}

@Override
Expand Down

0 comments on commit ed54068

Please sign in to comment.