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,105 +419,134 @@ 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();
ArrayList<File> list = new ArrayList<File>(); private LinearLayout listView;
private String heading;
private File parent;
final String heading = root_sd.getAbsolutePath(); private final class DirSort implements Comparator<File> {
createListHeader(heading, v, false); @Override
public int compare(File filea, File fileb) {
File flist[] = root_sd.listFiles(); return ((filea.isFile() ? "a" : "b") + filea.getName().toLowerCase(
File parent = root_sd.getParentFile(); Locale.getDefault()))
.compareTo((fileb.isFile() ? "a" : "b")
list.add(null); + fileb.getName().toLowerCase(Locale.getDefault()));
}
if (parent != null) {
list.add(parent);
} }
if (flist != null) { @Override
Arrays.sort(flist, new DirSort()); protected void onPreExecute() {
Collections.addAll(list, flist); listView = (LinearLayout) getActivity().findViewById(R.id.game_list);
if (listView.getChildCount() > 0)
listView.removeAllViews();
} }
for (final File file : list) { @Override
if (file != null && !file.isDirectory() && !file.getAbsolutePath().equals("/data")) protected List<File> doInBackground(File... paths) {
continue; heading = paths[0].getAbsolutePath();
final View childview = getActivity().getLayoutInflater().inflate(
R.layout.browser_fragment_item, null, false);
if (file == null) { ArrayList<File> list = new ArrayList<File>();
((TextView) childview.findViewById(R.id.item_name)).setText(R.string.folder_select);
} else if (file == parent)
((TextView) childview.findViewById(R.id.item_name)).setText("..");
else
((TextView) childview.findViewById(R.id.item_name)).setText(file.getName());
((ImageView) childview.findViewById(R.id.item_icon)).setImageResource(file == null File flist[] = paths[0].listFiles();
? R.drawable.ic_settings: file.isDirectory() parent = paths[0].getParentFile();
? R.drawable.ic_folder_black_24dp : R.drawable.disk_unknown);;
childview.setTag(file); list.add(null);
orig_bg = childview.getBackground(); if (parent != null) {
list.add(parent);
}
// vw.findViewById(R.id.childview).setBackgroundColor(0xFFFFFFFF); if (flist != null) {
Arrays.sort(flist, new DirSort());
Collections.addAll(list, flist);
}
return list;
}
childview.findViewById(R.id.childview).setOnClickListener( @Override
new OnClickListener() { protected void onPostExecute(List<File> list) {
public void onClick(View view) { if (list != null && !list.isEmpty()) {
if (file != null && file.isDirectory()) { createListHeader(heading, listView, false);
navigate(file); for (final File file : list) {
ScrollView sv = (ScrollView) getActivity() if (file != null && !file.isDirectory() && !file.getAbsolutePath().equals("/data"))
.findViewById(R.id.game_scroller); continue;
sv.scrollTo(0, 0); final View childview = getActivity().getLayoutInflater().inflate(
vib.vibrate(50); R.layout.browser_fragment_item, null, false);
} else if (view.getTag() == null) {
vib.vibrate(250); if (file == null) {
((TextView) childview.findViewById(R.id.item_name)).setText(R.string.folder_select);
} else if (file == parent)
((TextView) childview.findViewById(R.id.item_name)).setText("..");
else
((TextView) childview.findViewById(R.id.item_name)).setText(file.getName());
((ImageView) childview.findViewById(R.id.item_icon)).setImageResource(file == null
? R.drawable.ic_settings: file.isDirectory()
? R.drawable.ic_folder_black_24dp : R.drawable.disk_unknown);;
childview.setTag(file);
orig_bg = childview.getBackground();
// vw.findViewById(R.id.childview).setBackgroundColor(0xFFFFFFFF);
childview.findViewById(R.id.childview).setOnClickListener(
new OnClickListener() {
public void onClick(View view) {
if (file != null && file.isDirectory()) {
(new navigate()).executeOnExecutor(
AsyncTask.THREAD_POOL_EXECUTOR, file);
ScrollView sv = (ScrollView) getActivity()
.findViewById(R.id.game_scroller);
sv.scrollTo(0, 0);
vib.vibrate(50);
} else if (view.getTag() == null) {
vib.vibrate(250);
if (games) {
game_directory = heading;
mPrefs.edit().putString(Config.pref_games, heading).apply();
mCallback.onFolderSelected(Uri.fromFile(new File(game_directory)));
} else {
home_directory = heading.replace("/data", "");
mPrefs.edit().putString(Config.pref_home, home_directory).apply();
if (!DataDirectoryBIOS()) {
showToastMessage(getActivity().getString(R.string.config_data, home_directory),
Snackbar.LENGTH_LONG
);
}
mCallback.onFolderSelected(Uri.fromFile(new File(home_directory)));
JNIdc.config(home_directory);
}
if (games) {
game_directory = heading;
mPrefs.edit().putString(Config.pref_games, heading).apply();
mCallback.onFolderSelected(Uri.fromFile(new File(game_directory)));
} else {
home_directory = heading.replace("/data", "");
mPrefs.edit().putString(Config.pref_home, home_directory).apply();
if (!DataDirectoryBIOS()) {
showToastMessage(getActivity().getString(R.string.config_data, home_directory),
Snackbar.LENGTH_LONG
);
} }
mCallback.onFolderSelected(Uri.fromFile(new File(home_directory)));
JNIdc.config(home_directory);
} }
});
}
}
});
childview.findViewById(R.id.childview).setOnTouchListener( childview.findViewById(R.id.childview).setOnTouchListener(
new OnTouchListener() { new OnTouchListener() {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public boolean onTouch(View view, MotionEvent event) { public boolean onTouch(View view, MotionEvent event) {
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
view.setBackgroundColor(0xFF4F3FFF); view.setBackgroundColor(0xFF4F3FFF);
} else if (event.getActionMasked() == MotionEvent.ACTION_CANCEL } else if (event.getActionMasked() == MotionEvent.ACTION_CANCEL
|| event.getActionMasked() == MotionEvent.ACTION_UP) { || event.getActionMasked() == MotionEvent.ACTION_UP) {
view.setBackgroundDrawable(orig_bg); view.setBackgroundDrawable(orig_bg);
} }
return false; return false;
} }
}); });
v.addView(childview); listView.addView(childview);
}
listView.invalidate();
}
} }
v.invalidate();
} }
private boolean DataDirectoryBIOS() { private boolean DataDirectoryBIOS() {