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

View file

@ -3,6 +3,7 @@ package com.kabouzeid.materialmusic.util;
import android.content.Context;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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 {
try {
String tempFileName = "TEMP_" + key;
FileOutputStream fos;
fos = context.openFileOutput(key, Context.MODE_PRIVATE);
fos = context.openFileOutput(tempFileName, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object);
oos.close();
fos.close();
renameAppFile(context, tempFileName, key);
} catch (IOException 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 {
FileInputStream fis = context.openFileInput(key);
ObjectInputStream ois = new ObjectInputStream(fis);