Add the accents preference

This replaces the "disable accent keys" checkbox.
The default should work for anyone: Accents will be hidden unless the
user has the french language installed.

The value "show every accents" is useful for versions of android that
don't have subtypes.
This commit is contained in:
Jules Aguillon 2021-05-09 00:09:10 +02:00
parent ebfb8f3b39
commit 7a3312fd01
7 changed files with 106 additions and 27 deletions

View File

@ -5,4 +5,19 @@
<item>azerty</item>
<item>qwerty</item>
</string-array>
<string name="pref_accents_default">1</string>
<string-array name="pref_accents_entries">
<item>@string/pref_accents_e_all_installed</item>
<item>@string/pref_accents_e_selected</item>
<item>@string/pref_accents_e_all</item>
<item>@string/pref_accents_e_none</item>
</string-array>
<string-array name="pref_accents_values">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
</resources>

View File

@ -16,8 +16,13 @@
<item>Azerty</item>
<item>Qwerty</item>
</string-array>
<string name="pref_disable_accent_keys_title">Toggle accent keys</string>
<string name="pref_disable_accent_keys_summary">Whether to remove the accent keys from the keyboard</string>
<string name="pref_accents_title">Accents</string>
<string name="pref_accents_summary">%s</string>
<string name="pref_accents_e_all_installed">Show accents for all the installed languages</string>
<string name="pref_accents_e_selected">Show accents for the selected language</string>
<string name="pref_accents_e_all">Show every accents</string>
<string name="pref_accents_e_none">Hide accents</string>
<string name="pref_category_typing">Typing</string>
<string name="pref_preci_title">Precision</string>

View File

@ -14,6 +14,6 @@
android:imeSubtypeLocale="fr_FR"
android:imeSubtypeMode="keyboard"
android:isAsciiCapable="true"
android:imeSubtypeExtraValue="default_layout=azerty"
android:imeSubtypeExtraValue="default_layout=azerty,accents=grave|aigu|circonflexe|tilde|cedille|trema"
/>
</input-method>

View File

@ -9,12 +9,13 @@
android:entries="@array/pref_layout_entries"
android:entryValues="@array/pref_layout_values" />
/>
<CheckBoxPreference
android:key="disable_accent_keys"
android:title="@string/pref_disable_accent_keys_title"
android:summary="@string/pref_disable_accent_keys_summary"
android:defaultValue="false"
/>
<ListPreference
android:key="accents"
android:title="@string/pref_accents_title"
android:summary="@string/pref_accents_summary"
android:defaultValue="@string/pref_accents_default"
android:entries="@array/pref_accents_entries"
android:entryValues="@array/pref_accents_values" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_category_typing">
<juloo.common.SlideBarPreference

View File

@ -9,12 +9,14 @@ class Config
{
private Keyboard2 _context;
// From resources
public final float marginTop;
public final float keyPadding;
public final float keyVerticalInterval;
public final float keyHorizontalInterval;
public final float keyRound;
// From preferences
public int layout; // Or '-1' for the system defaults
public float subValueDist;
public boolean vibrateEnabled;
@ -24,11 +26,13 @@ class Config
public float marginBottom;
public float keyHeight;
public float horizontalMargin;
public boolean disableAccentKeys;
public boolean preciseRepeat;
public float characterSize; // Ratio
public int accents; // Values are R.values.pref_accents_v_*
// Dynamically set
public boolean shouldOfferSwitchingToNextInputMethod;
public int accent_flags_to_remove;
public Config(Keyboard2 context)
{
@ -51,13 +55,14 @@ class Config
marginBottom = res.getDimension(R.dimen.margin_bottom);
keyHeight = res.getDimension(R.dimen.key_height);
horizontalMargin = res.getDimension(R.dimen.horizontal_margin);
disableAccentKeys = false;
preciseRepeat = true;
characterSize = 1.f;
accents = 0;
// from prefs
refresh();
// initialized later
shouldOfferSwitchingToNextInputMethod = false;
accent_flags_to_remove = 0;
}
/*
@ -76,9 +81,9 @@ class Config
marginBottom = getDipPref(prefs, "margin_bottom", marginBottom);
keyHeight = getDipPref(prefs, "key_height", keyHeight);
horizontalMargin = getDipPref(prefs, "horizontal_margin", horizontalMargin);
disableAccentKeys = prefs.getBoolean("disable_accent_keys", disableAccentKeys);
preciseRepeat = prefs.getBoolean("precise_repeat", preciseRepeat);
characterSize = prefs.getFloat("character_size", characterSize);
accents = Integer.valueOf(prefs.getString("accents", ""));
}
private float getDipPref(SharedPreferences prefs, String pref_name, float def)
@ -102,4 +107,18 @@ class Config
}
}
/* Used for the accents option. */
public static int accentFlag_of_name(String name)
{
switch (name)
{
case "grave": return KeyValue.FLAG_ACCENT1;
case "aigu": return KeyValue.FLAG_ACCENT2;
case "circonflexe": return KeyValue.FLAG_ACCENT3;
case "tilde": return KeyValue.FLAG_ACCENT4;
case "cedille": return KeyValue.FLAG_ACCENT5;
case "trema": return KeyValue.FLAG_ACCENT6;
default: throw new RuntimeException(name);
}
}
}

