Refactored ListView mode changes into seperate methods.

This commit is contained in:
Felix Ableitner 2013-12-13 21:14:42 +01:00
parent 956a16c5cc
commit 33c6eababa
3 changed files with 72 additions and 48 deletions

View file

@ -179,14 +179,9 @@ public class RouteFragment extends MediaRouteDiscoveryFragment implements
if (savedInstanceState != null) { if (savedInstanceState != null) {
mListView.onRestoreInstanceState(savedInstanceState.getParcelable("list_state")); mListView.onRestoreInstanceState(savedInstanceState.getParcelable("list_state"));
if (savedInstanceState.getBoolean("route_selected")) { if (savedInstanceState.getBoolean("route_selected"))
mSelectedRoute = MediaRouter.getInstance(getActivity()) playlistMode(MediaRouter.getInstance(getActivity())
.getSelectedRoute(); .getSelectedRoute());
mListView.setAdapter(mPlaylistAdapter);
mControls.setVisibility(View.VISIBLE);
TextView emptyView = (TextView) mListView.getEmptyView();
emptyView.setText(R.string.playlist_empty);
}
} }
} }
@ -274,22 +269,40 @@ public class RouteFragment extends MediaRouteDiscoveryFragment implements
*/ */
@Override @Override
public void onItemClick(AdapterView<?> a, View v, final int position, long id) { public void onItemClick(AdapterView<?> a, View v, final int position, long id) {
if (mListView.getAdapter() == mRouteAdapter) { if (mListView.getAdapter() == mRouteAdapter)
mSelectedRoute = mRouteAdapter.getItem(position); playlistMode(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);
}
else else
mMediaRouterPlayService.getService().play(position); 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. * 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, public void onClick(DialogInterface dialog,
int which) { int which) {
mMediaRouterPlayService.getService().stop(); mMediaRouterPlayService.getService().stop();
mControls.setVisibility(View.GONE); deviceListMode();
mListView.setAdapter(mRouteAdapter);
disableTrackHighlight();
mSelectedRoute = null;
TextView emptyView = (TextView) mListView.getEmptyView();
emptyView.setText(R.string.device_list_empty);
} }
}) })
.setNegativeButton(android.R.string.no, null) .setNegativeButton(android.R.string.no, null)
.show(); .show();
} }
else { else
mControls.setVisibility(View.GONE); deviceListMode();
mListView.setAdapter(mRouteAdapter);
disableTrackHighlight();
mSelectedRoute = null;
TextView emptyView = (TextView) mListView.getEmptyView();
emptyView.setText(R.string.device_list_empty);
}
return true; return true;
} }
return false; return false;

View file

@ -215,13 +215,8 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene
*/ */
@Override @Override
public void onListItemClick(ListView l, View v, int position, long id) { public void onListItemClick(ListView l, View v, int position, long id) {
if (getListAdapter() == mServerAdapter) { if (getListAdapter() == mServerAdapter)
setListAdapter(mFileAdapter); browsingMode(mServerAdapter.getItem(position));
mCurrentServer = mServerAdapter.getItem(position);
getFiles(ROOT_DIRECTORY);
TextView emptyView = (TextView) getListView().getEmptyView();
emptyView.setText(R.string.folder_list_etmpy);
}
else if (getListAdapter() == mFileAdapter) { else if (getListAdapter() == mFileAdapter) {
if (mFileAdapter.getItem(position) instanceof Container) if (mFileAdapter.getItem(position) instanceof Container)
getFiles(((Container) mFileAdapter.getItem(position)).getId()); 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. * Opens a new directory and displays it.
*/ */
@ -307,13 +323,8 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene
mCurrentPath.pop(); mCurrentPath.pop();
mListState.pop(); mListState.pop();
if (mCurrentPath.empty()) { if (mCurrentPath.empty())
setListAdapter(mServerAdapter); serverMode();
getListView().onRestoreInstanceState(mListState.peek());
mCurrentServer = null;
TextView emptyView = (TextView) getListView().getEmptyView();
emptyView.setText(R.string.device_list_empty);
}
else else
getFiles(true); getFiles(true);
return true; return true;

View file

@ -292,14 +292,25 @@ public class MediaRouterPlayService extends Service {
mPlaylist = playlist; mPlaylist = playlist;
} }
/**
* Plays the track after current in the playlist.
*/
public void playNext() { public void playNext() {
play(mCurrentTrack + 1); play(mCurrentTrack + 1);
} }
/**
* Plays the track before current in the playlist.
*/
public void playPrevious() { public void playPrevious() {
play(mCurrentTrack - 1); play(mCurrentTrack - 1);
} }
/**
* Returns index of the track that is currently played (zero-based).
* @return
*/
public int getCurrentTrack() { public int getCurrentTrack() {
return mCurrentTrack; return mCurrentTrack;
} }