Android Open Source - PlayerHater Song Host






From Project

Back to project page PlayerHater.

License

The source code is released under:

Apache License

If you think the Android project PlayerHater listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright 2013 Chris Rhoden, Rebecca Nesson, Public Radio Exchange
 * //from   www.j a  v  a  2 s.c o m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.prx.playerhater.songs;

import java.util.HashMap;
import java.util.Map;

import org.prx.playerhater.Song;
import org.prx.playerhater.ipc.IPlayerHaterClient;
import org.prx.playerhater.ipc.IPlayerHaterServer;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.SparseArray;

public class SongHost {

  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;
  }

  public static void setRemote(IPlayerHaterClient client) {
    sRemote = new ClientRemote(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() {
    if (sRemote == null) { 
      return new NullRemote(); 
    }
    return sRemote;
  }

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

  public static int getTag(Song song) {
    if (song == null) {
      return INVALID_TAG;
    }
    if (getTags().containsKey(song)) {
      return getTags().get(song);
    } else {
      int tag = song.hashCode();
      getTags().put(song, tag);
      getSongs().put(tag, song);
      return tag;
    }
  }

  public static Song getSong(int tag) {
    if (tag == INVALID_TAG) {
      return null;
    }
    Song song = getSongs().get(tag);
    if (song != null) {
      return song;
    } else {
      song = new RemoteSong(tag);
      getTags().put(song, tag);
      getSongs().put(tag, song);
      return song;
    }
  }
  
  public static Song getSong(int tag, Bundle songData) { 
    Song song = getSong(tag); 
    if (song instanceof RemoteSong) { 
      ((RemoteSong) song).setSong(Songs.fromBundle(songData));
    }
    return song; 
  }
  
  public static Song getLocalSong(int tag) { 
    Song song = getSongs().get(tag); 
    if (song instanceof RemoteSong) { 
      song = ((RemoteSong) song).getSong(); 
    }
    if (song == null) { 
      throw new IllegalStateException("No locally stored song data available."); 
    }
    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;

    Uri getSongUri(int tag) throws RemoteException;

    String getSongAlbumTitle(int tag) throws RemoteException;

    String getSongTitle(int tag) throws RemoteException;

    String getSongArtist(int tag) throws RemoteException;

    Bundle getSongExtra(int tag) throws RemoteException;
  }

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

    private ClientRemote(IPlayerHaterClient client) {
      mClient = client;
    }

    @Override
    public Uri getSongAlbumArt(int tag) throws RemoteException {
      return mClient.getSongAlbumArt(tag);
    }

    @Override
    public Uri getSongUri(int tag) throws RemoteException {
      return mClient.getSongUri(tag);
    }

    @Override
    public String getSongTitle(int tag) throws RemoteException {
      return mClient.getSongTitle(tag);
    }

    @Override
    public String getSongArtist(int tag) throws RemoteException {
      return mClient.getSongArtist(tag);
    }

    @Override
    public Bundle getSongExtra(int tag) throws RemoteException {
      return mClient.getSongExtra(tag);
    }

    @Override
    public String getSongAlbumTitle(int tag) throws RemoteException {
      return mClient.getSongAlbumTitle(tag);
    }
  }

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

    private ServerRemote(IPlayerHaterServer server) {
      mServer = server;
    }

    @Override
    public Uri getSongAlbumArt(int tag) throws RemoteException {
      return mServer.getSongAlbumArt(tag);
    }

    @Override
    public Uri getSongUri(int tag) throws RemoteException {
      return mServer.getSongUri(tag);
    }

    @Override
    public String getSongTitle(int tag) throws RemoteException {
      return mServer.getSongTitle(tag);
    }

    @Override
    public String getSongArtist(int tag) throws RemoteException {
      return mServer.getSongArtist(tag);
    }

    @Override
    public Bundle getSongExtra(int tag) throws RemoteException {
      return mServer.getSongExtra(tag);
    }

    @Override
    public String getSongAlbumTitle(int tag) throws RemoteException {
      return mServer.getSongAlbumTitle(tag);
    }
  }
  
  private static class NullRemote implements Remote {

    @Override
    public Uri getSongAlbumArt(int tag) throws RemoteException {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public Uri getSongUri(int tag) throws RemoteException {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public String getSongAlbumTitle(int tag) throws RemoteException {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public String getSongTitle(int tag) throws RemoteException {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public String getSongArtist(int tag) throws RemoteException {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public Bundle getSongExtra(int tag) throws RemoteException {
      // TODO Auto-generated method stub
      return null;
    }
    
  }
}




Java Source Code List

.AbstractPlugin.java
.AudioFocusPlugin.java
.BackgroundedPlugin.java
.BoundPlayerHater.java
.BroadcastReceiver.java
.ClientPlugin.java
.Config.java
.ExpandableNotificationPlugin.java
.HeadphoneButtonGestureHelper.java
.IPlayerHater.java
.LockScreenControlsPlugin.java
.Log.java
.MediaPlayerPool.java
.NotificationPlugin.java
.OnAudioFocusChangedListener.java
.PebblePlugin.java
.PlaybackService.java
.PlayerHaterClient.java
.PlayerHaterListenerPlugin.java
.PlayerHaterListener.java
.PlayerHaterPlugin.java
.PlayerHaterServer.java
.PlayerHaterService.java
.PlayerHater.java
.PlayerStateWatcher.java
.Player.java
.PlaylistParser.java
.PlaylistSupportingPlayer.java
.PluginCollection.java
.Receiver.java
.RemoteControlButtonReceiver.java
.RemoteSong.java
.ServerPlayerHater.java
.ServicePlayerHater.java
.SongHost.java
.SongQueue.java
.Song.java
.Songs.java
.StatelyPlayer.java
.SynchronousPlayer.java
.ThreadsafePlayerHater.java
.ThreadsafeServicePlayerHater.java
.TouchableNotificationPlugin.java
org.prx.playerhater.BroadcastReceiver.java
org.prx.playerhater.PlaybackService.java
org.prx.playerhater.PlayerHaterListener.java
org.prx.playerhater.PlayerHaterPlugin.java
org.prx.playerhater.PlayerHater.java
org.prx.playerhater.Song.java
org.prx.playerhater.broadcast.HeadphoneButtonGestureHelper.java
org.prx.playerhater.broadcast.OnAudioFocusChangedListener.java
org.prx.playerhater.broadcast.Receiver.java
org.prx.playerhater.broadcast.RemoteControlButtonReceiver.java
org.prx.playerhater.ipc.ClientPlugin.java
org.prx.playerhater.ipc.PlayerHaterClient.java
org.prx.playerhater.ipc.PlayerHaterServer.java
org.prx.playerhater.ipc.ServerPlayerHater.java
org.prx.playerhater.mediaplayer.MediaPlayerPool.java
org.prx.playerhater.mediaplayer.Player.java
org.prx.playerhater.mediaplayer.PlaylistSupportingPlayer.java
org.prx.playerhater.mediaplayer.StatelyPlayer.java
org.prx.playerhater.mediaplayer.SynchronousPlayer.java
org.prx.playerhater.plugins.AbstractPlugin.java
org.prx.playerhater.plugins.AudioFocusPlugin.java
org.prx.playerhater.plugins.BackgroundedPlugin.java
org.prx.playerhater.plugins.ExpandableNotificationPlugin.java
org.prx.playerhater.plugins.LockScreenControlsPlugin.java
org.prx.playerhater.plugins.NotificationPlugin.java
org.prx.playerhater.plugins.PebblePlugin.java
org.prx.playerhater.plugins.PlayerHaterListenerPlugin.java
org.prx.playerhater.plugins.PluginCollection.java
org.prx.playerhater.plugins.ScrubbableLockScreenControlsPlugin.java
org.prx.playerhater.plugins.TouchableNotificationPlugin.java
org.prx.playerhater.service.PlayerHaterService.java
org.prx.playerhater.service.PlayerStateWatcher.java
org.prx.playerhater.songs.RemoteSong.java
org.prx.playerhater.songs.SongHost.java
org.prx.playerhater.songs.SongQueue.java
org.prx.playerhater.songs.Songs.java
org.prx.playerhater.util.Config.java
org.prx.playerhater.util.IPlayerHater.java
org.prx.playerhater.util.Log.java
org.prx.playerhater.util.PlaylistParser.java
org.prx.playerhater.wrappers.BoundPlayerHater.java
org.prx.playerhater.wrappers.ServicePlayerHater.java
org.prx.playerhater.wrappers.ThreadsafePlayerHater.java
org.prx.playerhater.wrappers.ThreadsafeServicePlayerHater.java