Skip to content

Commit

Permalink
android: disable import/export home when SAF is disabled. Code clean up
Browse files Browse the repository at this point in the history
Disable import/export home folder buttons when not using SAF file
picker.
Get rid of unused home migration feature when onboarding.
Android TV screenshots and banner.
  • Loading branch information
flyinghead committed Oct 18, 2024
1 parent 4750ddb commit 54e8589
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 154 deletions.
13 changes: 8 additions & 5 deletions core/ui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1768,11 +1768,14 @@ static void gui_settings_general()
ImGui::Text("%s", s.c_str());
ImguiStyleVar _(ImGuiStyleVar_FramePadding, ScaledVec2(24, 3));
#ifdef __ANDROID__
if (ImGui::Button("Import"))
hostfs::importHomeDirectory();
ImGui::SameLine();
if (ImGui::Button("Export"))
hostfs::exportHomeDirectory();
{
DisabledScope _(!config::UseSafFilePicker);
if (ImGui::Button("Import"))
hostfs::importHomeDirectory();
ImGui::SameLine();
if (ImGui::Button("Export"))
hostfs::exportHomeDirectory();
}
#endif
#ifdef TARGET_MAC
if (ImGui::Button("Reveal in Finder"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
Expand Down Expand Up @@ -35,9 +34,6 @@
import com.flycast.emulator.periph.SipEmulator;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -96,19 +92,13 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
new HttpClient().nativeInit();

String homeDir = prefs.getString(Config.pref_home, "");
// Check that home dir is valid, migrate if needed
String newHome = checkHomeDirectory(homeDir);
if (newHome != null) {
if (!newHome.equals(homeDir))
prefs.edit().putString(Config.pref_home, newHome).apply();
finishCreation();
// Check that home dir is valid
if (homeDir.isEmpty()) {
// home dir not set: use default
homeDir = getDefaultHomeDir();
prefs.edit().putString(Config.pref_home, homeDir).apply();
}
Log.i("flycast", "BaseGLActivity.onCreate done");
}

protected void finishCreation()
{
String homeDir = prefs.getString(Config.pref_home, getDefaultHomeDir());
String result = JNIdc.initEnvironment((Emulator)getApplicationContext(), getFilesDir().getAbsolutePath(), homeDir,
Locale.getDefault().toString());
if (result != null) {
Expand Down Expand Up @@ -178,7 +168,7 @@ public void onClick(DialogInterface dialog,int id) {
pendingIntentUrl = gameUri.toString();
}
}
Log.i("flycast", "BaseGLActivity.finishCreation done");
Log.i("flycast", "BaseGLActivity.onCreate done");
}

private void setStorageDirectories()
Expand Down Expand Up @@ -430,132 +420,6 @@ private String getDefaultHomeDir() {
return dir.getAbsolutePath();
}

private String checkHomeDirectory(String homeDir)
{
if (homeDir.isEmpty())
// home dir not set: use default
return getDefaultHomeDir();
// must account for the fact that homeDir may be on internal storage but external storage is now available
if (homeDir.startsWith(getDefaultHomeDir()) || homeDir.startsWith(getFilesDir().getAbsolutePath()))
// home dir is ok
return homeDir;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P)
// no need to migrate on Android 9 or earlier
return homeDir;
// migration disabled for now
return homeDir;
/*
// Only ask to migrate once
String migrationPref = "legacy-storage-migration-done";
if (prefs.getBoolean(migrationPref, false))
return homeDir;
// Ask the user if he wants to migrate
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("The current Flycast home folder will be inaccessible in future versions.\n\n"
+ "Do you want to move config and save files to a valid location?");
dlgAlert.setTitle("Migrate Home");
dlgAlert.setPositiveButton("Yes",
(dialog, id) -> BaseGLActivity.this.migrateHome(homeDir));
dlgAlert.setNegativeButton("No",
(dialog, id) -> BaseGLActivity.this.finishCreation());
dlgAlert.setIcon(android.R.drawable.ic_dialog_alert);
dlgAlert.setCancelable(false);
dlgAlert.create().show();
// Don't ask again
prefs.edit().putBoolean(migrationPref, true).apply();
return null;
*/
}

private boolean migrationThreadCancelled = false;
private void migrateHome(String oldHome)
{
File source = new File(oldHome);
File dest = new File(getDefaultHomeDir());
ProgressDialog progress = ProgressDialog.show(this, "Migrating", "Moving files to their new home",
true, true, dialogInterface -> migrationThreadCancelled = true);
progress.show();

migrationThreadCancelled = false;
Thread thread = new Thread(new Runnable() {
private void moveFile(File file, File toDir)
{
//Log.d("flycast", "Moving " + file.getAbsolutePath() + " to " + toDir.getAbsolutePath());
try {
File dest = new File(toDir, file.getName());
// file.renameTo(dest) doesn't seem to work
FileInputStream in = new FileInputStream(file);
FileOutputStream out = new FileOutputStream(dest);
byte[] buf = new byte[8192];
while (true) {
int len = in.read(buf);
if (len == -1)
break;
out.write(buf, 0, len);
}
out.close();
in.close();
file.delete();
} catch (IOException e) {
Log.e("flycast", "Error moving " + file.getAbsolutePath(), e);
}
}

private void moveDir(File from, File to)
{
//Log.d("flycast", "Moving dir " + from.getAbsolutePath() + " to " + to.getAbsolutePath());
if (!from.exists())
return;
File[] files = from.listFiles();
if (files == null) {
Log.e("flycast", "Can't list content of " + from.getAbsolutePath());
return;
}
for (File file : files)
{
if (migrationThreadCancelled)
break;
if (file.isFile())
moveFile(file, to);
else if (file.isDirectory() && !file.getName().equals("boxart")) {
File subDir = new File(to, file.getName());
subDir.mkdir();
moveDir(file, subDir);
}
}
from.delete();
}

private void migrate()
{
moveFile(new File(source, "emu.cfg"), dest);
if (migrationThreadCancelled)
return;
File mappings = new File(dest, "mappings");
mappings.mkdirs();
moveDir(new File(source, "mappings"), mappings);
if (migrationThreadCancelled)
return;
File data = new File(dest, "data");
data.mkdirs();
moveDir(new File(source, "data"), data);
}

@Override
public void run()
{
migrate();
runOnUiThread(() -> {
prefs.edit().putString(Config.pref_home, getDefaultHomeDir()).apply();
progress.dismiss();
BaseGLActivity.this.finishCreation();
});
}
});
thread.start();
}

// Called from native code
public void onGameStateChange(boolean started) {
runOnUiThread(new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,13 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Log.i("flycast", "NativeGLActivity.onCreate done");
}

protected void finishCreation()
{
super.finishCreation();
// Create the actual GL view
mView = new NativeGLView(this);
mLayout = new RelativeLayout(this);
mLayout.addView(mView);

setContentView(mLayout);
Log.i("flycast", "NativeGLActivity.finishCreation done");
Log.i("flycast", "NativeGLActivity.onCreate done");
}

@Override
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 54e8589

Please sign in to comment.