/**
   * This is called from the thread with a message containing updated info of what's currently
   * playing.
   *
   * @param msg Message object containing currently playing info
   */
  public synchronized boolean handleMessage(Message msg) {
    final Bundle data = msg.getData();
    final ICurrentlyPlaying currentlyPlaying =
        (ICurrentlyPlaying) data.getSerializable(NowPlayingPollerThread.BUNDLE_CURRENTLY_PLAYING);
    switch (msg.what) {
      case NowPlayingPollerThread.MESSAGE_PROGRESS_CHANGED:
        mPlayStatus = currentlyPlaying.getPlayStatus();
        if (currentlyPlaying.isPlaying()) {
          mPlaylistActivity.setTime(Song.getDuration(currentlyPlaying.getTime() + 1));
        } else {
          mPlaylistActivity.clear();
        }
        return true;

      case NowPlayingPollerThread.MESSAGE_PLAYLIST_ITEM_CHANGED:
        mLastPosition = data.getInt(NowPlayingPollerThread.BUNDLE_LAST_PLAYPOSITION);
        onTrackChanged(currentlyPlaying);
        return true;

      case NowPlayingPollerThread.MESSAGE_PLAYSTATE_CHANGED:
        mPlayListId = data.getInt(NowPlayingPollerThread.BUNDLE_LAST_PLAYLIST);
        return true;

      case MESSAGE_PLAYLIST_SIZE:
        final int size = msg.getData().getInt(BUNDLE_PLAYLIST_SIZE);
        mPlaylistActivity.setNumItems(size == 0 ? "empty" : size + " tracks");
        return true;

      case NowPlayingPollerThread.MESSAGE_CONNECTION_ERROR:
      case NowPlayingPollerThread.MESSAGE_RECONFIGURE:
        mPlayStatus = PlayStatus.UNKNOWN;
        return true;

      default:
        return false;
    }
  }
  public void onCreate(final PlaylistActivity activity, Handler handler, final AbsListView list) {

    mPlaylistActivity = activity;
    mMusicManager = ManagerFactory.getMusicManager(this);
    mControlManager = ManagerFactory.getControlManager(this);
    mEventClient = ManagerFactory.getEventClientManager(this);
    mNowPlayingHandler = new Handler(this);

    if (!isCreated()) {
      super.onCreate(activity, handler, list);

      activity.registerForContextMenu(mList);

      mFallbackBitmap =
          BitmapFactory.decodeResource(activity.getResources(), R.drawable.icon_song_light);
      sPlayingBitmap = BitmapFactory.decodeResource(activity.getResources(), R.drawable.icon_play);

      mMusicManager.getPlaylistPosition(
          new DataResponse<Integer>() {
            public void run() {
              mCurrentPosition = value;
            }
          },
          mActivity.getApplicationContext());

      mMusicManager.getPlaylist(
          new DataResponse<ArrayList<String>>() {
            public void run() {
              if (value.size() > 0) {
                final ArrayList<PlaylistItem> items = new ArrayList<PlaylistItem>();
                int i = 0;
                for (String path : value) {
                  items.add(new PlaylistItem(path, i++));
                }
                setTitle(
                    "Music playlist ("
                        + (value.size() > MusicClient.PLAYLIST_LIMIT
                            ? MusicClient.PLAYLIST_LIMIT + "+"
                            : value.size())
                        + ")");
                mSongAdapter = new SongAdapter(activity, items);
                mList.setAdapter(mSongAdapter);
                if (mCurrentPosition >= 0) {
                  mList.setSelection(mCurrentPosition);
                }
              } else {
                setTitle("Music playlist");
                setNoDataMessage("No tracks in playlist.", R.drawable.icon_playlist_dark);
              }
            }
          },
          mActivity.getApplicationContext());
      mList.setOnItemClickListener(
          new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              final PlaylistItem item =
                  (PlaylistItem) mList.getAdapter().getItem(((OneLabelItemView) view).position);
              final DataResponse<Boolean> doNothing = new DataResponse<Boolean>();
              mControlManager.setPlaylistId(
                  doNothing, mPlayListId < 0 ? 0 : mPlayListId, mActivity.getApplicationContext());
              mMusicManager.setPlaylistSong(
                  doNothing, item.position, mActivity.getApplicationContext());
            }
          });
      mList.setOnKeyListener(new ListControllerOnKeyListener<Song>());
      setTitle("Music playlist...");
    }
  }