Rewrite data folder creation

- Make code easier to read
- Fall back to internal memory when folder is not writable
This commit is contained in:
ByteHamster 2022-03-25 10:11:46 +01:00
parent 3d54aa7064
commit 5e4f38cfae
1 changed files with 30 additions and 41 deletions

View File

@ -9,6 +9,7 @@ import android.util.Log;
import android.view.KeyEvent; import android.view.KeyEvent;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
@ -888,52 +889,40 @@ public class UserPreferences {
* *
* @param type The name of the folder inside the data folder. May be null * @param type The name of the folder inside the data folder. May be null
* when accessing the root of the data folder. * when accessing the root of the data folder.
* @return The data folder that has been requested or null if the folder * @return The data folder that has been requested or null if the folder could not be created.
* could not be created.
*/ */
public static File getDataFolder(String type) { public static File getDataFolder(@Nullable String type) {
String strDir = prefs.getString(PREF_DATA_FOLDER, null); File dataFolder = getTypeDir(prefs.getString(PREF_DATA_FOLDER, null), type);
if (strDir == null) { if (dataFolder == null || !dataFolder.canWrite()) {
Log.d(TAG, "Using default data folder"); Log.d(TAG, "User data folder not writable or not set. Trying default.");
return context.getExternalFilesDir(type); dataFolder = context.getExternalFilesDir(type);
} else {
File dataDir = new File(strDir);
if (!dataDir.exists()) {
if (!dataDir.mkdir()) {
Log.w(TAG, "Could not create data folder");
return null;
} }
if (dataFolder == null || !dataFolder.canWrite()) {
Log.d(TAG, "Default data folder not available or not writable. Falling back to internal memory.");
dataFolder = getTypeDir(context.getFilesDir().getAbsolutePath(), type);
}
return dataFolder;
} }
if (type == null) { @Nullable
return dataDir; private static File getTypeDir(@Nullable String baseDirPath, @Nullable String type) {
} else { if (baseDirPath == null) {
// handle path separators
String[] dirs = type.split("/");
for (int i = 0; i < dirs.length; i++) {
if (dirs.length > 0) {
if (i < dirs.length - 1) {
dataDir = getDataFolder(dirs[i]);
if (dataDir == null) {
return null; return null;
} }
} File baseDir = new File(baseDirPath);
type = dirs[i]; File typeDir = type == null ? baseDir : new File(baseDir, type);
}
}
File typeDir = new File(dataDir, type);
if (!typeDir.exists()) { if (!typeDir.exists()) {
if (dataDir.canWrite()) { if (!baseDir.canWrite()) {
if (!typeDir.mkdir()) { Log.e(TAG, "Base dir is not writable " + baseDir.getAbsolutePath());
Log.e(TAG, "Could not create data folder named " + type);
return null; return null;
} }
if (!typeDir.mkdirs()) {
Log.e(TAG, "Could not create type dir " + typeDir.getAbsolutePath());
return null;
} }
} }
return typeDir; return typeDir;
} }
}
}
public static void setDataFolder(String dir) { public static void setDataFolder(String dir) {
Log.d(TAG, "setDataFolder(dir: " + dir + ")"); Log.d(TAG, "setDataFolder(dir: " + dir + ")");