diff options
| author | Jules Aguillon | 2023-07-29 18:31:26 +0200 |
|---|---|---|
| committer | Jules Aguillon | 2023-07-29 18:42:36 +0200 |
| commit | 0856fb4e31a98b7f14f50006d95c89e23c19503d (patch) | |
| tree | 1c81fcb97af653d3c3214bd22e11f133d01312f6 /srcs/juloo.keyboard2/CustomExtraKeysPreference.java | |
| parent | 0a114bd2bc0fabf204190457d671f2a719acf6e9 (diff) | |
| download | unexpected-keyboard-0856fb4e31a98b7f14f50006d95c89e23c19503d.tar.gz unexpected-keyboard-0856fb4e31a98b7f14f50006d95c89e23c19503d.zip | |
Refactor: Split out ListGroupPreference
Split out the implementation of a string-list preference from
CustomExtraKeysPreference.
Allows to share the implementation with future preferences.
Diffstat (limited to 'srcs/juloo.keyboard2/CustomExtraKeysPreference.java')
| -rw-r--r-- | srcs/juloo.keyboard2/CustomExtraKeysPreference.java | 162 |
1 files changed, 23 insertions, 139 deletions
diff --git a/srcs/juloo.keyboard2/CustomExtraKeysPreference.java b/srcs/juloo.keyboard2/CustomExtraKeysPreference.java index 668e6f1..7bbc449 100644 --- a/srcs/juloo.keyboard2/CustomExtraKeysPreference.java +++ b/srcs/juloo.keyboard2/CustomExtraKeysPreference.java @@ -17,161 +17,45 @@ import org.json.JSONException; /** Allows to enter custom keys to be added to the keyboard. This shows up at the top of the "Add keys to the keyboard" option. */ -public class CustomExtraKeysPreference extends PreferenceCategory +public class CustomExtraKeysPreference extends ListGroupPreference { /** This pref stores a list of strings encoded as JSON. */ - static String KEY = "custom_extra_keys"; - - boolean _attached = false; - /** Mutable. This is the list of the key strings, not the key names. */ - List<String> _keys; + static final String KEY = "custom_extra_keys"; public CustomExtraKeysPreference(Context context, AttributeSet attrs) { super(context, attrs); setKey(KEY); - setOrderingAsAdded(true); - _keys = new ArrayList<String>(); } public static List<KeyValue> get(SharedPreferences prefs) { List<KeyValue> kvs = new ArrayList<KeyValue>(); - String inp = prefs.getString(KEY, null); - if (inp != null) - for (String key_name : load_from_string(inp)) - kvs.add(KeyValue.makeStringKey(key_name)); - return kvs; - } - - @Override - protected void onSetInitialValue(boolean restoreValue, Object defaultValue) - { - if (restoreValue) + List<String> key_names = load_from_preferences(KEY, prefs, null); + if (key_names != null) { - String persisted = getPersistedString(null); - if (persisted != null) - set_keys(load_from_string(persisted), false); + for (String key_name : key_names) + kvs.add(KeyValue.makeStringKey(key_name)); } - else if (defaultValue != null) - set_keys(load_from_string((String)defaultValue), false); + return kvs; } @Override - protected void onAttachedToActivity() - { - super.onAttachedToActivity(); - if (_attached) - return; - _attached = true; - reattach(); - } - - void reattach() - { - removeAll(); - for (String k : _keys) - addPreference(this.new CustomExtraKey(getContext(), k)); - addPreference(this.new AddButton(getContext())); - } - - void set_keys(List<String> v, boolean persist) - { - _keys = v; - reattach(); - if (persist) - persistString(save_to_string(_keys)); - } - - void add_key(String k) - { - _keys.add(k); - set_keys(_keys, true); - } - - void remove_key(String k) - { - _keys.remove(k); - set_keys(_keys, true); - } - - static String save_to_string(List<String> keys) - { - return (new JSONArray(keys)).toString(); - } - - static List<String> load_from_string(String inp) - { - List<String> keys = new ArrayList<String>(); - try - { - JSONArray arr = new JSONArray(inp); - for (int i = 0; i < arr.length(); i++) - keys.add(arr.getString(i)); - } - catch (JSONException e) - { - Logs.err_load_custom_extra_keys(e); - } - return keys; - } - - /** A preference with no key that is only intended to be rendered. */ - final class CustomExtraKey extends Preference implements View.OnClickListener - { - String _key; - - public CustomExtraKey(Context ctx, String key) - { - super(ctx); - _key = key; - setTitle(key); - setPersistent(false); - setWidgetLayoutResource(R.layout.custom_extra_key_widget); - } - - /** Remove-button listener. */ - @Override - public void onClick(View _v) - { - CustomExtraKeysPreference.this.remove_key(_key); - } - - @Override - protected View onCreateView(ViewGroup parent) - { - View v = super.onCreateView(parent); - v.findViewById(R.id.btn_custom_extra_key_remove).setOnClickListener(this); - return v; - } - } - - final class AddButton extends Preference - { - public AddButton(Context ctx) - { - super(ctx); - setPersistent(false); - setLayoutResource(R.layout.custom_extra_key_add); - } - - @Override - protected void onClick() - { - new AlertDialog.Builder(getContext()) - .setView(R.layout.custom_extra_key_add_dialog) - .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){ - public void onClick(DialogInterface dialog, int which) - { - EditText input = (EditText)((AlertDialog)dialog).findViewById(R.id.key_name); - String k = input.getText().toString(); - if (!k.equals("")) - CustomExtraKeysPreference.this.add_key(k); - } - }) - .setNegativeButton(android.R.string.cancel, null) - .setIcon(android.R.drawable.ic_dialog_alert) - .show(); - } + void select(final SelectionCallback callback) + { + new AlertDialog.Builder(getContext()) + .setView(R.layout.custom_extra_key_add_dialog) + .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){ + public void onClick(DialogInterface dialog, int which) + { + EditText input = (EditText)((AlertDialog)dialog).findViewById(R.id.key_name); + final String k = input.getText().toString(); + if (!k.equals("")) + callback.select(k); + } + }) + .setNegativeButton(android.R.string.cancel, null) + .setIcon(android.R.drawable.ic_dialog_alert) + .show(); } } |
