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

Commit f838cbd4 authored by Sooraj S's avatar Sooraj S 👽 Committed by Alexandre Roux
Browse files

Use helper function for parsing version code



Fix: Handle use case 1.0 is bigger than 0.9 Semantic versioning
Signed-off-by: default avatarAlexandre Roux D'Anzi <alexandre.roux.danzi@lostpod.me>
parent ce3254fa
Loading
Loading
Loading
Loading
+34 −5
Original line number Original line Diff line number Diff line
@@ -53,6 +53,8 @@ import java.util.Locale;
import java.util.Set;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipFile;
import java.util.regex.Pattern;



public class Utils {
public class Utils {


@@ -96,10 +98,17 @@ public class Utils {
    }
    }


    public static boolean isCompatible(UpdateBaseInfo update) {
    public static boolean isCompatible(UpdateBaseInfo update) {
        if (update.getVersion().compareTo(SystemProperties.get(Constants.PROP_BUILD_VERSION)) < 0) {
        int[] updateVersionParts = parseSemVer(update.getVersion());
            Log.d(TAG, update.getName() + " is older than current Android version");
        int updateMajorVersion = updateVersionParts[0];
            return false;
        int updateMinorVersion = updateVersionParts[1];
        }
        Log.d(TAG, "Update : Major "+updateMajorVersion +" Minor "+ updateMinorVersion );

        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 (!SystemProperties.getBoolean(Constants.PROP_UPDATER_ALLOW_DOWNGRADING, false) &&
        if (!SystemProperties.getBoolean(Constants.PROP_UPDATER_ALLOW_DOWNGRADING, false) &&
            update.getTimestamp() <= SystemProperties.getLong(Constants.PROP_BUILD_DATE, 0)) {
            update.getTimestamp() <= SystemProperties.getLong(Constants.PROP_BUILD_DATE, 0)) {
            Log.d(TAG, update.getName() + " is older than/equal to the current build");
            Log.d(TAG, update.getName() + " is older than/equal to the current build");
@@ -109,8 +118,28 @@ public class Utils {
            Log.d(TAG, update.getName() + " has type " + update.getType());
            Log.d(TAG, update.getName() + " has type " + update.getType());
            return false;
            return false;
        }
        }
        if(updateMajorVersion > deviceMajorVersion){
            Log.d(TAG, update.getName() + " is Newer to current Major version");
            return true;
            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) {
    public static boolean canInstall(UpdateBaseInfo update) {
        return (SystemProperties.getBoolean(Constants.PROP_UPDATER_ALLOW_DOWNGRADING, false) ||
        return (SystemProperties.getBoolean(Constants.PROP_UPDATER_ALLOW_DOWNGRADING, false) ||