Android: Convert ISOPaths to INI settings

This commit is contained in:
Ryan Meredith 2020-10-29 15:06:55 -04:00 committed by JosJuice
parent 5493a86086
commit 64afe97491
6 changed files with 71 additions and 57 deletions

View File

@ -16,6 +16,9 @@ import java.io.File;
*/ */
public final class SettingsFile 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_TYPE = "SIDevice";
public static final String KEY_GCPAD_PLAYER_1 = "SIDevice0"; public static final String KEY_GCPAD_PLAYER_1 = "SIDevice0";
public static final String KEY_GCPAD_G_TYPE = "PadType"; public static final String KEY_GCPAD_G_TYPE = "PadType";

View File

@ -1,22 +1,18 @@
package org.dolphinemu.dolphinemu.model; package org.dolphinemu.dolphinemu.model;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import androidx.annotation.Keep; import androidx.annotation.Keep;
import org.dolphinemu.dolphinemu.NativeLibrary;
import org.dolphinemu.dolphinemu.features.settings.model.BooleanSetting; 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.io.File;
import java.util.HashSet; import java.util.LinkedHashSet;
import java.util.Set;
public class GameFileCache public class GameFileCache
{ {
private static final String GAME_FOLDER_PATHS_PREFERENCE = "gameFolderPaths";
private static final Set<String> EMPTY_SET = new HashSet<>();
@Keep @Keep
private long mPointer; private long mPointer;
@ -30,50 +26,72 @@ public class GameFileCache
@Override @Override
public native void finalize(); public native void finalize();
public static void addGameFolder(String path, Context context) public static void addGameFolder(String path)
{ {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); File dolphinFile = SettingsFile.getSettingsFile(Settings.FILE_DOLPHIN);
Set<String> folderPaths = preferences.getStringSet(GAME_FOLDER_PATHS_PREFERENCE, EMPTY_SET); IniFile dolphinIni = new IniFile(dolphinFile);
LinkedHashSet<String> 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<String> newFolderPaths = new HashSet<>(folderPaths); private static LinkedHashSet<String> getPathSet(boolean removeNonExistentFolders)
newFolderPaths.add(path);
SharedPreferences.Editor editor = preferences.edit();
editor.putStringSet(GAME_FOLDER_PATHS_PREFERENCE, newFolderPaths);
editor.apply();
}
private void removeNonExistentGameFolders(Context context)
{ {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); File dolphinFile = SettingsFile.getSettingsFile(Settings.FILE_DOLPHIN);
Set<String> folderPaths = preferences.getStringSet(GAME_FOLDER_PATHS_PREFERENCE, EMPTY_SET); IniFile dolphinIni = new IniFile(dolphinFile);
LinkedHashSet<String> 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<String> newFolderPaths = new HashSet<>(); File folder = new File(path);
for (String folderPath : folderPaths)
{
File folder = new File(folderPath);
if (folder.exists()) 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 int setIndex = 0;
SharedPreferences.Editor editor = preferences.edit();
editor.putStringSet(GAME_FOLDER_PATHS_PREFERENCE, newFolderPaths); dolphinIni.setInt(Settings.SECTION_INI_GENERAL, SettingsFile.KEY_ISO_PATHS,
editor.apply(); 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 * @return true if the cache was modified
*/ */
public boolean scanLibrary(Context context) public boolean scanLibrary()
{ {
boolean recursiveScan = BooleanSetting.MAIN_RECURSIVE_ISO_PATHS.getBooleanGlobal(); boolean recursiveScan = BooleanSetting.MAIN_RECURSIVE_ISO_PATHS.getBooleanGlobal();
removeNonExistentGameFolders(context); LinkedHashSet<String> folderPathsSet = getPathSet(true);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
Set<String> folderPathsSet = preferences.getStringSet(GAME_FOLDER_PATHS_PREFERENCE, EMPTY_SET);
if (folderPathsSet == null)
{
return false;
}
String[] folderPaths = folderPathsSet.toArray(new String[0]); String[] folderPaths = folderPathsSet.toArray(new String[0]);

View File

@ -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 final String ACTION_RESCAN = "org.dolphinemu.dolphinemu.RESCAN_GAME_FILE_CACHE";
private static GameFileCache gameFileCache = null; private static GameFileCache gameFileCache = null;
private static AtomicReference<GameFile[]> gameFiles = new AtomicReference<>(new GameFile[]{}); private static final AtomicReference<GameFile[]> gameFiles =
private static AtomicBoolean hasLoadedCache = new AtomicBoolean(false); new AtomicReference<>(new GameFile[]{});
private static AtomicBoolean hasScannedLibrary = new AtomicBoolean(false); private static final AtomicBoolean hasLoadedCache = new AtomicBoolean(false);
private static final AtomicBoolean hasScannedLibrary = new AtomicBoolean(false);
public GameFileCacheService() public GameFileCacheService()
{ {
@ -166,7 +167,7 @@ public final class GameFileCacheService extends IntentService
{ {
synchronized (gameFileCache) synchronized (gameFileCache)
{ {
boolean changed = gameFileCache.scanLibrary(this); boolean changed = gameFileCache.scanLibrary();
if (changed) if (changed)
updateGameFileArray(); updateGameFileArray();
hasScannedLibrary.set(true); hasScannedLibrary.set(true);

View File

@ -45,7 +45,7 @@ public final class MainActivity extends AppCompatActivity implements MainView
private FloatingActionButton mFab; private FloatingActionButton mFab;
private static boolean sShouldRescanLibrary = true; private static boolean sShouldRescanLibrary = true;
private MainPresenter mPresenter = new MainPresenter(this, this); private final MainPresenter mPresenter = new MainPresenter(this, this);
@Override @Override
protected void onCreate(Bundle savedInstanceState) protected void onCreate(Bundle savedInstanceState)
@ -85,7 +85,7 @@ public final class MainActivity extends AppCompatActivity implements MainView
.run(this, false, this::setPlatformTabsAndStartGameFileCacheService); .run(this, false, this::setPlatformTabsAndStartGameFileCacheService);
} }
mPresenter.addDirIfNeeded(this); mPresenter.addDirIfNeeded();
// In case the user changed a setting that affects how games are displayed, // In case the user changed a setting that affects how games are displayed,
// such as system language, cover downloading... // such as system language, cover downloading...

View File

@ -107,11 +107,11 @@ public final class MainPresenter
return false; return false;
} }
public void addDirIfNeeded(Context context) public void addDirIfNeeded()
{ {
if (mDirToAdd != null) if (mDirToAdd != null)
{ {
GameFileCache.addGameFolder(mDirToAdd, context); GameFileCache.addGameFolder(mDirToAdd);
mDirToAdd = null; mDirToAdd = null;
} }
} }

View File

@ -39,11 +39,11 @@ public final class TvMainActivity extends FragmentActivity implements MainView
{ {
private static boolean sShouldRescanLibrary = true; private static boolean sShouldRescanLibrary = true;
private MainPresenter mPresenter = new MainPresenter(this, this); private final MainPresenter mPresenter = new MainPresenter(this, this);
private BrowseSupportFragment mBrowseFragment; private BrowseSupportFragment mBrowseFragment;
private ArrayList<ArrayObjectAdapter> mGameRows = new ArrayList<>(); private final ArrayList<ArrayObjectAdapter> mGameRows = new ArrayList<>();
@Override @Override
protected void onCreate(Bundle savedInstanceState) protected void onCreate(Bundle savedInstanceState)
@ -73,7 +73,7 @@ public final class TvMainActivity extends FragmentActivity implements MainView
GameFileCacheService.startLoad(this); GameFileCacheService.startLoad(this);
} }
mPresenter.addDirIfNeeded(this); mPresenter.addDirIfNeeded();
// In case the user changed a setting that affects how games are displayed, // In case the user changed a setting that affects how games are displayed,
// such as system language, cover downloading... // such as system language, cover downloading...