Android: Use storage access framework for custom SD card paths

This is part of my efforts to add support for scoped storage.
This commit is contained in:
JosJuice 2020-11-04 23:09:00 +01:00
parent 70df5446d3
commit 73f013e3cc
2 changed files with 40 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package org.dolphinemu.dolphinemu.features.settings.ui;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Menu;
@ -18,6 +19,7 @@ import androidx.lifecycle.ViewModelProvider;
import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.ui.main.MainActivity;
import org.dolphinemu.dolphinemu.ui.main.MainPresenter;
import org.dolphinemu.dolphinemu.ui.main.TvMainActivity;
import org.dolphinemu.dolphinemu.utils.FileBrowserHelper;
import org.dolphinemu.dolphinemu.utils.TvUtil;
@ -170,11 +172,31 @@ public final class SettingsActivity extends AppCompatActivity implements Setting
// If the user picked a file, as opposed to just backing out.
if (resultCode == MainActivity.RESULT_OK)
{
String path = FileBrowserHelper.getSelectedPath(result);
getFragment().getAdapter().onFilePickerConfirmation(path);
if (requestCode == MainPresenter.REQUEST_SD_FILE)
{
Uri uri = canonicalizeIfPossible(result.getData());
int takeFlags = result.getFlags() &
(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getContentResolver().takePersistableUriPermission(uri, takeFlags);
getFragment().getAdapter().onFilePickerConfirmation(uri.toString());
}
else
{
String path = FileBrowserHelper.getSelectedPath(result);
getFragment().getAdapter().onFilePickerConfirmation(path);
}
}
}
@NonNull
private Uri canonicalizeIfPossible(@NonNull Uri uri)
{
Uri canonicalizedUri = getContentResolver().canonicalize(uri);
return canonicalizedUri != null ? canonicalizedUri : uri;
}
@Override
public void showLoading()
{

View File

@ -2,6 +2,9 @@ package org.dolphinemu.dolphinemu.features.settings.ui;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.provider.DocumentsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -288,21 +291,28 @@ public final class SettingsAdapter extends RecyclerView.Adapter<SettingViewHolde
mClickedItem = item;
FilePicker filePicker = (FilePicker) item;
HashSet<String> extensions;
switch (filePicker.getRequestType())
{
case MainPresenter.REQUEST_SD_FILE:
extensions = FileBrowserHelper.RAW_EXTENSION;
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI,
filePicker.getSelectedValue(mView.getSettings()));
}
mView.getActivity().startActivityForResult(intent, filePicker.getRequestType());
break;
case MainPresenter.REQUEST_GAME_FILE:
extensions = FileBrowserHelper.GAME_EXTENSIONS;
FileBrowserHelper.openFilePicker(mView.getActivity(), filePicker.getRequestType(), false,
FileBrowserHelper.GAME_EXTENSIONS);
break;
default:
throw new InvalidParameterException("Unhandled request code");
}
FileBrowserHelper.openFilePicker(mView.getActivity(), filePicker.getRequestType(), false,
extensions);
}
public void onFilePickerConfirmation(String selectedFile)