From de5430b2225df7e41d466e4511e49d82f06ae83b Mon Sep 17 00:00:00 2001 From: JosJuice Date: Mon, 23 Mar 2020 00:53:22 -0400 Subject: [PATCH] Android: Don't use SettingsAdapter's static state in file picker --- .../activities/CustomFilePickerActivity.java | 23 ++++++++- .../activities/EmulationActivity.java | 3 +- .../settings/ui/SettingsActivity.java | 3 -- .../features/settings/ui/SettingsAdapter.java | 51 ++++++++----------- .../fragments/CustomFilePickerFragment.java | 21 ++++++-- .../dolphinemu/ui/main/MainActivity.java | 5 +- .../dolphinemu/ui/main/TvMainActivity.java | 5 +- .../dolphinemu/utils/FileBrowserHelper.java | 16 +++++- 8 files changed, 81 insertions(+), 46 deletions(-) diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/CustomFilePickerActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/CustomFilePickerActivity.java index f715766bffa0..0edf7aea17bd 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/CustomFilePickerActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/CustomFilePickerActivity.java @@ -1,5 +1,7 @@ package org.dolphinemu.dolphinemu.activities; +import android.content.Intent; +import android.os.Bundle; import android.os.Environment; import androidx.annotation.Nullable; @@ -10,21 +12,38 @@ import org.dolphinemu.dolphinemu.fragments.CustomFilePickerFragment; import java.io.File; +import java.util.HashSet; public class CustomFilePickerActivity extends FilePickerActivity - { + public static final String EXTRA_EXTENSIONS = "dolphinemu.org.filepicker.extensions"; + + private HashSet mExtensions; + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + + Intent intent = getIntent(); + if (intent != null) + { + mExtensions = (HashSet) intent.getSerializableExtra(EXTRA_EXTENSIONS); + } + } + @Override protected AbstractFilePickerFragment getFragment( @Nullable final String startPath, final int mode, final boolean allowMultiple, final boolean allowCreateDir, final boolean allowExistingFile, final boolean singleClick) { - AbstractFilePickerFragment fragment = new CustomFilePickerFragment(); + CustomFilePickerFragment fragment = new CustomFilePickerFragment(); // startPath is allowed to be null. In that case, default folder should be SD-card and not "/" fragment.setArgs( startPath != null ? startPath : Environment.getExternalStorageDirectory().getPath(), mode, allowMultiple, allowCreateDir, allowExistingFile, singleClick); + fragment.setExtensions(mExtensions); return fragment; } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java index c3e47651fcb2..0d4482968796 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java @@ -633,7 +633,8 @@ public void handleMenuAction(@MenuAction int menuAction) return; case MENU_ACTION_CHANGE_DISC: - FileBrowserHelper.openFilePicker(this, REQUEST_CHANGE_DISC, false); + FileBrowserHelper.openFilePicker(this, REQUEST_CHANGE_DISC, false, + FileBrowserHelper.GAME_EXTENSIONS); return; case MENU_SET_IR_SENSITIVITY: diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsActivity.java index c76dea4be56a..d5f6754225e8 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsActivity.java @@ -176,9 +176,6 @@ protected void onActivityResult(int requestCode, int resultCode, Intent result) Toast.makeText(this, "Saved settings to INI files", Toast.LENGTH_SHORT).show(); } } - // Clear static variables for File Picker activities that don't set extensions. - SettingsAdapter.sFilePicker = null; - SettingsAdapter.sItem = null; // TODO: After result of FilePicker, duplicate SettingsActivity appears. // Finish to avoid this. Is there a better method? diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsAdapter.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsAdapter.java index 400f3e41ea2c..04d68d17fcc5 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsAdapter.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsAdapter.java @@ -47,11 +47,9 @@ import org.dolphinemu.dolphinemu.utils.FileBrowserHelper; import org.dolphinemu.dolphinemu.utils.Log; +import java.security.InvalidParameterException; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; import java.util.HashSet; -import java.util.Set; public final class SettingsAdapter extends RecyclerView.Adapter implements DialogInterface.OnClickListener, SeekBar.OnSeekBarChangeListener @@ -67,13 +65,9 @@ public final class SettingsAdapter extends RecyclerView.Adapter gameExtensions = new HashSet<>(Arrays.asList( - "gcm", "tgc", "iso", "ciso", "gcz", "wbfs", "wad", "dol", "elf", "dff")); - - private static final Set SDExtensions = new HashSet<>(Collections.singletonList("raw")); + // TODO: Properly restore these two on activity recreation + private static FilePicker sFilePicker; + private static SettingsItem sItem; public SettingsAdapter(SettingsFragmentView view, Context context) { @@ -305,7 +299,7 @@ public void onFilePickerDirectoryClick(SettingsItem item) sFilePicker = (FilePicker) item; sItem = item; - FileBrowserHelper.openDirectoryPicker(mView.getActivity()); + FileBrowserHelper.openDirectoryPicker(mView.getActivity(), FileBrowserHelper.GAME_EXTENSIONS); } public void onFilePickerFileClick(SettingsItem item) @@ -313,7 +307,21 @@ public void onFilePickerFileClick(SettingsItem item) sFilePicker = (FilePicker) item; sItem = item; - FileBrowserHelper.openFilePicker(mView.getActivity(), sFilePicker.getRequestType(), false); + HashSet extensions; + switch (sFilePicker.getRequestType()) + { + case MainPresenter.REQUEST_SD_FILE: + extensions = FileBrowserHelper.RAW_EXTENSION; + break; + case MainPresenter.REQUEST_GAME_FILE: + extensions = FileBrowserHelper.GAME_EXTENSIONS; + break; + default: + throw new InvalidParameterException("Unhandled request code"); + } + + FileBrowserHelper.openFilePicker(mView.getActivity(), sFilePicker.getRequestType(), false, + extensions); } public static void onFilePickerConfirmation(String file) @@ -340,25 +348,6 @@ public static void resetPaths() NativeLibrary.ReloadConfig(); } - public static Set getExtensions() - { - try - { - if (sFilePicker.getRequestType() == MainPresenter.REQUEST_SD_FILE) - { - return SDExtensions; - } - else - { - return gameExtensions; - } - } - catch (NullPointerException ex) - { - return gameExtensions; - } - } - @Override public void onClick(DialogInterface dialog, int which) { diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/CustomFilePickerFragment.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/CustomFilePickerFragment.java index bbccacaee402..5e0e06582cd1 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/CustomFilePickerFragment.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/CustomFilePickerFragment.java @@ -10,14 +10,28 @@ import android.widget.TextView; import org.dolphinemu.dolphinemu.R; -import org.dolphinemu.dolphinemu.features.settings.ui.SettingsAdapter; import com.nononsenseapps.filepicker.FilePickerFragment; import java.io.File; +import java.util.HashSet; public class CustomFilePickerFragment extends FilePickerFragment { + public static final String KEY_EXTENSIONS = "KEY_EXTENSIONS"; + + private HashSet mExtensions; + + public void setExtensions(HashSet extensions) + { + Bundle b = getArguments(); + if (b == null) + b = new Bundle(); + + b.putSerializable(KEY_EXTENSIONS, extensions); + setArguments(b); + } + @NonNull @Override public Uri toUri(@NonNull final File file) @@ -32,6 +46,8 @@ public Uri toUri(@NonNull final File file) { super.onActivityCreated(savedInstanceState); + mExtensions = (HashSet) getArguments().getSerializable(KEY_EXTENSIONS); + if (mode == MODE_DIR) { TextView ok = getActivity().findViewById(R.id.nnf_button_ok); @@ -51,8 +67,7 @@ protected boolean isItemVisible(@NonNull final File file) return (showHiddenItems || !file.isHidden()) && (file.isDirectory() || - SettingsAdapter.getExtensions() - .contains(fileExtension(file.getName()).toLowerCase())); + mExtensions.contains(fileExtension(file.getName()).toLowerCase())); } @Override diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java index 3607e58ee220..be1a8a74a2ea 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java @@ -145,13 +145,14 @@ public void launchSettingsActivity(MenuTag menuTag) @Override public void launchFileListActivity() { - FileBrowserHelper.openDirectoryPicker(this); + FileBrowserHelper.openDirectoryPicker(this, FileBrowserHelper.GAME_EXTENSIONS); } @Override public void launchOpenFileActivity() { - FileBrowserHelper.openFilePicker(this, MainPresenter.REQUEST_GAME_FILE, false); + FileBrowserHelper.openFilePicker(this, MainPresenter.REQUEST_GAME_FILE, false, + FileBrowserHelper.GAME_EXTENSIONS); } /** diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java index d69ab4ece088..ed0c188532d0 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java @@ -142,13 +142,14 @@ public void launchSettingsActivity(MenuTag menuTag) @Override public void launchFileListActivity() { - FileBrowserHelper.openDirectoryPicker(this); + FileBrowserHelper.openDirectoryPicker(this, FileBrowserHelper.GAME_EXTENSIONS); } @Override public void launchOpenFileActivity() { - FileBrowserHelper.openFilePicker(this, MainPresenter.REQUEST_GAME_FILE, false); + FileBrowserHelper.openFilePicker(this, MainPresenter.REQUEST_GAME_FILE, false, + FileBrowserHelper.GAME_EXTENSIONS); } @Override diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/FileBrowserHelper.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/FileBrowserHelper.java index 03e36479f236..ae4a45b8e928 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/FileBrowserHelper.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/FileBrowserHelper.java @@ -14,11 +14,20 @@ import org.dolphinemu.dolphinemu.ui.main.MainPresenter; import java.io.File; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; import java.util.List; public final class FileBrowserHelper { - public static void openDirectoryPicker(FragmentActivity activity) + public static final HashSet GAME_EXTENSIONS = new HashSet<>(Arrays.asList( + "gcm", "tgc", "iso", "ciso", "gcz", "wbfs", "wad", "dol", "elf", "dff")); + + public static final HashSet RAW_EXTENSION = new HashSet<>(Collections.singletonList( + "raw")); + + public static void openDirectoryPicker(FragmentActivity activity, HashSet extensions) { Intent i = new Intent(activity, CustomFilePickerActivity.class); @@ -27,11 +36,13 @@ public static void openDirectoryPicker(FragmentActivity activity) i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_DIR); i.putExtra(FilePickerActivity.EXTRA_START_PATH, Environment.getExternalStorageDirectory().getPath()); + i.putExtra(CustomFilePickerActivity.EXTRA_EXTENSIONS, extensions); activity.startActivityForResult(i, MainPresenter.REQUEST_DIRECTORY); } - public static void openFilePicker(FragmentActivity activity, int requestCode, boolean allowMulti) + public static void openFilePicker(FragmentActivity activity, int requestCode, boolean allowMulti, + HashSet extensions) { Intent i = new Intent(activity, CustomFilePickerActivity.class); @@ -40,6 +51,7 @@ public static void openFilePicker(FragmentActivity activity, int requestCode, bo i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_FILE); i.putExtra(FilePickerActivity.EXTRA_START_PATH, Environment.getExternalStorageDirectory().getPath()); + i.putExtra(CustomFilePickerActivity.EXTRA_EXTENSIONS, extensions); activity.startActivityForResult(i, requestCode); }