Use subtypes to choose layout

This removes the "layout" setting. Every layouts that the user could use
will appear in the global settings.
This commit is contained in:
Jules Aguillon 2021-04-15 23:56:34 +02:00
parent c22ca7302c
commit ca07bff133
4 changed files with 69 additions and 37 deletions

View File

@ -5,13 +5,10 @@
<string name="settings_activity_label">Unexpected Keyboard Settings</string> <string name="settings_activity_label">Unexpected Keyboard Settings</string>
<string name="subtype_label_azerty">%s AZERTY</string>
<string name="subtype_label_qwerty">%s QWERTY</string>
<string name="pref_category_layout">Layout</string> <string name="pref_category_layout">Layout</string>
<string name="pref_layout_title">Change keyboard layout</string>
<string name="pref_layout_summary">%s</string>
<string-array name="pref_layout_entries">
<item>Azerty</item>
<item>Qwerty</item>
</string-array>
<string name="pref_category_typing">Typing</string> <string name="pref_category_typing">Typing</string>
<string name="pref_preci_title">Precision</string> <string name="pref_preci_title">Precision</string>

View File

@ -2,4 +2,16 @@
<input-method xmlns:android="http://schemas.android.com/apk/res/android" <input-method xmlns:android="http://schemas.android.com/apk/res/android"
android:settingsActivity="juloo.keyboard2.SettingsActivity" android:settingsActivity="juloo.keyboard2.SettingsActivity"
android:supportsSwitchingToNextInputMethod="true"> android:supportsSwitchingToNextInputMethod="true">
<subtype android:label="@string/subtype_label_azerty"
android:languageTag="fr"
android:imeSubtypeLocale="fr_FR"
android:imeSubtypeMode="keyboard"
android:isAsciiCapable="true"
/>
<subtype android:label="@string/subtype_label_qwerty"
android:languageTag="en"
android:imeSubtypeLocale="en_US"
android:imeSubtypeMode="keyboard"
android:isAsciiCapable="true"
/>
</input-method> </input-method>

View File

@ -1,14 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/pref_category_layout"> <PreferenceCategory android:title="@string/pref_category_layout">
<ListPreference
android:key="keyboard_layout"
android:title="@string/pref_layout_title"
android:summary="@string/pref_layout_summary"
android:defaultValue="azerty"
android:entries="@array/pref_layout_entries"
android:entryValues="@array/pref_layout_values"
/>
<CheckBoxPreference <CheckBoxPreference
android:key="disable_accent_keys" android:key="disable_accent_keys"
android:title="@string/pref_disable_accent_keys_title" android:title="@string/pref_disable_accent_keys_title"

View File

