From 8dc0b38f001a6980a2c54e499bf10900179b818d Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 19 Aug 2013 21:56:13 -0400 Subject: [PATCH] [Android] Decouple Dolphin.ini config file saving from GameListActivity. It doesn't make sense to save the config AFTER control is returned from PrefsFragment to GameListActivity, since the main purpose of PrefsFragment is to handle the user settings. So, instead, we call SaveConfigToDolphinIni() in the PrefsFragment.onDestroy() method. This way, when the PrefsFragment object is being 'destroyed', it will write the settings to the ini. --- .../dolphinemu/GameListActivity.java | 14 +---- .../dolphinemu/dolphinemu/PrefsFragment.java | 12 +++- .../dolphinemu/UserPreferences.java | 59 +++++++++++++++++++ 3 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 Source/Android/src/org/dolphinemu/dolphinemu/UserPreferences.java diff --git a/Source/Android/src/org/dolphinemu/dolphinemu/GameListActivity.java b/Source/Android/src/org/dolphinemu/dolphinemu/GameListActivity.java index 625265be35..b9e86fbbd9 100644 --- a/Source/Android/src/org/dolphinemu/dolphinemu/GameListActivity.java +++ b/Source/Android/src/org/dolphinemu/dolphinemu/GameListActivity.java @@ -106,19 +106,6 @@ public final class GameListActivity extends Activity case 1: recreateFragment(); break; - - // Settings - case 2: - { - // Saves the settings that the user has set in the settings menu to the Dolphin ini files. - // This is done so that changes can be reflected when the emulator is run next. - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); - - NativeLibrary.SetConfig("Dolphin.ini", "Core", "CPUCore", prefs.getString("cpuCorePref", "")); - NativeLibrary.SetConfig("Dolphin.ini", "Core", "CPUThread", prefs.getBoolean("dualCorePref", true) ? "True" : "False"); - NativeLibrary.SetConfig("Dolphin.ini", "Core", "GFXBackend", prefs.getString("gpuPref", "")); - } - break; case 3: // Gamepad settings { @@ -137,6 +124,7 @@ public final class GameListActivity extends Activity break; case 0: // Game List + case 2: // Settings case 4: // About /* Do Nothing */ break; diff --git a/Source/Android/src/org/dolphinemu/dolphinemu/PrefsFragment.java b/Source/Android/src/org/dolphinemu/dolphinemu/PrefsFragment.java index bdc036e38a..bacede461f 100644 --- a/Source/Android/src/org/dolphinemu/dolphinemu/PrefsFragment.java +++ b/Source/Android/src/org/dolphinemu/dolphinemu/PrefsFragment.java @@ -5,7 +5,6 @@ import android.os.Build; import android.os.Bundle; import android.preference.ListPreference; import android.preference.PreferenceFragment; - import javax.microedition.khronos.egl.*; import javax.microedition.khronos.opengles.GL10; @@ -203,4 +202,15 @@ public final class PrefsFragment extends PreferenceFragment + " must implement OnGameListZeroListener"); } } + + @Override + public void onDestroy() + { + super.onDestroy(); + + // When the fragment is done being used, save the settings + // to the Dolphin ini file. + UserPreferences userPrefs = new UserPreferences(m_activity); + userPrefs.SaveConfigToDolphinIni(); + } } diff --git a/Source/Android/src/org/dolphinemu/dolphinemu/UserPreferences.java b/Source/Android/src/org/dolphinemu/dolphinemu/UserPreferences.java new file mode 100644 index 0000000000..aea2ca6e31 --- /dev/null +++ b/Source/Android/src/org/dolphinemu/dolphinemu/UserPreferences.java @@ -0,0 +1,59 @@ +package org.dolphinemu.dolphinemu; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +/** + * A class that retrieves all of the set user preferences in Android, in a safe way. + *

+ * If any preferences are added to this emulator, an accessor for that preference + * should be added here. This way lengthy calls to getters from SharedPreferences + * aren't made necessary. + */ +public final class UserPreferences +{ + // The cached shared preferences. + private final SharedPreferences mPrefs; + + // Whether or not the user is using dual core. + private final boolean isUsingDualCore; + + // The current CPU core being used. + private final String currentEmuCore; + + // The current video back-end being used. + private final String currentVideoBackend; + + /** + * Constructor + * + * @param ctx The context to use an instance of this class in. + * This allows the class to retrieve the SharedPreferences + * instance from the given context, which, in turn allows + * this class to function for its intended purpose. + */ + public UserPreferences(Context ctx) + { + // Get an instance of all of our stored preferences. + this.mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx); + + //-- Assign to the variables to cache the settings. --// + + this.isUsingDualCore = mPrefs.getBoolean("dualCorePref", true); + + // Fall back to interpreter if it somehow can't find a CPU core. + this.currentEmuCore = mPrefs.getString("cpuCorePref", "0"); + + // Fall back to using software rendering if another valid backend can't be found. + this.currentVideoBackend = mPrefs.getString("gpuPref", "Software Renderer"); + } + + /** Writes the config to the Dolphin ini file. */ + public void SaveConfigToDolphinIni() + { + NativeLibrary.SetConfig("Dolphin.ini", "Core", "CPUCore", currentEmuCore); + NativeLibrary.SetConfig("Dolphin.ini", "Core", "CPUThread", isUsingDualCore ? "True" : "False"); + NativeLibrary.SetConfig("Dolphin.ini", "Core", "GFXBackend", currentVideoBackend); + } +}