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

Commit 8353ecc3 authored by Alexandre Roux's avatar Alexandre Roux Committed by Nishith Khanna
Browse files

Updater: Dont crash and hide lower android version

parent 3bf2fafd
Loading
Loading
Loading
Loading
+8 −2
Original line number Original line Diff line number Diff line
@@ -30,7 +30,7 @@ import java.util.List;


public class UpdatesDbHelper extends SQLiteOpenHelper {
public class UpdatesDbHelper extends SQLiteOpenHelper {


    public static final int DATABASE_VERSION = 2;
    public static final int DATABASE_VERSION = 3;
    public static final String DATABASE_NAME = "updates.db";
    public static final String DATABASE_NAME = "updates.db";


    public static class UpdateEntry implements BaseColumns {
    public static class UpdateEntry implements BaseColumns {
@@ -43,6 +43,7 @@ public class UpdatesDbHelper extends SQLiteOpenHelper {
        public static final String COLUMN_NAME_VERSION = "version";
        public static final String COLUMN_NAME_VERSION = "version";
        public static final String COLUMN_NAME_DISPLAY_VERSION = "display_version";
        public static final String COLUMN_NAME_DISPLAY_VERSION = "display_version";
        public static final String COLUMN_NAME_SIZE = "size";
        public static final String COLUMN_NAME_SIZE = "size";
        public static final String COLUMN_NAME_ANDROID_VERSION = "android_version";
    }
    }


    private static final String SQL_CREATE_ENTRIES =
    private static final String SQL_CREATE_ENTRIES =
@@ -55,7 +56,8 @@ public class UpdatesDbHelper extends SQLiteOpenHelper {
                    UpdateEntry.COLUMN_NAME_TYPE + " TEXT," +
                    UpdateEntry.COLUMN_NAME_TYPE + " TEXT," +
                    UpdateEntry.COLUMN_NAME_VERSION + " TEXT," +
                    UpdateEntry.COLUMN_NAME_VERSION + " TEXT," +
                    UpdateEntry.COLUMN_NAME_DISPLAY_VERSION + " TEXT," +
                    UpdateEntry.COLUMN_NAME_DISPLAY_VERSION + " TEXT," +
                    UpdateEntry.COLUMN_NAME_SIZE + " INTEGER)";
                    UpdateEntry.COLUMN_NAME_SIZE + " INTEGER," +
                    UpdateEntry.COLUMN_NAME_ANDROID_VERSION + " TEXT)";


    private static final String SQL_DELETE_ENTRIES =
    private static final String SQL_DELETE_ENTRIES =
            "DROP TABLE IF EXISTS " + UpdateEntry.TABLE_NAME;
            "DROP TABLE IF EXISTS " + UpdateEntry.TABLE_NAME;
@@ -93,6 +95,7 @@ public class UpdatesDbHelper extends SQLiteOpenHelper {
        values.put(UpdateEntry.COLUMN_NAME_VERSION, update.getVersion());
        values.put(UpdateEntry.COLUMN_NAME_VERSION, update.getVersion());
        values.put(UpdateEntry.COLUMN_NAME_DISPLAY_VERSION, update.getDisplayVersion());
        values.put(UpdateEntry.COLUMN_NAME_DISPLAY_VERSION, update.getDisplayVersion());
        values.put(UpdateEntry.COLUMN_NAME_SIZE, update.getFileSize());
        values.put(UpdateEntry.COLUMN_NAME_SIZE, update.getFileSize());
        values.put(UpdateEntry.COLUMN_NAME_ANDROID_VERSION, update.getAndroidVersion());
    }
    }


