From c44395757eba32cbcf5f327698d0e3ea74a9c368 Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Fri, 14 Aug 2015 19:02:22 +0200 Subject: [PATCH] fix dbh leak in playcounts helper --- .../android/vanilla/MediaAdapter.java | 2 +- .../android/vanilla/PlayCountsHelper.java | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/ch/blinkenlights/android/vanilla/MediaAdapter.java b/src/ch/blinkenlights/android/vanilla/MediaAdapter.java index 820c3351..ecc0e898 100644 --- a/src/ch/blinkenlights/android/vanilla/MediaAdapter.java +++ b/src/ch/blinkenlights/android/vanilla/MediaAdapter.java @@ -267,7 +267,7 @@ public class MediaAdapter // Magic sort mode: sort by playcount if (sortStringRaw == SORT_MAGIC_PLAYCOUNT) { - ArrayList topSongs = (new PlayCountsHelper(mActivity)).getTopSongs(); + ArrayList topSongs = (new PlayCountsHelper(mActivity)).getTopSongs(4096); 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 diff --git a/src/ch/blinkenlights/android/vanilla/PlayCountsHelper.java b/src/ch/blinkenlights/android/vanilla/PlayCountsHelper.java index e02521a1..51b0f2e8 100644 --- a/src/ch/blinkenlights/android/vanilla/PlayCountsHelper.java +++ b/src/ch/blinkenlights/android/vanilla/PlayCountsHelper.java @@ -68,26 +68,31 @@ public class PlayCountsHelper extends SQLiteOpenHelper { public void countSong(Song 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("UPDATE "+TABLE_PLAYCOUNTS+" SET playcount=playcount+1 WHERE type="+MediaUtils.TYPE_SONG+" AND type_id="+id+";"); - performGC(dbh, MediaUtils.TYPE_SONG); dbh.close(); + + performGC(MediaUtils.TYPE_SONG); } + + /** * Returns a sorted array list of most often listen song ids */ - public ArrayList getTopSongs() { + public ArrayList getTopSongs(int limit) { ArrayList payload = new ArrayList(); - SQLiteDatabase dbh = this.getReadableDatabase(); - Cursor cursor = dbh.rawQuery("SELECT type_id FROM "+TABLE_PLAYCOUNTS+" WHERE type="+MediaUtils.TYPE_SONG+" ORDER BY playcount DESC limit 4096", null); + SQLiteDatabase dbh = getReadableDatabase(); + + 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()) { payload.add(cursor.getLong(0)); } cursor.close(); + dbh.close(); return payload; } @@ -96,7 +101,8 @@ public class PlayCountsHelper extends SQLiteOpenHelper { * and checks them against Androids media 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 toCheck = new ArrayList(); // List of songs we are going to check QueryTask query; // Reused query object Cursor cursor; // recycled cursor @@ -119,6 +125,7 @@ public class PlayCountsHelper extends SQLiteOpenHelper { cursor.close(); } Log.v("VanillaMusic", "performGC: items removed="+removed); + dbh.close(); return removed; }