From 5822f98bbbc3b96a90ff09551de8252ca18bca22 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Tue, 15 Aug 2023 20:23:33 +0200 Subject: Migrate layouts preferences The new `layouts` preference replaces three previous preferences: layout second_layout custom_layout Add a preference migration function, which first migration is to migrate layouts into the new preference. The migration must also be called from the SettingsActivity as it might use a different preference store due to the boot-aware preference copy. --- srcs/juloo.keyboard2/Config.java | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'srcs/juloo.keyboard2/Config.java') diff --git a/srcs/juloo.keyboard2/Config.java b/srcs/juloo.keyboard2/Config.java index 4684194..fdc7730 100644 --- a/srcs/juloo.keyboard2/Config.java +++ b/srcs/juloo.keyboard2/Config.java @@ -348,6 +348,7 @@ final class Config public static void initGlobalConfig(SharedPreferences prefs, Resources res, IKeyEventHandler handler) { + migrate(prefs); _globalConfig = new Config(prefs, res, handler); } @@ -360,4 +361,45 @@ final class Config { public void key_up(KeyValue value, Pointers.Modifiers flags); } + + /** Config migrations. */ + + private static int CONFIG_VERSION = 1; + + public static void migrate(SharedPreferences prefs) + { + int saved_version = prefs.getInt("version", 0); + Logs.debug_config_migration(saved_version, CONFIG_VERSION); + if (saved_version == CONFIG_VERSION) + return; + SharedPreferences.Editor e = prefs.edit(); + e.putInt("version", CONFIG_VERSION); + // Migrations might run on an empty [prefs] for new installs, in this case + // they set the default values of complex options. + switch (saved_version) // Fallback switch + { + case 0: + // Primary, secondary and custom layout options are merged into the new + // Layouts option. This also sets the default value. + List l = new ArrayList(); + l.add(migrate_layout(prefs.getString("layout", "system"))); + String snd_layout = prefs.getString("second_layout", "none"); + if (snd_layout != null && !snd_layout.equals("none")) + l.add(migrate_layout(snd_layout)); + String custom_layout = prefs.getString("custom_layout", ""); + if (custom_layout != null && !custom_layout.equals("")) + l.add(new LayoutsPreference.CustomLayout(custom_layout)); + LayoutsPreference.save_to_preferences(e, l); + case 1: + default: break; + } + e.commit(); + } + + private static LayoutsPreference.Layout migrate_layout(String name) + { + if (name == null || name.equals("system")) + return new LayoutsPreference.SystemLayout(); + return new LayoutsPreference.NamedLayout(name); + } } -- cgit v1.2.3