Split service state handling code into discrete class

This commit is contained in:
Christopher Eby committed 2010-03-18 19:49:11 -05:00
1 parent 11d175cb94
commit adde0a4e95
2 files changed
+96 -52

No files matched your search

+15 -52
View File
@@ -18,9 +18,6 @@
package org.kreed.vanilla;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -315,51 +312,32 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
boolean stateLoaded = true;
try {
DataInputStream in = new DataInputStream(openFileInput(STATE_FILE));
if (in.readLong() == STATE_FILE_MAGIC) {
mCurrentSong = in.readInt();
int n = in.readInt();
PlaybackServiceState state = new PlaybackServiceState();
if (state.load(this)) {
Song song = new Song(state.savedIds[state.savedIndex]);
if (song.populate())
broadcastChange(mState, mState, song);
else
stateLoaded = false;
if (n > 0) {
int[] ids = new int[n];
for (int i = 0; i != n; ++i)
ids[i] = in.readInt();
mPendingSeek = in.readInt();
in.close();
mSongTimeline = new ArrayList<Song>(state.savedIds.length);
mCurrentSong = state.savedIndex;
mPendingSeek = state.savedSeek;
Song song = new Song(ids[mCurrentSong]);
song.populate();
if (song.path == null)
stateLoaded = false;
else
broadcastChange(mState, mState, song);
ArrayList<Song> timeline = new ArrayList<Song>(n);
for (int i = 0; i != n; ++i)
timeline.add(i == mCurrentSong ? song : new Song(ids[i]));
mSongTimeline = timeline;
}
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
Log.w("VanillaMusic", e);
for (int i = 0; i != state.savedIds.length; ++i)
mSongTimeline.add(i == mCurrentSong ? song : new Song(state.savedIds[i]));
}
if (stateLoaded)
stateLoaded = mSongTimeline != null;
retrieveSongs();
if (!stateLoaded) {
retrieveSongs();
mSongTimeline = new ArrayList<Song>();
broadcastChange(mState, mState, getSong(0));
}
if (stateLoaded)
retrieveSongs();
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
@@ -639,24 +617,9 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
return song;
}
private static final String STATE_FILE = "state";
private static final long STATE_FILE_MAGIC = 0x8a9d3f9fca31L;
private void saveState(boolean savePosition)
{
try {
DataOutputStream out = new DataOutputStream(openFileOutput(STATE_FILE, 0));
out.writeLong(STATE_FILE_MAGIC);
out.writeInt(mCurrentSong);
int n = mSongTimeline == null ? 0 : mSongTimeline.size();
out.writeInt(n);
for (int i = 0; i != n; ++i)
out.writeInt(mSongTimeline.get(i).id);
out.writeInt(savePosition && mMediaPlayer != null ? mMediaPlayer.getCurrentPosition() : 0);
out.close();
} catch (IOException e) {
Log.w("VanillaMusic", e);
}
PlaybackServiceState.saveState(this, mSongTimeline, mCurrentSong, savePosition && mMediaPlayer != null ? mMediaPlayer.getCurrentPosition() : 0);
}
private class MusicHandler extends Handler {
@@ -0,0 +1,81 @@
/*
* Copyright (C) 2010 Christopher Eby <kreed@kreed.org>
*
* This file is part of Vanilla Music Player.
*
* Vanilla Music Player is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Vanilla Music Player is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.kreed.vanilla;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import android.content.Context;
import android.util.Log;
public class PlaybackServiceState {
private static final String STATE_FILE = "state";
private static final long STATE_FILE_MAGIC = 0x8a9d3f9fca31L;
public int savedIndex;
public int[] savedIds;
public int savedSeek;
public boolean load(Context context)
{
try {
DataInputStream in = new DataInputStream(context.openFileInput(STATE_FILE));
if (in.readLong() == STATE_FILE_MAGIC) {
savedIndex = in.readInt();
int n = in.readInt();
if (n > 0) {
savedIds = new int[n];
for (int i = 0; i != n; ++i)
savedIds[i] = in.readInt();
savedSeek = in.readInt();
return true;
}
in.close();
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
Log.w("VanillaMusic", e);
}
return false;
}
public static void saveState(Context context, List<Song> songs, int index, int seek)
{
try {
DataOutputStream out = new DataOutputStream(context.openFileOutput(STATE_FILE, 0));
out.writeLong(STATE_FILE_MAGIC);
out.writeInt(index);
int n = songs == null ? 0 : songs.size();
out.writeInt(n);
for (int i = 0; i != n; ++i)
out.writeInt(songs.get(i).id);
out.writeInt(seek);
out.close();
} catch (IOException e) {
Log.w("VanillaMusic", e);
}
}
}