From 2c560e05a82410be8d017167baee9328746afb81 Mon Sep 17 00:00:00 2001
From: Charles Lombardo <clombardo169@gmail.com>
Date: Wed, 15 Mar 2023 03:25:47 -0400
Subject: [PATCH] Android: Convert Settings to Kotlin

---
 .../features/cheats/ui/CheatsActivity.kt      |   2 +-
 .../features/settings/model/Settings.java     | 181 ------------------
 .../features/settings/model/Settings.kt       | 144 ++++++++++++++
 3 files changed, 145 insertions(+), 182 deletions(-)
 delete mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/Settings.java
 create mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/Settings.kt

diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatsActivity.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatsActivity.kt
index d22431752b..dd1226d3f2 100644
--- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatsActivity.kt
+++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatsActivity.kt
@@ -160,7 +160,7 @@ class CheatsActivity : AppCompatActivity(), PanelSlideListener {
 
     fun loadGameSpecificSettings(): Settings {
         val settings = Settings()
-        settings.loadSettings(gameId, revision, isWii)
+        settings.loadSettings(gameId!!, revision, isWii)
         return settings
     }
 
diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/Settings.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/Settings.java
deleted file mode 100644
index 0bb935f3df..0000000000
--- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/Settings.java
+++ /dev/null
@@ -1,181 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-or-later
-
-package org.dolphinemu.dolphinemu.features.settings.model;
-
-import android.content.Context;
-import android.text.TextUtils;
-import android.widget.Toast;
-
-import org.dolphinemu.dolphinemu.NativeLibrary;
-import org.dolphinemu.dolphinemu.R;
-import org.dolphinemu.dolphinemu.features.input.model.MappingCommon;
-import org.dolphinemu.dolphinemu.services.GameFileCacheManager;
-
-import java.io.Closeable;
-
-public class Settings implements Closeable
-{
-  public static final String FILE_DOLPHIN = "Dolphin";
-  public static final String FILE_SYSCONF = "SYSCONF";
-  public static final String FILE_GFX = "GFX";
-  public static final String FILE_LOGGER = "Logger";
-  public static final String FILE_WIIMOTE = "WiimoteNew";
-  public static final String FILE_GAME_SETTINGS_ONLY = "GameSettingsOnly";
-
-  public static final String SECTION_INI_ANDROID = "Android";
-  public static final String SECTION_INI_ANDROID_OVERLAY_BUTTONS = "AndroidOverlayButtons";
-  public static final String SECTION_INI_GENERAL = "General";
-  public static final String SECTION_INI_CORE = "Core";
-  public static final String SECTION_INI_INTERFACE = "Interface";
-  public static final String SECTION_INI_DSP = "DSP";
-
-  public static final String SECTION_LOGGER_LOGS = "Logs";
-  public static final String SECTION_LOGGER_OPTIONS = "Options";
-
-  public static final String SECTION_GFX_SETTINGS = "Settings";
-  public static final String SECTION_GFX_ENHANCEMENTS = "Enhancements";
-  public static final String SECTION_GFX_HACKS = "Hacks";
-
-  public static final String SECTION_DEBUG = "Debug";
-  public static final String SECTION_EMULATED_USB_DEVICES = "EmulatedUSBDevices";
-
-  public static final String SECTION_STEREOSCOPY = "Stereoscopy";
-
-  public static final String SECTION_BINDINGS = "Android";
-  public static final String SECTION_PROFILE = "Profile";
-
-  public static final String SECTION_ANALYTICS = "Analytics";
-
-  private String mGameId;
-  private int mRevision;
-  private boolean mIsWii;
-
-  private boolean mSettingsLoaded = false;
-
-  private boolean mLoadedRecursiveIsoPathsValue = false;
-
-  public boolean isGameSpecific()
-  {
-    return !TextUtils.isEmpty(mGameId);
-  }
-
-  public boolean isWii()
-  {
-    return mIsWii;
-  }
-
-  public int getWriteLayer()
-  {
-    return isGameSpecific() ? NativeConfig.LAYER_LOCAL_GAME : NativeConfig.LAYER_BASE_OR_CURRENT;
-  }
-
-  public boolean areSettingsLoaded()
-  {
-    return mSettingsLoaded;
-  }
-
-  public void loadSettings()
-  {
-    // The value of isWii doesn't matter if we don't have any SettingsActivity
-    loadSettings(true);
-  }
-
-  public void loadSettings(boolean isWii)
-  {
-    mIsWii = isWii;
-    mSettingsLoaded = true;
-
-    if (isGameSpecific())
-    {
-      // Loading game INIs while the core is running will mess with the game INIs loaded by the core
-      if (NativeLibrary.IsRunning())
-        throw new IllegalStateException("Attempted to load game INI while emulating");
-
-      NativeConfig.loadGameInis(mGameId, mRevision);
-    }
-
-    mLoadedRecursiveIsoPathsValue = BooleanSetting.MAIN_RECURSIVE_ISO_PATHS.getBoolean();
-  }
-
-  public void loadSettings(String gameId, int revision, boolean isWii)
-  {
-    mGameId = gameId;
-    mRevision = revision;
-    loadSettings(isWii);
-  }
-
-  public void saveSettings(Context context)
-  {
-    if (!isGameSpecific())
-    {
-      if (context != null)
-        Toast.makeText(context, R.string.settings_saved, Toast.LENGTH_SHORT).show();
-
-      MappingCommon.save();
-
-      NativeConfig.save(NativeConfig.LAYER_BASE);
-
-      NativeLibrary.ReloadLoggerConfig();
-      NativeLibrary.UpdateGCAdapterScanThread();
-
-      if (mLoadedRecursiveIsoPathsValue != BooleanSetting.MAIN_RECURSIVE_ISO_PATHS.getBoolean())
-      {
-        // Refresh game library
-        GameFileCacheManager.startRescan();
-      }
-    }
-    else
-    {
-      // custom game settings
-
-      if (context != null)
-      {
-        Toast.makeText(context, context.getString(R.string.settings_saved_game_specific, mGameId),
-                Toast.LENGTH_SHORT).show();
-      }
-
-      NativeConfig.save(NativeConfig.LAYER_LOCAL_GAME);
-    }
-  }
-
-  public void clearGameSettings()
-  {
-    NativeConfig.deleteAllKeys(NativeConfig.LAYER_LOCAL_GAME);
-  }
-
-  public boolean gameIniContainsJunk()
-  {
-    // Older versions of Android Dolphin would copy the entire contents of most of the global INIs
-    // into any game INI that got saved (with some of the sections renamed to match the game INI
-    // section names). The problems with this are twofold:
-    //
-    // 1. The user game INIs will contain entries that Dolphin doesn't support reading from
-    //    game INIs. This is annoying when editing game INIs manually but shouldn't really be
-    //    a problem for those who only use the GUI.
-    //
-    // 2. Global settings will stick around in user game INIs. For instance, if someone wants to
-    //    change the texture cache accuracy to safe for all games, they have to edit not only the
-    //    global settings but also every single game INI they have created, since the old value of
-    //    the texture cache accuracy setting has been copied into every user game INI.
-    //
-    // These problems are serious enough that we should detect and delete such INI files.
-    // Problem 1 is easy to detect, but due to the nature of problem 2, it's unfortunately not
-    // possible to know which lines were added intentionally by the user and which lines were added
-    // unintentionally, which is why we have to delete the whole file in order to fix everything.
-
-    if (!isGameSpecific())
-      return false;
-
-    return NativeConfig.exists(NativeConfig.LAYER_LOCAL_GAME, FILE_DOLPHIN, SECTION_INI_INTERFACE,
-            "ThemeName");
-  }
-
-  @Override
-  public void close()
-  {
-    if (isGameSpecific())
-    {
-      NativeConfig.unloadGameInis();
-    }
-  }
-}
diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/Settings.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/Settings.kt
new file mode 100644
index 0000000000..b363104703
--- /dev/null
+++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/Settings.kt
@@ -0,0 +1,144 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package org.dolphinemu.dolphinemu.features.settings.model
+
+import android.content.Context
+import android.text.TextUtils
+import android.widget.Toast
+import org.dolphinemu.dolphinemu.NativeLibrary
+import org.dolphinemu.dolphinemu.R
+import org.dolphinemu.dolphinemu.features.input.model.MappingCommon
+import org.dolphinemu.dolphinemu.services.GameFileCacheManager
+import java.io.Closeable
+
+class Settings : Closeable {
+    private var gameId: String = ""
+    private var revision = 0
+
+    var isWii = false
+        private set
+
+    private var settingsLoaded = false
+    private var loadedRecursiveIsoPathsValue = false
+
+    private val isGameSpecific: Boolean
+        get() = !TextUtils.isEmpty(gameId)
+
+    val writeLayer: Int
+        get() = if (isGameSpecific) NativeConfig.LAYER_LOCAL_GAME else NativeConfig.LAYER_BASE_OR_CURRENT
+
+    fun areSettingsLoaded(): Boolean {
+        return settingsLoaded
+    }
+
+    @JvmOverloads
+    fun loadSettings(isWii: Boolean = true) {
+        this.isWii = isWii
+        settingsLoaded = true
+
+        if (isGameSpecific) {
+            // Loading game INIs while the core is running will mess with the game INIs loaded by the core
+            check(!NativeLibrary.IsRunning()) { "Attempted to load game INI while emulating" }
+            NativeConfig.loadGameInis(gameId, revision)
+        }
+
+        loadedRecursiveIsoPathsValue = BooleanSetting.MAIN_RECURSIVE_ISO_PATHS.boolean
+    }
+
+    fun loadSettings(gameId: String, revision: Int, isWii: Boolean) {
+        this.gameId = gameId
+        this.revision = revision
+        loadSettings(isWii)
+    }
+
+    fun saveSettings(context: Context?) {
+        if (!isGameSpecific) {
+            if (context != null) Toast.makeText(
+                context,
+                R.string.settings_saved,
+                Toast.LENGTH_SHORT
+            ).show()
+
+            MappingCommon.save()
+
+            NativeConfig.save(NativeConfig.LAYER_BASE)
+
+            NativeLibrary.ReloadLoggerConfig()
+            NativeLibrary.UpdateGCAdapterScanThread()
+
+            if (loadedRecursiveIsoPathsValue != BooleanSetting.MAIN_RECURSIVE_ISO_PATHS.boolean) {
+                // Refresh game library
+                GameFileCacheManager.startRescan()
+            }
+        } else {
+            // custom game settings
+            if (context != null) {
+                Toast.makeText(
+                    context, context.getString(R.string.settings_saved_game_specific, gameId),
+                    Toast.LENGTH_SHORT
+                ).show()
+            }
+            NativeConfig.save(NativeConfig.LAYER_LOCAL_GAME)
+        }
+    }
+
+    fun clearGameSettings() {
+        NativeConfig.deleteAllKeys(NativeConfig.LAYER_LOCAL_GAME)
+    }
+
+    fun gameIniContainsJunk(): Boolean {
+        // Older versions of Android Dolphin would copy the entire contents of most of the global INIs
+        // into any game INI that got saved (with some of the sections renamed to match the game INI
+        // section names). The problems with this are twofold:
+        //
+        // 1. The user game INIs will contain entries that Dolphin doesn't support reading from
+        //    game INIs. This is annoying when editing game INIs manually but shouldn't really be
+        //    a problem for those who only use the GUI.
+        //
+        // 2. Global settings will stick around in user game INIs. For instance, if someone wants to
+        //    change the texture cache accuracy to safe for all games, they have to edit not only the
+        //    global settings but also every single game INI they have created, since the old value of
+        //    the texture cache accuracy Setting has been copied into every user game INI.
+        //
+        // These problems are serious enough that we should detect and delete such INI files.
+        // Problem 1 is easy to detect, but due to the nature of problem 2, it's unfortunately not
+        // possible to know which lines were added intentionally by the user and which lines were added
+        // unintentionally, which is why we have to delete the whole file in order to fix everything.
+        return if (!isGameSpecific) false else NativeConfig.exists(
+            NativeConfig.LAYER_LOCAL_GAME,
+            FILE_DOLPHIN,
+            SECTION_INI_INTERFACE,
+            "ThemeName"
+        )
+    }
+
+    override fun close() {
+        if (isGameSpecific) {
+            NativeConfig.unloadGameInis()
+        }
+    }
+
+    companion object {
+        const val FILE_DOLPHIN = "Dolphin"
+        const val FILE_SYSCONF = "SYSCONF"
+        const val FILE_GFX = "GFX"
+        const val FILE_LOGGER = "Logger"
+        const val FILE_WIIMOTE = "WiimoteNew"
+        const val FILE_GAME_SETTINGS_ONLY = "GameSettingsOnly"
+        const val SECTION_INI_ANDROID = "Android"
+        const val SECTION_INI_ANDROID_OVERLAY_BUTTONS = "AndroidOverlayButtons"
+        const val SECTION_INI_GENERAL = "General"
+        const val SECTION_INI_CORE = "Core"
+        const val SECTION_INI_INTERFACE = "Interface"
+        const val SECTION_INI_DSP = "DSP"
+        const val SECTION_LOGGER_LOGS = "Logs"
+        const val SECTION_LOGGER_OPTIONS = "Options"
+        const val SECTION_GFX_SETTINGS = "Settings"
+        const val SECTION_GFX_ENHANCEMENTS = "Enhancements"
+        const val SECTION_GFX_HACKS = "Hacks"
+        const val SECTION_DEBUG = "Debug"
+        const val SECTION_EMULATED_USB_DEVICES = "EmulatedUSBDevices"
+        const val SECTION_STEREOSCOPY = "Stereoscopy"
+        const val SECTION_ANALYTICS = "Analytics"
+    }
+}