Avoid using intents in broadcasts
This commit is contained in:
parent
34326718ad
commit
1a24a3df4d
@ -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 <code>i</code> to <code>song</code>, 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.
|
||||
*
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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<PlaybackActivity> 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<PlaybackActivity> list = sActivities;
|
||||
for (int i = list.size(); --i != -1; )
|
||||
list.get(i).setState(uptime, state);
|
||||
}
|
||||
|
||||
if (song != null) {
|
||||
ArrayList<PlaybackActivity> 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<PlaybackActivity> 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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,25 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2010 Christopher Eby <kreed@kreed.org>
|
||||
*
|
||||
* 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;
|
@ -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<Song> CREATOR = new Parcelable.Creator<Song>() {
|
||||
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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user