From 1a24a3df4d626851406f149228b50e43929c2987 Mon Sep 17 00:00:00 2001 From: Christopher Eby Date: Sun, 18 Sep 2011 15:54:34 -0500 Subject: [PATCH] Avoid using intents in broadcasts --- src/org/kreed/vanilla/CoverView.java | 18 +------- src/org/kreed/vanilla/PlaybackActivity.java | 40 +++++++++-------- src/org/kreed/vanilla/PlaybackService.java | 48 +++++++-------------- src/org/kreed/vanilla/Song.aidl | 25 ----------- src/org/kreed/vanilla/Song.java | 41 +----------------- 5 files changed, 38 insertions(+), 134 deletions(-) delete mode 100644 src/org/kreed/vanilla/Song.aidl diff --git a/src/org/kreed/vanilla/CoverView.java b/src/org/kreed/vanilla/CoverView.java index 15ccdf88..c1d4be16 100644 --- a/src/org/kreed/vanilla/CoverView.java +++ b/src/org/kreed/vanilla/CoverView.java @@ -23,7 +23,6 @@ package org.kreed.vanilla; import android.content.Context; -import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; @@ -338,7 +337,7 @@ public final class CoverView extends View implements Handler.Callback { * Set the Song at position i to song, generating * the bitmap for it in the background if needed. */ - private void setSong(int i, final Song song) + public void setSong(int i, Song song) { mSongs[i] = song; if (song != null) @@ -358,21 +357,6 @@ public final class CoverView extends View implements Handler.Callback { invalidate(); } - /** - * Handle an intent broadcasted by the PlaybackService. This must be called - * to react to song changes in PlaybackService. - * - * @param intent The intent that was broadcast - */ - public void receive(Intent intent) - { - String action = intent.getAction(); - if (PlaybackService.EVENT_REPLACE_SONG.equals(action)) { - int i = STORE_SIZE / 2 + intent.getIntExtra("pos", 0); - setSong(i, (Song)intent.getParcelableExtra("song")); - } - } - /** * Call {@link CoverView#generateBitmap(Song)} for the given song. * diff --git a/src/org/kreed/vanilla/PlaybackActivity.java b/src/org/kreed/vanilla/PlaybackActivity.java index b0b00d3c..5bf57513 100644 --- a/src/org/kreed/vanilla/PlaybackActivity.java +++ b/src/org/kreed/vanilla/PlaybackActivity.java @@ -209,6 +209,15 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View } } + /** + * Called by PlaybackService to update the state. + */ + public void setState(long uptime, int state) + { + if (uptime > mLastStateEvent) + setState(state); + } + /** * Sets up components when the PlaybackService is initialized and available to * interact with. Override to implement further post-initialization behavior. @@ -243,29 +252,22 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View } /** - * Called by PlaybackService when it broadcasts an intent. - * - * @param intent The intent that was broadcast. + * Called by PlaybackService to update the current song. */ - public void receive(Intent intent) + public void setSong(long uptime, Song song) { - String action = intent.getAction(); + if (uptime > mLastSongEvent) + setSong(song); + } - if (PlaybackService.EVENT_CHANGED.equals(action)) { - long time = intent.getLongExtra("time", -1); - assert(time != -1); - - int state = intent.getIntExtra("state", -1); - if (state != -1 && time > mLastStateEvent) - setState(state); - - if (intent.hasExtra("song") && time > mLastSongEvent) { - Song song = intent.getParcelableExtra("song"); - setSong(song); - } - } + /** + * Called by PlaybackService to update an active song (next, previous, or + * current). + */ + public void replaceSong(int delta, Song song) + { if (mCoverView != null) - mCoverView.receive(intent); + mCoverView.setSong(delta + 1, song); } /** diff --git a/src/org/kreed/vanilla/PlaybackService.java b/src/org/kreed/vanilla/PlaybackService.java index 96feab80..7ccb7292 100644 --- a/src/org/kreed/vanilla/PlaybackService.java +++ b/src/org/kreed/vanilla/PlaybackService.java @@ -97,9 +97,6 @@ public final class PlaybackService extends Service implements Handler.Callback, */ public static final String ACTION_PREVIOUS_SONG_AUTOPLAY = "org.kreed.vanilla.action.PREVIOUS_SONG_AUTOPLAY"; - public static final String EVENT_REPLACE_SONG = "org.kreed.vanilla.event.REPLACE_SONG"; - public static final String EVENT_CHANGED = "org.kreed.vanilla.event.CHANGED"; - /** * Set when there is no media available on the device. */ @@ -454,22 +451,19 @@ public final class PlaybackService extends Service implements Handler.Callback, } } - private void broadcast(Intent intent) - { - ArrayList list = sActivities; - for (int i = list.size(); --i != -1; ) - list.get(i).receive(intent); - } - private void broadcastChange(int state, Song song, long uptime) { - Intent intent = new Intent(EVENT_CHANGED); - if (state != -1) - intent.putExtra("state", state); - if (song != null) - intent.putExtra("song", song); - intent.putExtra("time", uptime); - broadcast(intent); + if (state != -1) { + ArrayList list = sActivities; + for (int i = list.size(); --i != -1; ) + list.get(i).setState(uptime, state); + } + + if (song != null) { + ArrayList list = sActivities; + for (int i = list.size(); --i != -1; ) + list.get(i).setSong(uptime, song); + } updateWidgets(); @@ -801,14 +795,6 @@ public final class PlaybackService extends Service implements Handler.Callback, * obj should an Integer representing the delta to pass to go. */ private static final int CALL_GO = 8; - /** - * Broadcast the given intent with ContextApplication. - * - * obj should contain the intent to broadcast. - * - * @see ContextApplication#broadcast(Intent) - */ - private static final int BROADCAST = 9; private static final int BROADCAST_CHANGE = 10; private static final int SAVE_STATE = 12; private static final int PROCESS_SONG = 13; @@ -860,9 +846,6 @@ public final class PlaybackService extends Service implements Handler.Callback, case PROCESS_STATE: processNewState(message.arg1, message.arg2); break; - case BROADCAST: - broadcast((Intent)message.obj); - break; case BROADCAST_CHANGE: broadcastChange(message.arg1, (Song)message.obj, message.getWhen()); break; @@ -916,13 +899,12 @@ public final class PlaybackService extends Service implements Handler.Callback, @Override public void activeSongReplaced(int delta, Song song) { + ArrayList list = sActivities; + for (int i = list.size(); --i != -1; ) + list.get(i).replaceSong(delta, song); + if (delta == 0) setCurrentSong(0); - - Intent intent = new Intent(EVENT_REPLACE_SONG); - intent.putExtra("pos", delta); - intent.putExtra("song", song); - mHandler.sendMessage(mHandler.obtainMessage(BROADCAST, intent)); } /** diff --git a/src/org/kreed/vanilla/Song.aidl b/src/org/kreed/vanilla/Song.aidl deleted file mode 100644 index 5b2c63d1..00000000 --- a/src/org/kreed/vanilla/Song.aidl +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) 2010 Christopher Eby - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -package org.kreed.vanilla; - -parcelable Song; \ No newline at end of file diff --git a/src/org/kreed/vanilla/Song.java b/src/org/kreed/vanilla/Song.java index 71d5572a..2f9ff3ce 100644 --- a/src/org/kreed/vanilla/Song.java +++ b/src/org/kreed/vanilla/Song.java @@ -28,9 +28,7 @@ import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; -import android.os.Parcel; import android.os.ParcelFileDescriptor; -import android.os.Parcelable; import android.provider.MediaStore; import java.io.FileDescriptor; @@ -40,7 +38,7 @@ import java.io.FileNotFoundException; * Represents a Song backed by the MediaStore. Includes basic metadata and * utilities to retrieve songs from the MediaStore. */ -public class Song implements Parcelable { +public class Song { /** * Indicates that this song was randomly selected from all songs. */ @@ -363,43 +361,6 @@ public class Song implements Parcelable { return song.id; } - public static Parcelable.Creator CREATOR = new Parcelable.Creator() { - public Song createFromParcel(Parcel in) - { - return new Song(in); - } - - public Song[] newArray(int size) - { - return new Song[size]; - } - }; - - public Song(Parcel in) - { - id = in.readLong(); - albumId = in.readLong(); - path = in.readString(); - title = in.readString(); - album = in.readString(); - artist = in.readString(); - } - - public void writeToParcel(Parcel out, int flags) - { - out.writeLong(id); - out.writeLong(albumId); - out.writeString(path); - out.writeString(title); - out.writeString(album); - out.writeString(artist); - } - - public int describeContents() - { - return 0; - } - private static final BitmapFactory.Options BITMAP_OPTIONS = new BitmapFactory.Options(); static {