Merge branch 'fix-511--remove-dead-code' into 'master'
More misc code cleanup around database code I'm pulling out the final bit of unrelated code from my database refactor branch in the hope of making the final diff easier. This cleans up a few switch statements with only one option, closes some cursors, and removes some dead code. Comments in the commits explain the dead code. See merge request !374
This commit is contained in:
commit
ef21bf973c
@ -62,30 +62,6 @@ public class ApkProvider extends FDroidProvider {
|
||||
return resolver.delete(uri, null, null);
|
||||
}
|
||||
|
||||
public static void deleteApksByApp(Context context, App app) {
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
final Uri uri = getAppUri(app.packageName);
|
||||
resolver.delete(uri, null, null);
|
||||
}
|
||||
|
||||
public static void deleteApks(final Context context, final List<Apk> apks) {
|
||||
if (apks.size() > ApkProvider.MAX_APKS_TO_QUERY) {
|
||||
int middle = apks.size() / 2;
|
||||
List<Apk> apks1 = apks.subList(0, middle);
|
||||
List<Apk> apks2 = apks.subList(middle, apks.size());
|
||||
deleteApks(context, apks1);
|
||||
deleteApks(context, apks2);
|
||||
} else {
|
||||
deleteApksSafely(context, apks);
|
||||
}
|
||||
}
|
||||
|
||||
private static void deleteApksSafely(final Context context, final List<Apk> apks) {
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
final Uri uri = getContentUri(apks);
|
||||
resolver.delete(uri, null, null);
|
||||
}
|
||||
|
||||
public static Apk find(Context context, String packageName, int versionCode) {
|
||||
return find(context, packageName, versionCode, Cols.ALL);
|
||||
}
|
||||
|
@ -785,22 +785,12 @@ public class AppProvider extends FDroidProvider {
|
||||
|
||||
@Override
|
||||
public int delete(Uri uri, String where, String[] whereArgs) {
|
||||
|
||||
QuerySelection query = new QuerySelection(where, whereArgs);
|
||||
switch (MATCHER.match(uri)) {
|
||||
|
||||
case NO_APKS:
|
||||
query = query.add(queryNoApks());
|
||||
break;
|
||||
|
||||
default:
|
||||
if (MATCHER.match(uri) != NO_APKS) {
|
||||
throw new UnsupportedOperationException("Delete not supported for " + uri + ".");
|
||||
|
||||
}
|
||||
|
||||
int count = db().delete(getTableName(), query.getSelection(), query.getArgs());
|
||||
getContext().getContentResolver().notifyChange(uri, null);
|
||||
return count;
|
||||
AppQuerySelection selection = new AppQuerySelection(where, whereArgs).add(queryNoApks());
|
||||
return db().delete(getTableName(), selection.getSelection(), selection.getArgs());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -786,13 +786,19 @@ class DBHelper extends SQLiteOpenHelper {
|
||||
}
|
||||
|
||||
private static boolean columnExists(SQLiteDatabase db, String table, String column) {
|
||||
return db.rawQuery("select * from " + table + " limit 0,1", null)
|
||||
.getColumnIndex(column) != -1;
|
||||
Cursor cursor = db.rawQuery("select * from " + table + " limit 0,1", null);
|
||||
boolean exists = cursor.getColumnIndex(column) != -1;
|
||||
cursor.close();
|
||||
return exists;
|
||||
}
|
||||
|
||||
private static boolean tableExists(SQLiteDatabase db, String table) {
|
||||
return db.rawQuery("SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?",
|
||||
new String[] {table}).getCount() > 0;
|
||||
Cursor cursor = db.query("sqlite_master", new String[] {"name"},
|
||||
"type = 'table' AND name = ?", new String[] {table}, null, null, null);
|
||||
|
||||
boolean exists = cursor.getCount() > 0;
|
||||
cursor.close();
|
||||
return exists;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import android.content.Context;
|
||||
import android.content.UriMatcher;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
|
||||
import org.fdroid.fdroid.data.Schema.ApkTable;
|
||||
|
||||
@ -16,8 +15,6 @@ import java.util.List;
|
||||
*/
|
||||
public class TempApkProvider extends ApkProvider {
|
||||
|
||||
private static final String TAG = "TempApkProvider";
|
||||
|
||||
private static final String PROVIDER_NAME = "TempApkProvider";
|
||||
|
||||
static final String TABLE_TEMP_APK = "temp_" + ApkTable.NAME;
|
||||
@ -89,18 +86,16 @@ public class TempApkProvider extends ApkProvider {
|
||||
|
||||
@Override
|
||||
public Uri insert(Uri uri, ContentValues values) {
|
||||
switch (MATCHER.match(uri)) {
|
||||
case CODE_INIT:
|
||||
if (MATCHER.match(uri) == CODE_INIT) {
|
||||
initTable();
|
||||
return null;
|
||||
default:
|
||||
return super.insert(uri, values);
|
||||
}
|
||||
|
||||
return super.insert(uri, values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
|
||||
|
||||
if (MATCHER.match(uri) != CODE_SINGLE) {
|
||||
throw new UnsupportedOperationException("Cannot update anything other than a single apk.");
|
||||
}
|
||||
@ -110,20 +105,15 @@ public class TempApkProvider extends ApkProvider {
|
||||
|
||||
@Override
|
||||
public int delete(Uri uri, String where, String[] whereArgs) {
|
||||
|
||||
QuerySelection query = new QuerySelection(where, whereArgs);
|
||||
|
||||
switch (MATCHER.match(uri)) {
|
||||
case CODE_REPO_APK:
|
||||
List<String> pathSegments = uri.getPathSegments();
|
||||
query = query.add(queryRepo(Long.parseLong(pathSegments.get(1)), false)).add(queryApks(pathSegments.get(2), false));
|
||||
break;
|
||||
|
||||
default:
|
||||
Log.e(TAG, "Invalid URI for apk content provider: " + uri);
|
||||
if (MATCHER.match(uri) != CODE_REPO_APK) {
|
||||
throw new UnsupportedOperationException("Invalid URI for apk content provider: " + uri);
|
||||
}
|
||||
|
||||
List<String> pathSegments = uri.getPathSegments();
|
||||
QuerySelection query = new QuerySelection(where, whereArgs)
|
||||
.add(queryRepo(Long.parseLong(pathSegments.get(1)), false))
|
||||
.add(queryApks(pathSegments.get(2), false));
|
||||
|
||||
int rowsAffected = db().delete(getTableName(), query.getSelection(), query.getArgs());
|
||||
if (!isApplyingBatch()) {
|
||||
getContext().getContentResolver().notifyChange(uri, null);
|
||||
|
@ -122,16 +122,12 @@ public class TempAppProvider extends AppProvider {
|
||||
|
||||
@Override
|
||||
public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
|
||||
QuerySelection query = new QuerySelection(where, whereArgs);
|
||||
switch (MATCHER.match(uri)) {
|
||||
case CODE_SINGLE:
|
||||
query = query.add(querySingle(uri.getLastPathSegment()));
|
||||
break;
|
||||
|
||||
default:
|
||||
if (MATCHER.match(uri) != CODE_SINGLE) {
|
||||
throw new UnsupportedOperationException("Update not supported for " + uri + ".");
|
||||
}
|
||||
|
||||
QuerySelection query = new QuerySelection(where, whereArgs).add(querySingle(uri.getLastPathSegment()));
|
||||
|
||||
int count = db().update(getTableName(), values, query.getSelection(), query.getArgs());
|
||||
if (!isApplyingBatch()) {
|
||||
getContext().getContentResolver().notifyChange(uri, null);
|
||||
|
@ -56,8 +56,6 @@ import org.fdroid.fdroid.data.Schema;
|
||||
*/
|
||||
public class InstallConfirmActivity extends FragmentActivity implements OnCancelListener, OnClickListener {
|
||||
|
||||
private static final int RESULT_CANNOT_PARSE = RESULT_FIRST_USER + 1;
|
||||
|
||||
private Intent intent;
|
||||
|
||||
private AppDiff appDiff;
|
||||
@ -196,10 +194,6 @@ public class InstallConfirmActivity extends FragmentActivity implements OnCancel
|
||||
app = AppProvider.Helper.findByPackageName(getContentResolver(), apk.packageName);
|
||||
|
||||
appDiff = new AppDiff(getPackageManager(), apk);
|
||||
if (appDiff.pkgInfo == null) {
|
||||
setResult(RESULT_CANNOT_PARSE, intent);
|
||||
finish();
|
||||
}
|
||||
|
||||
setContentView(R.layout.install_start);
|
||||
|
||||
|
@ -10,7 +10,6 @@ import org.fdroid.fdroid.BuildConfig;
|
||||
import org.fdroid.fdroid.data.Schema.ApkTable.Cols;
|
||||
import org.fdroid.fdroid.data.Schema.RepoTable;
|
||||
import org.fdroid.fdroid.mock.MockApk;
|
||||
import org.fdroid.fdroid.mock.MockApp;
|
||||
import org.fdroid.fdroid.mock.MockRepo;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@ -23,7 +22,6 @@ import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static org.fdroid.fdroid.Assert.assertCantDelete;
|
||||
import static org.fdroid.fdroid.Assert.assertContainsOnly;
|
||||
import static org.fdroid.fdroid.Assert.assertResultCount;
|
||||
import static org.fdroid.fdroid.Assert.insertApp;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@ -64,65 +62,6 @@ public class ApkProviderTest extends FDroidProviderTest {
|
||||
assertResultCount(10, exampleApks);
|
||||
assertBelongsToApp(exampleApks, "com.example");
|
||||
exampleApks.close();
|
||||
|
||||
ApkProvider.Helper.deleteApksByApp(context, new MockApp("com.example"));
|
||||
|
||||
Cursor all = queryAllApks();
|
||||
assertResultCount(10, all);
|
||||
assertBelongsToApp(all, "org.fdroid.fdroid");
|
||||
all.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteArbitraryApks() {
|
||||
Apk one = insertApkForRepo("com.example.one", 1, 10);
|
||||
Apk two = insertApkForRepo("com.example.two", 1, 10);
|
||||
Apk three = insertApkForRepo("com.example.three", 1, 10);
|
||||
Apk four = insertApkForRepo("com.example.four", 1, 10);
|
||||
Apk five = insertApkForRepo("com.example.five", 1, 10);
|
||||
|
||||
assertTotalApkCount(5);
|
||||
|
||||
assertEquals("com.example.one", one.packageName);
|
||||
assertEquals("com.example.two", two.packageName);
|
||||
assertEquals("com.example.five", five.packageName);
|
||||
|
||||
String[] expectedIds = {
|
||||
"com.example.one",
|
||||
"com.example.two",
|
||||
"com.example.three",
|
||||
"com.example.four",
|
||||
"com.example.five",
|
||||
};
|
||||
|
||||
List<Apk> all = ApkProvider.Helper.findByRepo(context, new MockRepo(10), Cols.ALL);
|
||||
List<String> actualIds = new ArrayList<>();
|
||||
for (Apk apk : all) {
|
||||
actualIds.add(apk.packageName);
|
||||
}
|
||||
|
||||
assertContainsOnly(actualIds, expectedIds);
|
||||
|
||||
List<Apk> toDelete = new ArrayList<>(3);
|
||||
toDelete.add(two);
|
||||
toDelete.add(three);
|
||||
toDelete.add(four);
|
||||
ApkProvider.Helper.deleteApks(context, toDelete);
|
||||
|
||||
assertTotalApkCount(2);
|
||||
|
||||
List<Apk> allRemaining = ApkProvider.Helper.findByRepo(context, new MockRepo(10), Cols.ALL);
|
||||
List<String> actualRemainingIds = new ArrayList<>();
|
||||
for (Apk apk : allRemaining) {
|
||||
actualRemainingIds.add(apk.packageName);
|
||||
}
|
||||
|
||||
String[] expectedRemainingIds = {
|
||||
"com.example.one",
|
||||
"com.example.five",
|
||||
};
|
||||
|
||||
assertContainsOnly(actualRemainingIds, expectedRemainingIds);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -1,16 +0,0 @@
|
||||
package org.fdroid.fdroid.mock;
|
||||
|
||||
import org.fdroid.fdroid.data.App;
|
||||
|
||||
public class MockApp extends App {
|
||||
|
||||
public MockApp(String id) {
|
||||
this(id, "App " + id);
|
||||
}
|
||||
|
||||
public MockApp(String id, String name) {
|
||||
this.packageName = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user