private void fetch() {
    final String title =
        mArtist != null
            ? mArtist.name + " - "
            : mGenre != null
                ? mGenre.name + " - "
                : "" + (mCompilationsOnly ? "Compilations" : "Albums");
    DataResponse<ArrayList<Album>> response =
        new DataResponse<ArrayList<Album>>() {
          public void run() {
            if (value.size() > 0) {
              setTitle(title + " (" + value.size() + ")");
              setAdapter(value);
            } else {
              setTitle(title);
              setNoDataMessage("No albums found.", R.drawable.default_album);
            }
          }
        };

    showOnLoading();
    setTitle(title + "...");
    if (mArtist != null) { // albums of an artist
      mMusicManager.getAlbums(response, mArtist, mActivity.getApplicationContext());
    } else if (mGenre != null) { // albums of a genre
      mMusicManager.getAlbums(response, mGenre, mActivity.getApplicationContext());
    } else if (mCompilationsOnly) { // compilations
      mMusicManager.getCompilations(response, mActivity.getApplicationContext());
    } else {
      mMusicManager.getAlbums(response, mActivity.getApplicationContext());
    }
  }
 public void onActivityPause() {
   if (mMusicManager != null) {
     mMusicManager.setController(null);
     mMusicManager.postActivity();
   }
   if (mControlManager != null) {
     mControlManager.setController(null);
   }
   super.onActivityPause();
 }
 public void onActivityPause() {
   ConnectionFactory.getNowPlayingPoller(mActivity.getApplicationContext())
       .unSubscribe(mNowPlayingHandler);
   if (mMusicManager != null) {
     mMusicManager.setController(null);
     mMusicManager.postActivity();
   }
   if (mControlManager != null) {
     mControlManager.setController(null);
   }
   if (mEventClient != null) {
     mEventClient.setController(null);
   }
   super.onActivityPause();
 }
 public void onActivityResume(Activity activity) {
   super.onActivityResume(activity);
   if (mMusicManager != null) {
     mMusicManager.setController(this);
   }
   if (mControlManager != null) {
     mControlManager.setController(this);
   }
 }
 public void onContextItemSelected(MenuItem item) {
   // be aware that this must be explicitly called by your activity!
   final PlaylistItem playlistItem =
       (PlaylistItem)
           mList
               .getAdapter()
               .getItem(
                   ((OneLabelItemView) ((AdapterContextMenuInfo) item.getMenuInfo()).targetView)
                       .position);
   switch (item.getItemId()) {
     case ITEM_CONTEXT_PLAY:
       mMusicManager.setPlaylistSong(
           new DataResponse<Boolean>(), playlistItem.position, mActivity.getApplicationContext());
       break;
     case ITEM_CONTEXT_REMOVE:
       mMusicManager.removeFromPlaylist(
           new DataResponse<Boolean>(), playlistItem.path, mActivity.getApplicationContext());
       break;
     default:
       return;
   }
 }
 public void onActivityResume(Activity activity) {
   super.onActivityResume(activity);
   ConnectionFactory.getNowPlayingPoller(activity.getApplicationContext())
       .subscribe(mNowPlayingHandler);
   if (mEventClient != null) {
     mEventClient.setController(this);
   }
   if (mMusicManager != null) {
     mMusicManager.setController(this);
   }
   if (mControlManager != null) {
     mControlManager.setController(this);
   }
 }
 public void onContextItemSelected(MenuItem item) {
   final Album album =
       (Album)
           mList
               .getAdapter()
               .getItem(
                   ((ThreeLabelsItemView) ((AdapterContextMenuInfo) item.getMenuInfo()).targetView)
                       .position);
   switch (item.getItemId()) {
     case ITEM_CONTEXT_QUEUE:
       mMusicManager.addToPlaylist(
           new QueryResponse(
               mActivity,
               "Adding album \"" + album.name + "\" by " + album.artist + " to playlist...",
               "Error adding album!"),
           album,
           mActivity.getApplicationContext());
       break;
     case ITEM_CONTEXT_PLAY:
       mMusicManager.play(
           new QueryResponse(
               mActivity,
               "Playing album \"" + album.name + "\" by " + album.artist + "...",
               "Error playing album!",
               true),
           album,
           mActivity.getApplicationContext());
       break;
     case ITEM_CONTEXT_INFO:
       DialogFactory.getAlbumDetail(
               mMusicManager, mActivity, album, mActivity.getApplicationContext())
           .show();
       break;
     default:
       return;
   }
 }
 @Override
 public void onOptionsItemSelected(MenuItem item) {
   final SharedPreferences.Editor ed;
   switch (item.getItemId()) {
     case MENU_PLAY_ALL:
       final Artist artist = mArtist;
       final Genre genre = mGenre;
       if (artist != null && genre == null) {
         mMusicManager.play(
             new QueryResponse(
                 mActivity,
                 "Playing all albums by " + artist.name + "...",
                 "Error playing songs!",
                 true),
             genre,
             mActivity.getApplicationContext());
       } else if (genre != null && artist == null) {
         mMusicManager.play(
             new QueryResponse(
                 mActivity,
                 "Playing all albums of genre " + genre.name + "...",
                 "Error playing songs!",
                 true),
             genre,
             mActivity.getApplicationContext());
       } else if (genre != null && artist != null) {
         mMusicManager.play(
             new QueryResponse(
                 mActivity,
                 "Playing all songs of genre " + genre.name + " by " + artist.name + "...",
                 "Error playing songs!",
                 true),
             artist,
             genre,
             mActivity.getApplicationContext());
       }
       break;
     case MENU_SORT_BY_ALBUM_ASC:
       ed = mActivity.getPreferences(Context.MODE_PRIVATE).edit();
       ed.putInt(
           AbstractManager.PREF_SORT_BY_PREFIX + AbstractManager.PREF_SORT_KEY_ALBUM,
           SortType.ALBUM);
       ed.putString(
           AbstractManager.PREF_SORT_ORDER_PREFIX + AbstractManager.PREF_SORT_KEY_ALBUM,
           SortType.ORDER_ASC);
       ed.commit();
       fetch();
       break;
     case MENU_SORT_BY_ALBUM_DESC:
       ed = mActivity.getPreferences(Context.MODE_PRIVATE).edit();
       ed.putInt(
           AbstractManager.PREF_SORT_BY_PREFIX + AbstractManager.PREF_SORT_KEY_ALBUM,
           SortType.ALBUM);
       ed.putString(
           AbstractManager.PREF_SORT_ORDER_PREFIX + AbstractManager.PREF_SORT_KEY_ALBUM,
           SortType.ORDER_DESC);
       ed.commit();
       fetch();
       break;
     case MENU_SORT_BY_ARTIST_ASC:
       ed = mActivity.getPreferences(Context.MODE_PRIVATE).edit();
       ed.putInt(
           AbstractManager.PREF_SORT_BY_PREFIX + AbstractManager.PREF_SORT_KEY_ALBUM,
           SortType.ARTIST);
       ed.putString(
           AbstractManager.PREF_SORT_ORDER_PREFIX + AbstractManager.PREF_SORT_KEY_ALBUM,
           SortType.ORDER_ASC);
       ed.commit();
       fetch();
       break;
     case MENU_SORT_BY_ARTIST_DESC:
       ed = mActivity.getPreferences(Context.MODE_PRIVATE).edit();
       ed.putInt(
           AbstractManager.PREF_SORT_BY_PREFIX + AbstractManager.PREF_SORT_KEY_ALBUM,
           SortType.ARTIST);
       ed.putString(
           AbstractManager.PREF_SORT_ORDER_PREFIX + AbstractManager.PREF_SORT_KEY_ALBUM,
           SortType.ORDER_DESC);
       ed.commit();
       fetch();
       break;
     case MENU_SORT_BY_YEAR_ASC:
       ed = mActivity.getPreferences(Context.MODE_PRIVATE).edit();
       ed.putInt(
           AbstractManager.PREF_SORT_BY_PREFIX + AbstractManager.PREF_SORT_KEY_ALBUM,
           SortType.YEAR);
       ed.putString(
           AbstractManager.PREF_SORT_ORDER_PREFIX + AbstractManager.PREF_SORT_KEY_ALBUM,
           SortType.ORDER_ASC);
       ed.commit();
       fetch();
       break;
     case MENU_SORT_BY_YEAR_DESC:
       ed = mActivity.getPreferences(Context.MODE_PRIVATE).edit();
       ed.putInt(
           AbstractManager.PREF_SORT_BY_PREFIX + AbstractManager.PREF_SORT_KEY_ALBUM,
           SortType.YEAR);
       ed.putString(
           AbstractManager.PREF_SORT_ORDER_PREFIX + AbstractManager.PREF_SORT_KEY_ALBUM,
           SortType.ORDER_DESC);
       ed.commit();
       fetch();
       break;
     case MENU_SWITCH_VIEW:
       mCurrentView = (mCurrentView % 2) + 1;
       fetch();
       break;
   }
 }
  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...");
    }
  }