Skip to content

Commit

Permalink
throw songs accross the IPC divide
Browse files Browse the repository at this point in the history
  • Loading branch information
cqr committed Jun 19, 2013
1 parent 09d868a commit 1f4ebee
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 42 deletions.
Expand Up @@ -68,4 +68,5 @@ interface IPlayerHaterServer {
Uri getSongAlbumArt(int songTag);
Uri getSongUri(int songTag);
Bundle getSongExtra(int songTag);
void slurp(int songTag, in Bundle songData);
}
5 changes: 5 additions & 0 deletions src/main/java/org/prx/playerhater/ipc/PlayerHaterServer.java
Expand Up @@ -216,4 +216,9 @@ public Bundle getSongExtra(int songTag) throws RemoteException {
public void setPendingIntent(PendingIntent intent) throws RemoteException {
mService.setPendingIntent(intent);
}

@Override
public void slurp(int songTag, Bundle songData) throws RemoteException {
SongHost.slurp(songTag, songData);
}
}
15 changes: 15 additions & 0 deletions src/main/java/org/prx/playerhater/ipc/ServerPlayerHater.java
Expand Up @@ -21,7 +21,9 @@
import org.prx.playerhater.util.Log;

import android.app.PendingIntent;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.SparseArray;

public class ServerPlayerHater extends PlayerHater {

Expand Down Expand Up @@ -272,4 +274,17 @@ public void setPendingIntent(PendingIntent intent) {
throw new IllegalStateException(SERVER_ERROR, e);
}
}

public void slurp(SparseArray<Bundle> songs) {
try {
int tag = 0;
for (int i = 0; i < songs.size(); i++) {
tag = songs.keyAt(i);
mServer.slurp(tag, songs.get(tag));
}
} catch (RemoteException e) {
Log.e(SERVER_ERROR, e);
throw new IllegalStateException(SERVER_ERROR, e);
}
}
}
Expand Up @@ -72,6 +72,10 @@ public synchronized void release() {
}

