Refactor: Don't define extra keys in xml

The current approach is hard to maintain, for example the last key
"autofill" was not displayed.

This implements a PreferenceGroup that contains the check boxes for
every extra keys without involving listing the preferences in
settings.xml.

A custom layout is used to remove the 'title' text view.
The list of extra keys is moved into the new class.
'ExtraKeyCheckBoxPreference' becomes a nested class.
This commit is contained in:
Jules Aguillon 2023-07-09 18:06:12 +02:00
parent a2957a43d6
commit 4669192a01
5 changed files with 143 additions and 147 deletions

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingEnd="?android:attr/scrollbarSize" android:background="?android:attr/selectableItemBackground">
<!-- Preference layout that do not define the text view with id 'title'. -->
<LinearLayout android:id="@android:id/widget_frame" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="vertical"/>
</LinearLayout>

View File

@ -7,42 +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"/> <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"/> <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"> <PreferenceScreen android:title="@string/pref_extra_keys_title">
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="0"/> <juloo.keyboard2.ExtraKeysPreference/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="1"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="2"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="3"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="4"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="5"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="6"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="7"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="8"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="9"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="10"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="11"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="12"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="13"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="14"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="15"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="16"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="17"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="18"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="19"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="20"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="21"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="22"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="23"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="24"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="25"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="26"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="27"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="28"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="29"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="30"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="31"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="32"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="33"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="34"/>
<juloo.keyboard2.ExtraKeyCheckBoxPreference app:index="35"/>
</PreferenceScreen> </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"/> <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"/>
</PreferenceCategory> </PreferenceCategory>

View File

@ -154,7 +154,7 @@ final class Config
theme = getThemeId(res, _prefs.getString("theme", "")); theme = getThemeId(res, _prefs.getString("theme", ""));
autocapitalisation = _prefs.getBoolean("autocapitalisation", true); autocapitalisation = _prefs.getBoolean("autocapitalisation", true);
switch_input_immediate = _prefs.getBoolean("switch_input_immediate", false); switch_input_immediate = _prefs.getBoolean("switch_input_immediate", false);
extra_keys_param = ExtraKeyCheckBoxPreference.get_extra_keys(_prefs); extra_keys_param = ExtraKeysPreference.get_extra_keys(_prefs);
} }
KeyValue action_key() KeyValue action_key()

View File

@ -1,110 +0,0 @@
package juloo.keyboard2;
import android.content.Context;
import android.content.res.TypedArray;
import android.content.SharedPreferences;
import android.preference.CheckBoxPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import java.util.HashSet;
import java.util.Set;
public class ExtraKeyCheckBoxPreference extends CheckBoxPreference
{
public static String[] extra_keys = new String[]
{
"alt",
"meta",
"voice_typing",
"accent_aigu",
"accent_grave",
"accent_double_aigu",
"accent_dot_above",
"accent_circonflexe",
"accent_tilde",
"accent_cedille",
"accent_trema",
"accent_ring",
"accent_caron",
"accent_macron",
"accent_ogonek",
"accent_breve",
"accent_slash",
"accent_bar",
"accent_dot_below",
"accent_hook_above",
"accent_horn",
"",
"ß",
"£",
"switch_greekmath",
"capslock",
"copy",
"paste",
"cut",
"selectAll",
"shareText",
"pasteAsPlainText",
"undo",
"redo",
"replaceText",
"textAssist",
"autofill",
};
public static boolean default_checked(String name)
{
switch (name)
{
case "voice_typing":
return true;
default:
return false;
}
}
boolean _key_font;
public ExtraKeyCheckBoxPreference(Context context, AttributeSet attrs)
{
super(context, attrs);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ExtraKeyCheckBoxPreference);
int index = a.getInteger(R.styleable.ExtraKeyCheckBoxPreference_index, 0);
a.recycle();
String key_name = extra_keys[index];
KeyValue kv = KeyValue.getKeyByName(key_name);
String title = kv.getString();
String descr = KeyValue.getKeyDescription(key_name);
if (descr != null)
title += " (" + descr + ")";
setKey(pref_key_of_key_name(key_name));
setDefaultValue(default_checked(key_name));
setTitle(title);
_key_font = kv.hasFlags(KeyValue.FLAG_KEY_FONT);
}
@Override
protected void onBindView(View view)
{
super.onBindView(view);
TextView title = (TextView)view.findViewById(android.R.id.title);
title.setTypeface(_key_font ? Theme.getKeyFont(getContext()) : null);
}
static String pref_key_of_key_name(String key_name)
{
return "extra_key_" + key_name;
}
public static Set<KeyValue> get_extra_keys(SharedPreferences prefs)
{
HashSet<KeyValue> ks = new HashSet<KeyValue>();
for (String key_name : extra_keys)
{
if (prefs.getBoolean(pref_key_of_key_name(key_name), default_checked(key_name)))
ks.add(KeyValue.getKeyByName(key_name));
}
return ks;
}
}

