Fix getDefaultConfigPath not correctly looking up external and internal data directories

This commit is contained in:
Peter-Simon Dieterich 2015-02-28 19:04:13 +01:00
parent 4a0d2367d6
commit b7779f04e0
1 changed files with 62 additions and 44 deletions

View File

@ -39,8 +39,15 @@ public final class UserPreferences
public static String getDefaultConfigPath(Context ctx)
{
// Internal/External storage dirs.
final String internal = System.getenv("INTERNAL_STORAGE");
final String external = System.getenv("EXTERNAL_STORAGE");
final String internal = ctx.getFilesDir().getAbsolutePath();
String external = null;
// Get the App's external storage folder
final String state = android.os.Environment.getExternalStorageState();
if (android.os.Environment.MEDIA_MOUNTED.equals(state)) {
File extsd = ctx.getExternalFilesDir(null);
external = extsd.getAbsolutePath();
}
// Native library directory and data directory for this front-end.
final String dataDir = ctx.getApplicationInfo().dataDir;
@ -84,15 +91,26 @@ public final class UserPreferences
return confPath;
}
if (internal != null && new File(internal + append_path).canWrite())
return internal + append_path;
else if (external != null && new File(internal + append_path).canWrite())
return external + append_path;
// Config file does not exist. Create empty one.
// emergency fallback
String new_path = "/mnt/sd" + append_path;
if (external != null)
new_path = external + append_path;
else if (internal != null)
new_path = internal + append_path;
else if (dataDir != null)
return dataDir + append_path;
else
// emergency fallback, all else failed
return "/mnt/sd" + append_path;
new_path = dataDir + append_path;
try {
new File(new_path).createNewFile();
}
catch (IOException e)
{
Log.e(TAG, "Failed to create config file to: " + new_path);
}
return new_path;
}
/**
@ -226,15 +244,15 @@ public final class UserPreferences
if (prefs.getBoolean("savefile_directory_enable", false))
{
config.setString("savefile_directory", prefs.getString("savefile_directory", ""));
config.setString("savefile_directory", prefs.getString("savefile_directory", ""));
}
if (prefs.getBoolean("savestate_directory_enable", false))
{
config.setString("savestate_directory", prefs.getString("savestate_directory", ""));
config.setString("savestate_directory", prefs.getString("savestate_directory", ""));
}
if (prefs.getBoolean("system_directory_enable", false))
{
config.setString("system_directory", prefs.getString("system_directory", ""));
config.setString("system_directory", prefs.getString("system_directory", ""));
}
config.setBoolean("video_font_enable", prefs.getBoolean("video_font_enable", true));
@ -244,11 +262,11 @@ public final class UserPreferences
for (int i = 1; i <= 4; i++)
{
final String[] btns =
{
"up", "down", "left", "right",
"a", "b", "x", "y", "start", "select",
"l", "r", "l2", "r2", "l3", "r3"
};
{
"up", "down", "left", "right",
"a", "b", "x", "y", "start", "select",
"l", "r", "l2", "r2", "l3", "r3"
};
for (String b : btns)
{
@ -305,14 +323,14 @@ public final class UserPreferences
*/
/**
private static void readbackInt(ConfigFile cfg, SharedPreferences.Editor edit, String key)
{
if (cfg.keyExists(key))
edit.putInt(key, cfg.getInt(key));
else
edit.remove(key);
}
*/
private static void readbackInt(ConfigFile cfg, SharedPreferences.Editor edit, String key)
{
if (cfg.keyExists(key))
edit.putInt(key, cfg.getInt(key));
else
edit.remove(key);
}
*/
/**
* Sanitizes a libretro core path.