getExternalFilesDir compat

pull/209/head
M66B 2 years ago
parent 8b5fd03117
commit cc0b6b1560

@ -164,7 +164,7 @@ public class EntityAttachment {
boolean external_storage = prefs.getBoolean("external_storage", false); boolean external_storage = prefs.getBoolean("external_storage", false);
File root = (external_storage File root = (external_storage
? context.getExternalFilesDir(null) ? Helper.getExternalFilesDir(context)
: context.getFilesDir()); : context.getFilesDir());
return root; return root;
} }

@ -1118,7 +1118,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
} }
}); });
swExternalStorage.setEnabled(getContext().getExternalFilesDir(null) != null); swExternalStorage.setEnabled(Helper.getExternalFilesDir(getContext()) != null);
swExternalStorage.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { swExternalStorage.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
@ -1133,11 +1133,11 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
boolean external_storage = args.getBoolean("external_storage"); boolean external_storage = args.getBoolean("external_storage");
File source = (!external_storage File source = (!external_storage
? context.getExternalFilesDir(null) ? Helper.getExternalFilesDir(context)
: context.getFilesDir()); : context.getFilesDir());
File target = (external_storage File target = (external_storage
? context.getExternalFilesDir(null) ? Helper.getExternalFilesDir(context)
: context.getFilesDir()); : context.getFilesDir());
source = Helper.ensureExists(new File(source, "attachments")); source = Helper.ensureExists(new File(source, "attachments"));
@ -1644,7 +1644,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
files.addAll(getFiles(context.getCacheDir(), MIN_FILE_SIZE)); files.addAll(getFiles(context.getCacheDir(), MIN_FILE_SIZE));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
files.addAll(getFiles(context.getDataDir(), MIN_FILE_SIZE)); files.addAll(getFiles(context.getDataDir(), MIN_FILE_SIZE));
files.addAll(getFiles(context.getExternalFilesDir(null), MIN_FILE_SIZE)); files.addAll(getFiles(Helper.getExternalFilesDir(context), MIN_FILE_SIZE));
Collections.sort(files, new Comparator<File>() { Collections.sort(files, new Comparator<File>() {
@Override @Override
@ -1678,7 +1678,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
? null : context.getDataDir()); ? null : context.getDataDir());
File filesDir = context.getFilesDir(); File filesDir = context.getFilesDir();
File cacheDir = context.getCacheDir(); File cacheDir = context.getCacheDir();
File externalDir = context.getExternalFilesDir(null); File externalDir = Helper.getExternalFilesDir(context);
if (dataDir != null) if (dataDir != null)
ssb.append("Data: ").append(dataDir.getAbsolutePath()).append("\r\n"); ssb.append("Data: ").append(dataDir.getAbsolutePath()).append("\r\n");
@ -1850,7 +1850,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
setLastCleanup(prefs.getLong("last_cleanup", -1)); setLastCleanup(prefs.getLong("last_cleanup", -1));
File external = getContext().getExternalFilesDir(null); File external = Helper.getExternalFilesDir(getContext());
boolean emulated = (external != null && Environment.isExternalStorageEmulated(external)); boolean emulated = (external != null && Environment.isExternalStorageEmulated(external));
tvExternalStorageFolder.setText( tvExternalStorageFolder.setText(
(external == null ? null : external.getAbsolutePath()) + (emulated ? " emulated" : "")); (external == null ? null : external.getAbsolutePath()) + (emulated ? " emulated" : ""));

@ -2275,6 +2275,18 @@ public class Helper {
return dir; return dir;
} }
static File getExternalFilesDir(Context context) {
return getExternalFilesDir(context, null);
}
static File getExternalFilesDir(Context context, String type) {
File[] dirs = ContextCompat.getExternalFilesDirs(context, type);
if (dirs == null || dirs.length == 0)
return context.getExternalFilesDir(type);
else
return dirs[0];
}
static String sanitizeFilename(String name) { static String sanitizeFilename(String name) {
if (name == null) if (name == null)
return null; return null;

@ -2844,7 +2844,7 @@ public class Log {
size += write(os, String.format("Source: %s\r\n public: %s\r\n", size += write(os, String.format("Source: %s\r\n public: %s\r\n",
ai.sourceDir, ai.publicSourceDir)); ai.sourceDir, ai.publicSourceDir));
size += write(os, String.format("Files: %s\r\n external: %s\r\n storage: %s\r\n", size += write(os, String.format("Files: %s\r\n external: %s\r\n storage: %s\r\n",
context.getFilesDir(), context.getExternalFilesDir(null), context.getFilesDir(), Helper.getExternalFilesDir(context),
Environment.getExternalStorageDirectory())); Environment.getExternalStorageDirectory()));
size += write(os, String.format("Cache: %s\r\n external: %s\n", size += write(os, String.format("Cache: %s\r\n external: %s\n",
context.getCacheDir(), context.getExternalCacheDir())); context.getCacheDir(), context.getExternalCacheDir()));

Loading…
Cancel
Save