Android: Simplify and optimize FileBrowser

This commit is contained in:
Ender's Games 2018-09-07 12:20:14 -04:00
parent 8f981e648c
commit c8d2517d95
1 changed files with 53 additions and 54 deletions

View File

@ -92,7 +92,7 @@ public class FileBrowser extends Fragment {
} }
public static HashSet<String> getExternalMounts() { public static HashSet<String> getExternalMounts() {
final HashSet<String> out = new HashSet<String>(); final HashSet<String> out = new HashSet<>();
String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4|fuse|sdfat).*rw.*"; String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4|fuse|sdfat).*rw.*";
StringBuilder s = new StringBuilder(); StringBuilder s = new StringBuilder();
try { try {
@ -133,7 +133,7 @@ public class FileBrowser extends Fragment {
void onFolderSelected(Uri uri); void onFolderSelected(Uri uri);
} }
@Override @Override @SuppressWarnings("deprecation")
public void onAttach(Activity activity) { public void onAttach(Activity activity) {
super.onAttach(activity); super.onAttach(activity);
@ -176,9 +176,9 @@ public class FileBrowser extends Fragment {
} }
installButtons(); installButtons();
if (!games) { if (!games) {
new LocateGames(R.array.flash).execute(home_directory); new LocateGames(this, R.array.flash).execute(home_directory);
} else { } else {
new LocateGames(R.array.images).execute(game_directory); new LocateGames(this, R.array.images).execute(game_directory);
} }
} }
@ -216,11 +216,13 @@ public class FileBrowser extends Fragment {
} }
} }
private final class LocateGames extends AsyncTask<String, Integer, List<File>> { private static final class LocateGames extends AsyncTask<String, Integer, List<File>> {
private WeakReference<FileBrowser> browser;
private int array; private int array;
public LocateGames(int arrayType) { LocateGames(FileBrowser context, int arrayType) {
browser = new WeakReference<>(context);
this.array = arrayType; this.array = arrayType;
} }
@ -229,7 +231,7 @@ public class FileBrowser extends Fragment {
File storage = new File(paths[0]); File storage = new File(paths[0]);
// array of valid image file extensions // array of valid image file extensions
String[] mediaTypes = getActivity().getResources().getStringArray(array); String[] mediaTypes = browser.get().getActivity().getResources().getStringArray(array);
FilenameFilter[] filter = new FilenameFilter[mediaTypes.length]; FilenameFilter[] filter = new FilenameFilter[mediaTypes.length];
int i = 0; int i = 0;
@ -237,18 +239,14 @@ public class FileBrowser extends Fragment {
filter[i] = new FilenameFilter() { filter[i] = new FilenameFilter() {
public boolean accept(File dir, String name) { public boolean accept(File dir, String name) {
if (dir.getName().equals("obb") || dir.getName().equals("cache") return (!dir.getName().equals("obb") && !dir.getName().equals("cache")
|| dir.getName().startsWith(".") || name.startsWith(".")) { && !dir.getName().startsWith(".") && !name.startsWith("."))
return false; && (array != R.array.flash && name.startsWith("dc_"))
} else if (array == R.array.flash && !name.startsWith("dc_")) { && (browser.get().searchQuery == null
return false; || name.toLowerCase(Locale.getDefault()).contains(
} else if (searchQuery == null || name.toLowerCase(Locale.getDefault()) browser.get().searchQuery.toLowerCase(Locale.getDefault())))
.contains(searchQuery.toLowerCase(Locale.getDefault()))) && StringUtils.endsWithIgnoreCase(name, "." + type);
return StringUtils.endsWithIgnoreCase(name, "." + type);
else
return false;
} }
}; };
i++; i++;
} }
@ -265,19 +263,19 @@ 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()) {
LinearLayout list = (LinearLayout) getActivity().findViewById(R.id.game_list); LinearLayout list = (LinearLayout) browser.get().getActivity().findViewById(R.id.game_list);
if (list.getChildCount() > 0) { if (list.getChildCount() > 0) {
list.removeAllViews(); list.removeAllViews();
} }
String heading = getActivity().getString(R.string.games_listing); String heading = browser.get().getActivity().getString(R.string.games_listing);
createListHeader(heading, list, array == R.array.images); browser.get().createListHeader(heading, list, array == R.array.images);
for (int i = 0; i < items.size(); i++) { for (int i = 0; i < items.size(); i++) {
createListItem(list, items.get(i), i, array == R.array.images); browser.get().createListItem(list, items.get(i), i, array == R.array.images);
} }
list.invalidate(); list.invalidate();
} else { } else {
browseStorage(array == R.array.images); browser.get().browseStorage(array == R.array.images);
} }
} }
} }
@ -290,15 +288,15 @@ public class FileBrowser extends Fragment {
if (game_directory.equals(sdcard.getAbsolutePath())) { if (game_directory.equals(sdcard.getAbsolutePath())) {
HashSet<String> extStorage = FileBrowser.getExternalMounts(); HashSet<String> extStorage = FileBrowser.getExternalMounts();
if (extStorage != null && !extStorage.isEmpty()) { if (extStorage != null && !extStorage.isEmpty()) {
for (Iterator<String> sd = extStorage.iterator(); sd.hasNext();) { for (String sd : extStorage) {
String sdCardPath = sd.next().replace("mnt/media_rw", "storage"); String sdCardPath = sd.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()) {
(new navigate(this)).execute(new File(sdCardPath)); (new navigate(this)).execute(new File(sdCardPath));
return; return;
} }
} }
} }
} }
} }
(new navigate(this)).executeOnExecutor( (new navigate(this)).executeOnExecutor(
@ -376,7 +374,7 @@ public class FileBrowser extends Fragment {
home_directory = game.getAbsolutePath().substring(0, home_directory = game.getAbsolutePath().substring(0,
game.getAbsolutePath().lastIndexOf(File.separator)) game.getAbsolutePath().lastIndexOf(File.separator))
.replace("/data", ""); .replace("/data", "");
if (!DataDirectoryBIOS()) { if (requireDataBIOS()) {
showToastMessage(getActivity().getString(R.string.config_data, showToastMessage(getActivity().getString(R.string.config_data,
home_directory), Snackbar.LENGTH_LONG); home_directory), Snackbar.LENGTH_LONG);
} }
@ -437,7 +435,7 @@ public class FileBrowser extends Fragment {
protected List<File> doInBackground(File... paths) { protected List<File> doInBackground(File... paths) {
heading = paths[0].getAbsolutePath(); heading = paths[0].getAbsolutePath();
ArrayList<File> list = new ArrayList<File>(); ArrayList<File> list = new ArrayList<>();
File flist[] = paths[0].listFiles(); File flist[] = paths[0].listFiles();
parent = paths[0].getParentFile(); parent = paths[0].getParentFile();
@ -476,7 +474,7 @@ public class FileBrowser extends Fragment {
((ImageView) childview.findViewById(R.id.item_icon)).setImageResource(file == null ((ImageView) childview.findViewById(R.id.item_icon)).setImageResource(file == null
? R.drawable.ic_settings: file.isDirectory() ? R.drawable.ic_settings: file.isDirectory()
? R.drawable.ic_folder_black_24dp : R.drawable.disk_unknown);; ? R.drawable.ic_folder_black_24dp : R.drawable.disk_unknown);
childview.setTag(file); childview.setTag(file);
@ -508,7 +506,7 @@ public class FileBrowser extends Fragment {
.replace("/data", ""); .replace("/data", "");
browser.get().mPrefs.edit().putString( browser.get().mPrefs.edit().putString(
Config.pref_home, browser.get().home_directory).apply(); Config.pref_home, browser.get().home_directory).apply();
if (!browser.get().DataDirectoryBIOS()) { if (browser.get().requireDataBIOS()) {
browser.get().showToastMessage(browser.get() browser.get().showToastMessage(browser.get()
.getActivity().getString(R.string.config_data, .getActivity().getString(R.string.config_data,
browser.get().home_directory), browser.get().home_directory),
@ -545,22 +543,23 @@ public class FileBrowser extends Fragment {
} }
} }
private boolean DataDirectoryBIOS() { private boolean requireDataBIOS() {
File data_directory = new File(home_directory, "data"); File data_directory = new File(home_directory, "data");
if (!data_directory.exists() || !data_directory.isDirectory()) { if (data_directory.exists() && data_directory.isDirectory()) {
data_directory.mkdirs(); File bios = new File(home_directory, "data/dc_boot.bin");
File bios = new File(home_directory, "dc_boot.bin"); File flash = new File(home_directory, "data/dc_flash.bin");
if (bios.renameTo(new File(home_directory, "data/dc_boot.bin"))) { return !(bios.exists() && flash.exists());
File flash = new File(home_directory, "dc_flash.bin"); } else {
return flash.renameTo(new File(home_directory, "data/dc_flash.bin")); if (data_directory.mkdirs()) {
} File bios = new File(home_directory, "dc_boot.bin");
return false; if (bios.renameTo(new File(home_directory, "data/dc_boot.bin"))) {
} else { return !new File(home_directory, "dc_flash.bin").renameTo(
File bios = new File(home_directory, "data/dc_boot.bin"); new File(home_directory, "data/dc_flash.bin"));
File flash = new File(home_directory, "data/dc_flash.bin"); }
return (bios.exists() && flash.exists()); }
} return true;
} }
}
private void showToastMessage(String message, int duration) { private void showToastMessage(String message, int duration) {
ConstraintLayout layout = (ConstraintLayout) getActivity().findViewById(R.id.mainui_layout); ConstraintLayout layout = (ConstraintLayout) getActivity().findViewById(R.id.mainui_layout);