Skip to content

Commit

Permalink
Merge pull request #8251 from stenzek/android-open-file
Browse files Browse the repository at this point in the history
Android: Support opening files directly
  • Loading branch information
Helios747 committed Jul 19, 2019
2 parents 9825caf + 6a29e8f commit 15a429d
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 7 deletions.
Expand Up @@ -198,6 +198,38 @@ public static void launch(FragmentActivity activity, GameFile gameFile)
activity.startActivity(launcher); 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[0]);

// 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 @Override
protected void onCreate(Bundle savedInstanceState) protected void onCreate(Bundle savedInstanceState)
{ {
Expand Down Expand Up @@ -589,7 +621,7 @@ public void handleMenuAction(@MenuAction int menuAction)
return; return;


case MENU_ACTION_CHANGE_DISC: case MENU_ACTION_CHANGE_DISC:
FileBrowserHelper.openFilePicker(this, REQUEST_CHANGE_DISC); FileBrowserHelper.openFilePicker(this, REQUEST_CHANGE_DISC, false);
return; return;


case MENU_SET_IR_SENSITIVITY: case MENU_SET_IR_SENSITIVITY:
Expand Down
Expand Up @@ -19,7 +19,7 @@
public class CustomFilePickerFragment extends FilePickerFragment public class CustomFilePickerFragment extends FilePickerFragment
{ {
private static final Set<String> extensions = new HashSet<>(Arrays.asList( private static final Set<String> extensions = new HashSet<>(Arrays.asList(
"gcm", "tgc", "iso", "ciso", "gcz", "wbfs", "wad", "dol", "elf")); "gcm", "tgc", "iso", "ciso", "gcz", "wbfs", "wad", "dol", "elf", "dff"));


@NonNull @NonNull
@Override @Override
Expand Down
Expand Up @@ -11,6 +11,8 @@ private GameFile(long pointer)
mPointer = pointer; mPointer = pointer;
} }


public native static GameFile parse(String path);

@Override @Override
public native void finalize(); public native void finalize();


Expand Down
Expand Up @@ -16,6 +16,7 @@
import android.widget.Toast; import android.widget.Toast;


import org.dolphinemu.dolphinemu.R; import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.activities.EmulationActivity;
import org.dolphinemu.dolphinemu.adapters.PlatformPagerAdapter; import org.dolphinemu.dolphinemu.adapters.PlatformPagerAdapter;
import org.dolphinemu.dolphinemu.features.settings.ui.MenuTag; import org.dolphinemu.dolphinemu.features.settings.ui.MenuTag;
import org.dolphinemu.dolphinemu.features.settings.ui.SettingsActivity; import org.dolphinemu.dolphinemu.features.settings.ui.SettingsActivity;
Expand Down Expand Up @@ -143,6 +144,12 @@ public void launchFileListActivity()
FileBrowserHelper.openDirectoryPicker(this); 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 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. * @param resultCode An int describing what Activity is giving us this callback.
Expand All @@ -160,6 +167,14 @@ protected void onActivityResult(int requestCode, int resultCode, Intent result)
mPresenter.onDirectorySelected(FileBrowserHelper.getSelectedDirectory(result)); mPresenter.onDirectorySelected(FileBrowserHelper.getSelectedDirectory(result));
} }
break; 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;
} }
} }


Expand Down
Expand Up @@ -15,6 +15,7 @@
public final class MainPresenter public final class MainPresenter
{ {
public static final int REQUEST_ADD_DIRECTORY = 1; public static final int REQUEST_ADD_DIRECTORY = 1;
public static final int REQUEST_OPEN_FILE = 2;


private final MainView mView; private final MainView mView;
private final Context mContext; private final Context mContext;
Expand Down Expand Up @@ -85,6 +86,10 @@ public boolean handleOptionSelection(int itemId, Context context)
case R.id.button_add_directory: case R.id.button_add_directory:
mView.launchFileListActivity(); mView.launchFileListActivity();
return true; return true;

case R.id.menu_open_file:
mView.launchOpenFileActivity();
return true;
} }


return false; return false;
Expand Down
Expand Up @@ -21,6 +21,8 @@ public interface MainView


void launchFileListActivity(); void launchFileListActivity();


void launchOpenFileActivity();

/** /**
* To be called when the game file cache is updated. * To be called when the game file cache is updated.
*/ */
Expand Down
Expand Up @@ -143,6 +143,12 @@ public void launchFileListActivity()
FileBrowserHelper.openDirectoryPicker(this); FileBrowserHelper.openDirectoryPicker(this);
} }


@Override
public void launchOpenFileActivity()
{
FileBrowserHelper.openFilePicker(this, MainPresenter.REQUEST_OPEN_FILE, true);
}

@Override @Override
public void showGames() public void showGames()
{ {
Expand Down Expand Up @@ -171,6 +177,14 @@ protected void onActivityResult(int requestCode, int resultCode, Intent result)
mPresenter.onDirectorySelected(FileBrowserHelper.getSelectedDirectory(result)); mPresenter.onDirectorySelected(FileBrowserHelper.getSelectedDirectory(result));
} }
break; 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;
} }
} }


Expand Down
Expand Up @@ -30,11 +30,11 @@ public static void openDirectoryPicker(FragmentActivity activity)
activity.startActivityForResult(i, MainPresenter.REQUEST_ADD_DIRECTORY); 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); 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_ALLOW_CREATE_DIR, false);
i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_FILE); i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_FILE);
i.putExtra(FilePickerActivity.EXTRA_START_PATH, i.putExtra(FilePickerActivity.EXTRA_START_PATH,
Expand All @@ -56,4 +56,20 @@ public static String getSelectedDirectory(Intent result)


return null; 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;
}
} }
Expand Up @@ -44,9 +44,7 @@ public static void HandleInit(FragmentActivity parent)
if (start_files != null && start_files.length > 0) if (start_files != null && start_files.length > 0)
{ {
// Start the emulation activity, send the ISO passed in and finish the main activity // Start the emulation activity, send the ISO passed in and finish the main activity
Intent emulation_intent = new Intent(parent, EmulationActivity.class); EmulationActivity.launchFile(parent, start_files);
emulation_intent.putExtra(EmulationActivity.EXTRA_SELECTED_GAMES, start_files);
parent.startActivity(emulation_intent);
parent.finish(); parent.finish();
} }
} }
Expand Down
5 changes: 5 additions & 0 deletions Source/Android/app/src/main/res/menu/menu_game_grid.xml
Expand Up @@ -31,5 +31,10 @@
android:title="@string/grid_menu_refresh" android:title="@string/grid_menu_refresh"
android:icon="@drawable/ic_refresh" android:icon="@drawable/ic_refresh"
app:showAsAction="ifRoom"/> 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> </menu>
11 changes: 11 additions & 0 deletions Source/Android/jni/GameList/GameFile.cpp
Expand Up @@ -165,6 +165,17 @@ JNIEXPORT jint JNICALL Java_org_dolphinemu_dolphinemu_model_GameFile_getBannerHe
return static_cast<jint>(GetRef(env, obj)->GetBannerImage().height); return static_cast<jint>(GetRef(env, obj)->GetBannerImage().height);
} }


JNIEXPORT jobject JNICALL Java_org_dolphinemu_dolphinemu_model_GameFile_parse(JNIEnv* env,
jobject obj,
jstring path)
{
auto game_file = std::make_shared<UICommon::GameFile>(GetJString(env, path));
if (!game_file->IsValid())
game_file.reset();

return GameFileToJava(env, game_file);
}

#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

0 comments on commit 15a429d

Please sign in to comment.