@ -1,5 +1,6 @@
package juloo.keyboard2; package juloo.keyboard2;
import android.content.Context;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
@ -10,22 +11,51 @@ import android.text.InputType;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.view.inputmethod.EditorInfo; import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.util.Log; import android.util.Log;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public class Keyboard2 extends InputMethodService public class Keyboard2 extends InputMethodService
implements SharedPreferences.OnSharedPreferenceChangeListener implements SharedPreferences.OnSharedPreferenceChangeListener
{ {
private Keyboard2View _keyboardView; private Keyboard2View _keyboardView;
private KeyboardData _textKeyboard = null; private int _currentTextLayout;
private KeyboardData _numericKeyboard = null;
private ViewGroup _emojiPane = null; private ViewGroup _emojiPane = null;
private Typeface _specialKeyFont = null; private Typeface _specialKeyFont = null;
private Config _config; private Config _config;
private Map<Integer, KeyboardData> _layoutCache = new HashMap<Integer, KeyboardData>();
private static final int DEFAULT_LAYOUT = R.xml.qwerty;
private static final Map<String, Integer> LAYOUTS = new HashMap<String, Integer>();
private static void add_layout(String lang, int resId)
{
LAYOUTS.put(new Locale(lang).getLanguage(), resId);
}
static
{
add_layout("fr", R.xml.azerty);
}
private KeyboardData getLayout(int resId)
{
KeyboardData l = _layoutCache.get(resId);
if (l == null)
{
l = KeyboardData.parse(getResources().getXml(resId));
_layoutCache.put(resId, l);
}
return l;
}
@Override @Override
public void onCreate() public void onCreate()
{ {
@ -34,7 +64,6 @@ public class Keyboard2 extends InputMethodService
PreferenceManager.setDefaultValues(this, R.xml.settings, false); PreferenceManager.setDefaultValues(this, R.xml.settings, false);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this); PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
_config = new Config(this); _config = new Config(this);
_numericKeyboard = KeyboardData.parse(getResources().getXml(R.xml.numeric));
_keyboardView = (Keyboard2View)getLayoutInflater().inflate(R.layout.keyboard, null); _keyboardView = (Keyboard2View)getLayoutInflater().inflate(R.layout.keyboard, null);
_keyboardView.reset(); _keyboardView.reset();
} }
@ -49,6 +78,14 @@ public class Keyboard2 extends InputMethodService
return (_specialKeyFont); return (_specialKeyFont);
} }
private void refreshSubtype(InputMethodSubtype subtype)
{
Integer l = LAYOUTS.get(subtype.getLanguageTag());
if (l == null)
l = DEFAULT_LAYOUT;
_currentTextLayout = l;
}
@Override @Override
public View onCreateInputView() public View onCreateInputView()
{ {
@ -62,17 +99,25 @@ public class Keyboard2 extends InputMethodService
@Override @Override
public void onStartInputView(EditorInfo info, boolean restarting) public void onStartInputView(EditorInfo info, boolean restarting)
{ {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
refreshSubtype(imm.getCurrentInputMethodSubtype());
if ((info.inputType & InputType.TYPE_CLASS_NUMBER) != 0) if ((info.inputType & InputType.TYPE_CLASS_NUMBER) != 0)
_keyboardView.setKeyboard(_numericKeyboard); _keyboardView.setKeyboard(getLayout(R.xml.numeric));
else else
_keyboardView.setKeyboard(_textKeyboard); _keyboardView.setKeyboard(getLayout(_currentTextLayout));
} }
@Override
public void onCurrentInputMethodSubtypeChanged(InputMethodSubtype subtype)
{
refreshSubtype(subtype);
_keyboardView.setKeyboard(getLayout(_currentTextLayout));
}
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{ {
_config.refresh(); _config.refresh();
updateConfig();
_keyboardView.reset(); _keyboardView.reset();
} }
@ -82,20 +127,6 @@ public class Keyboard2 extends InputMethodService
_keyboardView.reset(); _keyboardView.reset();
} }
private int _getKeyboardLayoutRes(SharedPreferences prefs)
{
// Not looking up using [getIdentifier] as it was intended because the
// [packageName] argument can't be passed reliably (eg. debug builds)
switch (prefs.getString("keyboard_layout", null))
{
case "azerty":
return R.xml.azerty;
default:
case "qwerty":
return R.xml.qwerty;
}
}
public void handleKeyUp(KeyValue key, int flags) public void handleKeyUp(KeyValue key, int flags)
{ {
int eventCode = key.getEventCode(); int eventCode = key.getEventCode();
@ -110,9 +141,9 @@ public class Keyboard2 extends InputMethodService
startActivity(intent); startActivity(intent);
} }
else if (eventCode == KeyValue.EVENT_SWITCH_TEXT) else if (eventCode == KeyValue.EVENT_SWITCH_TEXT)
_keyboardView.setKeyboard(_textKeyboard); _keyboardView.setKeyboard(getLayout(_currentTextLayout));
else if (eventCode == KeyValue.EVENT_SWITCH_NUMERIC) else if (eventCode == KeyValue.EVENT_SWITCH_NUMERIC)
_keyboardView.setKeyboard(_numericKeyboard); _keyboardView.setKeyboard(getLayout(R.xml.numeric));
else if (eventCode == KeyValue.EVENT_SWITCH_EMOJI) else if (eventCode == KeyValue.EVENT_SWITCH_EMOJI)
{ {
if (_emojiPane == null) if (_emojiPane == null)