1
0
Fork 0
mirror of https://github.com/MatomoCamp/matomocamp-companion-android.git synced 2024-09-19 16:13:46 +02:00

Use DatabaseUtils where applicable to simplify code

This commit is contained in:
Christophe Beyls 2018-01-29 18:16:10 +01:00
parent 892e6ccf7a
commit aacb75954d

View file

@ -8,6 +8,7 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.database.Cursor; import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteConstraintException; import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement; import android.database.sqlite.SQLiteStatement;
@ -77,18 +78,6 @@ public class DatabaseManager {
helper = new DatabaseHelper(context); helper = new DatabaseHelper(context);
} }
private static final String[] COUNT_PROJECTION = new String[]{"count(*)"};
private static long queryNumEntries(SQLiteDatabase db, String table, String selection, String[] selectionArgs) {
Cursor cursor = db.query(table, COUNT_PROJECTION, selection, selectionArgs, null, null, null);
try {
cursor.moveToFirst();
return cursor.getLong(0);
} finally {
cursor.close();
}
}
private static final String TRACK_INSERT_STATEMENT = "INSERT INTO " + DatabaseHelper.TRACKS_TABLE_NAME + " (id, name, type) VALUES (?, ?, ?);"; private static final String TRACK_INSERT_STATEMENT = "INSERT INTO " + DatabaseHelper.TRACKS_TABLE_NAME + " (id, name, type) VALUES (?, ?, ?);";
private static final String EVENT_INSERT_STATEMENT = "INSERT INTO " + DatabaseHelper.EVENTS_TABLE_NAME private static final String EVENT_INSERT_STATEMENT = "INSERT INTO " + DatabaseHelper.EVENTS_TABLE_NAME
+ " (id, day_index, start_time, end_time, room_name, slug, track_id, abstract, description) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"; + " (id, day_index, start_time, end_time, room_name, slug, track_id, abstract, description) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";
@ -363,15 +352,9 @@ public class DatabaseManager {
} }
} else { } else {
// Perform a quick DB query to retrieve the time of the first day // Perform a quick DB query to retrieve the time of the first day
Cursor cursor = helper.getReadableDatabase().query(DatabaseHelper.DAYS_TABLE_NAME, new String[]{"date"}, null, null, null, null, long date = DatabaseUtils.longForQuery(helper.getReadableDatabase(),
"_index ASC LIMIT 1"); "SELECT date FROM " + DatabaseHelper.DAYS_TABLE_NAME + " ORDER BY _index ASC LIMIT 1", null);
try { cal.setTimeInMillis(date);
if (cursor.moveToFirst()) {
cal.setTimeInMillis(cursor.getLong(0));
}
} finally {
cursor.close();
}
} }
// If the calendar has not been set at this point, it will simply return the current year // If the calendar has not been set at this point, it will simply return the current year
@ -407,7 +390,7 @@ public class DatabaseManager {
@WorkerThread @WorkerThread
public long getEventsCount() { public long getEventsCount() {
return queryNumEntries(helper.getReadableDatabase(), DatabaseHelper.EVENTS_TABLE_NAME, null, null); return DatabaseUtils.queryNumEntries(helper.getReadableDatabase(), DatabaseHelper.EVENTS_TABLE_NAME, null, null);
} }
/** /**
@ -814,7 +797,7 @@ public class DatabaseManager {
@WorkerThread @WorkerThread
public boolean isBookmarked(Event event) { public boolean isBookmarked(Event event) {
String[] selectionArgs = new String[]{String.valueOf(event.getId())}; String[] selectionArgs = new String[]{String.valueOf(event.getId())};
return queryNumEntries(helper.getReadableDatabase(), DatabaseHelper.BOOKMARKS_TABLE_NAME, "event_id = ?", selectionArgs) > 0L; return DatabaseUtils.queryNumEntries(helper.getReadableDatabase(), DatabaseHelper.BOOKMARKS_TABLE_NAME, "event_id = ?", selectionArgs) > 0L;
} }
@WorkerThread @WorkerThread