Removed external attachment storage

This commit is contained in:
M66B
2023-12-12 08:37:59 +01:00
parent 84deb380dd
commit fbf258fa89
11 changed files with 3016 additions and 118 deletions

View File

@@ -69,7 +69,7 @@ import javax.mail.internet.InternetAddress;
// https://developer.android.com/topic/libraries/architecture/room.html
@Database(
version = 285,
version = 286,
entities = {
EntityIdentity.class,
EntityAccount.class,
@@ -2884,6 +2884,16 @@ public abstract class DB extends RoomDatabase {
logMigration(startVersion, endVersion);
db.execSQL("ALTER TABLE `message` ADD COLUMN `write_below` INTEGER");
}
}).addMigrations(new Migration(285, 286) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase db) {
logMigration(startVersion, endVersion);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean external_storage = prefs.getBoolean("external_storage", false);
if (external_storage || BuildConfig.DEBUG)
db.execSQL("UPDATE `attachment` SET available = 0");
prefs.edit().remove("external_storage").apply();
}
}).addMigrations(new Migration(998, 999) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase db) {

View File

@@ -172,7 +172,7 @@ public class EntityAttachment {
}
static File getFile(Context context, long id, String name) {
File dir = Helper.ensureExists(new File(getRoot(context), "attachments"));
File dir = Helper.ensureExists(new File(context.getFilesDir(), "attachments"));
String filename = Long.toString(id);
if (!TextUtils.isEmpty(name))
filename += "." + Helper.sanitizeFilename(name);
@@ -181,16 +181,6 @@ public class EntityAttachment {
return new File(dir, filename);
}
static File getRoot(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean external_storage = prefs.getBoolean("external_storage", false);
File root = (external_storage
? Helper.getExternalFilesDir(context)
: context.getFilesDir());
return root;
}
static void copy(Context context, long oldid, long newid) {
DB db = DB.getInstance(context);

View File

@@ -1186,9 +1186,6 @@ public class FragmentOptionsBackup extends FragmentBase implements SharedPrefere
continue;
}
if ("external_storage".equals(key))
continue;
if ("reformatted_hint".equals(key))
continue;

View File

@@ -168,8 +168,6 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
private SwitchCompat swAutostart;
private SwitchCompat swEmergency;
private SwitchCompat swWorkManager;
private SwitchCompat swExternalStorage;
private TextView tvExternalStorageFolder;
private SwitchCompat swIntegrity;
private SwitchCompat swWal;
private SwitchCompat swCheckpoints;
@@ -272,7 +270,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
"crash_reports", "cleanup_attachments",
"watchdog", "experiments", "main_log", "main_log_memory", "protocol", "log_level", "debug", "leak_canary",
"test1", "test2", "test3", "test4", "test5",
"emergency_file", "work_manager", // "external_storage",
"emergency_file", "work_manager",
"sqlite_integrity_check", "wal", "sqlite_checkpoints", "sqlite_analyze", "sqlite_auto_vacuum", "sqlite_sync_extra", "sqlite_cache",
"chunk_size", "thread_range",
"autoscroll_editor", "undo_manager",
@@ -405,8 +403,6 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
swAutostart = view.findViewById(R.id.swAutostart);
swEmergency = view.findViewById(R.id.swEmergency);
swWorkManager = view.findViewById(R.id.swWorkManager);
swExternalStorage = view.findViewById(R.id.swExternalStorage);
tvExternalStorageFolder = view.findViewById(R.id.tvExternalStorageFolder);
swIntegrity = view.findViewById(R.id.swIntegrity);
swWal = view.findViewById(R.id.swWal);
swCheckpoints = view.findViewById(R.id.swCheckpoints);
@@ -1088,59 +1084,6 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
}
});
swExternalStorage.setEnabled(Helper.getExternalFilesDir(getContext()) != null);
swExternalStorage.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("external_storage", isChecked);
editor.apply();
Bundle args = new Bundle();
args.putBoolean("external_storage", isChecked);
new SimpleTask<Integer>() {
@Override
protected Integer onExecute(Context context, Bundle args) throws IOException {
boolean external_storage = args.getBoolean("external_storage");
File sourceRoot = (!external_storage
? Helper.getExternalFilesDir(context)
: context.getFilesDir());
File targetRoot = (external_storage
? Helper.getExternalFilesDir(context)
: context.getFilesDir());
File source = Helper.ensureExists(new File(sourceRoot, "attachments"));
File target = Helper.ensureExists(new File(targetRoot, "attachments"));
File[] attachments = source.listFiles();
if (attachments != null)
for (File attachment : attachments) {
File dest = new File(target, attachment.getName());
Log.i("Move " + attachment + " to " + dest);
Helper.copy(attachment, dest);
Helper.secureDelete(attachment);
}
return (attachments == null ? -1 : attachments.length);
}
@Override
protected void onExecuted(Bundle args, Integer count) {
String msg = String.format("Moved %d attachments", count);
ToastEx.makeText(getContext(), msg, Toast.LENGTH_LONG).show();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(FragmentOptionsMisc.this, args, "external");
}
});
swIntegrity.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton v, boolean checked) {
@@ -1984,11 +1927,6 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
else
tvLastDaily.setText(("-"));
File external = Helper.getExternalFilesDir(getContext());
boolean emulated = (external != null && Environment.isExternalStorageEmulated(external));
tvExternalStorageFolder.setText(
(external == null ? null : external.getAbsolutePath()) + (emulated ? " emulated" : ""));
swExactAlarms.setEnabled(AlarmManagerCompatEx.canScheduleExactAlarms(getContext()));
swTestIab.setVisibility(BuildConfig.DEBUG && BuildConfig.TEST_RELEASE ? View.VISIBLE : View.GONE);
@@ -2276,7 +2214,6 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
swAutostart.setChecked(Helper.isComponentEnabled(getContext(), ReceiverAutoStart.class));
swEmergency.setChecked(prefs.getBoolean("emergency_file", true));
swWorkManager.setChecked(prefs.getBoolean("work_manager", true));
swExternalStorage.setChecked(prefs.getBoolean("external_storage", false));
swIntegrity.setChecked(prefs.getBoolean("sqlite_integrity_check", true));
swWal.setChecked(prefs.getBoolean("wal", true));

View File

@@ -3455,9 +3455,12 @@ public class Log {
if (ai != null)
size += write(os, String.format("Source: %s\r\n public: %s\r\n",
ai.sourceDir, ai.publicSourceDir));
size += write(os, String.format("Files: %s\r\n external: %s\r\n storage: %s\r\n",
context.getFilesDir(), Helper.getExternalFilesDir(context),
File external = Helper.getExternalFilesDir(context);
boolean emulated = (external != null && Environment.isExternalStorageEmulated(external));
size += write(os, String.format("Files: %s\r\n external: %s\r\n emulated: %b\r\n storage: %s\r\n",
context.getFilesDir(), external, emulated,
Environment.getExternalStorageDirectory()));
size += write(os, String.format("Cache: %s\r\n external: %s\n",
context.getCacheDir(), context.getExternalCacheDir()));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)

View File

@@ -260,7 +260,7 @@ public class WorkerCleanup extends Worker {
// Cleanup attachment files
{
Log.breadcrumb("worker", "cleanup", "attachment files");
File[] attachments = new File(EntityAttachment.getRoot(context), "attachments").listFiles();
File[] attachments = new File(context.getFilesDir(), "attachments").listFiles();
if (attachments != null)
for (File file : attachments)
if (manual || file.lastModified() + KEEP_FILES_DURATION < now)