fix dbh leak in playcounts helper

This commit is contained in:
Adrian Ulrich 2015-08-14 19:02:22 +02:00
parent 5bf72d1c57
commit c44395757e
2 changed files with 14 additions and 7 deletions

View File

@ -267,7 +267,7 @@ public class MediaAdapter
// Magic sort mode: sort by playcount // Magic sort mode: sort by playcount
if (sortStringRaw == SORT_MAGIC_PLAYCOUNT) { if (sortStringRaw == SORT_MAGIC_PLAYCOUNT) {
ArrayList<Long> topSongs = (new PlayCountsHelper(mActivity)).getTopSongs(); ArrayList<Long> topSongs = (new PlayCountsHelper(mActivity)).getTopSongs(4096);
int sortWeight = -1 * topSongs.size(); // Sort mode is actually reversed (default: mostplayed -> leastplayed) int sortWeight = -1 * topSongs.size(); // Sort mode is actually reversed (default: mostplayed -> leastplayed)
StringBuilder sb = new StringBuilder("CASE WHEN _id=0 THEN 0"); // include dummy statement in initial string -> topSongs may be empty StringBuilder sb = new StringBuilder("CASE WHEN _id=0 THEN 0"); // include dummy statement in initial string -> topSongs may be empty

View File

@ -68,26 +68,31 @@ public class PlayCountsHelper extends SQLiteOpenHelper {
public void countSong(Song song) { public void countSong(Song song) {
long id = Song.getId(song); long id = Song.getId(song);
SQLiteDatabase dbh = this.getWritableDatabase(); SQLiteDatabase dbh = getWritableDatabase();
dbh.execSQL("INSERT OR IGNORE INTO "+TABLE_PLAYCOUNTS+" (type, type_id, playcount) VALUES ("+MediaUtils.TYPE_SONG+", "+id+", 0);"); // Creates row if not exists dbh.execSQL("INSERT OR IGNORE INTO "+TABLE_PLAYCOUNTS+" (type, type_id, playcount) VALUES ("+MediaUtils.TYPE_SONG+", "+id+", 0);"); // Creates row if not exists
dbh.execSQL("UPDATE "+TABLE_PLAYCOUNTS+" SET playcount=playcount+1 WHERE type="+MediaUtils.TYPE_SONG+" AND type_id="+id+";"); dbh.execSQL("UPDATE "+TABLE_PLAYCOUNTS+" SET playcount=playcount+1 WHERE type="+MediaUtils.TYPE_SONG+" AND type_id="+id+";");
performGC(dbh, MediaUtils.TYPE_SONG);
dbh.close(); dbh.close();
performGC(MediaUtils.TYPE_SONG);
} }
/** /**
* Returns a sorted array list of most often listen song ids * Returns a sorted array list of most often listen song ids
*/ */
public ArrayList<Long> getTopSongs() { public ArrayList<Long> getTopSongs(int limit) {
ArrayList<Long> payload = new ArrayList<Long>(); ArrayList<Long> payload = new ArrayList<Long>();
SQLiteDatabase dbh = this.getReadableDatabase(); SQLiteDatabase dbh = getReadableDatabase();
Cursor cursor = dbh.rawQuery("SELECT type_id FROM "+TABLE_PLAYCOUNTS+" WHERE type="+MediaUtils.TYPE_SONG+" ORDER BY playcount DESC limit 4096", null);
Cursor cursor = dbh.rawQuery("SELECT type_id FROM "+TABLE_PLAYCOUNTS+" WHERE type="+MediaUtils.TYPE_SONG+" ORDER BY playcount DESC limit "+limit, null);
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
payload.add(cursor.getLong(0)); payload.add(cursor.getLong(0));
} }
cursor.close(); cursor.close();
dbh.close();
return payload; return payload;
} }
@ -96,7 +101,8 @@ public class PlayCountsHelper extends SQLiteOpenHelper {
* and checks them against Androids media database. * and checks them against Androids media database.
* Items not found in the media library are removed from the DBH's database * Items not found in the media library are removed from the DBH's database
*/ */
private int performGC(SQLiteDatabase dbh, int type) { private int performGC(int type) {
SQLiteDatabase dbh = getWritableDatabase();
ArrayList<Long> toCheck = new ArrayList<Long>(); // List of songs we are going to check ArrayList<Long> toCheck = new ArrayList<Long>(); // List of songs we are going to check
QueryTask query; // Reused query object QueryTask query; // Reused query object
Cursor cursor; // recycled cursor Cursor cursor; // recycled cursor
@ -119,6 +125,7 @@ public class PlayCountsHelper extends SQLiteOpenHelper {
cursor.close(); cursor.close();
} }
Log.v("VanillaMusic", "performGC: items removed="+removed); Log.v("VanillaMusic", "performGC: items removed="+removed);
dbh.close();
return removed; return removed;
} }