Changed media browser to save ListView state (including scroll position).

This commit is contained in:
Felix Ableitner 2013-09-19 23:15:26 +02:00
parent 2926d3af2f
commit b9802245fe

View file

@ -50,6 +50,7 @@ import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.os.Parcelable;
import android.support.v4.app.ListFragment; import android.support.v4.app.ListFragment;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
@ -92,6 +93,11 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene
*/ */
private Stack<String> mCurrentPath = new Stack<String>(); private Stack<String> mCurrentPath = new Stack<String>();
/**
* Holds the scroll position in the list view at each directory level.
*/
private Stack<Parcelable> mListState = new Stack<Parcelable>();
/** /**
* Cling UPNP service. * Cling UPNP service.
*/ */
@ -132,7 +138,7 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene
new Intent(getActivity(), AndroidUpnpServiceImpl.class), new Intent(getActivity(), AndroidUpnpServiceImpl.class),
mUpnpServiceConnection, mUpnpServiceConnection,
Context.BIND_AUTO_CREATE Context.BIND_AUTO_CREATE
); );
} }
/** /**
@ -176,14 +182,15 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene
* Opens a new directory and displays it. * Opens a new directory and displays it.
*/ */
private void getFiles(String directory) { private void getFiles(String directory) {
mCurrentPath.push(directory); mListState.push(getListView().onSaveInstanceState());
getFiles(); mCurrentPath.push(directory);
getFiles(false);
} }
/** /**
* Displays the current directory on the ListView. * Displays the current directory on the ListView.
*/ */
private void getFiles() { private void getFiles(final boolean restoreListState) {
Service<?, ?> service = mCurrentServer.findService( Service<?, ?> service = mCurrentServer.findService(
new ServiceType("schemas-upnp-org", "ContentDirectory")); new ServiceType("schemas-upnp-org", "ContentDirectory"));
mUpnpService.getControlPoint().execute(new Browse(service, mUpnpService.getControlPoint().execute(new Browse(service,
@ -202,6 +209,10 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene
mFileAdapter.add(c); mFileAdapter.add(c);
for (Item i : didl.getItems()) for (Item i : didl.getItems())
mFileAdapter.add(i); mFileAdapter.add(i);
if (restoreListState)
getListView().onRestoreInstanceState(mListState.pop());
else
getListView().setSelectionFromTop(0, 0);
} }
}); });
} }
@ -228,13 +239,15 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene
public boolean onBackPressed() { public boolean onBackPressed() {
if (getListAdapter() == mServerAdapter) if (getListAdapter() == mServerAdapter)
return false; return false;
mCurrentPath.pop(); mCurrentPath.pop();
if (mCurrentPath.empty()) { if (mCurrentPath.empty()) {
setListAdapter(mServerAdapter); setListAdapter(mServerAdapter);
getListView().onRestoreInstanceState(mListState.pop());
mCurrentServer = null; mCurrentServer = null;
} }
else { else {
getFiles(); getFiles(true);
} }
return true; return true;
} }