@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // populate the optionsMenu setHasOptionsMenu(true); // set adapter mAdapter = new PlaylistsAdapter(); setListAdapter(mAdapter); // restore state restoreLocalState(savedInstanceState); // if not authorized, request auth // but only if the task wasnt restored on restoreLocalState if (!RdioComm.getComm().isAuthorized() && !isAuthTaskRunning()) { login(); } // if the adapter wasnt restored, fetch the adapter // but only if the task wasnt restored on restoreLocalState if (RdioComm.getComm().isAuthorized() && mAdapter.mPlaylists == null && !isAuthTaskRunning() && !isUserPlaylistsTaskRunning()) { mUserPlaylistsTask = (UserPlaylistsTask) new UserPlaylistsTask().execute(); } }
@Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.menu_new_playlist) { if (!RdioComm.getComm().isAuthorized()) { Toast.makeText(getActivity(), R.string.not_authorized, Toast.LENGTH_SHORT).show(); return true; } if (isAnyTaskRunning()) { Toast.makeText( getActivity().getApplicationContext(), R.string.operation_in_progress, Toast.LENGTH_SHORT) .show(); return true; } InputDialogFragment dialog = InputDialogFragment.newInstance(R.string.playlist_name, "playlistName"); dialog.showDialog(getActivity()); return true; } else if (item.getItemId() == R.id.menu_login) { if (isAnyTaskRunning()) { Toast.makeText(getActivity(), R.string.operation_in_progress, Toast.LENGTH_SHORT).show(); return true; } RdioComm.getComm().cleanAuthTokens(getActivity()); login(); return true; } else { return super.onOptionsItemSelected(item); } }
@Override protected Void doInBackground(Void... args) { try { if (isCancelled()) return null; if (mIsRequest) RdioComm.getComm().retrieveRequestToken(getActivity()); else RdioComm.getComm().retrieveAccessTokens(mVerifier, getActivity()); } catch (ServiceCommException e) { err = e.getMessage(getActivity()); return null; } return null; }
@Override protected ArrayList<PlaylistInfo> doInBackground(Void... arg0) { ArrayList<PlaylistInfo> playlists; try { playlists = RdioComm.getComm().getUserPlaylists(); } catch (ServiceCommException e) { err = e.getMessage(getActivity()); return null; } return playlists; }
@Override protected Void doInBackground(Void... params) { if (!RdioComm.getComm().isAuthorized()) { ServiceCommException e = new ServiceCommException(ServiceID.RDIO, ServiceErr.NOT_AUTH); err = e.getMessage(getActivity()); return null; } final AtomicInteger fetchCount = mFetchCount; final ArrayList<SongInfo> playlist = new ArrayList<SongInfo>(mPlaylist.subList(fetchCount.get(), mPlaylist.size())); for (SongInfo song : playlist) { if (isCancelled()) { return null; } try { mSongIds.add(RdioComm.getComm().queryTrackID(song.name, song.artist.name)); } catch (ServiceCommException e) { if (e.getErr() == ServiceErr.SONG_NOT_FOUND) { publishProgress(fetchCount.incrementAndGet()); Log.i( Util.APP, "Song [" + song.name + " - " + song.artist.name + "] not found in Rdio, ignoring..."); continue; } Log.w(Util.APP, "Failed to export playlist to Rdio!", e); err = e.getMessage(getActivity()); return null; } publishProgress(fetchCount.incrementAndGet()); } if (isCancelled()) { return null; } if (mSongIds.isEmpty()) { err = getString(R.string.no_song_found); return null; } publishProgress(-1); try { if (mChosenPlaylist.id != null) { // add songs to existing playlist, if id is given RdioComm.getComm().addToPlaylist(mChosenPlaylist.id, mSongIds, getActivity()); } else { // create new playlist if the name is given RdioComm.getComm().createPlaylist(mChosenPlaylist.name, mSongIds, getActivity()); } } catch (ServiceCommException e) { err = e.getMessage(getActivity()); } return null; }