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.
This commit is contained in:
Peter Serwylo 2016-10-05 23:40:16 +11:00
parent 6c462713aa
commit 97cf69341a

View File

@ -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) {