Fix problem with back key and list going back to beginning after screen rotation.
This commit is contained in:
parent
a4a6ed2a78
commit
8fe3f814c8
|
@ -36,6 +36,17 @@ public class FileActivity extends BaseActivity {
|
|||
|
||||
private NavigationDrawerFragment navigationDrawerFragment;
|
||||
|
||||
public interface OnBackPressedListener {
|
||||
void onBackPressed();
|
||||
boolean currentPageAtRootDirectory();
|
||||
}
|
||||
|
||||
OnBackPressedListener fragmentBackListener;
|
||||
|
||||
public void setBackPressedListener(OnBackPressedListener listener) {
|
||||
fragmentBackListener = listener;
|
||||
}
|
||||
|
||||
@TargetApi(21)
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -91,6 +102,19 @@ public class FileActivity extends BaseActivity {
|
|||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
// tell fragment to move up one directory
|
||||
if (fragmentBackListener != null) {
|
||||
if (!fragmentBackListener.currentPageAtRootDirectory())
|
||||
fragmentBackListener.onBackPressed();
|
||||
else
|
||||
// already at the root level of the current page
|
||||
super.onBackPressed();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void setupActionBar(String title) {
|
||||
Toolbar toolbar = (Toolbar)findViewById(R.id.default_toolbar);
|
||||
setSupportActionBar(toolbar);
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
*/
|
||||
package org.xbmc.kore.ui;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.view.ViewPager;
|
||||
|
@ -32,16 +33,12 @@ import butterknife.InjectView;
|
|||
/**
|
||||
* Created by danhdroid on 3/18/15.
|
||||
*/
|
||||
public class FileListFragment extends Fragment {
|
||||
public class FileListFragment extends Fragment
|
||||
implements FileActivity.OnBackPressedListener {
|
||||
|
||||
@InjectView(R.id.pager_tab_strip) PagerSlidingTabStrip pagerTabStrip;
|
||||
@InjectView(R.id.pager) ViewPager viewPager;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_media_list, container, false);
|
||||
|
@ -51,9 +48,12 @@ public class FileListFragment extends Fragment {
|
|||
videoFileListArgs.putString(MediaFileListFragment.MEDIA_TYPE, Files.Media.VIDEO);
|
||||
Bundle musicFileListArgs = new Bundle();
|
||||
musicFileListArgs.putString(MediaFileListFragment.MEDIA_TYPE, Files.Media.MUSIC);
|
||||
Bundle pictureFileListArgs = new Bundle();
|
||||
pictureFileListArgs.putString(MediaFileListFragment.MEDIA_TYPE, Files.Media.PICTURES);
|
||||
TabsAdapter tabsAdapter = new TabsAdapter(getActivity(), getChildFragmentManager())
|
||||
.addTab(MediaFileListFragment.class, videoFileListArgs, R.string.video, 1)
|
||||
.addTab(MediaFileListFragment.class, musicFileListArgs, R.string.music, 2);
|
||||
.addTab(MediaFileListFragment.class, musicFileListArgs, R.string.music, 2)
|
||||
.addTab(MediaFileListFragment.class, pictureFileListArgs, R.string.pictures, 3);
|
||||
viewPager.setAdapter(tabsAdapter);
|
||||
pagerTabStrip.setViewPager(viewPager);
|
||||
return root;
|
||||
|
@ -65,4 +65,41 @@ public class FileListFragment extends Fragment {
|
|||
|
||||
setHasOptionsMenu(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Activity activity) {
|
||||
super.onAttach(activity);
|
||||
try {
|
||||
FileActivity listenerActivity = (FileActivity) activity;
|
||||
listenerActivity.setBackPressedListener(this);
|
||||
} catch (ClassCastException e) {
|
||||
throw new ClassCastException(activity.toString() + " unable to register BackPressedListener");
|
||||
}
|
||||
}
|
||||
|
||||
MediaFileListFragment findFragmentByPosition(int position) {
|
||||
String tag = "android:switcher:" + viewPager.getId() + ":" + position;
|
||||
return (MediaFileListFragment) getChildFragmentManager().findFragmentByTag(tag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
// tell current fragment to move up one directory
|
||||
|
||||
MediaFileListFragment curPage = findFragmentByPosition(viewPager.getCurrentItem() + 1);
|
||||
if (curPage != null) {
|
||||
// based on the current position cast the page to the correct
|
||||
// class and call the method
|
||||
curPage.onBackPressed();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean currentPageAtRootDirectory() {
|
||||
MediaFileListFragment curPage = findFragmentByPosition(viewPager.getCurrentItem() + 1);
|
||||
if (curPage != null) {
|
||||
return curPage.atRootDirectory();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -18,10 +18,10 @@ package org.xbmc.kore.ui;
|
|||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
@ -59,10 +59,14 @@ import butterknife.InjectView;
|
|||
* Presents a list of files of different types (Video/Music)
|
||||
*/
|
||||
public class MediaFileListFragment extends Fragment {
|
||||
|
||||
public static final String MEDIA_TYPE = "mediaType";
|
||||
public static final String PATH_CONTENTS = "pathContents";
|
||||
public static final String ROOT_PATH_CONTENTS = "rootPathContents";
|
||||
public static final String ROOT_VISITED = "rootVisited";
|
||||
private static final String ADDON_SOURCE = "addons:";
|
||||
private static final int MUSIC_PLAYLISTID = 0;
|
||||
private static final int VIDEO_PLAYLISTID = 1;
|
||||
private static final int PICTURE_PLAYLISTID = 2;
|
||||
|
||||
private HostManager hostManager;
|
||||
/**
|
||||
|
@ -92,18 +96,28 @@ public class MediaFileListFragment extends Fragment {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putString(MEDIA_TYPE, mediaType);
|
||||
try {
|
||||
outState.putParcelableArrayList(PATH_CONTENTS, (ArrayList) adapter.getFileItemList());
|
||||
} catch (NullPointerException npe) {
|
||||
// adapter is null probably nothing was save in bundle because the directory is empty
|
||||
// ignore this so that the empty message would display later on
|
||||
}
|
||||
outState.putParcelableArrayList(ROOT_PATH_CONTENTS, rootFileLocation);
|
||||
outState.putBoolean(ROOT_VISITED, browseRootAlready);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
|
||||
Bundle args = getArguments();
|
||||
if (args != null) {
|
||||
mediaType = args.getString(MEDIA_TYPE, Files.Media.MUSIC);
|
||||
if (mediaType.equalsIgnoreCase(Files.Media.VIDEO)) {
|
||||
playlistId = VIDEO_PLAYLISTID;
|
||||
} else if (mediaType.equalsIgnoreCase(Files.Media.PICTURES)) {
|
||||
playlistId = PICTURE_PLAYLISTID;
|
||||
}
|
||||
}
|
||||
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_playlist, container, false);
|
||||
|
@ -113,45 +127,66 @@ public class MediaFileListFragment extends Fragment {
|
|||
folderGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
FileLocation f = adapter.getItem(position);
|
||||
// if selection is a directory, browse the the level below
|
||||
if (f.isDirectory) {
|
||||
// a directory - store the path of this directory so that we can reverse travel if
|
||||
// we want to
|
||||
if (f.isRootDir()) {
|
||||
if (browseRootAlready)
|
||||
browseFolderForFiles(f);
|
||||
else {
|
||||
browsingSourceForFolders();
|
||||
}
|
||||
} else {
|
||||
browseFolderForFiles(f);
|
||||
}
|
||||
}
|
||||
handleDirectoryBrowsing(adapter.getItem(position));
|
||||
}
|
||||
});
|
||||
|
||||
adapter = new MediaFileListAdapter(getActivity(), R.layout.grid_item_playlist);
|
||||
folderGridView.setAdapter(adapter);
|
||||
if (savedInstanceState != null) {
|
||||
mediaType = savedInstanceState.getString(MEDIA_TYPE);
|
||||
//currentPath = savedInstanceState.getString(CURRENT_PATH);
|
||||
if (mediaType.equalsIgnoreCase(Files.Media.VIDEO)) {
|
||||
playlistId = VIDEO_PLAYLISTID;
|
||||
} else if (mediaType.equalsIgnoreCase(Files.Media.PICTURES)) {
|
||||
playlistId = PICTURE_PLAYLISTID;
|
||||
}
|
||||
ArrayList<FileLocation> list = savedInstanceState.getParcelableArrayList(PATH_CONTENTS);
|
||||
rootFileLocation = savedInstanceState.getParcelableArrayList(ROOT_PATH_CONTENTS);
|
||||
browseRootAlready = savedInstanceState.getBoolean(ROOT_VISITED);
|
||||
adapter.setFilelistItems(list);
|
||||
if ((list == null) || (list.size() == 0)) {
|
||||
displayEmptyListMessage();
|
||||
} else {
|
||||
switchToPanel(R.id.playlist);
|
||||
}
|
||||
}
|
||||
else {
|
||||
browsingSourceForFolders();
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
void handleDirectoryBrowsing(FileLocation f) {
|
||||
// if selection is a directory, browse the the level below
|
||||
if (f.isDirectory) {
|
||||
// a directory - store the path of this directory so that we can reverse travel if
|
||||
// we want to
|
||||
if (f.isRootDir()) {
|
||||
if (browseRootAlready)
|
||||
browseFolderForFiles(f);
|
||||
else {
|
||||
browsingSourceForFolders();
|
||||
}
|
||||
} else {
|
||||
browseFolderForFiles(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated (Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
public void onBackPressed() {
|
||||
handleDirectoryBrowsing(adapter.getItem(0));
|
||||
}
|
||||
|
||||
public boolean atRootDirectory() {
|
||||
if (adapter.getCount() == 0)
|
||||
browsingSourceForFolders();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
super.onCreateOptionsMenu(menu, inflater);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
return super.onOptionsItemSelected(item);
|
||||
return true;
|
||||
FileLocation fl = adapter.getItem(0);
|
||||
if (fl == null)
|
||||
return true;
|
||||
else
|
||||
// if we still see "..", it is not the real root directory
|
||||
return fl.isRootDir() && (fl.label.contentEquals("..") == false);
|
||||
}
|
||||
|
||||
private void browsingSourceForFolders() {
|
||||
|
@ -165,9 +200,11 @@ public class MediaFileListFragment extends Fragment {
|
|||
rootFileLocation.clear();
|
||||
FileLocation fl;
|
||||
for (ItemType.Source item : result) {
|
||||
fl = new FileLocation(item.label, item.file, true);
|
||||
fl.setRootDir(true);
|
||||
rootFileLocation.add(fl);
|
||||
if (!item.file.contains(ADDON_SOURCE)) {
|
||||
fl = new FileLocation(item.label, item.file, true);
|
||||
fl.setRootDir(true);
|
||||
rootFileLocation.add(fl);
|
||||
}
|
||||
}
|
||||
|
||||
browseRootAlready = true;
|
||||
|
@ -450,6 +487,12 @@ public class MediaFileListFragment extends Fragment {
|
|||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public List<FileLocation> getFileItemList() {
|
||||
if (fileLocationItems == null)
|
||||
return new ArrayList<FileLocation>();
|
||||
return new ArrayList<FileLocation>(fileLocationItems);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
if (fileLocationItems == null) {
|
||||
|
@ -546,7 +589,7 @@ public class MediaFileListFragment extends Fragment {
|
|||
int position;
|
||||
}
|
||||
|
||||
public class FileLocation {
|
||||
public class FileLocation implements Parcelable {
|
||||
public final String label;
|
||||
public final String file;
|
||||
public final boolean isDirectory;
|
||||
|
@ -562,6 +605,34 @@ public class MediaFileListFragment extends Fragment {
|
|||
this.isDirectory = isDir;
|
||||
this.isRoot = false;
|
||||
}
|
||||
|
||||
private FileLocation(Parcel in) {
|
||||
this.label = in.readString();
|
||||
this.file = in.readString();
|
||||
this.isDirectory = (in.readInt() != 0);
|
||||
this.isRoot = (in.readInt() != 0);
|
||||
}
|
||||
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void writeToParcel(Parcel out, int flags) {
|
||||
out.writeString(label);
|
||||
out.writeString(file);
|
||||
out.writeInt(isDirectory ? 1 : 0);
|
||||
out.writeInt(isRoot ? 1 : 0);
|
||||
}
|
||||
|
||||
public final Parcelable.Creator<FileLocation> CREATOR = new Parcelable.Creator<FileLocation>() {
|
||||
public FileLocation createFromParcel(Parcel in) {
|
||||
return new FileLocation(in);
|
||||
}
|
||||
|
||||
public FileLocation[] newArray(int size) {
|
||||
return new FileLocation[size];
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue