Android: Support opening files directly

This enables us to boot FIFO logs as well as homebrew directly, without
having to add it to the game list first.
This commit is contained in:
Stenzek 2019-07-18 20:42:42 +10:00
parent 6a6bbd7071
commit 34e6a41d05
7 changed files with 92 additions and 3 deletions

View File

@ -198,6 +198,38 @@ public final class EmulationActivity extends AppCompatActivity
activity.startActivity(launcher);
}
public static void launchFile(FragmentActivity activity, String[] filePaths)
{
Intent launcher = new Intent(activity, EmulationActivity.class);
launcher.putExtra(EXTRA_SELECTED_GAMES, filePaths);
// Try parsing a GameFile first. This should succeed for disc images.
GameFile gameFile = GameFile.parse(filePaths[0]);
if (gameFile != null)
{
// We don't want to pollute the game file cache with this new file,
// so we can't just call launch() and let it handle the setup.
launcher.putExtra(EXTRA_SELECTED_TITLE, gameFile.getTitle());
launcher.putExtra(EXTRA_SELECTED_GAMEID, gameFile.getGameId());
launcher.putExtra(EXTRA_PLATFORM, gameFile.getPlatform());
}
else
{
// Display the path to the file as the game title in the menu.
launcher.putExtra(EXTRA_SELECTED_TITLE, filePaths);
// Use 00000000 as the game ID. This should match the Desktop version behavior.
// TODO: This should really be pulled from the Core.
launcher.putExtra(EXTRA_SELECTED_GAMEID, "00000000");
// GameFile might be a FIFO log. Assume GameCube for the platform. It doesn't really matter
// anyway, since this only controls the input, and the FIFO player doesn't take any input.
launcher.putExtra(EXTRA_PLATFORM, Platform.GAMECUBE);
}
activity.startActivity(launcher);
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
@ -589,7 +621,7 @@ public final class EmulationActivity extends AppCompatActivity
return;
case MENU_ACTION_CHANGE_DISC:
FileBrowserHelper.openFilePicker(this, REQUEST_CHANGE_DISC);
FileBrowserHelper.openFilePicker(this, REQUEST_CHANGE_DISC, false);
return;
case MENU_SET_IR_SENSITIVITY:

View File

@ -16,6 +16,7 @@ import android.view.View;
import android.widget.Toast;
import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.activities.EmulationActivity;
import org.dolphinemu.dolphinemu.adapters.PlatformPagerAdapter;
import org.dolphinemu.dolphinemu.features.settings.ui.MenuTag;
import org.dolphinemu.dolphinemu.features.settings.ui.SettingsActivity;
@ -143,6 +144,12 @@ public final class MainActivity extends AppCompatActivity implements MainView
FileBrowserHelper.openDirectoryPicker(this);
}
@Override
public void launchOpenFileActivity()
{
FileBrowserHelper.openFilePicker(this, MainPresenter.REQUEST_OPEN_FILE, true);
}
/**
* @param requestCode An int describing whether the Activity that is returning did so successfully.
* @param resultCode An int describing what Activity is giving us this callback.
@ -160,6 +167,14 @@ public final class MainActivity extends AppCompatActivity implements MainView
mPresenter.onDirectorySelected(FileBrowserHelper.getSelectedDirectory(result));
}
break;
case MainPresenter.REQUEST_OPEN_FILE:
// If the user picked a file, as opposed to just backing out.
if (resultCode == MainActivity.RESULT_OK)
{
EmulationActivity.launchFile(this, FileBrowserHelper.getSelectedFiles(result));
}
break;
}
}

View File

@ -15,6 +15,7 @@ import org.dolphinemu.dolphinemu.services.GameFileCacheService;
public final class MainPresenter
{
public static final int REQUEST_ADD_DIRECTORY = 1;
public static final int REQUEST_OPEN_FILE = 2;
private final MainView mView;
private final Context mContext;
@ -85,6 +86,10 @@ public final class MainPresenter
case R.id.button_add_directory:
mView.launchFileListActivity();
return true;
case R.id.menu_open_file:
mView.launchOpenFileActivity();
return true;
}
return false;

View File

@ -21,6 +21,8 @@ public interface MainView
void launchFileListActivity();
void launchOpenFileActivity();
/**
* To be called when the game file cache is updated.
*/

View File

@ -143,6 +143,12 @@ public final class TvMainActivity extends FragmentActivity implements MainView
FileBrowserHelper.openDirectoryPicker(this);
}
@Override
public void launchOpenFileActivity()
{
FileBrowserHelper.openFilePicker(this, MainPresenter.REQUEST_OPEN_FILE, true);
}
@Override
public void showGames()
{
@ -171,6 +177,14 @@ public final class TvMainActivity extends FragmentActivity implements MainView
mPresenter.onDirectorySelected(FileBrowserHelper.getSelectedDirectory(result));
}
break;
case MainPresenter.REQUEST_OPEN_FILE:
// If the user picked a file, as opposed to just backing out.
if (resultCode == MainActivity.RESULT_OK)
{
EmulationActivity.launchFile(this, FileBrowserHelper.getSelectedFiles(result));
}
break;
}
}

View File

@ -30,11 +30,11 @@ public final class FileBrowserHelper
activity.startActivityForResult(i, MainPresenter.REQUEST_ADD_DIRECTORY);
}
public static void openFilePicker(FragmentActivity activity, int requestCode)
public static void openFilePicker(FragmentActivity activity, int requestCode, boolean allowMulti)
{
Intent i = new Intent(activity, CustomFilePickerActivity.class);
i.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false);
i.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, allowMulti);
i.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, false);
i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_FILE);
i.putExtra(FilePickerActivity.EXTRA_START_PATH,
@ -56,4 +56,20 @@ public final class FileBrowserHelper
return null;
}
@Nullable
public static String[] getSelectedFiles(Intent result)
{
// Use the provided utility method to parse the result
List<Uri> files = Utils.getSelectedFilesFromResult(result);
if (!files.isEmpty())
{
String[] paths = new String[files.size()];
for (int i = 0; i < files.size(); i++)
paths[i] = Utils.getFileForUri(files.get(i)).getAbsolutePath();
return paths;
}
return null;
}
}

View File

@ -31,5 +31,10 @@
android:title="@string/grid_menu_refresh"
android:icon="@drawable/ic_refresh"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/menu_open_file"
android:icon="@android:drawable/ic_media_play"
android:title="Open File"
app:showAsAction="ifRoom" />
</menu>