View File

@ -11,6 +11,7 @@ import android.os.IBinder;
import android.text.InputType;
import android.preference.PreferenceManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
import android.view.KeyEvent;
@ -18,6 +19,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.util.Log;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@ -66,25 +68,59 @@ public class Keyboard2 extends InputMethodService
return (_specialKeyFont);
}
private void refreshSubtype(InputMethodSubtype subtype)
private List<InputMethodSubtype> getEnabledSubtypes(InputMethodManager imm)
{
String pkg = getPackageName();
for (InputMethodInfo imi : imm.getEnabledInputMethodList())
if (imi.getPackageName().equals(pkg))
return imm.getEnabledInputMethodSubtypeList(imi, true);
return null;
}
private void refreshSubtypeLayout(InputMethodSubtype subtype)
{
int l;
if (_config.layout == -1)
l = Config.layoutId_of_string(subtype.getExtraValueOf("default_layout"));
_currentTextLayout = Config.layoutId_of_string(subtype.getExtraValueOf("default_layout"));
else
l = _config.layout;
if (_currentTextLayout != l)
_currentTextLayout = _config.layout;
}
private int accents_of_subtype(InputMethodSubtype subtype)
{
String accents_option = subtype.getExtraValueOf("accents");
int flags = 0;
if (accents_option != null)
for (String acc : accents_option.split("\\|"))
flags |= Config.accentFlag_of_name(acc);
return flags;
}
private void refreshAccentsOption(InputMethodManager imm, InputMethodSubtype subtype)
{
final int DONT_REMOVE = KeyValue.FLAG_ACCENT_SUPERSCRIPT | KeyValue.FLAG_ACCENT_SUBSCRIPT;
int to_keep = DONT_REMOVE;
switch (_config.accents)
{
_currentTextLayout = l;
_keyboardView.setKeyboard(getLayout(l));
case 1:
to_keep |= accents_of_subtype(subtype);
for (InputMethodSubtype s : getEnabledSubtypes(imm))
to_keep |= accents_of_subtype(s);
break;
case 2: to_keep |= accents_of_subtype(subtype); break;
case 3: to_keep = KeyValue.FLAGS_ACCENTS; break;
case 4: break;
default: throw new IllegalArgumentException();
}
_config.accent_flags_to_remove = ~to_keep & KeyValue.FLAGS_ACCENTS;
}
private void refreshSubtypeImm()
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
_config.shouldOfferSwitchingToNextInputMethod = imm.shouldOfferSwitchingToNextInputMethod(getConnectionToken());
refreshSubtype(imm.getCurrentInputMethodSubtype());
InputMethodSubtype subtype = imm.getCurrentInputMethodSubtype();
refreshSubtypeLayout(subtype);
refreshAccentsOption(imm, subtype);
}
@Override
@ -103,13 +139,16 @@ public class Keyboard2 extends InputMethodService
refreshSubtypeImm();
if ((info.inputType & InputType.TYPE_CLASS_NUMBER) != 0)
_keyboardView.setKeyboard(getLayout(R.xml.numeric));
else
_keyboardView.setKeyboard(getLayout(_currentTextLayout));
_keyboardView.reset(); // Layout might need to change due to rotation
}
@Override
public void onCurrentInputMethodSubtypeChanged(InputMethodSubtype subtype)
{
refreshSubtype(subtype);
refreshSubtypeImm();
_keyboardView.setKeyboard(getLayout(_currentTextLayout));
}
@Override

View File

@ -92,15 +92,15 @@ public class Keyboard2View extends View
return (paint);
}
public void setKeyboard(KeyboardData kw)
{
public void setKeyboard(KeyboardData kw)
{
if (!_config.shouldOfferSwitchingToNextInputMethod)
kw = kw.removeKeys(new KeyboardData.RemoveKeysByEvent(KeyValue.EVENT_CHANGE_METHOD));
if (_config.disableAccentKeys)
kw = kw.removeKeys(new KeyboardData.RemoveKeysByFlags(KeyValue.FLAGS_ACCENTS));
if (_config.accent_flags_to_remove != 0)
kw = kw.removeKeys(new KeyboardData.RemoveKeysByFlags(_config.accent_flags_to_remove));
_keyboard = kw;
reset();
}
reset();
}
public void reset()
{