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.
This commit is contained in:
Jules Aguillon 2023-07-18 00:31:32 +02:00
parent 324aa26ba4
commit 458e17bf31
24 changed files with 211 additions and 2 deletions

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center">
<View android:layout_width="@dimen/pref_button_size" android:layout_height="@dimen/pref_button_size" android:background="@android:drawable/ic_menu_add"/>
</LinearLayout>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content">
<EditText android:id="@+id/key_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dp"/>
</LinearLayout>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal">
<Button android:id="@+id/btn_custom_extra_key_remove" android:layout_width="@dimen/pref_button_size" android:layout_height="@dimen/pref_button_size" android:layout_gravity="center" android:background="@android:drawable/ic_menu_close_clear_cancel"/>
</LinearLayout>

View File

@ -27,6 +27,7 @@
<string name="pref_numpad_layout_e_high_first">Vyšší číslice jako první</string>
<string name="pref_numpad_layout_e_low_first">Nižší číslice jako první</string>
<string name="pref_extra_keys_title">Přidat klávesy do klávesnice</string>
<!-- <string name="pref_extra_keys_custom">Add custom keys</string> -->
<!-- <string name="pref_extra_keys_internal">Select keys to add to the keyboard</string> -->
<string name="pref_second_layout_title">Vedlejší rozvržení</string>
<string name="pref_second_layout_none">Žádné</string>

View File

@ -27,6 +27,7 @@
<string name="pref_numpad_layout_e_high_first">Hohe Ziffern zuerst</string>
<string name="pref_numpad_layout_e_low_first">Niedrige Ziffern zuerst</string>
<string name="pref_extra_keys_title">Zusätzliche Zeichen zur Tastatur hinzufügen</string>
<!-- <string name="pref_extra_keys_custom">Add custom keys</string> -->
<!-- <string name="pref_extra_keys_internal">Select keys to add to the keyboard</string> -->
<string name="pref_second_layout_title">Zweites Layout</string>
<string name="pref_second_layout_none">Keines</string>

View File

@ -27,6 +27,7 @@
<!-- <string name="pref_numpad_layout_e_high_first">High digits first</string> -->
<!-- <string name="pref_numpad_layout_e_low_first">Low digits first</string> -->
<!-- <string name="pref_extra_keys_title">Add keys to the keyboard</string> -->
<!-- <string name="pref_extra_keys_custom">Add custom keys</string> -->
<!-- <string name="pref_extra_keys_internal">Select keys to add to the keyboard</string> -->
<!-- <string name="pref_second_layout_title">Secondary layout</string> -->
<!-- <string name="pref_second_layout_none">None</string> -->

View File

@ -27,6 +27,7 @@
<string name="pref_numpad_layout_e_high_first">ابتدا اعداد بزرگ</string>
<string name="pref_numpad_layout_e_low_first">ابتدا اعداد کوچک</string>
<string name="pref_extra_keys_title">افزودن کلیدها به صفحه کلید</string>
<!-- <string name="pref_extra_keys_custom">Add custom keys</string> -->
<!-- <string name="pref_extra_keys_internal">Select keys to add to the keyboard</string> -->
<string name="pref_second_layout_title">طرح دوم</string>
<string name="pref_second_layout_none">هیچ</string>

View File

@ -27,6 +27,7 @@
<string name="pref_numpad_layout_e_high_first">Du plus haut au plus bas</string>
<string name="pref_numpad_layout_e_low_first">Du plus bas au plus haut</string>
<string name="pref_extra_keys_title">Ajouter des touches au clavier</string>
<!-- <string name="pref_extra_keys_custom">Add custom keys</string> -->
<!-- <string name="pref_extra_keys_internal">Select keys to add to the keyboard</string> -->
<string name="pref_second_layout_title">Disposition secondaire</string>
<string name="pref_second_layout_none">Aucune</string>

View File

