Import key option in android (#620)

* Implement option to import keys

* Fix crashes

* Remove redundant code
This commit is contained in:
Gabriel Machado 2024-10-25 03:22:26 -04:00 committed by GitHub
parent 85d363c17d
commit a6750e7aef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 90 additions and 1 deletions

View file

@ -89,6 +89,7 @@ AlberFunction(void, Finalize)(JNIEnv* env, jobject obj) {
emulator = nullptr;
hidService = nullptr;
renderer = nullptr;
romLoaded = false;
}
AlberFunction(jboolean, HasRomLoaded)(JNIEnv* env, jobject obj) { return romLoaded; }

View file

@ -15,6 +15,7 @@ import android.renderscript.Matrix3f;
import android.renderscript.Matrix4f;
import android.util.Log;
import android.util.Rational;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Surface;
@ -209,6 +210,9 @@ public class GameActivity extends BaseActivity implements EmulatorCallback, Sens
}
private float getDeviceRotationAngle() {
if (getWindow().getDecorView() == null || getWindow().getDecorView().getDisplay() == null)
return 0.0f;
int rotation = getWindow().getDecorView().getDisplay().getRotation();
switch (rotation) {
case Surface.ROTATION_90: return 90.0f;

View file

@ -26,6 +26,10 @@ public abstract class BasePreferenceFragment extends PreferenceFragmentCompat {
((SwitchPreferenceCompat)findPreference(id)).setChecked(value);
}
protected void setSummaryValue(String id,String text) {
findPreference(id).setSummary(text);
}
protected void setActivityTitle(@StringRes int titleId) {
ActionBar header = ((AppCompatActivity) requireActivity()).getSupportActionBar();
if (header != null) {

View file

@ -1,7 +1,13 @@
package com.panda3ds.pandroid.app.preferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.preference.SwitchPreferenceCompat;
@ -10,8 +16,11 @@ import com.panda3ds.pandroid.app.PreferenceActivity;
import com.panda3ds.pandroid.app.base.BasePreferenceFragment;
import com.panda3ds.pandroid.app.preferences.screen_editor.ScreenLayoutsPreference;
import com.panda3ds.pandroid.data.config.GlobalConfig;
import com.panda3ds.pandroid.utils.FileUtils;
public class GeneralPreferences extends BasePreferenceFragment {
public class GeneralPreferences extends BasePreferenceFragment implements ActivityResultCallback<Uri> {
private final ActivityResultContracts.OpenDocument openFolderContract = new ActivityResultContracts.OpenDocument();
private ActivityResultLauncher<String[]> pickFileRequest;
@Override
public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
setPreferencesFromResource(R.xml.general_preference, rootKey);
@ -21,6 +30,11 @@ public class GeneralPreferences extends BasePreferenceFragment {
setItemClick("behavior.pictureInPicture", (pref)-> GlobalConfig.set(GlobalConfig.KEY_PICTURE_IN_PICTURE, ((SwitchPreferenceCompat)pref).isChecked()));
setActivityTitle(R.string.general);
refresh();
setItemClick("games.aes_key", pref -> pickFileRequest.launch(new String[]{ "text/plain" }));
setItemClick("games.seed_db", pref -> pickFileRequest.launch(new String[]{ "application/octet-stream" }));
pickFileRequest = registerForActivityResult(openFolderContract, this);
}
@Override
@ -31,5 +45,45 @@ public class GeneralPreferences extends BasePreferenceFragment {
private void refresh() {
setSwitchValue("behavior.pictureInPicture", GlobalConfig.get(GlobalConfig.KEY_PICTURE_IN_PICTURE));
setSummaryValue("games.aes_key", String.format(getString(FileUtils.exists(FileUtils.getPrivatePath()+"/sysdata/aes_keys.txt") ? R.string.file_available : R.string.file_not_available), "aes_key.txt"));
setSummaryValue("games.seed_db", String.format(getString(FileUtils.exists(FileUtils.getPrivatePath()+"/sysdata/seeddb.bin") ? R.string.file_available : R.string.file_not_available), "seeddb.bin"));
}
@Override
public void onDestroy() {
super.onDestroy();
if (pickFileRequest != null) {
pickFileRequest.unregister();
pickFileRequest = null;
}
}
@Override
public void onActivityResult(Uri result) {
if (result != null) {
String path = result.toString();
Log.w("File", path + " -> " + FileUtils.getName(path));
switch (String.valueOf(FileUtils.getName(path))) {
case "aes_keys.txt":
case "seeddb.bin": {
String name = FileUtils.getName(path);
if (FileUtils.getLength(path) < 1024 * 256) {
String sysdataFolder = FileUtils.getPrivatePath() + "/sysdata";
if (!FileUtils.exists(sysdataFolder)) {
FileUtils.createDir(FileUtils.getPrivatePath(), "sysdata");
}
if (FileUtils.exists(sysdataFolder + "/" + name)) {
FileUtils.delete(sysdataFolder + "/" + name);
}
FileUtils.copyFile(path, FileUtils.getPrivatePath() + "/sysdata/", name);
Toast.makeText(getActivity(), String.format(getString(R.string.file_imported), name), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), R.string.invalid_file, Toast.LENGTH_LONG).show();
}
} break;
default: Toast.makeText(getActivity(), R.string.invalid_file, Toast.LENGTH_LONG).show(); break;
}
refresh();
}
}
}

View file

@ -230,6 +230,10 @@ public class FileUtils {
return parseFile(path).lastModified();
}
public static long getLength(String path) {
return parseFile(path).length();
}
public static String[] listFiles(String path) {
DocumentFile folder = parseFile(path);
DocumentFile[] files = folder.listFiles();

View file

@ -92,4 +92,10 @@
<string name="tools">Ferramentas</string>
<string name="pref_accurate_shader_title">Multiplicação precisa de shader</string>
<string name="pref_accurate_shader_summary">Usar calculos mais precisos para shaders</string>
<string name="pref_game_crypto_keys">Importar chaves</string>
<string name="file_available">%s disponível</string>
<string name="file_not_available">%s não disponível</string>
<string name="pref_game_seed_db_keys">Importar SeedDB</string>
<string name="invalid_file">Arquivo inválido</string>
<string name="file_imported">%s Importado</string>
</resources>

View file

@ -98,4 +98,10 @@
<string name="invalid_game">Invalid game</string>
<string name="pref_accurate_shader_title">Accurate shader multiplication</string>
<string name="pref_accurate_shader_summary">Can improve rendering at a small performance loss</string>
<string name="pref_game_crypto_keys">Import keys</string>
<string name="file_imported">%s imported</string>
<string name="file_available">%s available</string>
<string name="file_not_available">%s not available</string>
<string name="pref_game_seed_db_keys">Import SeedDB</string>
<string name="invalid_file">Invalid file</string>
</resources>

View file

@ -23,6 +23,16 @@
app:title="@string/pref_game_folders"
app:summary="@string/pref_game_folders_summary"
app:iconSpaceReserved="false"/>
<Preference
android:key="games.aes_key"
app:title="@string/pref_game_crypto_keys"
app:summary="@string/pref_game_crypto_keys"
app:iconSpaceReserved="false"/>
<Preference
android:key="games.seed_db"
app:title="@string/pref_game_seed_db_keys"
app:summary="@string/pref_game_crypto_keys"
app:iconSpaceReserved="false"/>
</PreferenceCategory>
<PreferenceCategory
app:title="@string/behavior"