From 64afe97491c1b713c18a79e4cd18a0cca732ade1 Mon Sep 17 00:00:00 2001 From: Ryan Meredith Date: Thu, 29 Oct 2020 15:06:55 -0400 Subject: [PATCH] Android: Convert ISOPaths to INI settings --- .../features/settings/utils/SettingsFile.java | 3 + .../dolphinemu/model/GameFileCache.java | 102 ++++++++++-------- .../services/GameFileCacheService.java | 9 +- .../dolphinemu/ui/main/MainActivity.java | 4 +- .../dolphinemu/ui/main/MainPresenter.java | 4 +- .../dolphinemu/ui/main/TvMainActivity.java | 6 +- 6 files changed, 71 insertions(+), 57 deletions(-) diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/utils/SettingsFile.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/utils/SettingsFile.java index e65805fd95..82ea3a84f1 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/utils/SettingsFile.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/utils/SettingsFile.java @@ -16,6 +16,9 @@ import java.io.File; */ public final class SettingsFile { + public static final String KEY_ISO_PATH_BASE = "ISOPath"; + public static final String KEY_ISO_PATHS = "ISOPaths"; + public static final String KEY_GCPAD_TYPE = "SIDevice"; public static final String KEY_GCPAD_PLAYER_1 = "SIDevice0"; public static final String KEY_GCPAD_G_TYPE = "PadType"; diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameFileCache.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameFileCache.java index 08a999bd32..2fdc0cb179 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameFileCache.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameFileCache.java @@ -1,22 +1,18 @@ package org.dolphinemu.dolphinemu.model; -import android.content.Context; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; - import androidx.annotation.Keep; +import org.dolphinemu.dolphinemu.NativeLibrary; import org.dolphinemu.dolphinemu.features.settings.model.BooleanSetting; +import org.dolphinemu.dolphinemu.features.settings.model.Settings; +import org.dolphinemu.dolphinemu.features.settings.utils.SettingsFile; +import org.dolphinemu.dolphinemu.utils.IniFile; import java.io.File; -import java.util.HashSet; -import java.util.Set; +import java.util.LinkedHashSet; public class GameFileCache { - private static final String GAME_FOLDER_PATHS_PREFERENCE = "gameFolderPaths"; - private static final Set EMPTY_SET = new HashSet<>(); - @Keep private long mPointer; @@ -30,50 +26,72 @@ public class GameFileCache @Override public native void finalize(); - public static void addGameFolder(String path, Context context) + public static void addGameFolder(String path) { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - Set folderPaths = preferences.getStringSet(GAME_FOLDER_PATHS_PREFERENCE, EMPTY_SET); + File dolphinFile = SettingsFile.getSettingsFile(Settings.FILE_DOLPHIN); + IniFile dolphinIni = new IniFile(dolphinFile); + LinkedHashSet pathSet = getPathSet(false); + int totalISOPaths = + dolphinIni.getInt(Settings.SECTION_INI_GENERAL, SettingsFile.KEY_ISO_PATHS, 0); - if (folderPaths == null) + if (!pathSet.contains(path)) { - return; + dolphinIni.setInt(Settings.SECTION_INI_GENERAL, SettingsFile.KEY_ISO_PATHS, + totalISOPaths + 1); + dolphinIni.setString(Settings.SECTION_INI_GENERAL, SettingsFile.KEY_ISO_PATH_BASE + + totalISOPaths, path); + dolphinIni.save(dolphinFile); + NativeLibrary.ReloadConfig(); } - - Set newFolderPaths = new HashSet<>(folderPaths); - newFolderPaths.add(path); - SharedPreferences.Editor editor = preferences.edit(); - editor.putStringSet(GAME_FOLDER_PATHS_PREFERENCE, newFolderPaths); - editor.apply(); } - private void removeNonExistentGameFolders(Context context) + private static LinkedHashSet getPathSet(boolean removeNonExistentFolders) { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - Set folderPaths = preferences.getStringSet(GAME_FOLDER_PATHS_PREFERENCE, EMPTY_SET); + File dolphinFile = SettingsFile.getSettingsFile(Settings.FILE_DOLPHIN); + IniFile dolphinIni = new IniFile(dolphinFile); + LinkedHashSet pathSet = new LinkedHashSet<>(); + int totalISOPaths = + dolphinIni.getInt(Settings.SECTION_INI_GENERAL, SettingsFile.KEY_ISO_PATHS, 0); - if (folderPaths == null) + for (int i = 0; i < totalISOPaths; i++) { - return; - } + String path = dolphinIni.getString(Settings.SECTION_INI_GENERAL, + SettingsFile.KEY_ISO_PATH_BASE + i, ""); - Set newFolderPaths = new HashSet<>(); - for (String folderPath : folderPaths) - { - File folder = new File(folderPath); + File folder = new File(path); if (folder.exists()) { - newFolderPaths.add(folderPath); + pathSet.add(path); } } - if (folderPaths.size() != newFolderPaths.size()) + if (removeNonExistentFolders && totalISOPaths > pathSet.size()) { - // One or more folders are being deleted - SharedPreferences.Editor editor = preferences.edit(); - editor.putStringSet(GAME_FOLDER_PATHS_PREFERENCE, newFolderPaths); - editor.apply(); + int setIndex = 0; + + dolphinIni.setInt(Settings.SECTION_INI_GENERAL, SettingsFile.KEY_ISO_PATHS, + pathSet.size()); + + // One or more folders have been removed. + for (String entry : pathSet) + { + dolphinIni.setString(Settings.SECTION_INI_GENERAL, SettingsFile.KEY_ISO_PATH_BASE + + setIndex, entry); + + setIndex++; + } + + // Delete known unnecessary keys. Ignore i values beyond totalISOPaths. + for (int i = setIndex; i < totalISOPaths; i++) + { + dolphinIni.deleteKey(Settings.SECTION_INI_GENERAL, SettingsFile.KEY_ISO_PATH_BASE + i); + } + + dolphinIni.save(dolphinFile); + NativeLibrary.ReloadConfig(); } + + return pathSet; } /** @@ -81,19 +99,11 @@ public class GameFileCache * * @return true if the cache was modified */ - public boolean scanLibrary(Context context) + public boolean scanLibrary() { boolean recursiveScan = BooleanSetting.MAIN_RECURSIVE_ISO_PATHS.getBooleanGlobal(); - removeNonExistentGameFolders(context); - - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - Set folderPathsSet = preferences.getStringSet(GAME_FOLDER_PATHS_PREFERENCE, EMPTY_SET); - - if (folderPathsSet == null) - { - return false; - } + LinkedHashSet folderPathsSet = getPathSet(true); String[] folderPaths = folderPathsSet.toArray(new String[0]); diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/GameFileCacheService.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/GameFileCacheService.java index ff37ccac4b..ab295c5550 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/GameFileCacheService.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/GameFileCacheService.java @@ -29,9 +29,10 @@ public final class GameFileCacheService extends IntentService private static final String ACTION_RESCAN = "org.dolphinemu.dolphinemu.RESCAN_GAME_FILE_CACHE"; private static GameFileCache gameFileCache = null; - private static AtomicReference gameFiles = new AtomicReference<>(new GameFile[]{}); - private static AtomicBoolean hasLoadedCache = new AtomicBoolean(false); - private static AtomicBoolean hasScannedLibrary = new AtomicBoolean(false); + private static final AtomicReference gameFiles = + new AtomicReference<>(new GameFile[]{}); + private static final AtomicBoolean hasLoadedCache = new AtomicBoolean(false); + private static final AtomicBoolean hasScannedLibrary = new AtomicBoolean(false); public GameFileCacheService() { @@ -166,7 +167,7 @@ public final class GameFileCacheService extends IntentService { synchronized (gameFileCache) { - boolean changed = gameFileCache.scanLibrary(this); + boolean changed = gameFileCache.scanLibrary(); if (changed) updateGameFileArray(); hasScannedLibrary.set(true); diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java index cd5dd80c75..d5525bcf61 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java @@ -45,7 +45,7 @@ public final class MainActivity extends AppCompatActivity implements MainView private FloatingActionButton mFab; private static boolean sShouldRescanLibrary = true; - private MainPresenter mPresenter = new MainPresenter(this, this); + private final MainPresenter mPresenter = new MainPresenter(this, this); @Override protected void onCreate(Bundle savedInstanceState) @@ -85,7 +85,7 @@ public final class MainActivity extends AppCompatActivity implements MainView .run(this, false, this::setPlatformTabsAndStartGameFileCacheService); } - mPresenter.addDirIfNeeded(this); + mPresenter.addDirIfNeeded(); // In case the user changed a setting that affects how games are displayed, // such as system language, cover downloading... diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainPresenter.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainPresenter.java index 75f2002ba3..a161ac782b 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainPresenter.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainPresenter.java @@ -107,11 +107,11 @@ public final class MainPresenter return false; } - public void addDirIfNeeded(Context context) + public void addDirIfNeeded() { if (mDirToAdd != null) { - GameFileCache.addGameFolder(mDirToAdd, context); + GameFileCache.addGameFolder(mDirToAdd); mDirToAdd = null; } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java index 23d1050d04..8d9ab73cd9 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java @@ -39,11 +39,11 @@ public final class TvMainActivity extends FragmentActivity implements MainView { private static boolean sShouldRescanLibrary = true; - private MainPresenter mPresenter = new MainPresenter(this, this); + private final MainPresenter mPresenter = new MainPresenter(this, this); private BrowseSupportFragment mBrowseFragment; - private ArrayList mGameRows = new ArrayList<>(); + private final ArrayList mGameRows = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) @@ -73,7 +73,7 @@ public final class TvMainActivity extends FragmentActivity implements MainView GameFileCacheService.startLoad(this); } - mPresenter.addDirIfNeeded(this); + mPresenter.addDirIfNeeded(); // In case the user changed a setting that affects how games are displayed, // such as system language, cover downloading...