From 458e17bf31c9ef6b4dfadd56dd0c0dfb709eb32c Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Tue, 18 Jul 2023 00:31:32 +0200 Subject: Add custom extra keys preference This is a new section in the extra keys option that allows to enter arbitrary strings which are then added to the keyboard. A new string is needed for the title of the section, Android's icons and strings are used as much as possible to avoid adding more strings. Keys are stored in the preferences as a JSON array of strings. --- srcs/juloo.keyboard2/Config.java | 4 + .../juloo.keyboard2/CustomExtraKeysPreference.java | 174 +++++++++++++++++++++ srcs/juloo.keyboard2/ExtraKeysPreference.java | 5 +- 3 files changed, 181 insertions(+), 2 deletions(-) create mode 100644 srcs/juloo.keyboard2/CustomExtraKeysPreference.java (limited to 'srcs') diff --git a/srcs/juloo.keyboard2/Config.java b/srcs/juloo.keyboard2/Config.java index 1bc406c..c6b3ed4 100644 --- a/srcs/juloo.keyboard2/Config.java +++ b/srcs/juloo.keyboard2/Config.java @@ -62,6 +62,7 @@ final class Config public boolean swapEnterActionKey; // Swap the "enter" and "action" keys public ExtraKeys extra_keys_subtype; public Set extra_keys_param; + public List extra_keys_custom; public final IKeyEventHandler handler; public boolean orientation_landscape = false; @@ -155,6 +156,7 @@ final class Config autocapitalisation = _prefs.getBoolean("autocapitalisation", true); switch_input_immediate = _prefs.getBoolean("switch_input_immediate", false); extra_keys_param = ExtraKeysPreference.get_extra_keys(_prefs); + extra_keys_custom = CustomExtraKeysPreference.get(_prefs); } KeyValue action_key() @@ -170,6 +172,7 @@ final class Config * - Replace the action key to show the right label * - Swap the enter and action keys * - Add the optional numpad and number row + * - Add the extra keys */ public KeyboardData modify_layout(KeyboardData kw) { @@ -181,6 +184,7 @@ final class Config if (extra_keys_subtype != null) extra_keys_subtype.compute(extra_keys, kw.script); extra_keys.addAll(extra_keys_param); + extra_keys.addAll(extra_keys_custom); boolean number_row = this.number_row && !show_numpad; if (number_row) KeyboardData.number_row.getKeys(remove_keys); diff --git a/srcs/juloo.keyboard2/CustomExtraKeysPreference.java b/srcs/juloo.keyboard2/CustomExtraKeysPreference.java new file mode 100644 index 0000000..228eae8 --- /dev/null +++ b/srcs/juloo.keyboard2/CustomExtraKeysPreference.java @@ -0,0 +1,174 @@ +package juloo.keyboard2; + +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.content.SharedPreferences; +import android.preference.Preference; +import android.preference.PreferenceCategory; +import android.util.AttributeSet; +import android.view.View; +import android.view.ViewGroup; +import android.widget.EditText; +import java.util.ArrayList; +import java.util.List; +import org.json.JSONArray; +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 +{ + /** 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 _keys; + + public CustomExtraKeysPreference(Context context, AttributeSet attrs) + { + super(context, attrs); + setKey(KEY); + setOrderingAsAdded(true); + _keys = new ArrayList(); + } + + public static List get(SharedPreferences prefs) + { + List kvs = new ArrayList(); + 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) + { + String persisted = getPersistedString(null); + if (persisted != null) + set_keys(load_from_string(persisted), false); + } + else if (defaultValue != null) + set_keys(load_from_string((String)defaultValue), false); + } + + @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 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 keys) + { + return (new JSONArray(keys)).toString(); + } + + static List load_from_string(String inp) + { + List keys = new ArrayList(); + try + { + JSONArray arr = new JSONArray(inp); + for (int i = 0; i < arr.length(); i++) + keys.add(arr.getString(i)); + } + catch (JSONException 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(); + } + } +} diff --git a/srcs/juloo.keyboard2/ExtraKeysPreference.java b/srcs/juloo.keyboard2/ExtraKeysPreference.java index 340db1e..6b0547b 100644 --- a/srcs/juloo.keyboard2/ExtraKeysPreference.java +++ b/srcs/juloo.keyboard2/ExtraKeysPreference.java @@ -81,7 +81,7 @@ public class ExtraKeysPreference extends PreferenceCategory return ks; } - boolean _attached; /** Whether it has already been attached. */ + boolean _attached = false; /** Whether it has already been attached. */ public ExtraKeysPreference(Context context, AttributeSet attrs) { @@ -89,6 +89,7 @@ public class ExtraKeysPreference extends PreferenceCategory setOrderingAsAdded(true); } + @Override protected void onAttachedToActivity() { if (_attached) @@ -104,7 +105,7 @@ public class ExtraKeysPreference extends PreferenceCategory return "extra_key_" + key_name; } - final class ExtraKeyCheckBoxPreference extends CheckBoxPreference + static class ExtraKeyCheckBoxPreference extends CheckBoxPreference { boolean _key_font; -- cgit v1.2.3