Move state file code to PlaybackService and add a version code to state file
This commit is contained in:
parent
48e2aa1d91
commit
5341772d0c
@ -22,6 +22,9 @@
|
|||||||
|
|
||||||
package org.kreed.vanilla;
|
package org.kreed.vanilla;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.EOFException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
@ -50,6 +53,20 @@ import android.util.Log;
|
|||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
public final class PlaybackService extends Service implements Handler.Callback, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, SharedPreferences.OnSharedPreferenceChangeListener, SongTimeline.Callback {
|
public final class PlaybackService extends Service implements Handler.Callback, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, SharedPreferences.OnSharedPreferenceChangeListener, SongTimeline.Callback {
|
||||||
|
/**
|
||||||
|
* Name of the state file.
|
||||||
|
*/
|
||||||
|
private static final String STATE_FILE = "state";
|
||||||
|
/**
|
||||||
|
* Header for state file to help indicate if the file is in the right
|
||||||
|
* format.
|
||||||
|
*/
|
||||||
|
private static final long STATE_FILE_MAGIC = 0x1533574DC74B6ECL;
|
||||||
|
/**
|
||||||
|
* State file version that indicates data order.
|
||||||
|
*/
|
||||||
|
private static final int STATE_VERSION = 1;
|
||||||
|
|
||||||
private static final int NOTIFICATION_ID = 2;
|
private static final int NOTIFICATION_ID = 2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -186,7 +203,7 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
|
|
||||||
mTimeline = new SongTimeline(this);
|
mTimeline = new SongTimeline(this);
|
||||||
mTimeline.setCallback(this);
|
mTimeline.setCallback(this);
|
||||||
mPendingSeek = mTimeline.loadState();
|
int state = loadState();
|
||||||
|
|
||||||
mMediaPlayer = new MediaPlayer();
|
mMediaPlayer = new MediaPlayer();
|
||||||
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
||||||
@ -218,14 +235,6 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
|
|
||||||
initWidgets();
|
initWidgets();
|
||||||
|
|
||||||
int state = 0;
|
|
||||||
int finishAction = mTimeline.getFinishAction();
|
|
||||||
if (finishAction == SongTimeline.FINISH_RANDOM)
|
|
||||||
state |= FLAG_RANDOM;
|
|
||||||
else if (finishAction == SongTimeline.FINISH_REPEAT)
|
|
||||||
state |= FLAG_REPEAT;
|
|
||||||
if (mTimeline.isShuffling())
|
|
||||||
state |= FLAG_SHUFFLE;
|
|
||||||
updateState(state);
|
updateState(state);
|
||||||
setCurrentSong(0);
|
setCurrentSong(0);
|
||||||
|
|
||||||
@ -285,7 +294,7 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
stopForeground(true);
|
stopForeground(true);
|
||||||
|
|
||||||
if (mMediaPlayer != null) {
|
if (mMediaPlayer != null) {
|
||||||
mTimeline.saveState(mMediaPlayer.getCurrentPosition());
|
saveState(mMediaPlayer.getCurrentPosition());
|
||||||
mMediaPlayer.release();
|
mMediaPlayer.release();
|
||||||
mMediaPlayer = null;
|
mMediaPlayer = null;
|
||||||
}
|
}
|
||||||
@ -826,7 +835,7 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
case SAVE_STATE:
|
case SAVE_STATE:
|
||||||
// For unexpected terminations: crashes, task killers, etc.
|
// For unexpected terminations: crashes, task killers, etc.
|
||||||
// In most cases onDestroy will handle this
|
// In most cases onDestroy will handle this
|
||||||
mTimeline.saveState(0);
|
saveState(0);
|
||||||
break;
|
break;
|
||||||
case PROCESS_SONG:
|
case PROCESS_SONG:
|
||||||
processSong((Song)message.obj);
|
processSong((Song)message.obj);
|
||||||
@ -1135,4 +1144,60 @@ public final class PlaybackService extends Service implements Handler.Callback,
|
|||||||
{
|
{
|
||||||
sActivities.remove(activity);
|
sActivities.remove(activity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the service state, loading songs saved from the disk into the
|
||||||
|
* song timeline.
|
||||||
|
*
|
||||||
|
* @return The loaded value for mState.
|
||||||
|
*/
|
||||||
|
public int loadState()
|
||||||
|
{
|
||||||
|
int state = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
DataInputStream in = new DataInputStream(openFileInput(STATE_FILE));
|
||||||
|
|
||||||
|
if (in.readLong() == STATE_FILE_MAGIC && in.readInt() == STATE_VERSION) {
|
||||||
|
mPendingSeek = in.readInt();
|
||||||
|
mTimeline.readState(in);
|
||||||
|
|
||||||
|
int finishAction = mTimeline.getFinishAction();
|
||||||
|
if (finishAction == SongTimeline.FINISH_RANDOM)
|
||||||
|
state |= FLAG_RANDOM;
|
||||||
|
else if (finishAction == SongTimeline.FINISH_REPEAT)
|
||||||
|
state |= FLAG_REPEAT;
|
||||||
|
if (mTimeline.isShuffling())
|
||||||
|
state |= FLAG_SHUFFLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
in.close();
|
||||||
|
} catch (EOFException e) {
|
||||||
|
Log.w("VanillaMusic", "Failed to load state", e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.w("VanillaMusic", "Failed to load state", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save the service state to disk.
|
||||||
|
*
|
||||||
|
* @param pendingSeek The pendingSeek to store. Should be the current
|
||||||
|
* MediaPlayer position or 0.
|
||||||
|
*/
|
||||||
|
public void saveState(int pendingSeek)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
DataOutputStream out = new DataOutputStream(openFileOutput(STATE_FILE, 0));
|
||||||
|
out.writeLong(STATE_FILE_MAGIC);
|
||||||
|
out.writeInt(STATE_VERSION);
|
||||||
|
out.writeInt(pendingSeek);
|
||||||
|
mTimeline.writeState(out);
|
||||||
|
out.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.w("VanillaMusic", "Failed to save state", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,6 @@ import android.content.Context;
|
|||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.provider.MediaStore;
|
import android.provider.MediaStore;
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a series of songs that can be moved through backward or forward.
|
* Represents a series of songs that can be moved through backward or forward.
|
||||||
@ -82,23 +81,12 @@ public final class SongTimeline {
|
|||||||
*/
|
*/
|
||||||
public static final int MODE_ENQUEUE = 2;
|
public static final int MODE_ENQUEUE = 2;
|
||||||
|
|
||||||
/**
|
|
||||||
* Name of the state file.
|
|
||||||
*/
|
|
||||||
private static final String STATE_FILE = "state";
|
|
||||||
/**
|
|
||||||
* Header for state file to help indicate if the file is in the right
|
|
||||||
* format.
|
|
||||||
*/
|
|
||||||
private static final long STATE_FILE_MAGIC = 0xf89daa2fac33L;
|
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All the songs currently contained in the timeline. Each Song object
|
* All the songs currently contained in the timeline. Each Song object
|
||||||
* should be unique, even if it refers to the same media.
|
* should be unique, even if it refers to the same media.
|
||||||
*/
|
*/
|
||||||
private ArrayList<Song> mSongs;
|
private ArrayList<Song> mSongs = new ArrayList<Song>();
|
||||||
/**
|
/**
|
||||||
* The position of the current song (i.e. the playing song).
|
* The position of the current song (i.e. the playing song).
|
||||||
*/
|
*/
|
||||||
@ -176,19 +164,14 @@ public final class SongTimeline {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the timeline with the state stored in the state file created
|
* Initializes the timeline with data read from the stream. Data should have
|
||||||
* by a call to save state.
|
* been saved by a call to {@link SongTimeline#writeState(DataOutputStream)}.
|
||||||
*
|
*
|
||||||
* @return The optional extra data, or -1 if loading failed
|
* @param in The stream to read from.
|
||||||
*/
|
*/
|
||||||
public int loadState()
|
public void readState(DataInputStream in) throws IOException, EOFException
|
||||||
{
|
{
|
||||||
int extra = -1;
|
|
||||||
|
|
||||||
try {
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
DataInputStream in = new DataInputStream(mContext.openFileInput(STATE_FILE));
|
|
||||||
if (in.readLong() == STATE_FILE_MAGIC) {
|
|
||||||
int n = in.readInt();
|
int n = in.readInt();
|
||||||
if (n > 0) {
|
if (n > 0) {
|
||||||
ArrayList<Song> songs = new ArrayList<Song>(n);
|
ArrayList<Song> songs = new ArrayList<Song>(n);
|
||||||
@ -247,36 +230,18 @@ public final class SongTimeline {
|
|||||||
mCurrentPos = Math.min(mSongs == null ? 0 : mSongs.size(), in.readInt());
|
mCurrentPos = Math.min(mSongs == null ? 0 : mSongs.size(), in.readInt());
|
||||||
mFinishAction = in.readInt();
|
mFinishAction = in.readInt();
|
||||||
mShuffle = in.readBoolean();
|
mShuffle = in.readBoolean();
|
||||||
extra = in.readInt();
|
|
||||||
|
|
||||||
in.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (EOFException e) {
|
|
||||||
Log.w("VanillaMusic", "Failed to load state", e);
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.w("VanillaMusic", "Failed to load state", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mSongs == null)
|
|
||||||
mSongs = new ArrayList<Song>();
|
|
||||||
|
|
||||||
return extra;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a byte array representing the current state of the timeline.
|
* Writes the current songs and state to the given stream.
|
||||||
* This can be passed to the appropriate constructor to initialize the
|
|
||||||
* timeline with this state.
|
|
||||||
*
|
*
|
||||||
* @param extra Optional extra data to be included. Should not be -1.
|
* @param out The stream to write to.
|
||||||
*/
|
*/
|
||||||
public void saveState(int extra)
|
public void writeState(DataOutputStream out) throws IOException
|
||||||
{
|
{
|
||||||
try {
|
// Must update PlaybackService.STATE_VERSION when changing behavior
|
||||||
DataOutputStream out = new DataOutputStream(mContext.openFileOutput(STATE_FILE, 0));
|
// here.
|
||||||
out.writeLong(STATE_FILE_MAGIC);
|
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
ArrayList<Song> songs = mSongs;
|
ArrayList<Song> songs = mSongs;
|
||||||
|
|
||||||
@ -297,12 +262,6 @@ public final class SongTimeline {
|
|||||||
out.writeInt(mCurrentPos);
|
out.writeInt(mCurrentPos);
|
||||||
out.writeInt(mFinishAction);
|
out.writeInt(mFinishAction);
|
||||||
out.writeBoolean(mShuffle);
|
out.writeBoolean(mShuffle);
|
||||||
out.writeInt(extra);
|
|
||||||
}
|
|
||||||
|
|
||||||
out.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.w("VanillaMusic", "Failed to save state", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user