From adde0a4e956079ff6769923efe254d5625202ba9 Mon Sep 17 00:00:00 2001 From: Christopher Eby Date: Thu, 18 Mar 2010 19:27:15 -0500 Subject: [PATCH] Split service state handling code into discrete class --- src/org/kreed/vanilla/PlaybackService.java | 67 ++++----------- .../kreed/vanilla/PlaybackServiceState.java | 81 +++++++++++++++++++ 2 files changed, 96 insertions(+), 52 deletions(-) create mode 100644 src/org/kreed/vanilla/PlaybackServiceState.java diff --git a/src/org/kreed/vanilla/PlaybackService.java b/src/org/kreed/vanilla/PlaybackService.java index 200a846a..fd0dd44d 100644 --- a/src/org/kreed/vanilla/PlaybackService.java +++ b/src/org/kreed/vanilla/PlaybackService.java @@ -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(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 timeline = new ArrayList(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(); 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 { diff --git a/src/org/kreed/vanilla/PlaybackServiceState.java b/src/org/kreed/vanilla/PlaybackServiceState.java new file mode 100644 index 00000000..f7af6f28 --- /dev/null +++ b/src/org/kreed/vanilla/PlaybackServiceState.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2010 Christopher Eby + * + * 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 . + */ + +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 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); + } + } +} \ No newline at end of file