Skip to content

Commit

Permalink
Android: Don't use SettingsAdapter's static state in file picker
Browse files Browse the repository at this point in the history
  • Loading branch information
JosJuice authored and Ebola16 committed Mar 23, 2020
1 parent ccda75f commit de5430b
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String> mExtensions;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

Intent intent = getIntent();
if (intent != null)
{
mExtensions = (HashSet<String>) intent.getSerializableExtra(EXTRA_EXTENSIONS);
}
}

@Override
protected AbstractFilePickerFragment<File> getFragment(
@Nullable final String startPath, final int mode, final boolean allowMultiple,
final boolean allowCreateDir, final boolean allowExistingFile,
final boolean singleClick)
{
AbstractFilePickerFragment<File> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SettingViewHolder>
implements DialogInterface.OnClickListener, SeekBar.OnSeekBarChangeListener
Expand All @@ -67,13 +65,9 @@ public final class SettingsAdapter extends RecyclerView.Adapter<SettingViewHolde
private AlertDialog mDialog;
private TextView mTextSliderValue;

public static FilePicker sFilePicker;
public static SettingsItem sItem;

private static final Set<String> gameExtensions = new HashSet<>(Arrays.asList(
"gcm", "tgc", "iso", "ciso", "gcz", "wbfs", "wad", "dol", "elf", "dff"));

private static final Set<String> 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)
{
Expand Down Expand Up @@ -305,15 +299,29 @@ 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)
{
sFilePicker = (FilePicker) item;
sItem = item;

FileBrowserHelper.openFilePicker(mView.getActivity(), sFilePicker.getRequestType(), false);
HashSet<String> 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)
Expand All @@ -340,25 +348,6 @@ public static void resetPaths()
NativeLibrary.ReloadConfig();
}

public static Set<String> 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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> mExtensions;

public void setExtensions(HashSet<String> 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)
Expand All @@ -32,6 +46,8 @@ public Uri toUri(@NonNull final File file)
{
super.onActivityCreated(savedInstanceState);

mExtensions = (HashSet<String>) getArguments().getSerializable(KEY_EXTENSIONS);

if (mode == MODE_DIR)
{
TextView ok = getActivity().findViewById(R.id.nnf_button_ok);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> GAME_EXTENSIONS = new HashSet<>(Arrays.asList(
"gcm", "tgc", "iso", "ciso", "gcz", "wbfs", "wad", "dol", "elf", "dff"));

public static final HashSet<String> RAW_EXTENSION = new HashSet<>(Collections.singletonList(
"raw"));

public static void openDirectoryPicker(FragmentActivity activity, HashSet<String> extensions)
{
Intent i = new Intent(activity, CustomFilePickerActivity.class);

Expand All @@ -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<String> extensions)
{
Intent i = new Intent(activity, CustomFilePickerActivity.class);

Expand All @@ -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);
}
Expand Down

0 comments on commit de5430b

Please sign in to comment.