@ -27,6 +27,7 @@
<!-- <string name="pref_numpad_layout_e_high_first">High digits first</string> -->
<!-- <string name="pref_numpad_layout_e_low_first">Low digits first</string> -->
<!-- <string name="pref_extra_keys_title">Add keys to the keyboard</string> -->
<!-- <string name="pref_extra_keys_custom">Add custom keys</string> -->
<!-- <string name="pref_extra_keys_internal">Select keys to add to the keyboard</string> -->
<!-- <string name="pref_second_layout_title">Secondary layout</string> -->
<string name="pref_second_layout_none">Nessuno</string>

View File

@ -27,6 +27,7 @@
<!-- <string name="pref_numpad_layout_e_high_first">High digits first</string> -->
<!-- <string name="pref_numpad_layout_e_low_first">Low digits first</string> -->
<!-- <string name="pref_extra_keys_title">Add keys to the keyboard</string> -->
<!-- <string name="pref_extra_keys_custom">Add custom keys</string> -->
<!-- <string name="pref_extra_keys_internal">Select keys to add to the keyboard</string> -->
<!-- <string name="pref_second_layout_title">Secondary layout</string> -->
<!-- <string name="pref_second_layout_none">None</string> -->

View File

@ -27,6 +27,7 @@
<string name="pref_numpad_layout_e_high_first">Vispirms lielākie cipari</string>
<string name="pref_numpad_layout_e_low_first">Vispirms mazākie cipari</string>
<string name="pref_extra_keys_title">Pievienot tastatūrai taustiņus</string>
<!-- <string name="pref_extra_keys_custom">Add custom keys</string> -->
<!-- <string name="pref_extra_keys_internal">Select keys to add to the keyboard</string> -->
<string name="pref_second_layout_title">Otrējais izkārtojums</string>
<string name="pref_second_layout_none">Neviens</string>

View File

@ -27,6 +27,7 @@
<string name="pref_numpad_layout_e_high_first">Od największej cyfry</string>
<string name="pref_numpad_layout_e_low_first">Od najmniejszej cyfry</string>
<string name="pref_extra_keys_title">Dodaj klawisze do klawiatury</string>
<!-- <string name="pref_extra_keys_custom">Add custom keys</string> -->
<!-- <string name="pref_extra_keys_internal">Select keys to add to the keyboard</string> -->
<string name="pref_second_layout_title">Drugi układ</string>
<string name="pref_second_layout_none">Żaden</string>

View File

@ -27,6 +27,7 @@
<string name="pref_numpad_layout_e_high_first">Dígitos maiores primeiro</string>
<string name="pref_numpad_layout_e_low_first">Dígitos menores primeiro</string>
<string name="pref_extra_keys_title">Adicionar teclas ao teclado</string>
<!-- <string name="pref_extra_keys_custom">Add custom keys</string> -->
<!-- <string name="pref_extra_keys_internal">Select keys to add to the keyboard</string> -->
<string name="pref_second_layout_title">Layout secundário</string>
<string name="pref_second_layout_none">Nenhum</string>

View File

@ -27,6 +27,7 @@
<string name="pref_numpad_layout_e_high_first">Mai întâi cifrele mari</string>
<string name="pref_numpad_layout_e_low_first">Mai întâi cifrele mici</string>
<string name="pref_extra_keys_title">Adaugă taste pe tastatură</string>
<!-- <string name="pref_extra_keys_custom">Add custom keys</string> -->
<!-- <string name="pref_extra_keys_internal">Select keys to add to the keyboard</string> -->
<string name="pref_second_layout_title">Aranjament secundar</string>
<string name="pref_second_layout_none">Nimic</string>

View File

@ -27,6 +27,7 @@
<string name="pref_numpad_layout_e_high_first">Старшие цифры сверху</string>
<string name="pref_numpad_layout_e_low_first">Младшие цифры сверху</string>
<string name="pref_extra_keys_title">Добавить клавиши на клавиатуру</string>
<!-- <string name="pref_extra_keys_custom">Add custom keys</string> -->
<!-- <string name="pref_extra_keys_internal">Select keys to add to the keyboard</string> -->
<string name="pref_second_layout_title">Дополнительная раскладка</string>
<string name="pref_second_layout_none">Нет</string>

View File

