From dbbc7111e947abd6743cd9a479f85162eca2ab81 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 18 Mar 2024 13:05:22 +0100 Subject: [PATCH] Reset options on import --- .../java/eu/faircode/email/ApplicationEx.java | 11 +- .../faircode/email/FragmentOptionsBackup.java | 132 +++++++++++------- .../email/FragmentOptionsBehavior.java | 2 +- .../email/FragmentOptionsConnection.java | 2 +- .../email/FragmentOptionsDisplay.java | 2 +- .../email/FragmentOptionsEncryption.java | 2 +- .../faircode/email/FragmentOptionsMisc.java | 2 +- .../email/FragmentOptionsNotifications.java | 2 +- .../email/FragmentOptionsPrivacy.java | 2 +- .../faircode/email/FragmentOptionsSend.java | 2 +- .../email/FragmentOptionsSynchronize.java | 2 +- 11 files changed, 97 insertions(+), 64 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index 2a29020877..ae68a324c4 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -406,19 +406,14 @@ public class ApplicationEx extends Application case "watchdog": ServiceSynchronize.scheduleWatchdog(this); break; - case "secure": // privacy - case "load_emoji": // privacy - case "shortcuts": // misc - case "language": // misc - case "wal": // misc - // Should be excluded for import - restart(this, key); - break; case "debug": case "log_level": Log.setLevel(this); FairEmailLoggingProvider.setLevel(this); break; + default: + if (FragmentOptionsBackup.RESTART_OPTIONS.contains(key)) + restart(this, key); } } catch (Throwable ex) { Log.e(ex); diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsBackup.java b/app/src/main/java/eu/faircode/email/FragmentOptionsBackup.java index 2c57ea52cc..a144954652 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsBackup.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsBackup.java @@ -86,6 +86,7 @@ import java.security.spec.KeySpec; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.HashMap; @@ -135,6 +136,32 @@ public class FragmentOptionsBackup extends FragmentBase implements SharedPrefere private static final int REQUEST_EXPORT_HANDLE = 3; private static final int REQUEST_IMPORT_HANDLE = 4; + final static List DEFER_OPTIONS = Collections.unmodifiableList(Arrays.asList( + "theme", + "beige" + )); + + final static List RESTART_OPTIONS = Collections.unmodifiableList(Arrays.asList( + "secure", // privacy + "load_emoji", // privacy + "shortcuts", // misc + "language", // misc + "wal" // misc + )); + + static final String[][] RESET_ALL_OPTIONS = { + FragmentOptionsSynchronize.RESET_OPTIONS.toArray(new String[0]), + FragmentOptionsSend.RESET_OPTIONS.toArray(new String[0]), + FragmentOptionsConnection.RESET_OPTIONS.toArray(new String[0]), + FragmentOptionsDisplay.RESET_OPTIONS.toArray(new String[0]), + FragmentOptionsBehavior.RESET_OPTIONS.toArray(new String[0]), + FragmentOptionsPrivacy.RESET_OPTIONS.toArray(new String[0]), + FragmentOptionsEncryption.RESET_OPTIONS.toArray(new String[0]), + FragmentOptionsNotifications.RESET_OPTIONS.toArray(new String[0]), + //FragmentOptionsIntegrations.RESET_OPTIONS.toArray(new String[0]), + FragmentOptionsMisc.RESET_OPTIONS.toArray(new String[0]) + }; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -1136,65 +1163,34 @@ public class FragmentOptionsBackup extends FragmentBase implements SharedPrefere // Settings SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor editor = prefs.edit(); + + for (String[] options : RESET_ALL_OPTIONS) + for (String key : options) + if (!skipOption(key) && + !DEFER_OPTIONS.contains(key) && + !RESTART_OPTIONS.contains(key)) { + if (BuildConfig.DEBUG && prefs.contains(key)) + postProgress("Clearing " + key); + editor.remove(key); + } + JSONArray jsettings = jimport.getJSONArray("settings"); for (int s = 0; s < jsettings.length(); s++) { JSONObject jsetting = (JSONObject) jsettings.get(s); String key = jsetting.getString("key"); - if ("pro".equals(key) && !BuildConfig.DEBUG) - continue; - if ("iab_json".equals(key) || "iab_signature".equals(key)) - continue; - - if ("accept_unsupported".equals(key)) - continue; - - if ("biometrics".equals(key) || "pin".equals(key)) - continue; - - if ("alert_once".equals(key) && !Helper.isXiaomi()) - continue; - - if ("default_folder".equals(key)) - continue; - - if ("background_service".equals(key) && - Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - continue; - - if ("tcp_keep_alive".equals(key)) - continue; - - // Prevent restart - if ("secure".equals(key) || - "load_emoji".equals(key) || - "shortcuts".equals(key) || - "language".equals(key) || - "wal".equals(key)) { - postProgress("Skipping " + key + "=" + jsetting.get("value")); - continue; - } - - if ("theme".equals(key) || "beige".equals(key)) { + if (DEFER_OPTIONS.contains(key)) { defer.put(key, jsetting.get("value")); continue; } - if (key != null && key.startsWith("widget.")) - continue; - if (key != null && key.startsWith("unset.")) - continue; - - if ("external_search".equals(key)) { - boolean external_search = jsetting.getBoolean("value"); - Helper.enableComponent(context, ActivitySearch.class, external_search); + // Prevent restart + if (RESTART_OPTIONS.contains(key)) { + postProgress("Skipping " + key + "=" + jsetting.get("value")); continue; } - if ("external_storage".equals(key)) - continue; - - if ("reformatted_hint".equals(key)) + if (skipOption(key)) continue; Object value = jsetting.get("value"); @@ -1323,6 +1319,48 @@ public class FragmentOptionsBackup extends FragmentBase implements SharedPrefere }.setHandler(tvLog.getHandler()).execute(this, args, "setup:import"); } + private static boolean skipOption(String key) { + if ("pro".equals(key) && !BuildConfig.DEBUG) + return true; + if ("iab_json".equals(key) || "iab_signature".equals(key)) + return true; + + if ("accept_unsupported".equals(key)) + return true; + + if ("biometrics".equals(key) || "pin".equals(key)) + return true; + + if ("alert_once".equals(key) && !Helper.isXiaomi()) + return true; + + if ("default_folder".equals(key)) + return true; + + if ("background_service".equals(key) && + Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + return true; + + if ("tcp_keep_alive".equals(key)) + return true; + + if ("external_storage".equals(key)) + return true; + + if ("reformatted_hint".equals(key)) + return true; + + if ("external_search".equals(key)) + return true; + + if (key != null && key.startsWith("widget.")) + return true; + if (key != null && key.startsWith("unset.")) + return true; + + return false; + } + private void handleK9Import(Uri uri) { Bundle args = new Bundle(); args.putParcelable("uri", uri); diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsBehavior.java b/app/src/main/java/eu/faircode/email/FragmentOptionsBehavior.java index b24e3f17f9..167911160a 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsBehavior.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsBehavior.java @@ -118,7 +118,7 @@ public class FragmentOptionsBehavior extends FragmentBase implements SharedPrefe final static int REQUEST_DEFAULT_FOLDER = 1; - private final static List RESET_OPTIONS = Collections.unmodifiableList(Arrays.asList( + final static List RESET_OPTIONS = Collections.unmodifiableList(Arrays.asList( "restore_on_launch", "sync_on_launch", "double_back", "conversation_actions", "conversation_actions_replies", "language_detection", "photo_picker", "default_snooze", "pull", "pull_all", "autoscroll", "quick_filter", "quick_scroll", "quick_actions", "swipe_sensitivity", "foldernav", diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsConnection.java b/app/src/main/java/eu/faircode/email/FragmentOptionsConnection.java index ac34b56752..c0c63c1ccf 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsConnection.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsConnection.java @@ -128,7 +128,7 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre private Group grpBC; private Group grpCustomSsl; - private final static List RESET_OPTIONS = Collections.unmodifiableList(Arrays.asList( + final static List RESET_OPTIONS = Collections.unmodifiableList(Arrays.asList( "metered", "download", "download_limited", "roaming", "rlah", "download_headers", "download_eml", "download_plain", "require_validated", "require_validated_captive", "vpn_only", diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsDisplay.java b/app/src/main/java/eu/faircode/email/FragmentOptionsDisplay.java index 0c8a3b1f11..2008f17ee1 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsDisplay.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsDisplay.java @@ -209,7 +209,7 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer private NumberFormat NF = NumberFormat.getNumberInstance(); - private final static List RESET_OPTIONS = Collections.unmodifiableList(Arrays.asList( + final static List RESET_OPTIONS = Collections.unmodifiableList(Arrays.asList( "theme", "startup", "date", "date_week", "date_fixed", "date_bold", "date_time", "group_category", "cards", "beige", "tabular_card_bg", "shadow_unread", "shadow_border", "shadow_highlight", "dividers", "tabular_unread_bg", diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsEncryption.java b/app/src/main/java/eu/faircode/email/FragmentOptionsEncryption.java index f6c4c3b0f5..cc518bd2fa 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsEncryption.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsEncryption.java @@ -118,7 +118,7 @@ public class FragmentOptionsEncryption extends FragmentBase static final int REQUEST_IMPORT_CERTIFICATE = 1; - private final static List RESET_OPTIONS = Collections.unmodifiableList(Arrays.asList( + final static List RESET_OPTIONS = Collections.unmodifiableList(Arrays.asList( "sign_default", "encrypt_default", "encrypt_auto", "auto_verify", "auto_decrypt", "auto_undecrypt", "openpgp_provider", "autocrypt", "autocrypt_mutual", "encrypt_subject", diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java index 25af878b54..c7f41c944f 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java @@ -268,7 +268,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc private static final int REQUEST_CLASSIFIER = 1; private static final long MIN_FILE_SIZE = 1024 * 1024L; - private final static List RESET_OPTIONS = Collections.unmodifiableList(Arrays.asList( + final static List RESET_OPTIONS = Collections.unmodifiableList(Arrays.asList( "sort_answers", "shortcuts", "ical_tentative", "fts", "classification", "class_min_probability", "class_min_difference", "show_filtered", "haptic_feedback", diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsNotifications.java b/app/src/main/java/eu/faircode/email/FragmentOptionsNotifications.java index b04ee30448..eef0c76777 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsNotifications.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsNotifications.java @@ -135,7 +135,7 @@ public class FragmentOptionsNotifications extends FragmentBase implements Shared private Group grpBackground; private Group grpTiles; - private final static List RESET_OPTIONS = Collections.unmodifiableList(Arrays.asList( + final static List RESET_OPTIONS = Collections.unmodifiableList(Arrays.asList( "notify_newest_first", "notify_summary", "notify_trash", "notify_junk", "notify_block_sender", "notify_archive", "notify_move", "notify_reply", "notify_reply_direct", diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsPrivacy.java b/app/src/main/java/eu/faircode/email/FragmentOptionsPrivacy.java index 558444b0ca..b6633ef176 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsPrivacy.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsPrivacy.java @@ -114,7 +114,7 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer private final static int BIP39_WORDS = 6; - private final static List RESET_OPTIONS = Collections.unmodifiableList(Arrays.asList( + final static List RESET_OPTIONS = Collections.unmodifiableList(Arrays.asList( "confirm_links", "sanitize_links", "adguard", "adguard_auto_update", "check_links_dbl", "confirm_files", "confirm_images", "ask_images", "html_always_images", "confirm_html", "ask_html", diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsSend.java b/app/src/main/java/eu/faircode/email/FragmentOptionsSend.java index 658f6e34c1..fd706a4d9b 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsSend.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsSend.java @@ -130,7 +130,7 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc private EditText etSendRetryMax; private SwitchCompat swSendPartial; - private final static List RESET_OPTIONS = Collections.unmodifiableList(Arrays.asList( + final static List RESET_OPTIONS = Collections.unmodifiableList(Arrays.asList( "keyboard", "keyboard_no_fullscreen", "suggest_names", "suggest_sent", "suggested_received", "suggest_frequently", "suggest_account", "auto_identity", "purge_contact_age", "purge_contact_freq", diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsSynchronize.java b/app/src/main/java/eu/faircode/email/FragmentOptionsSynchronize.java index 1d1a9798d1..b1e739deb9 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsSynchronize.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsSynchronize.java @@ -126,7 +126,7 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr private int textColorTertiary; private int colorAccent; - private final static List RESET_OPTIONS = Collections.unmodifiableList(Arrays.asList( + final static List RESET_OPTIONS = Collections.unmodifiableList(Arrays.asList( "enabled", "poll_interval", "auto_optimize", "poll_metered", "poll_unmetered", "schedule", "schedule_start", "schedule_end", "schedule_start_weekend", "schedule_end_weekend", "weekend",