Move file list navigation to background (multi-threaded)

An xml view will never not exist, but it may be empty. Check the appropriate condition to avoid a crash.

Fix some error checking for file list clearing
This commit is contained in:
TwistedUmbrella 2018-03-31 12:43:17 -04:00
parent 1d3964588c
commit f88cb7f0e4
1 changed files with 114 additions and 95 deletions

View File

@ -279,8 +279,8 @@ public class FileBrowser extends Fragment {
@Override @Override
protected void onPostExecute(List<File> items) { protected void onPostExecute(List<File> items) {
if (items != null && !items.isEmpty()) { if (items != null && !items.isEmpty()) {
final LinearLayout list = (LinearLayout) getActivity().findViewById(R.id.game_list); LinearLayout list = (LinearLayout) getActivity().findViewById(R.id.game_list);
if (list != null) { if (list.getChildCount() > 0) {
list.removeAllViews(); list.removeAllViews();
} }
@ -298,7 +298,8 @@ public class FileBrowser extends Fragment {
private void browseStorage(boolean images) { private void browseStorage(boolean images) {
if (images) { if (images) {
navigate(new File(home_directory)); (new navigate()).executeOnExecutor(
AsyncTask.THREAD_POOL_EXECUTOR, new File(home_directory));
} else { } else {
if (game_directory.equals(sdcard.getAbsolutePath())) { if (game_directory.equals(sdcard.getAbsolutePath())) {
HashSet<String> extStorage = FileBrowser.getExternalMounts(); HashSet<String> extStorage = FileBrowser.getExternalMounts();
@ -307,26 +308,15 @@ public class FileBrowser extends Fragment {
String sdCardPath = sd.next().replace("mnt/media_rw", "storage"); String sdCardPath = sd.next().replace("mnt/media_rw", "storage");
if (!sdCardPath.equals(sdcard.getAbsolutePath())) { if (!sdCardPath.equals(sdcard.getAbsolutePath())) {
if (new File(sdCardPath).canRead()) { if (new File(sdCardPath).canRead()) {
navigate(new File(sdCardPath)); (new navigate()).execute(new File(sdCardPath));
return; return;
} }
} }
} }
} }
} }
navigate(new File(game_directory)); (new navigate()).executeOnExecutor(
} AsyncTask.THREAD_POOL_EXECUTOR, new File(game_directory));
}
private static final class DirSort implements Comparator<File> {
@Override
public int compare(File filea, File fileb) {
return ((filea.isFile() ? "a" : "b") + filea.getName().toLowerCase(
Locale.getDefault()))
.compareTo((fileb.isFile() ? "a" : "b")
+ fileb.getName().toLowerCase(Locale.getDefault()));
} }
} }
@ -429,20 +419,40 @@ public class FileBrowser extends Fragment {
} }
}); });
list.addView(childview); list.addView(childview);
xmlParser.execute(game.getName()); xmlParser.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, game.getName());
} }
void navigate(final File root_sd) { private final class navigate extends AsyncTask<File, Integer, List<File>> {
LinearLayout v = (LinearLayout) getActivity().findViewById(R.id.game_list);
v.removeAllViews(); private LinearLayout listView;
private String heading;
private File parent;
private final class DirSort implements Comparator<File> {
@Override
public int compare(File filea, File fileb) {
return ((filea.isFile() ? "a" : "b") + filea.getName().toLowerCase(
Locale.getDefault()))
.compareTo((fileb.isFile() ? "a" : "b")
+ fileb.getName().toLowerCase(Locale.getDefault()));
}
}
@Override
protected void onPreExecute() {
listView = (LinearLayout) getActivity().findViewById(R.id.game_list);
if (listView.getChildCount() > 0)
listView.removeAllViews();
}
@Override
protected List<File> doInBackground(File... paths) {
heading = paths[0].getAbsolutePath();
ArrayList<File> list = new ArrayList<File>(); ArrayList<File> list = new ArrayList<File>();
final String heading = root_sd.getAbsolutePath(); File flist[] = paths[0].listFiles();
createListHeader(heading, v, false); parent = paths[0].getParentFile();
File flist[] = root_sd.listFiles();
File parent = root_sd.getParentFile();
list.add(null); list.add(null);
@ -454,7 +464,13 @@ public class FileBrowser extends Fragment {
Arrays.sort(flist, new DirSort()); Arrays.sort(flist, new DirSort());
Collections.addAll(list, flist); Collections.addAll(list, flist);
} }
return list;
}
@Override
protected void onPostExecute(List<File> list) {
if (list != null && !list.isEmpty()) {
createListHeader(heading, listView, false);
for (final File file : list) { for (final File file : list) {
if (file != null && !file.isDirectory() && !file.getAbsolutePath().equals("/data")) if (file != null && !file.isDirectory() && !file.getAbsolutePath().equals("/data"))
continue; continue;
@ -482,7 +498,8 @@ public class FileBrowser extends Fragment {
new OnClickListener() { new OnClickListener() {
public void onClick(View view) { public void onClick(View view) {
if (file != null && file.isDirectory()) { if (file != null && file.isDirectory()) {
navigate(file); (new navigate()).executeOnExecutor(
AsyncTask.THREAD_POOL_EXECUTOR, file);
ScrollView sv = (ScrollView) getActivity() ScrollView sv = (ScrollView) getActivity()
.findViewById(R.id.game_scroller); .findViewById(R.id.game_scroller);
sv.scrollTo(0, 0); sv.scrollTo(0, 0);
@ -525,9 +542,11 @@ public class FileBrowser extends Fragment {
} }
}); });
v.addView(childview); listView.addView(childview);
}
listView.invalidate();
}
} }
v.invalidate();
} }
private boolean DataDirectoryBIOS() { private boolean DataDirectoryBIOS() {