Skip to content

Commit

Permalink
Import key option in android (#620)
Browse files Browse the repository at this point in the history
* Implement option to import keys

* Fix crashes

* Remove redundant code
  • Loading branch information
GabrielBRDeveloper authored Oct 25, 2024
1 parent 85d363c commit a6750e7
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/jni_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand Down Expand Up @@ -209,6 +210,9 @@ protected void onDestroy() {
}

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ protected void setSwitchValue(String id, boolean value) {
((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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -10,8 +16,11 @@
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);
Expand All @@ -21,6 +30,11 @@ public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable S
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
Expand All @@ -31,5 +45,45 @@ public void onResume() {

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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ public static long getLastModified(String path) {
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();
Expand Down
6 changes: 6 additions & 0 deletions src/pandroid/app/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
6 changes: 6 additions & 0 deletions src/pandroid/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
10 changes: 10 additions & 0 deletions src/pandroid/app/src/main/res/xml/general_preference.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit a6750e7

Please sign in to comment.