    public void removeUpdate(String downloadId) {
    public void removeUpdate(String downloadId) {
@@ -131,6 +134,7 @@ public class UpdatesDbHelper extends SQLiteOpenHelper {
                UpdateEntry.COLUMN_NAME_DISPLAY_VERSION,
                UpdateEntry.COLUMN_NAME_DISPLAY_VERSION,
                UpdateEntry.COLUMN_NAME_STATUS,
                UpdateEntry.COLUMN_NAME_STATUS,
                UpdateEntry.COLUMN_NAME_SIZE,
                UpdateEntry.COLUMN_NAME_SIZE,
                UpdateEntry.COLUMN_NAME_ANDROID_VERSION,
        };
        };
        String sort = UpdateEntry.COLUMN_NAME_TIMESTAMP + " DESC";
        String sort = UpdateEntry.COLUMN_NAME_TIMESTAMP + " DESC";
        Cursor cursor = db.query(UpdateEntry.TABLE_NAME, projection, selection, selectionArgs,
        Cursor cursor = db.query(UpdateEntry.TABLE_NAME, projection, selection, selectionArgs,
@@ -156,6 +160,8 @@ public class UpdatesDbHelper extends SQLiteOpenHelper {
                update.setPersistentStatus(cursor.getInt(index));
                update.setPersistentStatus(cursor.getInt(index));
                index = cursor.getColumnIndex(UpdateEntry.COLUMN_NAME_SIZE);
                index = cursor.getColumnIndex(UpdateEntry.COLUMN_NAME_SIZE);
                update.setFileSize(cursor.getLong(index));
                update.setFileSize(cursor.getLong(index));
                index = cursor.getColumnIndex(UpdateEntry.COLUMN_NAME_ANDROID_VERSION);
                update.setAndroidVersion(cursor.getString(index));
                updates.add(update);
                updates.add(update);
            }
            }
            cursor.close();
            cursor.close();
+1 −1
Original line number Original line Diff line number Diff line
@@ -560,6 +560,6 @@ public class UpdaterService extends Service {
    }
    }


    private void notifySystemUpdaterService(int status, UpdateInfo update) {
    private void notifySystemUpdaterService(int status, UpdateInfo update) {
        Utils.updateSystemUpdaterService(this, status, Float.parseFloat(update.getVersion()));
        Utils.updateSystemUpdaterService(this, status, update.getVersion());
    }
    }
}
}
+56 −13
Original line number Original line Diff line number Diff line
@@ -22,6 +22,7 @@ import static android.os.SystemUpdateManager.STATUS_IDLE;
import static android.os.SystemUpdateManager.STATUS_WAITING_DOWNLOAD;
import static android.os.SystemUpdateManager.STATUS_WAITING_DOWNLOAD;


import android.app.AlarmManager;
import android.app.AlarmManager;
import android.os.Build;
import android.content.ClipData;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Context;
@@ -62,6 +63,8 @@ import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Enumeration;
import java.util.List;
import java.util.List;
import java.util.Locale;
import java.util.Locale;
import java.util.Objects;
import java.util.regex.Matcher;
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;
import java.util.regex.Pattern;
@@ -93,6 +96,7 @@ public class Utils {
        update.setFileSize(object.getLong("size"));
        update.setFileSize(object.getLong("size"));
        update.setDownloadUrl(object.getString("url"));
        update.setDownloadUrl(object.getString("url"));
        update.setVersion(object.getString("version"));
        update.setVersion(object.getString("version"));
        update.setAndroidVersion(object.getString("android_version"));
        if (object.has("pre_version") && !object.getString("pre_version").isEmpty()) {
        if (object.has("pre_version") && !object.getString("pre_version").isEmpty()) {
          update.setDisplayVersion(object.getString("version") + "-" + object.getString("pre_version"));
          update.setDisplayVersion(object.getString("version") + "-" + object.getString("pre_version"));
        } else {
        } else {
@@ -101,7 +105,27 @@ public class Utils {
        return update;
        return update;
    }
    }


    public static int parseAndroidVersion(String versionString) {
        // Parse android versions such as 8.1.0.
        // Older updates still shows in ota requests.
        Pattern pattern = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)");
        Matcher matcher = pattern.matcher(versionString);
        return matcher.matches() ? Integer.parseInt(Objects.requireNonNull(matcher.group(1)))
                : Float.valueOf(versionString).intValue();
    }

    public static boolean isCompatible(UpdateBaseInfo update) {
    public static boolean isCompatible(UpdateBaseInfo update) {
        String updateAndroidVersion = update.getAndroidVersion();
        if (!updateAndroidVersion.isEmpty()) {
            final int updateOSVersion = parseAndroidVersion(updateAndroidVersion);
            final int deviceOSVersion = parseAndroidVersion(Build.VERSION.RELEASE);
            if (deviceOSVersion > updateOSVersion) {
                    Log.d(TAG, "Update : Skipping " + update.getName() + " since the installed version "
                            + deviceOSVersion + " is newer than update " + updateOSVersion);
                    return false;
            }
        }

        int[] updateVersionParts = parseSemVer(update.getVersion());
        int[] updateVersionParts = parseSemVer(update.getVersion());
        int updateMajorVersion = updateVersionParts[0];
        int updateMajorVersion = updateVersionParts[0];
        int updateMinorVersion = updateVersionParts[1];
        int updateMinorVersion = updateVersionParts[1];
@@ -274,20 +298,39 @@ public class Utils {
    public static boolean checkForNewUpdates(File newJson, Context context)
    public static boolean checkForNewUpdates(File newJson, Context context)
            throws IOException, JSONException {
            throws IOException, JSONException {
        List<UpdateInfo> newList = parseJson(newJson, true);
        List<UpdateInfo> newList = parseJson(newJson, true);
        final float currentVersion = Float.parseFloat(BuildInfoUtils.getBuildVersion());
        int[] deviceVersionParts = parseSemVer(SystemProperties.get(Constants.PROP_BUILD_VERSION));
        float highestAvailableVersion = currentVersion;
        int deviceMajorVersion = deviceVersionParts[0];
        int deviceMinorVersion = deviceVersionParts[1];
        int deviceMaintenanceVersion = deviceVersionParts.length > 2 ? deviceVersionParts[2] : 0;
        int highestMajorVersion = deviceMajorVersion;
        int highestMinorVersion = deviceMinorVersion;
        int highestMaintenanceVersion = deviceMaintenanceVersion;
	    boolean hasUpdate = false;
        for (UpdateInfo update : newList) {
        for (UpdateInfo update : newList) {
            float availableversion = Float.parseFloat(update.getVersion());
            if (isCompatible(update)) {
            if (availableversion > highestAvailableVersion) {
                Log.d(TAG, "New compatible update available");
                highestAvailableVersion = availableversion;
                int[] updateVersionParts = parseSemVer(update.getVersion());
            }
                int updateMajorVersion = updateVersionParts[0];
        }
                int updateMinorVersion = updateVersionParts[1];

                int updateMaintenanceVersion = updateVersionParts.length > 2
        if (highestAvailableVersion > currentVersion) {
                        ? updateVersionParts[2] : 0;
            updateSystemUpdaterService(context, STATUS_WAITING_DOWNLOAD, highestAvailableVersion);
                if (updateMajorVersion * 10000 + updateMinorVersion * 100 + updateMaintenanceVersion
                        >= highestMajorVersion * 10000 + highestMinorVersion * 100
                        + highestMaintenanceVersion) {
                    highestMajorVersion = updateMajorVersion;
                    highestMinorVersion = updateMinorVersion;
					highestMaintenanceVersion = updateMaintenanceVersion;
				}
				hasUpdate = true;
            }
        }
        String updateVersion = highestMajorVersion + "." + highestMinorVersion +
                (highestMaintenanceVersion > 0 ? "." + highestMaintenanceVersion : "");
        if (hasUpdate) {
           updateSystemUpdaterService(context, STATUS_WAITING_DOWNLOAD, updateVersion);
           return true;
           return true;
        } else {
        } else {
            updateSystemUpdaterService(context, STATUS_IDLE, highestAvailableVersion);
           updateSystemUpdaterService(context, STATUS_IDLE, updateVersion);
           return false;
           return false;
        }
        }
    }
    }
@@ -460,7 +503,7 @@ public class Utils {
                Constants.AUTO_UPDATES_CHECK_INTERVAL_DAILY);
                Constants.AUTO_UPDATES_CHECK_INTERVAL_DAILY);
    }
    }


    public static void updateSystemUpdaterService(Context context, int status, float version) {
    public static void updateSystemUpdaterService(Context context, int status, String version) {
        final SystemUpdateManager updateManager = context.getSystemService(SystemUpdateManager.class);
        final SystemUpdateManager updateManager = context.getSystemService(SystemUpdateManager.class);


        final Bundle oldInfo = updateManager.retrieveSystemUpdateInfo();
        final Bundle oldInfo = updateManager.retrieveSystemUpdateInfo();
@@ -469,7 +512,7 @@ public class Utils {
        if (status != oldStatus) {
        if (status != oldStatus) {
            PersistableBundle infoBundle = new PersistableBundle();
            PersistableBundle infoBundle = new PersistableBundle();
            infoBundle.putInt(KEY_STATUS, status);
            infoBundle.putInt(KEY_STATUS, status);
            infoBundle.putString(KEY_TITLE, String.valueOf(version));
            infoBundle.putString(KEY_TITLE, version);
            updateManager.updateSystemUpdateInfo(infoBundle);
            updateManager.updateSystemUpdateInfo(infoBundle);
        }
        }
    }
    }
+10 −0
Original line number Original line Diff line number Diff line
@@ -24,6 +24,7 @@ public class UpdateBase implements UpdateBaseInfo {
    private String mType;
    private String mType;
    private String mVersion;
    private String mVersion;
    private String mDisplayVersion;
    private String mDisplayVersion;
    private String mAndroidVersion;
    private long mFileSize;
    private long mFileSize;


    public UpdateBase() {
    public UpdateBase() {
@@ -111,4 +112,13 @@ public class UpdateBase implements UpdateBaseInfo {
    public void setFileSize(long fileSize) {
    public void setFileSize(long fileSize) {
        mFileSize = fileSize;
        mFileSize = fileSize;
    }
    }

    @Override
    public String getAndroidVersion() {
        return mAndroidVersion;
    }

    public void setAndroidVersion(String androidVersion) {
        mAndroidVersion = androidVersion;
    }
}
}
+2 −0
Original line number Original line Diff line number Diff line
@@ -31,4 +31,6 @@ public interface UpdateBaseInfo {
    String getDownloadUrl();
    String getDownloadUrl();


    long getFileSize();
    long getFileSize();

    String getAndroidVersion();
}
}