public synchronized void prepare(Context context, Uri uri) {
if (uri == null) {
throw new IllegalArgumentException(
"can't prepare a player for a null uri!");
}
if (!mMediaPlayers.containsKey(uri)) {
P player = getPlayer();
Log.d("Preparing " + player + " for " + uri);
Expand Down
20 changes: 4 additions & 16 deletions src/main/java/org/prx/playerhater/plugins/BackgroundedPlugin.java
Expand Up @@ -20,6 +20,7 @@
import android.net.Uri;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;

Expand All @@ -31,8 +32,8 @@
import java.util.HashSet;
import java.util.Set;

public class BackgroundedPlugin extends Thread implements PlayerHaterPlugin,
Callback {
public class BackgroundedPlugin extends HandlerThread implements
PlayerHaterPlugin, Callback {

private static final int SONG_CHANGED = 0;
private static final int DURATION_CHANGED = 1;
Expand All @@ -58,7 +59,6 @@ public class BackgroundedPlugin extends Thread implements PlayerHaterPlugin,
private static final Integer[] DEFAULT_FOREGROUND_ACTIONS = {
CHANGES_COMPLETE, SERVICE_BOUND, PLAYER_HATER_LOADED,
SERVICE_STOPPING };
private final Looper mLooper;
private final PlayerHaterPlugin mPlugin;
private final Set<Integer> mForegroundActions;
private TargetableHandler mHandler;
Expand All @@ -78,9 +78,8 @@ public BackgroundedPlugin(PlayerHaterPlugin plugin,
mPlugin = plugin;
mForegroundActions = new HashSet<Integer>(
Arrays.asList(foregroundActions));
mLooper = foregroundLooper;
mHandler = new TargetableHandler(Looper.getMainLooper(), this); // Temporary.
start();
mHandler = new HandlerPair(this, mForegroundActions, foregroundLooper);
}

@Override
Expand All @@ -93,13 +92,6 @@ public void onPlayerHaterLoaded(Context context, PlayerHater playerHater) {
}
}

@Override
public void run() {
Looper.prepare();
mHandler = new HandlerPair(this, mForegroundActions, mLooper);
Looper.loop();
}

@Override
public void onSongChanged(Song song) {
mHandler.removeTargettedMessages(CHANGES_COMPLETE_INTERNAL);
Expand Down Expand Up @@ -381,10 +373,6 @@ public TargetableHandler() {
super();
}

public TargetableHandler(Looper looper, Callback callback) {
super(looper, callback);
}

@SuppressWarnings("unused")
public Message obtainTargettedMessage(int what) {
return obtainMessage(what);
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/prx/playerhater/songs/RemoteSong.java
Expand Up @@ -28,13 +28,17 @@ private static SongHost.Remote getRemote() {
}

private final int mTag;
private Song mSong = null;

RemoteSong(int tag) {
mTag = tag;
}

@Override
public String getTitle() {
if (mSong != null) {
return mSong.getTitle();
}
try {
return getRemote().getSongTitle(mTag);
} catch (RemoteException e) {
Expand All @@ -45,6 +49,9 @@ public String getTitle() {

@Override
public String getArtist() {
if (mSong != null) {
return mSong.getArtist();
}
try {
return getRemote().getSongArtist(mTag);
} catch (RemoteException e) {
Expand All @@ -55,6 +62,9 @@ public String getArtist() {

@Override
public Uri getAlbumArt() {
if (mSong != null) {
return mSong.getAlbumArt();
}
try {
return getRemote().getSongAlbumArt(mTag);
} catch (RemoteException e) {
Expand All @@ -65,6 +75,9 @@ public Uri getAlbumArt() {

@Override
public Uri getUri() {
if (mSong != null) {
return mSong.getUri();
}
try {
return getRemote().getSongUri(mTag);
} catch (RemoteException e) {
Expand All @@ -75,6 +88,9 @@ public Uri getUri() {

@Override
public Bundle getExtra() {
if (mSong != null) {
return mSong.getExtra();
}
try {
return getRemote().getSongExtra(mTag);
} catch (RemoteException e) {
Expand All @@ -85,11 +101,18 @@ public Bundle getExtra() {

@Override
public String getAlbumTitle() {
if (mSong != null) {
return mSong.getAlbumTitle();
}
try {
return getRemote().getSongAlbumTitle(mTag);
} catch (RemoteException e) {
throw new IllegalStateException(
"Remote Process has died or become disconnected", e);
}
}

void setSong(Song song) {
mSong = song;
}
}
55 changes: 37 additions & 18 deletions src/main/java/org/prx/playerhater/songs/SongHost.java
Expand Up @@ -27,13 +27,13 @@
import android.util.SparseArray;

public class SongHost {
public static final int INVALID_TAG = -1;

public static final int INVALID_TAG = -1;

private static Remote sRemote;
private static SparseArray<Song> sSongs;
private static Map<Song, Integer> sTags;

public static void setRemote(Remote remote) {
sRemote = remote;
}
Expand All @@ -45,20 +45,37 @@ public static void setRemote(IPlayerHaterClient client) {
public static void setRemote(IPlayerHaterServer server) {
sRemote = new ServerRemote(server);
}


public static void slurp(int songTag, Bundle songData) {
Song song = getSong(songTag);
if (song instanceof RemoteSong) {
((RemoteSong) getSong(songTag)).setSong(Songs.fromBundle(songData));
}
}

public static SparseArray<Bundle> localSongs() {
SparseArray<Bundle> data = new SparseArray<Bundle>();
for (Song song : getTags().keySet()) {
if (!(song instanceof RemoteSong)) {
data.put(getTag(song), Songs.toBundle(song));
}
}
return data;
}

static Remote remote() {
return sRemote;
}

public static void clear() {
sRemote = null;
sSongs = null;
sTags = null;
sSongs = null;
sTags = null;
}

public static int getTag(Song song) {
if (song == null) {
return INVALID_TAG;
if (song == null) {
return INVALID_TAG;
}
if (getTags().containsKey(song)) {
return getTags().get(song);
Expand All @@ -69,9 +86,11 @@ public static int getTag(Song song) {
return tag;
}
}

public static Song getSong(int tag) {
if (tag == INVALID_TAG) { return null; }
if (tag == INVALID_TAG) {
return null;
}
Song song = getSongs().get(tag);
if (song != null) {
return song;
Expand All @@ -82,21 +101,21 @@ public static Song getSong(int tag) {
return song;
}
}

private static SparseArray<Song> getSongs() {
if (sSongs == null) {
sSongs = new SparseArray<Song>();
}
return sSongs;
}

private static Map<Song, Integer> getTags() {
if (sTags == null) {
sTags = new HashMap<Song, Integer>();
}
return sTags;
}

static interface Remote {
Uri getSongAlbumArt(int tag) throws RemoteException;

Expand All @@ -110,7 +129,7 @@ static interface Remote {

Bundle getSongExtra(int tag) throws RemoteException;
}

private static final class ClientRemote implements Remote {
private final IPlayerHaterClient mClient;

Expand Down Expand Up @@ -148,7 +167,7 @@ public String getSongAlbumTitle(int tag) throws RemoteException {
return mClient.getSongAlbumTitle(tag);
}
}

private static class ServerRemote implements Remote {
private final IPlayerHaterServer mServer;

Expand Down
21 changes: 13 additions & 8 deletions src/main/java/org/prx/playerhater/wrappers/BoundPlayerHater.java
Expand Up @@ -57,24 +57,28 @@ public class BoundPlayerHater extends PlayerHater {
private static PendingIntent sPendingPendingIntent;
private static int sPendingTransportControlFlags = -1;
private static int sStartSeekPosition = -1;

private static Handler getHandler() {
if (sHandler == null) {
sHandler = new Handler();
}
return sHandler;
}

private static Runnable getDisconnectRunnable() {
if (sRunnable == null) {
sRunnable = new Runnable() {

@Override
public void run() {
sApplicationContext.unbindService(sServiceConnection);
sPlayerHater = null;
synchronized (BoundPlayerHater.class) {
((ServerPlayerHater) sPlayerHater).slurp(SongHost
.localSongs());
sApplicationContext.unbindService(sServiceConnection);
sPlayerHater = null;
}
}

};
}
return sRunnable;
Expand Down Expand Up @@ -143,7 +147,8 @@ private static SongQueue getSongQueue() {
.setQueuedSongsChangedListener(new OnQueuedSongsChangedListener() {

@Override
public void onNowPlayingChanged(Song nowPlaying, Song was) {
public void onNowPlayingChanged(Song nowPlaying,
Song was) {
getPlugin().onSongChanged(nowPlaying);
}

Expand All @@ -170,7 +175,7 @@ public void onServiceConnected(ComponentName name, IBinder service) {
if (!(service instanceof PlayerHaterServer)) {
SongHost.setRemote(server);
}

try {
server.setClient(getPlayerHaterClient());
} catch (RemoteException e) {
Expand Down Expand Up @@ -393,7 +398,7 @@ public int enqueue(Song song) {
return getSongQueue().appendSong(song);
}
}

@Override
public void enqueue(int position, Song song) {
if (getPlayerHater() != null) {
Expand Down

0 comments on commit 1f4ebee

Please sign in to comment.