From 33c6eababa70565bdc52b474e73f20ab24c1b776 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 13 Dec 2013 21:14:42 +0100 Subject: [PATCH] Refactored ListView mode changes into seperate methods. --- .../controldlna/gui/RouteFragment.java | 70 ++++++++++--------- .../controldlna/gui/ServerFragment.java | 39 +++++++---- .../mediarouter/MediaRouterPlayService.java | 11 +++ 3 files changed, 72 insertions(+), 48 deletions(-) diff --git a/src/com/github/nutomic/controldlna/gui/RouteFragment.java b/src/com/github/nutomic/controldlna/gui/RouteFragment.java index c9a0d93..aee0875 100644 --- a/src/com/github/nutomic/controldlna/gui/RouteFragment.java +++ b/src/com/github/nutomic/controldlna/gui/RouteFragment.java @@ -179,14 +179,9 @@ public class RouteFragment extends MediaRouteDiscoveryFragment implements if (savedInstanceState != null) { mListView.onRestoreInstanceState(savedInstanceState.getParcelable("list_state")); - if (savedInstanceState.getBoolean("route_selected")) { - mSelectedRoute = MediaRouter.getInstance(getActivity()) - .getSelectedRoute(); - mListView.setAdapter(mPlaylistAdapter); - mControls.setVisibility(View.VISIBLE); - TextView emptyView = (TextView) mListView.getEmptyView(); - emptyView.setText(R.string.playlist_empty); - } + if (savedInstanceState.getBoolean("route_selected")) + playlistMode(MediaRouter.getInstance(getActivity()) + .getSelectedRoute()); } } @@ -274,22 +269,40 @@ public class RouteFragment extends MediaRouteDiscoveryFragment implements */ @Override public void onItemClick(AdapterView a, View v, final int position, long id) { - if (mListView.getAdapter() == mRouteAdapter) { - mSelectedRoute = mRouteAdapter.getItem(position); - mMediaRouterPlayService.getService().selectRoute(mSelectedRoute); - mListView.setAdapter(mPlaylistAdapter); - mControls.setVisibility(View.VISIBLE); - if (mStartPlayingOnSelect != -1) { - mMediaRouterPlayService.getService().play(mStartPlayingOnSelect); - mStartPlayingOnSelect = -1; - } - TextView emptyView = (TextView) mListView.getEmptyView(); - emptyView.setText(R.string.playlist_empty); - } + if (mListView.getAdapter() == mRouteAdapter) + playlistMode(mRouteAdapter.getItem(position)); else mMediaRouterPlayService.getService().play(position); } + /** + * Displays UPNP devices in the ListView. + */ + private void deviceListMode() { + mControls.setVisibility(View.GONE); + mListView.setAdapter(mRouteAdapter); + disableTrackHighlight(); + mSelectedRoute = null; + TextView emptyView = (TextView) mListView.getEmptyView(); + emptyView.setText(R.string.device_list_empty); + } + + /** + * Displays playlist for route in the ListView. + */ + private void playlistMode(RouteInfo route) { + mSelectedRoute = route; + mMediaRouterPlayService.getService().selectRoute(mSelectedRoute); + mListView.setAdapter(mPlaylistAdapter); + mControls.setVisibility(View.VISIBLE); + if (mStartPlayingOnSelect != -1) { + mMediaRouterPlayService.getService().play(mStartPlayingOnSelect); + mStartPlayingOnSelect = -1; + } + TextView emptyView = (TextView) mListView.getEmptyView(); + emptyView.setText(R.string.playlist_empty); + } + /** * Sets colored background on the item that is currently playing. */ @@ -330,25 +343,14 @@ public class RouteFragment extends MediaRouteDiscoveryFragment implements public void onClick(DialogInterface dialog, int which) { mMediaRouterPlayService.getService().stop(); - mControls.setVisibility(View.GONE); - mListView.setAdapter(mRouteAdapter); - disableTrackHighlight(); - mSelectedRoute = null; - TextView emptyView = (TextView) mListView.getEmptyView(); - emptyView.setText(R.string.device_list_empty); + deviceListMode(); } }) .setNegativeButton(android.R.string.no, null) .show(); } - else { - mControls.setVisibility(View.GONE); - mListView.setAdapter(mRouteAdapter); - disableTrackHighlight(); - mSelectedRoute = null; - TextView emptyView = (TextView) mListView.getEmptyView(); - emptyView.setText(R.string.device_list_empty); - } + else + deviceListMode(); return true; } return false; diff --git a/src/com/github/nutomic/controldlna/gui/ServerFragment.java b/src/com/github/nutomic/controldlna/gui/ServerFragment.java index a9df4df..d3d2d1c 100644 --- a/src/com/github/nutomic/controldlna/gui/ServerFragment.java +++ b/src/com/github/nutomic/controldlna/gui/ServerFragment.java @@ -215,13 +215,8 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene */ @Override public void onListItemClick(ListView l, View v, int position, long id) { - if (getListAdapter() == mServerAdapter) { - setListAdapter(mFileAdapter); - mCurrentServer = mServerAdapter.getItem(position); - getFiles(ROOT_DIRECTORY); - TextView emptyView = (TextView) getListView().getEmptyView(); - emptyView.setText(R.string.folder_list_etmpy); - } + if (getListAdapter() == mServerAdapter) + browsingMode(mServerAdapter.getItem(position)); else if (getListAdapter() == mFileAdapter) { if (mFileAdapter.getItem(position) instanceof Container) getFiles(((Container) mFileAdapter.getItem(position)).getId()); @@ -236,7 +231,28 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene } } } + + /** + * Displays available servers in the ListView. + */ + private void serverMode() { + setListAdapter(mServerAdapter); + getListView().onRestoreInstanceState(mListState.peek()); + mCurrentServer = null; + TextView emptyView = (TextView) getListView().getEmptyView(); + emptyView.setText(R.string.device_list_empty); + } + /** + * Displays files for server (starting from root). + */ + private void browsingMode(Device server) { + setListAdapter(mFileAdapter); + mCurrentServer = server; + getFiles(ROOT_DIRECTORY); + TextView emptyView = (TextView) getListView().getEmptyView(); + emptyView.setText(R.string.folder_list_etmpy); + } /** * Opens a new directory and displays it. */ @@ -307,13 +323,8 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene mCurrentPath.pop(); mListState.pop(); - if (mCurrentPath.empty()) { - setListAdapter(mServerAdapter); - getListView().onRestoreInstanceState(mListState.peek()); - mCurrentServer = null; - TextView emptyView = (TextView) getListView().getEmptyView(); - emptyView.setText(R.string.device_list_empty); - } + if (mCurrentPath.empty()) + serverMode(); else getFiles(true); return true; diff --git a/src/com/github/nutomic/controldlna/mediarouter/MediaRouterPlayService.java b/src/com/github/nutomic/controldlna/mediarouter/MediaRouterPlayService.java index 4d010bc..ee39d52 100644 --- a/src/com/github/nutomic/controldlna/mediarouter/MediaRouterPlayService.java +++ b/src/com/github/nutomic/controldlna/mediarouter/MediaRouterPlayService.java @@ -292,14 +292,25 @@ public class MediaRouterPlayService extends Service { mPlaylist = playlist; } + /** + * Plays the track after current in the playlist. + */ public void playNext() { play(mCurrentTrack + 1); } + + /** + * Plays the track before current in the playlist. + */ public void playPrevious() { play(mCurrentTrack - 1); } + /** + * Returns index of the track that is currently played (zero-based). + * @return + */ public int getCurrentTrack() { return mCurrentTrack; }