Hopefully fixed playlist state save bug

This commit is contained in:
Karim Abou Zeid 2015-02-28 15:25:45 +01:00
commit 2fe7ead73d
2 changed files with 33 additions and 23 deletions

View file

@ -15,7 +15,7 @@ import java.util.List;
*/ */
public class SongLoader { public class SongLoader {
public static List<Song> getAllSongs(Context context) { public static List<Song> getAllSongs(Context context) {
Cursor cursor = makeAlbumSongCursor(context); Cursor cursor = makeSongCursor(context);
List<Song> songs = new ArrayList<>(); List<Song> songs = new ArrayList<>();
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
do { do {
@ -39,9 +39,11 @@ public class SongLoader {
return songs; return songs;
} }
public static final Cursor makeAlbumSongCursor(final Context context) { public static final Cursor makeSongCursor(final Context context) {
final StringBuilder selection = new StringBuilder(); return makeSongCursor(context, (MediaStore.Audio.AudioColumns.IS_MUSIC + "=1"));
selection.append(MediaStore.Audio.AudioColumns.IS_MUSIC + "=1"); }
private static final Cursor makeSongCursor(final Context context, final String selection) {
return context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, return context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{ new String[]{
/* 0 */ /* 0 */
@ -60,11 +62,11 @@ public class SongLoader {
MediaStore.Audio.AudioColumns.ARTIST_ID, MediaStore.Audio.AudioColumns.ARTIST_ID,
/* 7 */ /* 7 */
MediaStore.Audio.AudioColumns.ALBUM_ID MediaStore.Audio.AudioColumns.ALBUM_ID
}, selection.toString(), null, null); }, selection, null, null);
} }
public static List<Song> getSongs(Context context, String query) { public static List<Song> getSongs(Context context, String query) {
Cursor cursor = makeAlbumSongCursor(context); Cursor cursor = makeSongCursor(context);
List<Song> songs = new ArrayList<>(); List<Song> songs = new ArrayList<>();
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
do { do {
@ -91,23 +93,18 @@ public class SongLoader {
} }
public static Song getSong(Context context, int queryId) { public static Song getSong(Context context, int queryId) {
Cursor cursor = makeAlbumSongCursor(context); Cursor cursor = makeSongCursor(context, MediaStore.Audio.AudioColumns._ID + "=" + queryId);
Song song = null; Song song = null;
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
do { final int id = cursor.getInt(0);
final int id = cursor.getInt(0); final String songName = cursor.getString(1);
if (id == queryId) { final String artist = cursor.getString(2);
final String songName = cursor.getString(1); final String album = cursor.getString(3);
final String artist = cursor.getString(2); final long duration = cursor.getLong(4);
final String album = cursor.getString(3); final int trackNumber = cursor.getInt(5);
final long duration = cursor.getLong(4); final int artistId = cursor.getInt(6);
final int trackNumber = cursor.getInt(5); final int albumId = cursor.getInt(7);
final int artistId = cursor.getInt(6); song = new Song(id, albumId, artistId, songName, artist, album, duration, trackNumber);
final int albumId = cursor.getInt(7);
song = new Song(id, albumId, artistId, songName, artist, album, duration, trackNumber);
break;
}
} while (cursor.moveToNext());
} }
if (cursor != null) { if (cursor != null) {
cursor.close(); cursor.close();

View file

@ -3,6 +3,7 @@ package com.kabouzeid.materialmusic.util;
import android.content.Context; import android.content.Context;
import android.util.Log; import android.util.Log;
import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
@ -17,18 +18,30 @@ public final class InternalStorageUtil {
public static synchronized void writeObject(final Context context, final String key, final Object object) throws IOException { public static synchronized void writeObject(final Context context, final String key, final Object object) throws IOException {
try { try {
String tempFileName = "TEMP_" + key;
FileOutputStream fos; FileOutputStream fos;
fos = context.openFileOutput(key, Context.MODE_PRIVATE); fos = context.openFileOutput(tempFileName, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos); ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object); oos.writeObject(object);
oos.close(); oos.close();
fos.close(); fos.close();
renameAppFile(context, tempFileName, key);
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "Writing Object to internal storage failed! Maybe the Object is not serializable?", e); Log.e(TAG, "Writing Object to internal storage failed! Maybe the Object is not serializable?", e);
} }
} }
public static synchronized Object readObject(Context context, String key) throws IOException, public static synchronized void renameAppFile(final Context context, String originalFileName, String newFileName) {
File originalFile = context.getFileStreamPath(originalFileName);
File newFile = new File(originalFile.getParent(), newFileName);
if (newFile.exists()) {
context.deleteFile(newFileName);
}
//noinspection ResultOfMethodCallIgnored
originalFile.renameTo(newFile);
}
public static synchronized Object readObject(final Context context, String key) throws IOException,
ClassNotFoundException { ClassNotFoundException {
FileInputStream fis = context.openFileInput(key); FileInputStream fis = context.openFileInput(key);
ObjectInputStream ois = new ObjectInputStream(fis); ObjectInputStream ois = new ObjectInputStream(fis);