View File

@ -0,0 +1,136 @@
package juloo.keyboard2;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.CheckBoxPreference;
import android.preference.PreferenceGroup;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import java.util.HashSet;
import java.util.Set;
/** This class implements the "extra keys" preference but also defines the
possible extra keys. */
public class ExtraKeysPreference extends PreferenceGroup
{
public static String[] extra_keys = new String[]
{
"alt",
"meta",
"voice_typing",
"accent_aigu",
"accent_grave",
"accent_double_aigu",
"accent_dot_above",
"accent_circonflexe",
"accent_tilde",
"accent_cedille",
"accent_trema",
"accent_ring",
"accent_caron",
"accent_macron",
"accent_ogonek",
"accent_breve",
"accent_slash",
"accent_bar",
"accent_dot_below",
"accent_hook_above",
"accent_horn",
"",
"ß",
"£",
"switch_greekmath",
"capslock",
"copy",
"paste",
"cut",
"selectAll",
"shareText",
"pasteAsPlainText",
"undo",
"redo",
"replaceText",
"textAssist",
"autofill",
};
/** Whether an extra key is enabled by default. */
public static boolean default_checked(String name)
{
switch (name)
{
case "voice_typing":
return true;
default:
return false;
}
}
/** Get the set of enabled extra keys. */
public static Set<KeyValue> get_extra_keys(SharedPreferences prefs)
{
HashSet<KeyValue> ks = new HashSet<KeyValue>();
for (String key_name : extra_keys)
{
if (prefs.getBoolean(pref_key_of_key_name(key_name),
default_checked(key_name)))
ks.add(KeyValue.getKeyByName(key_name));
}
return ks;
}
boolean _attached; /** Whether it has already been attached. */
public ExtraKeysPreference(Context context, AttributeSet attrs)
{
super(context, attrs);
Resources res = context.getResources();
setOrderingAsAdded(true);
setLayoutResource(R.layout.extra_keys_preference);
}
protected void onAttachedToActivity()
{
if (_attached)
return;
_attached = true;
for (String key_name : extra_keys)
addPreference(new ExtraKeyCheckBoxPreference(getContext(), key_name,
default_checked(key_name)));
}
public static String pref_key_of_key_name(String key_name)
{
return "extra_key_" + key_name;
}
final class ExtraKeyCheckBoxPreference extends CheckBoxPreference
{
boolean _key_font;
public ExtraKeyCheckBoxPreference(Context context, String key_name,
boolean default_checked)
{
super(context);
KeyValue kv = KeyValue.getKeyByName(key_name);
String title = kv.getString();
String descr = KeyValue.getKeyDescription(key_name);
if (descr != null)
title += " (" + descr + ")";
setKey(pref_key_of_key_name(key_name));
setDefaultValue(default_checked);
setTitle(title);
_key_font = kv.hasFlags(KeyValue.FLAG_KEY_FONT);
}
@Override
protected void onBindView(View view)
{
super.onBindView(view);
TextView title = (TextView)view.findViewById(android.R.id.title);
title.setTypeface(_key_font ? Theme.getKeyFont(getContext()) : null);
}
}
}