mirror of
https://github.com/Julow/Unexpected-Keyboard.git
synced 2024-11-22 07:13:13 +01:00
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.
This commit is contained in:
parent
613aa283bd
commit
5822f98bbb
@ -348,6 +348,7 @@ final class Config
|
|||||||
public static void initGlobalConfig(SharedPreferences prefs, Resources res,
|
public static void initGlobalConfig(SharedPreferences prefs, Resources res,
|
||||||
IKeyEventHandler handler)
|
IKeyEventHandler handler)
|
||||||
{
|
{
|
||||||
|
migrate(prefs);
|
||||||
_globalConfig = new Config(prefs, res, handler);
|
_globalConfig = new Config(prefs, res, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -360,4 +361,45 @@ final class Config
|
|||||||
{
|
{
|
||||||
public void key_up(KeyValue value, Pointers.Modifiers flags);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,9 +93,9 @@ public class Keyboard2 extends InputMethodService
|
|||||||
super.onCreate();
|
super.onCreate();
|
||||||
KeyboardData.init(getResources());
|
KeyboardData.init(getResources());
|
||||||
SharedPreferences prefs = DirectBootAwarePreferences.get_shared_preferences(this);
|
SharedPreferences prefs = DirectBootAwarePreferences.get_shared_preferences(this);
|
||||||
prefs.registerOnSharedPreferenceChangeListener(this);
|
|
||||||
_keyeventhandler = new KeyEventHandler(getMainLooper(), this.new Receiver());
|
_keyeventhandler = new KeyEventHandler(getMainLooper(), this.new Receiver());
|
||||||
Config.initGlobalConfig(prefs, getResources(), _keyeventhandler);
|
Config.initGlobalConfig(prefs, getResources(), _keyeventhandler);
|
||||||
|
prefs.registerOnSharedPreferenceChangeListener(this);
|
||||||
_config = Config.globalConfig();
|
_config = Config.globalConfig();
|
||||||
_keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard);
|
_keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard);
|
||||||
_keyboardView.reset();
|
_keyboardView.reset();
|
||||||
|
@ -75,6 +75,12 @@ public class LayoutsPreference extends ListGroupPreference<LayoutsPreference.Lay
|
|||||||
return layouts;
|
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)
|
public static KeyboardData layout_of_string(Resources res, String name)
|
||||||
{
|
{
|
||||||
int id = layout_id_of_name(res, name);
|
int id = layout_id_of_name(res, name);
|
||||||
|
@ -78,6 +78,12 @@ public abstract class ListGroupPreference<E> extends PreferenceGroup
|
|||||||
return (s != null) ? load_from_string(s, serializer) : def;
|
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
|
/** Decode a list of string previously encoded with [save_to_string]. Returns
|
||||||
[null] on error. */
|
[null] on error. */
|
||||||
static <E> List<E> load_from_string(String inp, Serializer<E> serializer)
|
static <E> List<E> load_from_string(String inp, Serializer<E> serializer)
|
||||||
@ -92,6 +98,7 @@ public abstract class ListGroupPreference<E> extends PreferenceGroup
|
|||||||
}
|
}
|
||||||
catch (JSONException e)
|
catch (JSONException e)
|
||||||
{
|
{
|
||||||
|
Logs.exn("load_from_string", e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -107,7 +114,10 @@ public abstract class ListGroupPreference<E> extends PreferenceGroup
|
|||||||
{
|
{
|
||||||
serialized_items.add(serializer.save_item(it));
|
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();
|
return (new JSONArray(serialized_items)).toString();
|
||||||
}
|
}
|
||||||
|
@ -27,9 +27,25 @@ public final class Logs
|
|||||||
_debug_logs.println("actionLabel: "+conf.actionLabel);
|
_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)
|
public static void debug(String s)
|
||||||
{
|
{
|
||||||
if (_debug_logs != null)
|
if (_debug_logs != null)
|
||||||
_debug_logs.println(s);
|
_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()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,12 @@ public class SettingsActivity extends PreferenceActivity
|
|||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
// The preferences can't be read when in direct-boot mode. Avoid crashing
|
// The preferences can't be read when in direct-boot mode. Avoid crashing
|
||||||
// and don't allow changing the settings.
|
// 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; }
|
catch (Exception _e) { fallbackEncrypted(); return; }
|
||||||
addPreferencesFromResource(R.xml.settings);
|
addPreferencesFromResource(R.xml.settings);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user