diff options
| author | 2023-03-06 15:54:24 -0500 | |
|---|---|---|
| committer | 2023-06-03 00:05:35 -0700 | |
| commit | fa38c7be4fa46f46168efd35342d06867a4dc7d4 (patch) | |
| tree | bea554b3a2c4b25dff4f95aecb3795e47b7b68dd /src/android | |
| parent | android: Use androidx preferences (diff) | |
| download | yuzu-fa38c7be4fa46f46168efd35342d06867a4dc7d4.tar.gz yuzu-fa38c7be4fa46f46168efd35342d06867a4dc7d4.tar.xz yuzu-fa38c7be4fa46f46168efd35342d06867a4dc7d4.zip | |
android: Convert Settings to Kotlin
Diffstat (limited to 'src/android')
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/Settings.java | 127 | ||||
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/Settings.kt | 145 |
2 files changed, 145 insertions, 127 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/Settings.java b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/Settings.java deleted file mode 100644 index 3126eba73..000000000 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/Settings.java +++ /dev/null | |||
| @@ -1,127 +0,0 @@ | |||
| 1 | package org.yuzu.yuzu_emu.features.settings.model; | ||
| 2 | |||
| 3 | import android.text.TextUtils; | ||
| 4 | |||
| 5 | import org.yuzu.yuzu_emu.YuzuApplication; | ||
| 6 | import org.yuzu.yuzu_emu.R; | ||
| 7 | import org.yuzu.yuzu_emu.features.settings.ui.SettingsActivityView; | ||
| 8 | import org.yuzu.yuzu_emu.features.settings.utils.SettingsFile; | ||
| 9 | |||
| 10 | import java.util.Arrays; | ||
| 11 | import java.util.HashMap; | ||
| 12 | import java.util.List; | ||
| 13 | import java.util.Map; | ||
| 14 | import java.util.TreeMap; | ||
| 15 | |||
| 16 | public class Settings { | ||
| 17 | public static final String SECTION_GENERAL = "General"; | ||
| 18 | public static final String SECTION_SYSTEM = "System"; | ||
| 19 | public static final String SECTION_RENDERER = "Renderer"; | ||
| 20 | public static final String SECTION_AUDIO = "Audio"; | ||
| 21 | public static final String SECTION_CPU = "Cpu"; | ||
| 22 | |||
| 23 | private String gameId; | ||
| 24 | |||
| 25 | private static final Map<String, List<String>> configFileSectionsMap = new HashMap<>(); | ||
| 26 | |||
| 27 | static { | ||
| 28 | configFileSectionsMap.put(SettingsFile.FILE_NAME_CONFIG, Arrays.asList(SECTION_GENERAL, SECTION_SYSTEM, SECTION_RENDERER, SECTION_AUDIO, SECTION_CPU)); | ||
| 29 | } | ||
| 30 | |||
| 31 | /** | ||
| 32 | * A HashMap<String, SettingSection> that constructs a new SettingSection instead of returning null | ||
| 33 | * when getting a key not already in the map | ||
| 34 | */ | ||
| 35 | public static final class SettingsSectionMap extends HashMap<String, SettingSection> { | ||
| 36 | @Override | ||
| 37 | public SettingSection get(Object key) { | ||
| 38 | if (!(key instanceof String)) { | ||
| 39 | return null; | ||
| 40 | } | ||
| 41 | |||
| 42 | String stringKey = (String) key; | ||
| 43 | |||
| 44 | if (!super.containsKey(stringKey)) { | ||
| 45 | SettingSection section = new SettingSection(stringKey); | ||
| 46 | super.put(stringKey, section); | ||
| 47 | return section; | ||
| 48 | } | ||
| 49 | return super.get(key); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | private HashMap<String, SettingSection> sections = new Settings.SettingsSectionMap(); | ||
| 54 | |||
| 55 | public SettingSection getSection(String sectionName) { | ||
| 56 | return sections.get(sectionName); | ||
| 57 | } | ||
| 58 | |||
| 59 | public boolean isEmpty() { | ||
| 60 | return sections.isEmpty(); | ||
| 61 | } | ||
| 62 | |||
| 63 | public HashMap<String, SettingSection> getSections() { | ||
| 64 | return sections; | ||
| 65 | } | ||
| 66 | |||
| 67 | public void loadSettings(SettingsActivityView view) { | ||
| 68 | sections = new Settings.SettingsSectionMap(); | ||
| 69 | loadCitraSettings(view); | ||
| 70 | |||
| 71 | if (!TextUtils.isEmpty(gameId)) { | ||
| 72 | loadCustomGameSettings(gameId, view); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | private void loadCitraSettings(SettingsActivityView view) { | ||
| 77 | for (Map.Entry<String, List<String>> entry : configFileSectionsMap.entrySet()) { | ||
| 78 | String fileName = entry.getKey(); | ||
| 79 | sections.putAll(SettingsFile.readFile(fileName, view)); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | private void loadCustomGameSettings(String gameId, SettingsActivityView view) { | ||
| 84 | // custom game settings | ||
| 85 | mergeSections(SettingsFile.readCustomGameSettings(gameId, view)); | ||
| 86 | } | ||
| 87 | |||
| 88 | private void mergeSections(HashMap<String, SettingSection> updatedSections) { | ||
| 89 | for (Map.Entry<String, SettingSection> entry : updatedSections.entrySet()) { | ||
| 90 | if (sections.containsKey(entry.getKey())) { | ||
| 91 | SettingSection originalSection = sections.get(entry.getKey()); | ||
| 92 | SettingSection updatedSection = entry.getValue(); | ||
| 93 | originalSection.mergeSection(updatedSection); | ||
| 94 | } else { | ||
| 95 | sections.put(entry.getKey(), entry.getValue()); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | public void loadSettings(String gameId, SettingsActivityView view) { | ||
| 101 | this.gameId = gameId; | ||
| 102 | loadSettings(view); | ||
| 103 | } | ||
| 104 | |||
| 105 | public void saveSettings(SettingsActivityView view) { | ||
| 106 | if (TextUtils.isEmpty(gameId)) { | ||
| 107 | view.showToastMessage(YuzuApplication.getAppContext().getString(R.string.ini_saved), false); | ||
| 108 | |||
| 109 | for (Map.Entry<String, List<String>> entry : configFileSectionsMap.entrySet()) { | ||
| 110 | String fileName = entry.getKey(); | ||
| 111 | List<String> sectionNames = entry.getValue(); | ||
| 112 | TreeMap<String, SettingSection> iniSections = new TreeMap<>(); | ||
| 113 | for (String section : sectionNames) { | ||
| 114 | iniSections.put(section, sections.get(section)); | ||
| 115 | } | ||
| 116 | |||
| 117 | SettingsFile.saveFile(fileName, iniSections, view); | ||
| 118 | } | ||
| 119 | } else { | ||
| 120 | // custom game settings | ||
| 121 | view.showToastMessage(YuzuApplication.getAppContext().getString(R.string.gameid_saved, gameId), false); | ||
| 122 | |||
| 123 | SettingsFile.saveCustomGameSettings(gameId, sections); | ||
| 124 | } | ||
| 125 | |||
| 126 | } | ||
| 127 | } \ No newline at end of file | ||
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/Settings.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/Settings.kt new file mode 100644 index 000000000..af887402a --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/Settings.kt | |||
| @@ -0,0 +1,145 @@ | |||
| 1 | package org.yuzu.yuzu_emu.features.settings.model | ||
| 2 | |||
| 3 | import android.text.TextUtils | ||
| 4 | import org.yuzu.yuzu_emu.R | ||
| 5 | import org.yuzu.yuzu_emu.YuzuApplication | ||
| 6 | import org.yuzu.yuzu_emu.features.settings.ui.SettingsActivityView | ||
| 7 | import org.yuzu.yuzu_emu.features.settings.utils.SettingsFile | ||
| 8 | import java.util.* | ||
| 9 | |||
| 10 | class Settings { | ||
| 11 | private var gameId: String? = null | ||
| 12 | |||
| 13 | /** | ||
| 14 | * A HashMap<String></String>, SettingSection> that constructs a new SettingSection instead of returning null | ||
| 15 | * when getting a key not already in the map | ||
| 16 | */ | ||
| 17 | class SettingsSectionMap : HashMap<String, SettingSection?>() { | ||
| 18 | override operator fun get(key: String): SettingSection? { | ||
| 19 | if (!super.containsKey(key)) { | ||
| 20 | val section = SettingSection(key) | ||
| 21 | super.put(key, section) | ||
| 22 | return section | ||
| 23 | } | ||
| 24 | return super.get(key) | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | var sections: HashMap<String, SettingSection?> = SettingsSectionMap() | ||
| 29 | |||
| 30 | fun getSection(sectionName: String): SettingSection? { | ||
| 31 | return sections[sectionName] | ||
| 32 | } | ||
| 33 | |||
| 34 | val isEmpty: Boolean | ||
| 35 | get() = sections.isEmpty() | ||
| 36 | |||
| 37 | fun loadSettings(view: SettingsActivityView) { | ||
| 38 | sections = SettingsSectionMap() | ||
| 39 | loadYuzuSettings(view) | ||
| 40 | if (!TextUtils.isEmpty(gameId)) { | ||
| 41 | loadCustomGameSettings(gameId!!, view) | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | private fun loadYuzuSettings(view: SettingsActivityView) { | ||
| 46 | for ((fileName) in configFileSectionsMap) { | ||
| 47 | sections.putAll(SettingsFile.readFile(fileName, view)) | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | private fun loadCustomGameSettings(gameId: String, view: SettingsActivityView) { | ||
| 52 | // Custom game settings | ||
| 53 | mergeSections(SettingsFile.readCustomGameSettings(gameId, view)) | ||
| 54 | } | ||
| 55 | |||
| 56 | private fun mergeSections(updatedSections: HashMap<String, SettingSection?>) { | ||
| 57 | for ((key, updatedSection) in updatedSections) { | ||
| 58 | if (sections.containsKey(key)) { | ||
| 59 | val originalSection = sections[key] | ||
| 60 | originalSection!!.mergeSection(updatedSection!!) | ||
| 61 | } else { | ||
| 62 | sections[key] = updatedSection | ||
| 63 | } | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | fun loadSettings(gameId: String, view: SettingsActivityView) { | ||
| 68 | this.gameId = gameId | ||
| 69 | loadSettings(view) | ||
| 70 | } | ||
| 71 | |||
| 72 | fun saveSettings(view: SettingsActivityView) { | ||
| 73 | if (TextUtils.isEmpty(gameId)) { | ||
| 74 | view.showToastMessage( | ||
| 75 | YuzuApplication.appContext.getString(R.string.ini_saved), | ||
| 76 | false | ||
| 77 | ) | ||
| 78 | |||
| 79 | for ((fileName, sectionNames) in configFileSectionsMap) { | ||
| 80 | val iniSections = TreeMap<String, SettingSection>() | ||
| 81 | for (section in sectionNames) { | ||
| 82 | iniSections[section] = sections[section]!! | ||
| 83 | } | ||
| 84 | |||
| 85 | SettingsFile.saveFile(fileName, iniSections, view) | ||
| 86 | } | ||
| 87 | } else { | ||
| 88 | // Custom game settings | ||
| 89 | view.showToastMessage( | ||
| 90 | YuzuApplication.appContext.getString(R.string.gameid_saved, gameId), | ||
| 91 | false | ||
| 92 | ) | ||
| 93 | |||
| 94 | SettingsFile.saveCustomGameSettings(gameId, sections) | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | companion object { | ||
| 99 | const val SECTION_GENERAL = "General" | ||
| 100 | const val SECTION_SYSTEM = "System" | ||
| 101 | const val SECTION_RENDERER = "Renderer" | ||
| 102 | const val SECTION_AUDIO = "Audio" | ||
| 103 | const val SECTION_CPU = "Cpu" | ||
| 104 | |||
| 105 | const val PREF_OVERLAY_INIT = "OverlayInit" | ||
| 106 | const val PREF_CONTROL_SCALE = "controlScale" | ||
| 107 | const val PREF_TOUCH_ENABLED = "isTouchEnabled" | ||
| 108 | const val PREF_BUTTON_TOGGLE_0 = "buttonToggleO" | ||
| 109 | const val PREF_BUTTON_TOGGLE_1 = "buttonToggle1" | ||
| 110 | const val PREF_BUTTON_TOGGLE_2 = "buttonToggle2" | ||
| 111 | const val PREF_BUTTON_TOGGLE_3 = "buttonToggle3" | ||
| 112 | const val PREF_BUTTON_TOGGLE_4 = "buttonToggle4" | ||
| 113 | const val PREF_BUTTON_TOGGLE_5 = "buttonToggle5" | ||
| 114 | const val PREF_BUTTON_TOGGLE_6 = "buttonToggle6" | ||
| 115 | const val PREF_BUTTON_TOGGLE_7 = "buttonToggle7" | ||
| 116 | const val PREF_BUTTON_TOGGLE_8 = "buttonToggle8" | ||
| 117 | const val PREF_BUTTON_TOGGLE_9 = "buttonToggle9" | ||
| 118 | const val PREF_BUTTON_TOGGLE_10 = "buttonToggle10" | ||
| 119 | const val PREF_BUTTON_TOGGLE_11 = "buttonToggle11" | ||
| 120 | const val PREF_BUTTON_TOGGLE_12 = "buttonToggle12" | ||
| 121 | const val PREF_BUTTON_TOGGLE_13 = "buttonToggle13" | ||
| 122 | const val PREF_BUTTON_TOGGLE_14 = "buttonToggle14" | ||
| 123 | |||
| 124 | const val PREF_MENU_SETTINGS_JOYSTICK_REL_CENTER = "EmulationMenuSettings_JoystickRelCenter" | ||
| 125 | const val PREF_MENU_SETTINGS_DPAD_SLIDE = "EmulationMenuSettings_DpadSlideEnable" | ||
| 126 | const val PREF_MENU_SETTINGS_LANDSCAPE = "EmulationMenuSettings_LandscapeScreenLayout" | ||
| 127 | const val PREF_MENU_SETTINGS_SHOW_FPS = "EmulationMenuSettings_ShowFps" | ||
| 128 | const val PREF_MENU_SETTINGS_SHOW_OVERLAY = "EmulationMenuSettings_ShowOverlay" | ||
| 129 | |||
| 130 | const val PREF_FIRST_APP_LAUNCH = "FirstApplicationLaunch" | ||
| 131 | |||
| 132 | private val configFileSectionsMap: MutableMap<String, List<String>> = HashMap() | ||
| 133 | |||
| 134 | init { | ||
| 135 | configFileSectionsMap[SettingsFile.FILE_NAME_CONFIG] = | ||
| 136 | listOf( | ||
| 137 | SECTION_GENERAL, | ||
| 138 | SECTION_SYSTEM, | ||
| 139 | SECTION_RENDERER, | ||
| 140 | SECTION_AUDIO, | ||
| 141 | SECTION_CPU | ||
| 142 | ) | ||
| 143 | } | ||
| 144 | } | ||
| 145 | } | ||