@ -27,6 +27,7 @@
<!-- <string name="pref_numpad_layout_e_high_first">High digits first</string> -->
<!-- <string name="pref_numpad_layout_e_low_first">Low digits first</string> -->
<!-- <string name="pref_extra_keys_title">Add keys to the keyboard</string> -->
<!-- <string name="pref_extra_keys_custom">Add custom keys</string> -->
<!-- <string name="pref_extra_keys_internal">Select keys to add to the keyboard</string> -->
<!-- <string name="pref_second_layout_title">Secondary layout</string> -->
<string name="pref_second_layout_none">Hiçbiri</string>

View File

@ -29,6 +29,7 @@
<string name="pref_numpad_layout_e_high_first">Số lớn nhất trước</string>
<string name="pref_numpad_layout_e_low_first">Số nhỏ nhất trước</string>
<string name="pref_extra_keys_title">Thêm phím vào bàn phím</string>
<!-- <string name="pref_extra_keys_custom">Add custom keys</string> -->
<!-- <string name="pref_extra_keys_internal">Select keys to add to the keyboard</string> -->
<string name="pref_second_layout_title">Bố cục phụ</string>
<string name="pref_second_layout_none">Không</string>

View File

@ -27,6 +27,7 @@
<string name="pref_numpad_layout_e_high_first">大数字在上方</string>
<string name="pref_numpad_layout_e_low_first">小数字在上方</string>
<string name="pref_extra_keys_title">选择要显示的按键</string>
<!-- <string name="pref_extra_keys_custom">Add custom keys</string> -->
<!-- <string name="pref_extra_keys_internal">Select keys to add to the keyboard</string> -->
<string name="pref_second_layout_title">第二键盘布局</string>
<string name="pref_second_layout_none"></string>

View File

@ -27,6 +27,7 @@
<string name="pref_numpad_layout_e_high_first">High digits first</string>
<string name="pref_numpad_layout_e_low_first">Low digits first</string>
<string name="pref_extra_keys_title">Add keys to the keyboard</string>
<string name="pref_extra_keys_custom">Add custom keys</string>
<string name="pref_extra_keys_internal">Select keys to add to the keyboard</string>
<string name="pref_second_layout_title">Secondary layout</string>
<string name="pref_second_layout_none">None</string>

View File

@ -5,6 +5,7 @@
<dimen name="emoji_type_button_height">56dp</dimen>
<dimen name="emoji_grid_height">250dp</dimen>
<dimen name="emoji_text_size">28dp</dimen>
<dimen name="pref_button_size">28dp</dimen>
<bool name="debug_logs" product="debug">true</bool>
<bool name="debug_logs" product="default">false</bool>
</resources>

View File

@ -7,6 +7,7 @@
<ListPreference android:key="show_numpad" android:title="@string/pref_show_numpad_title" android:summary="%s" android:defaultValue="1" android:entries="@array/pref_show_numpad_entries" android:entryValues="@array/pref_show_numpad_values"/>
<CheckBoxPreference android:key="number_row" android:title="@string/pref_number_row_title" android:summary="@string/pref_number_row_summary" android:defaultValue="false"/>
<PreferenceScreen android:title="@string/pref_extra_keys_title">
<juloo.keyboard2.CustomExtraKeysPreference android:title="@string/pref_extra_keys_custom"/>
<juloo.keyboard2.ExtraKeysPreference android:title="@string/pref_extra_keys_internal"/>
</PreferenceScreen>
<ListPreference android:key="numpad_layout" android:title="@string/pref_numpad_layout" android:summary="%s" android:defaultValue="high_first" android:entries="@array/pref_numpad_layout_entries" android:entryValues="@array/pref_numpad_layout_values"/>

View File

@ -62,6 +62,7 @@ final class Config
public boolean swapEnterActionKey; // Swap the "enter" and "action" keys
public ExtraKeys extra_keys_subtype;
public Set<KeyValue> extra_keys_param;
public List<KeyValue> 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);

View File

@ -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<String> _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)
{
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<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) {}
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();
}
}
}

View File

@ -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;