From 6c08e054f55463bb759272f9c1e1b119d0444c25 Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Fri, 28 Apr 2017 10:50:27 +1000 Subject: [PATCH] Calc suggested versioncode properly, regardless of repo priorities. There was a bug where the repo with the highest priority would be responsible for specifying the suggested version code. When doing so, it would only select from the list of apks available in that repo. This improves the calculation so that when any given repos app gets a suggested version code assigned, it selects from _all_ available apks, not just those from the repository in question. Fixes #974. --- .../org/fdroid/fdroid/data/AppProvider.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) 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 ";