From 97cf69341ab89627737a5ec28c9db4dab2ca5274 Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Wed, 5 Oct 2016 23:40:16 +1100 Subject: [PATCH] When inserting a new repo, assign the priority appropriately. Even though this is not used yet, it will be a requirement in the near future for the `RepoProvider` to be the one who decides what the priority of new repositories is. This will prevent clients of this provider from specifying wrong priorities that result in gaps For example, if we accidentally ended up with priorities of 1, 2, 4, and then 5, this would cause problems if the user tried to drag the second repo to the position of the 4th repo. It is easier to do these priority shuffles if we can assume that the priorities are contiguous. --- .../org/fdroid/fdroid/data/RepoProvider.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/data/RepoProvider.java b/app/src/main/java/org/fdroid/fdroid/data/RepoProvider.java index dbe7dbb96..f08fc1589 100644 --- a/app/src/main/java/org/fdroid/fdroid/data/RepoProvider.java +++ b/app/src/main/java/org/fdroid/fdroid/data/RepoProvider.java @@ -331,6 +331,10 @@ public class RepoProvider extends FDroidProvider { @Override public Uri insert(Uri uri, ContentValues values) { + // Don't let people specify arbitrary priorities. Instead, we are responsible + // for making sure that newly created repositories by default have the highest priority. + values.put(Cols.PRIORITY, getMaxPriority() + 1); + if (!values.containsKey(Cols.ADDRESS)) { throw new UnsupportedOperationException("Cannot add repo without an address."); } @@ -342,10 +346,6 @@ public class RepoProvider extends FDroidProvider { values.put(Cols.IN_USE, 1); } - if (!values.containsKey(Cols.PRIORITY)) { - values.put(Cols.PRIORITY, 10); - } - if (!values.containsKey(Cols.MAX_AGE)) { values.put(Cols.MAX_AGE, 0); } @@ -365,6 +365,14 @@ public class RepoProvider extends FDroidProvider { return getContentUri(id); } + private int getMaxPriority() { + Cursor cursor = db().query(RepoTable.NAME, new String[] {"MAX(" + Cols.PRIORITY + ")"}, "COALESCE(" + Cols.IS_SWAP + ", 0) = 0", null, null, null, null); + cursor.moveToFirst(); + int max = cursor.getInt(0); + cursor.close(); + return max; + } + @Override public int delete(Uri uri, String where, String[] whereArgs) {