Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9093c5b4 authored by Sooraj S's avatar Sooraj S 👽
Browse files

Use helper function for parsing version code

Fix: Handle use case 1.0 is bigger than 0.9 Semantic versioning
parent 0799c9f7
Loading
Loading
Loading
Loading
+25 −16
Original line number Diff line number Diff line
@@ -98,27 +98,17 @@ public class Utils {
    }

    public static boolean isCompatible(UpdateBaseInfo update) {
        String updateVersion = update.getVersion();
        String[] updateVersionParts = updateVersion.split(Pattern.quote("."));
        int updateMajorVersion = Integer.parseInt(updateVersionParts[0]);
        int updateMinorVersion = Integer.parseInt(updateVersionParts[1]);
        int[] updateVersionParts = parseSemVer(update.getVersion());
        int updateMajorVersion = updateVersionParts[0];
        int updateMinorVersion = updateVersionParts[1];
        Log.d(TAG, "Update : Major "+updateMajorVersion +" Minor "+ updateMinorVersion );

        String deviceVersion = SystemProperties.get(Constants.PROP_BUILD_VERSION);
        String[] deviceVersionParts = deviceVersion.split(Pattern.quote("."));
        int deviceMajorVersion = Integer.parseInt(deviceVersionParts[0]);
        int deviceMinorVersion = Integer.parseInt(deviceVersionParts[1]);
        int[] deviceVersionParts = parseSemVer(SystemProperties.get(Constants.PROP_BUILD_VERSION));
        int deviceMajorVersion = deviceVersionParts[0];
        int deviceMinorVersion = deviceVersionParts[1];
        Log.d(TAG, "Device : Major "+ deviceMajorVersion +" Minor "+ deviceMinorVersion );


        if(updateMajorVersion < deviceMajorVersion){
            Log.d(TAG, "" + " is Older to current Major version");
            return false;
        }
        if(updateMinorVersion < deviceMinorVersion){
            Log.d(TAG, "" + " is Older to current Minor version");
            return false;
        }
        if (!SystemProperties.getBoolean(Constants.PROP_UPDATER_ALLOW_DOWNGRADING, false) &&
            update.getTimestamp() <= SystemProperties.getLong(Constants.PROP_BUILD_DATE, 0)) {
            Log.d(TAG, update.getName() + " is older than/equal to the current build");
@@ -128,10 +118,29 @@ public class Utils {
            Log.d(TAG, update.getName() + " has type " + update.getType());
            return false;
        }
        if(updateMajorVersion > deviceMajorVersion){
            Log.d(TAG, update.getName() + " is Newer to current Major version");
            return true;
        }
        if(updateMajorVersion < deviceMajorVersion){
            Log.d(TAG, update.getName() + " is Older to current Major version");
            return false;
        }
        if(updateMinorVersion < deviceMinorVersion){
            Log.d(TAG, update.getName() + " is Older to current Minor version");
            return false;
        }

        return true;
    }

    public static int[] parseSemVer(String versionCode) {
        String[] versionParts = versionCode.split(Pattern.quote("."));
        int major = Integer.parseInt(versionParts[0]);
        int minor = Integer.parseInt(versionParts[1]);
        return new int[]{ major, minor };
    }

    public static boolean canInstall(UpdateBaseInfo update) {
        return (SystemProperties.getBoolean(Constants.PROP_UPDATER_ALLOW_DOWNGRADING, false) ||
                update.getTimestamp() > SystemProperties.getLong(Constants.PROP_BUILD_DATE, 0));