Skip to content

Commit

Permalink
Migrate layouts preferences
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Julow committed Aug 16, 2023
1 parent 92217e6 commit 15e5f6b
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 3 deletions.
42 changes: 42 additions & 0 deletions srcs/juloo.keyboard2/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ char inverse_numpad_char(char c)
public static void initGlobalConfig(SharedPreferences prefs, Resources res,
IKeyEventHandler handler)
{
migrate(prefs);
_globalConfig = new Config(prefs, res, handler);
}

Expand All @@ -360,4 +361,45 @@ public static interface IKeyEventHandler
{
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<LayoutsPreference.Layout> l = new ArrayList<LayoutsPreference.Layout>();
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);
}
}
2 changes: 1 addition & 1 deletion srcs/juloo.keyboard2/Keyboard2.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ public void onCreate()
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();
Expand Down
6 changes: 6 additions & 0 deletions srcs/juloo.keyboard2/LayoutsPreference.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ else if (l instanceof CustomLayout)
return layouts;
}

/** Does not call [prefs.commit()]. */
public static void save_to_preferences(SharedPreferences.Editor prefs, List<Layout> 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);
Expand Down
12 changes: 11 additions & 1 deletion srcs/juloo.keyboard2/ListGroupPreference.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ static <E> List<E> load_from_preferences(String key,
return (s != null) ? load_from_string(s, serializer) : def;
}

/** Save items into the preferences. Does not call [prefs.commit()]. */
static <E> void save_to_preferences(String key, SharedPreferences.Editor prefs, List<E> items, Serializer<E> 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 <E> List<E> load_from_string(String inp, Serializer<E> serializer)
Expand All @@ -92,6 +98,7 @@ static <E> List<E> load_from_string(String inp, Serializer<E> serializer)
}
catch (JSONException e)
{
Logs.exn("load_from_string", e);
return null;
}
}
Expand All @@ -107,7 +114,10 @@ static <E> String save_to_string(List<E> items, Serializer<E> serializer)
{
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();
}
Expand Down
16 changes: 16 additions & 0 deletions srcs/juloo.keyboard2/Logs.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,25 @@ public static void debug_startup_input_view(EditorInfo info, Config conf)
_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()));
}
}
7 changes: 6 additions & 1 deletion srcs/juloo.keyboard2/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ public void onCreate(Bundle savedInstanceState)
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);
}
Expand Down

0 comments on commit 15e5f6b

Please sign in to comment.