Rewrite data folder creation
- Make code easier to read - Fall back to internal memory when folder is not writable
This commit is contained in:
parent
3d54aa7064
commit
5e4f38cfae
|
@ -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,51 +889,39 @@ 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 (dataFolder == null || !dataFolder.canWrite()) {
|
||||||
if (!dataDir.exists()) {
|
Log.d(TAG, "Default data folder not available or not writable. Falling back to internal memory.");
|
||||||
if (!dataDir.mkdir()) {
|
dataFolder = getTypeDir(context.getFilesDir().getAbsolutePath(), type);
|
||||||
Log.w(TAG, "Could not create data folder");
|
}
|
||||||
return null;
|
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
|
return null;
|
||||||
String[] dirs = type.split("/");
|
}
|
||||||
for (int i = 0; i < dirs.length; i++) {
|
File baseDir = new File(baseDirPath);
|
||||||
if (dirs.length > 0) {
|
File typeDir = type == null ? baseDir : new File(baseDir, type);
|
||||||
if (i < dirs.length - 1) {
|
if (!typeDir.exists()) {
|
||||||
dataDir = getDataFolder(dirs[i]);
|
if (!baseDir.canWrite()) {
|
||||||
if (dataDir == null) {
|
Log.e(TAG, "Base dir is not writable " + baseDir.getAbsolutePath());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
if (!typeDir.mkdirs()) {
|
||||||
type = dirs[i];
|
Log.e(TAG, "Could not create type dir " + typeDir.getAbsolutePath());
|
||||||
}
|
return null;
|
||||||
}
|
|
||||||
File typeDir = new File(dataDir, type);
|
|
||||||
if (!typeDir.exists()) {
|
|
||||||
if (dataDir.canWrite()) {
|
|
||||||
if (!typeDir.mkdir()) {
|
|
||||||
Log.e(TAG, "Could not create data folder named " + type);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return typeDir;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return typeDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setDataFolder(String dir) {
|
public static void setDataFolder(String dir) {
|
||||||
|
|
Loading…
Reference in New Issue