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); + } } diff --git a/srcs/juloo.keyboard2/Keyboard2.java b/srcs/juloo.keyboard2/Keyboard2.java index 92507ed..924f87b 100644 --- a/srcs/juloo.keyboard2/Keyboard2.java +++ b/srcs/juloo.keyboard2/Keyboard2.java @@ -93,9 +93,9 @@ public class Keyboard2 extends InputMethodService super.onCreate(); KeyboardData.init(getResources()); SharedPreferences prefs = DirectBootAwarePreferences.get_shared_preferences(this); - prefs.registerOnSharedPreferenceChangeListener(this); _keyeventhandler = new KeyEventHandler(getMainLooper(), this.new Receiver()); Config.initGlobalConfig(prefs, getResources(), _keyeventhandler); + prefs.registerOnSharedPreferenceChangeListener(this); _config = Config.globalConfig(); _keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard); _keyboardView.reset(); diff --git a/srcs/juloo.keyboard2/LayoutsPreference.java b/srcs/juloo.keyboard2/LayoutsPreference.java index d6669ba..f15bad9 100644 --- a/srcs/juloo.keyboard2/LayoutsPreference.java +++ b/srcs/juloo.keyboard2/LayoutsPreference.java @@ -75,6 +75,12 @@ public class LayoutsPreference extends ListGroupPreference items) + { + save_to_preferences(KEY, prefs, items, SERIALIZER); + } + public static KeyboardData layout_of_string(Resources res, String name) { int id = layout_id_of_name(res, name); diff --git a/srcs/juloo.keyboard2/ListGroupPreference.java b/srcs/juloo.keyboard2/ListGroupPreference.java index ad47411..075b38c 100644 --- a/srcs/juloo.keyboard2/ListGroupPreference.java +++ b/srcs/juloo.keyboard2/ListGroupPreference.java @@ -78,6 +78,12 @@ public abstract class ListGroupPreference extends PreferenceGroup return (s != null) ? load_from_string(s, serializer) : def; } + /** Save items into the preferences. Does not call [prefs.commit()]. */ + static void save_to_preferences(String key, SharedPreferences.Editor prefs, List items, Serializer serializer) + { + prefs.putString(key, save_to_string(items, serializer)); + } + /** Decode a list of string previously encoded with [save_to_string]. Returns [null] on error. */ static List load_from_string(String inp, Serializer serializer) @@ -92,6 +98,7 @@ public abstract class ListGroupPreference extends PreferenceGroup } catch (JSONException e) { + Logs.exn("load_from_string", e); return null; } } @@ -107,7 +114,10 @@ public abstract class ListGroupPreference extends PreferenceGroup { serialized_items.add(serializer.save_item(it)); } - catch (JSONException e) {} + catch (JSONException e) + { + Logs.exn("save_to_string", e); + } } return (new JSONArray(serialized_items)).toString(); } diff --git a/srcs/juloo.keyboard2/Logs.java b/srcs/juloo.keyboard2/Logs.java index a09c83f..8843052 100644 --- a/srcs/juloo.keyboard2/Logs.java +++ b/srcs/juloo.keyboard2/Logs.java @@ -27,9 +27,25 @@ public final class Logs _debug_logs.println("actionLabel: "+conf.actionLabel); } + public static void debug_config_migration(int from_version, int to_version) + { + debug("Migrating config version from " + from_version + " to " + to_version); + } + public static void debug(String s) { if (_debug_logs != null) _debug_logs.println(s); } + + public static void exn(String msg, Exception e) + { + Log.e(TAG, msg, e); + } + + public static void trace() + { + if (_debug_logs != null) + _debug_logs.println(Log.getStackTraceString(new Exception())); + } } diff --git a/srcs/juloo.keyboard2/SettingsActivity.java b/srcs/juloo.keyboard2/SettingsActivity.java index 6fd050c..d053552 100644 --- a/srcs/juloo.keyboard2/SettingsActivity.java +++ b/srcs/juloo.keyboard2/SettingsActivity.java @@ -16,7 +16,12 @@ public class SettingsActivity extends PreferenceActivity super.onCreate(savedInstanceState); // The preferences can't be read when in direct-boot mode. Avoid crashing // and don't allow changing the settings. - try { getPreferenceManager().getSharedPreferences(); } + // Run the config migration on this prefs as it might be different from the + // one used by the keyboard, which have been migrated. + try + { + Config.migrate(getPreferenceManager().getSharedPreferences()); + } catch (Exception _e) { fallbackEncrypted(); return; } addPreferencesFromResource(R.xml.settings); }