diff --git a/src/org/kreed/vanilla/PlaybackService.java b/src/org/kreed/vanilla/PlaybackService.java index a9deb95f..e90892b0 100644 --- a/src/org/kreed/vanilla/PlaybackService.java +++ b/src/org/kreed/vanilla/PlaybackService.java @@ -244,9 +244,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On mPlugged = intent.getIntExtra("state", 0) != 0; if (mPlugged != oldPlugged && (mHeadsetPause && !mPlugged || mHeadsetOnly && !isSpeakerOn())) unsetFlag(FLAG_PLAYING); - } else if (Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action) - || Intent.ACTION_MEDIA_SCANNER_SCAN_FILE.equals(action)) { - mHandler.sendEmptyMessage(RETRIEVE_SONGS); } } }; @@ -312,7 +309,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On private static final int DO_ITEM = 4; private static final int TRACK_CHANGED = 5; private static final int RELEASE_WAKE_LOCK = 6; - private static final int RETRIEVE_SONGS = 9; private static final int CALL = 10; private static final int SAVE_STATE = 12; private static final int PROCESS_SONG = 13; @@ -688,12 +684,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On if (mWakeLock != null && mWakeLock.isHeld()) mWakeLock.release(); break; - case RETRIEVE_SONGS: - if (!Song.retrieveSongs()) - setFlag(FLAG_NO_MEDIA); - else if ((mState & FLAG_NO_MEDIA) != 0) - unsetFlag(FLAG_NO_MEDIA); - break; case CALL: boolean inCall = message.arg1 == 1; if (inCall) { @@ -731,8 +721,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_HEADSET_PLUG); - filter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED); - filter.addAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); registerReceiver(mReceiver, filter); TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); diff --git a/src/org/kreed/vanilla/Song.java b/src/org/kreed/vanilla/Song.java index 49fce4b8..f9b5b381 100644 --- a/src/org/kreed/vanilla/Song.java +++ b/src/org/kreed/vanilla/Song.java @@ -26,7 +26,14 @@ import android.os.Parcelable; import android.provider.MediaStore; public class Song implements Parcelable { - private static int[] mIds; + public static final String[] FILLED_PROJECTION = { + MediaStore.Audio.Media._ID, + MediaStore.Audio.Media.DATA, + MediaStore.Audio.Media.TITLE, + MediaStore.Audio.Media.ALBUM, + MediaStore.Audio.Media.ARTIST, + MediaStore.Audio.Media.ALBUM_ID + }; public int id; @@ -54,42 +61,41 @@ public class Song implements Parcelable { if (id == -1) return false; - Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; - String[] projection = { - MediaStore.Audio.Media.DATA, - MediaStore.Audio.Media.TITLE, - MediaStore.Audio.Media.ALBUM, - MediaStore.Audio.Media.ARTIST, - MediaStore.Audio.Media.ALBUM_ID - }; - String selection = MediaStore.Audio.Media._ID + "==" + id;; - ContentResolver resolver = ContextApplication.getContext().getContentResolver(); - Cursor cursor = resolver.query(media, projection, selection, null, null); + Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; + String selection = MediaStore.Audio.Media._ID + '=' + id; + Cursor cursor = resolver.query(media, FILLED_PROJECTION, selection, null, null); - if (cursor == null || !cursor.moveToNext()) { - id = -1; - return false; - } + id = -1; - path = cursor.getString(0); - title = cursor.getString(1); - album = cursor.getString(2); - artist = cursor.getString(3); - int albumId = cursor.getInt(4); - cursor.close(); - - media = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI; - String[] albumProjection = { MediaStore.Audio.Albums.ALBUM_ART }; - String albumSelection = MediaStore.Audio.Albums._ID + "==" + albumId; - - cursor = resolver.query(media, albumProjection, albumSelection, null, null); - if (cursor != null && cursor.moveToNext()) { - coverPath = cursor.getString(0); + if (cursor != null) { + if (cursor.moveToNext()) + populate(resolver, cursor); cursor.close(); } - return true; + return id != -1; + } + + private void populate(ContentResolver resolver, Cursor cursor) + { + id = cursor.getInt(0); + path = cursor.getString(1); + title = cursor.getString(2); + album = cursor.getString(3); + artist = cursor.getString(4); + long albumId = cursor.getLong(5); + + Uri media = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI; + String[] albumProjection = { MediaStore.Audio.Albums.ALBUM_ART }; + String albumSelection = MediaStore.Audio.Albums._ID + '=' + albumId; + + cursor = resolver.query(media, albumProjection, albumSelection, null, null); + if (cursor != null) { + if (cursor.moveToNext()) + coverPath = cursor.getString(0); + cursor.close(); + } } public static int[] getAllSongIds(String selection) @@ -128,25 +134,26 @@ public class Song implements Parcelable { if (type == SongData.FIELD_TITLE) return new int[] { id }; else if (type == SongData.FIELD_ALBUM) - return Song.getAllSongIds(MediaStore.Audio.Media.ALBUM_ID + "=" + id); + return Song.getAllSongIds(MediaStore.Audio.Media.ALBUM_ID + '=' + id); else if (type == SongData.FIELD_ARTIST) - return Song.getAllSongIds(MediaStore.Audio.Media.ARTIST_ID + "=" + id); + return Song.getAllSongIds(MediaStore.Audio.Media.ARTIST_ID + '=' + id); return null; } - public static boolean retrieveSongs() - { - mIds = getAllSongIds(null); - return mIds != null; - } - public void randomize() { - if (mIds == null) { - if (!retrieveSongs()) - id = -1; + id = -1; + + ContentResolver resolver = ContextApplication.getContext().getContentResolver(); + Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; + String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0"; + Cursor cursor = resolver.query(media, FILLED_PROJECTION, selection, null, null); + + if (cursor != null) { + if (cursor.moveToPosition(ContextApplication.getRandom().nextInt(cursor.getCount()))) + populate(resolver, cursor); + cursor.close(); } - id = mIds[ContextApplication.getRandom().nextInt(mIds.length)]; } public boolean equals(Song other)