diff --git a/app/src/main/java/org/fdroid/fdroid/data/AppProvider.java b/app/src/main/java/org/fdroid/fdroid/data/AppProvider.java index 60056b62f..c7cf1fd04 100644 --- a/app/src/main/java/org/fdroid/fdroid/data/AppProvider.java +++ b/app/src/main/java/org/fdroid/fdroid/data/AppProvider.java @@ -957,12 +957,13 @@ public class AppProvider extends FDroidProvider { final boolean unstableUpdates = Preferences.get().getUnstableUpdates(); String restrictToStable = unstableUpdates ? "" : (apk + "." + ApkTable.Cols.VERSION_CODE + " <= " + app + "." + Cols.UPSTREAM_VERSION_CODE + " AND "); + String updateSql = "UPDATE " + app + " SET " + Cols.SUGGESTED_VERSION_CODE + " = ( " + " SELECT MAX( " + apk + "." + ApkTable.Cols.VERSION_CODE + " ) " + " FROM " + apk + " WHERE " + - app + "." + Cols.ROW_ID + " = " + apk + "." + ApkTable.Cols.APP_ID + " AND " + + joinToApksRegardlessOfRepo() + " AND " + restrictToStable + " ( " + app + "." + Cols.IS_COMPATIBLE + " = 0 OR " + apk + "." + Cols.IS_COMPATIBLE + " = 1 ) ) " + " WHERE " + Cols.UPSTREAM_VERSION_CODE + " > 0 "; @@ -970,6 +971,29 @@ public class AppProvider extends FDroidProvider { db().execSQL(updateSql); } + /** + * Ensure that when we select a list of {@link ApkTable} rows for which to calculate the + * {@link Cols#SUGGESTED_VERSION_CODE}, that we select all apks belonging to the same package, + * regardless of which repo they come from. We can't just join {@link ApkTable} onto the + * {@link AppMetadataTable}, because the {@link AppMetadataTable} table is specific to a repo. + * + * This is required so that apps always have the highest possible + * {@link Cols#SUGGESTED_VERSION_CODE}, regardless of the repository priorities. Without this, + * then each {@link AppMetadataTable} row will have a different {@link Cols#SUGGESTED_VERSION_CODE} + * depending on which repo it came from. With this, each {@link AppMetadataTable} row has the + * same {@link Cols#SUGGESTED_VERSION_CODE}, even if that version is from a different repo. + */ + private String joinToApksRegardlessOfRepo() { + final String apk = getApkTableName(); + final String app = getTableName(); + + return app + "." + Cols.PACKAGE_ID + " = (" + + " SELECT innerAppName." + Cols.PACKAGE_ID + + " FROM " + app + " as innerAppName " + + " WHERE innerAppName." + Cols.ROW_ID + " = " + apk + "." + ApkTable.Cols.APP_ID + + ") "; + } + /** * We set each app's suggested version to the latest available that is * compatible, or the latest available if none are compatible. @@ -989,7 +1013,7 @@ public class AppProvider extends FDroidProvider { " SELECT MAX( " + apk + "." + ApkTable.Cols.VERSION_CODE + " ) " + " FROM " + apk + " WHERE " + - app + "." + Cols.ROW_ID + " = " + apk + "." + ApkTable.Cols.APP_ID + " AND " + + joinToApksRegardlessOfRepo() + " AND " + " ( " + app + "." + Cols.IS_COMPATIBLE + " = 0 OR " + apk + "." + ApkTable.Cols.IS_COMPATIBLE + " = 1 ) ) " + " WHERE COALESCE(" + Cols.UPSTREAM_VERSION_CODE + ", 0) = 0 OR " + Cols.SUGGESTED_VERSION_CODE + " IS NULL ";