2015-10-29 12:49:40 +01:00
|
|
|
package juloo.keyboard2;
|
|
|
|
|
2021-12-28 16:47:19 +01:00
|
|
|
import android.content.Context;
|
2015-10-29 12:49:40 +01:00
|
|
|
import android.content.res.Resources;
|
2022-01-30 23:29:50 +01:00
|
|
|
import android.content.res.Configuration;
|
2015-10-29 12:49:40 +01:00
|
|
|
import android.content.SharedPreferences;
|
2022-01-30 23:29:50 +01:00
|
|
|
import android.os.Build;
|
2021-12-28 16:47:19 +01:00
|
|
|
import android.util.DisplayMetrics;
|
2015-10-29 12:49:40 +01:00
|
|
|
import android.util.TypedValue;
|
2022-03-05 18:15:36 +01:00
|
|
|
import android.view.KeyEvent;
|
2022-05-29 12:27:46 +02:00
|
|
|
import java.util.Iterator;
|
2022-03-13 00:14:18 +01:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.HashSet;
|
2015-10-29 12:49:40 +01:00
|
|
|
|
2021-12-28 16:47:19 +01:00
|
|
|
final class Config
|
2015-10-29 12:49:40 +01:00
|
|
|
{
|
2022-11-11 14:27:02 +01:00
|
|
|
private final SharedPreferences _prefs;
|
|
|
|
|
2021-05-09 00:09:10 +02:00
|
|
|
// From resources
|
2021-12-19 19:44:27 +01:00
|
|
|
public final float marginTop;
|
|
|
|
public final float keyPadding;
|
2015-10-29 12:49:40 +01:00
|
|
|
|
2022-02-27 01:50:24 +01:00
|
|
|
public final float labelTextSize;
|
|
|
|
public final float sublabelTextSize;
|
|
|
|
|
2021-05-09 00:09:10 +02:00
|
|
|
// From preferences
|
2021-04-28 00:23:52 +02:00
|
|
|
public int layout; // Or '-1' for the system defaults
|
2022-11-13 00:24:23 +01:00
|
|
|
public int second_layout; // Or '-1' for none
|
2022-09-25 02:23:33 +02:00
|
|
|
public boolean show_numpad = false;
|
2021-12-30 22:22:25 +01:00
|
|
|
public float swipe_dist_px;
|
2022-06-24 22:00:23 +02:00
|
|
|
public boolean vibrateEnabled;
|
2021-12-19 19:44:27 +01:00
|
|
|
public long longPressTimeout;
|
|
|
|
public long longPressInterval;
|
|
|
|
public float marginBottom;
|
|
|
|
public float keyHeight;
|
|
|
|
public float horizontalMargin;
|
2022-01-30 23:55:15 +01:00
|
|
|
public float keyVerticalInterval;
|
|
|
|
public float keyHorizontalInterval;
|
2022-11-11 19:47:37 +01:00
|
|
|
public int labelBrightness; // 0 - 255
|
2021-04-20 00:34:21 +02:00
|
|
|
public boolean preciseRepeat;
|
2022-11-06 19:34:57 +01:00
|
|
|
public boolean double_tap_lock_shift;
|
2021-04-24 23:22:25 +02:00
|
|
|
public float characterSize; // Ratio
|
2021-05-09 00:09:10 +02:00
|
|
|
public int accents; // Values are R.values.pref_accents_v_*
|
2021-12-30 00:26:05 +01:00
|
|
|
public int theme; // Values are R.style.*
|
2022-07-24 23:44:37 +02:00
|
|
|
public boolean autocapitalisation;
|
2015-10-29 12:49:40 +01:00
|
|
|
|
2021-05-09 00:09:10 +02:00
|
|
|
// Dynamically set
|
2021-04-18 00:55:31 +02:00
|
|
|
public boolean shouldOfferSwitchingToNextInputMethod;
|
2022-11-13 00:24:23 +01:00
|
|
|
public boolean shouldOfferSwitchingToSecond;
|
2022-01-09 20:26:06 +01:00
|
|
|
public String actionLabel; // Might be 'null'
|
|
|
|
public int actionId; // Meaningful only when 'actionLabel' isn't 'null'
|
2022-01-10 00:27:22 +01:00
|
|
|
public boolean swapEnterActionKey; // Swap the "enter" and "action" keys
|
2022-09-19 11:41:18 +02:00
|
|
|
public Set<KeyValue> extra_keys_subtype;
|
|
|
|
public Set<KeyValue> extra_keys_param;
|
2021-04-18 00:55:31 +02:00
|
|
|
|
2021-12-28 17:47:18 +01:00
|
|
|
public final IKeyEventHandler handler;
|
|
|
|
|
2022-11-11 14:27:02 +01:00
|
|
|
private Config(SharedPreferences prefs, Resources res, IKeyEventHandler h)
|
2021-12-19 19:44:27 +01:00
|
|
|
{
|
2022-11-11 14:27:02 +01:00
|
|
|
_prefs = prefs;
|
2021-12-19 19:44:27 +01:00
|
|
|
// static values
|
|
|
|
marginTop = res.getDimension(R.dimen.margin_top);
|
|
|
|
keyPadding = res.getDimension(R.dimen.key_padding);
|
2022-06-06 16:17:43 +02:00
|
|
|
labelTextSize = 0.33f;
|
|
|
|
sublabelTextSize = 0.22f;
|
2021-12-19 19:44:27 +01:00
|
|
|
// default values
|
2021-04-28 00:23:52 +02:00
|
|
|
layout = -1;
|
2022-11-13 00:24:23 +01:00
|
|
|
second_layout = -1;
|
2022-06-24 22:00:23 +02:00
|
|
|
vibrateEnabled = true;
|
2021-12-19 19:44:27 +01:00
|
|
|
longPressTimeout = 600;
|
|
|
|
longPressInterval = 65;
|
|
|
|
marginBottom = res.getDimension(R.dimen.margin_bottom);
|
|
|
|
keyHeight = res.getDimension(R.dimen.key_height);
|
|
|
|
horizontalMargin = res.getDimension(R.dimen.horizontal_margin);
|
2022-01-30 23:55:15 +01:00
|
|
|
keyVerticalInterval = res.getDimension(R.dimen.key_vertical_interval);
|
|
|
|
keyHorizontalInterval = res.getDimension(R.dimen.key_horizontal_interval);
|
2021-04-20 00:34:21 +02:00
|
|
|
preciseRepeat = true;
|
2021-04-24 23:22:25 +02:00
|
|
|
characterSize = 1.f;
|
2021-05-09 12:14:56 +02:00
|
|
|
accents = 1;
|
2021-12-19 19:44:27 +01:00
|
|
|
// from prefs
|
2022-11-11 14:27:02 +01:00
|
|
|
refresh(res);
|
2021-04-18 00:55:31 +02:00
|
|
|
// initialized later
|
|
|
|
shouldOfferSwitchingToNextInputMethod = false;
|
2022-11-13 00:24:23 +01:00
|
|
|
shouldOfferSwitchingToSecond = false;
|
2022-01-09 20:26:06 +01:00
|
|
|
actionLabel = null;
|
|
|
|
actionId = 0;
|
2022-01-10 00:27:22 +01:00
|
|
|
swapEnterActionKey = false;
|
2022-09-19 11:41:18 +02:00
|
|
|
extra_keys_subtype = null;
|
2021-12-28 17:47:18 +01:00
|
|
|
handler = h;
|
2021-12-19 19:44:27 +01:00
|
|
|
}
|
2015-10-29 12:49:40 +01:00
|
|
|
|
2021-12-19 19:44:27 +01:00
|
|
|
/*
|
|
|
|
** Reload prefs
|
|
|
|
*/
|
2022-11-11 14:27:02 +01:00
|
|
|
public void refresh(Resources res)
|
2021-12-19 19:44:27 +01:00
|
|
|
{
|
2022-01-30 23:29:50 +01:00
|
|
|
DisplayMetrics dm = res.getDisplayMetrics();
|
2022-06-06 15:26:07 +02:00
|
|
|
// The height of the keyboard is relative to the height of the screen.
|
|
|
|
// This is the height of the keyboard if it have 4 rows.
|
2022-02-13 15:58:30 +01:00
|
|
|
int keyboardHeightPercent;
|
2022-06-06 16:17:43 +02:00
|
|
|
// Scale some dimensions depending on orientation
|
2022-06-06 15:40:32 +02:00
|
|
|
float horizontalIntervalScale = 1.f;
|
2022-06-06 16:17:43 +02:00
|
|
|
float characterSizeScale = 1.f;
|
2022-11-11 14:27:02 +01:00
|
|
|
String show_numpad_s = _prefs.getString("show_numpad", "never");
|
2022-09-25 02:23:33 +02:00
|
|
|
show_numpad = "always".equals(show_numpad_s);
|
2022-02-13 15:58:30 +01:00
|
|
|
if (res.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) // Landscape mode
|
|
|
|
{
|
2022-09-25 02:23:33 +02:00
|
|
|
if ("landscape".equals(show_numpad_s))
|
|
|
|
show_numpad = true;
|
2022-11-11 14:27:02 +01:00
|
|
|
keyboardHeightPercent = _prefs.getInt("keyboard_height_landscape", 50);
|
2022-06-06 15:40:32 +02:00
|
|
|
horizontalIntervalScale = 2.f;
|
2022-06-06 16:17:43 +02:00
|
|
|
characterSizeScale = 1.25f;
|
2022-02-13 15:58:30 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-11 14:27:02 +01:00
|
|
|
keyboardHeightPercent = _prefs.getInt("keyboard_height", 35);
|
2022-02-13 15:58:30 +01:00
|
|
|
}
|
2022-11-13 00:24:23 +01:00
|
|
|
layout = layoutId_of_string(_prefs.getString("layout", "none"));
|
|
|
|
second_layout = layoutId_of_string(_prefs.getString("second_layout", "none"));
|
2022-04-02 16:01:48 +02:00
|
|
|
// The swipe distance is defined relatively to the "exact physical pixels
|
|
|
|
// per inch of the screen", which isn't affected by the scaling settings.
|
|
|
|
// Take the mean of both dimensions as an approximation of the diagonal.
|
|
|
|
float physical_scaling = (dm.widthPixels + dm.heightPixels) / (dm.xdpi + dm.ydpi);
|
2022-11-11 14:27:02 +01:00
|
|
|
swipe_dist_px = Float.valueOf(_prefs.getString("swipe_dist", "15")) * physical_scaling;;
|
|
|
|
vibrateEnabled = _prefs.getBoolean("vibrate_enabled", vibrateEnabled);
|
|
|
|
longPressTimeout = _prefs.getInt("longpress_timeout", (int)longPressTimeout);
|
|
|
|
longPressInterval = _prefs.getInt("longpress_interval", (int)longPressInterval);
|
|
|
|
marginBottom = getDipPref(dm, _prefs, "margin_bottom", marginBottom);
|
|
|
|
keyVerticalInterval = getDipPref(dm, _prefs, "key_vertical_space", keyVerticalInterval);
|
2022-06-06 15:40:32 +02:00
|
|
|
keyHorizontalInterval =
|
2022-11-11 14:27:02 +01:00
|
|
|
getDipPref(dm, _prefs, "key_horizontal_space", keyHorizontalInterval)
|
2022-06-06 15:40:32 +02:00
|
|
|
* horizontalIntervalScale;
|
2022-11-11 19:47:37 +01:00
|
|
|
// Label brightness is used as the alpha channel
|
|
|
|
labelBrightness = _prefs.getInt("label_brightness", 100) * 255 / 100;
|
2022-02-13 15:58:30 +01:00
|
|
|
// Do not substract keyVerticalInterval from keyHeight because this is done
|
|
|
|
// during rendered.
|
|
|
|
keyHeight = dm.heightPixels * keyboardHeightPercent / 100 / 4;
|
2022-06-06 15:40:32 +02:00
|
|
|
horizontalMargin =
|
2022-11-11 14:27:02 +01:00
|
|
|
getDipPref(dm, _prefs, "horizontal_margin", horizontalMargin)
|
2022-06-06 15:40:32 +02:00
|
|
|
+ res.getDimension(R.dimen.extra_horizontal_margin);
|
2022-11-11 14:27:02 +01:00
|
|
|
preciseRepeat = _prefs.getBoolean("precise_repeat", preciseRepeat);
|
|
|
|
double_tap_lock_shift = _prefs.getBoolean("lock_double_tap", false);
|
2022-06-06 16:17:43 +02:00
|
|
|
characterSize =
|
2022-11-11 14:27:02 +01:00
|
|
|
_prefs.getFloat("character_size", characterSize)
|
2022-06-06 16:17:43 +02:00
|
|
|
* characterSizeScale;
|
2022-11-11 14:27:02 +01:00
|
|
|
accents = Integer.valueOf(_prefs.getString("accents", "1"));
|
|
|
|
theme = getThemeId(res, _prefs.getString("theme", ""));
|
|
|
|
autocapitalisation = _prefs.getBoolean("autocapitalisation", true);
|
|
|
|
extra_keys_param = ExtraKeyCheckBoxPreference.get_extra_keys(_prefs);
|
2021-12-19 19:44:27 +01:00
|
|
|
}
|
2015-10-29 12:49:40 +01:00
|
|
|
|
2022-03-05 18:15:36 +01:00
|
|
|
/** Update the layout according to the configuration.
|
|
|
|
* - Remove the switching key if it isn't needed
|
2022-05-29 12:27:46 +02:00
|
|
|
* - Remove "localized" keys from other locales (not in 'extra_keys')
|
2022-03-05 18:15:36 +01:00
|
|
|
* - Replace the action key to show the right label
|
|
|
|
* - Swap the enter and action keys
|
|
|
|
*/
|
2022-05-29 12:27:46 +02:00
|
|
|
public KeyboardData modify_layout(KeyboardData original_kw)
|
2022-03-05 18:15:36 +01:00
|
|
|
{
|
2022-03-06 19:36:09 +01:00
|
|
|
// Update the name to avoid caching in KeyModifier
|
2022-04-16 23:36:54 +02:00
|
|
|
final KeyValue action_key = (actionLabel == null) ? null :
|
2022-06-06 00:23:45 +02:00
|
|
|
KeyValue.getKeyByName("action").withSymbol(actionLabel);
|
2022-05-29 12:27:46 +02:00
|
|
|
// Extra keys are removed from the set as they are encountered during the
|
|
|
|
// first iteration then automatically added.
|
2022-09-19 11:41:18 +02:00
|
|
|
final Set<KeyValue> extra_keys = new HashSet<KeyValue>();
|
|
|
|
extra_keys.addAll(extra_keys_subtype);
|
|
|
|
extra_keys.addAll(extra_keys_param);
|
2022-05-29 12:27:46 +02:00
|
|
|
KeyboardData kw = original_kw.mapKeys(new KeyboardData.MapKeyValues() {
|
2022-06-24 20:26:27 +02:00
|
|
|
public KeyValue apply(KeyValue key, boolean localized)
|
2022-03-05 18:15:36 +01:00
|
|
|
{
|
2022-06-06 00:23:45 +02:00
|
|
|
boolean is_extra_key = extra_keys.contains(key);
|
2022-05-29 12:27:46 +02:00
|
|
|
if (is_extra_key)
|
2022-06-06 00:23:45 +02:00
|
|
|
extra_keys.remove(key);
|
2022-06-24 20:26:27 +02:00
|
|
|
if (localized && !is_extra_key)
|
2022-06-05 17:26:34 +02:00
|
|
|
return null;
|
|
|
|
switch (key.getKind())
|
2022-04-16 23:36:54 +02:00
|
|
|
{
|
2022-06-05 17:26:34 +02:00
|
|
|
case Event:
|
|
|
|
switch (key.getEvent())
|
2022-04-16 23:36:54 +02:00
|
|
|
{
|
2022-06-05 19:30:53 +02:00
|
|
|
case CHANGE_METHOD:
|
2022-06-05 17:26:34 +02:00
|
|
|
return shouldOfferSwitchingToNextInputMethod ? key : null;
|
2022-06-05 19:30:53 +02:00
|
|
|
case ACTION:
|
2022-06-05 17:26:34 +02:00
|
|
|
return (swapEnterActionKey && action_key != null) ?
|
|
|
|
KeyValue.getKeyByName("enter") : action_key;
|
2022-11-13 00:24:23 +01:00
|
|
|
case SWITCH_SECOND:
|
|
|
|
return shouldOfferSwitchingToSecond ? key : null;
|
2022-04-16 23:36:54 +02:00
|
|
|
}
|
2022-06-05 19:30:53 +02:00
|
|
|
break;
|
|
|
|
case Keyevent:
|
|
|
|
switch (key.getKeyevent())
|
|
|
|
{
|
|
|
|
case KeyEvent.KEYCODE_ENTER:
|
|
|
|
return (swapEnterActionKey && action_key != null) ? action_key : key;
|
|
|
|
}
|
2022-06-05 17:26:34 +02:00
|
|
|
break;
|
|
|
|
case Modifier:
|
2022-11-06 19:34:57 +01:00
|
|
|
switch (key.getModifier())
|
|
|
|
{
|
|
|
|
case SHIFT:
|
|
|
|
if (double_tap_lock_shift)
|
|
|
|
return key.withFlags(key.getFlags() | KeyValue.FLAG_LOCK);
|
|
|
|
}
|
2022-06-05 17:26:34 +02:00
|
|
|
break;
|
2022-04-16 23:36:54 +02:00
|
|
|
}
|
2022-06-05 17:26:34 +02:00
|
|
|
return key;
|
2022-04-16 23:36:54 +02:00
|
|
|
}
|
|
|
|
});
|
2022-05-29 12:27:46 +02:00
|
|
|
if (extra_keys.size() > 0)
|
2022-06-06 00:23:45 +02:00
|
|
|
kw = kw.addExtraKeys(extra_keys.iterator());
|
2022-09-25 02:23:33 +02:00
|
|
|
if (original_kw.num_pad && show_numpad)
|
|
|
|
kw = kw.addNumPad();
|
2022-05-29 12:27:46 +02:00
|
|
|
return kw;
|
2022-03-05 18:15:36 +01:00
|
|
|
}
|
|
|
|
|
2021-12-30 22:22:25 +01:00
|
|
|
private float getDipPref(DisplayMetrics dm, SharedPreferences prefs, String pref_name, float def)
|
2021-12-19 19:44:27 +01:00
|
|
|
{
|
2022-01-30 23:55:15 +01:00
|
|
|
float value;
|
|
|
|
try { value = prefs.getInt(pref_name, -1); }
|
|
|
|
catch (Exception e) { value = prefs.getFloat(pref_name, -1f); }
|
|
|
|
if (value < 0f)
|
2021-12-19 19:44:27 +01:00
|
|
|
return (def);
|
2021-12-28 16:47:19 +01:00
|
|
|
return (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, dm));
|
2021-12-19 19:44:27 +01:00
|
|
|
}
|
2021-04-28 00:23:52 +02:00
|
|
|
|
2022-01-30 23:29:50 +01:00
|
|
|
private int getThemeId(Resources res, String theme_name)
|
|
|
|
{
|
|
|
|
switch (theme_name)
|
|
|
|
{
|
|
|
|
case "light": return R.style.Light;
|
|
|
|
case "black": return R.style.Black;
|
|
|
|
case "dark": return R.style.Dark;
|
2022-09-24 13:13:51 +02:00
|
|
|
case "white": return R.style.White;
|
2022-01-30 23:29:50 +01:00
|
|
|
default:
|
|
|
|
case "system":
|
|
|
|
if (Build.VERSION.SDK_INT >= 8)
|
|
|
|
{
|
|
|
|
int night_mode = res.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
|
|
|
|
if ((night_mode & Configuration.UI_MODE_NIGHT_NO) != 0)
|
|
|
|
return R.style.Light;
|
|
|
|
}
|
|
|
|
return R.style.Dark;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-28 00:23:52 +02:00
|
|
|
public static int layoutId_of_string(String name)
|
|
|
|
{
|
|
|
|
switch (name)
|
|
|
|
{
|
2022-11-13 00:24:23 +01:00
|
|
|
case "system": case "none": return -1;
|
2021-04-28 00:23:52 +02:00
|
|
|
case "azerty": return R.xml.azerty;
|
2022-09-24 11:40:10 +02:00
|
|
|
case "bangla": return R.xml.bangla;
|
2022-01-29 19:27:33 +01:00
|
|
|
case "bgph1": return R.xml.local_bgph1;
|
2022-09-24 16:17:10 +02:00
|
|
|
case "bone": return R.xml.bone;
|
2022-06-04 15:23:52 +02:00
|
|
|
case "colemak": return R.xml.colemak;
|
2022-02-07 00:06:49 +01:00
|
|
|
case "dvorak": return R.xml.dvorak;
|
2022-11-01 21:24:54 +01:00
|
|
|
case "hindi": return R.xml.hindi;
|
|
|
|
case "jcuken_ua": return R.xml.jcuken_ua;
|
2022-05-29 17:31:12 +02:00
|
|
|
case "neo2": return R.xml.neo2;
|
2022-11-01 21:24:54 +01:00
|
|
|
case "qwerty": return R.xml.qwerty;
|
2022-10-15 23:22:19 +02:00
|
|
|
case "qwerty_el": return R.xml.qwerty_el;
|
2022-11-01 21:24:54 +01:00
|
|
|
case "qwerty_es": return R.xml.qwerty_es;
|
2022-05-29 17:31:12 +02:00
|
|
|
case "qwerty_hu": return R.xml.qwerty_hu;
|
2022-04-02 16:33:32 +02:00
|
|
|
case "qwerty_ko": return R.xml.qwerty_ko;
|
2022-03-14 22:43:19 +01:00
|
|
|
case "qwerty_lv": return R.xml.qwerty_lv;
|
2022-09-24 11:44:01 +02:00
|
|
|
case "qwerty_no": return R.xml.qwerty_no;
|
2022-11-01 21:24:54 +01:00
|
|
|
case "qwerty_pt": return R.xml.qwerty_pt;
|
2022-03-14 22:43:19 +01:00
|
|
|
case "qwerty_sv_se": return R.xml.qwerty_sv_se;
|
2022-11-01 21:24:54 +01:00
|
|
|
case "qwerty_tr": return R.xml.qwerty_tr;
|
2022-05-29 17:31:12 +02:00
|
|
|
case "qwertz": return R.xml.qwertz;
|
2022-11-01 21:24:54 +01:00
|
|
|
case "qwertz_cs": return R.xml.qwertz_cs;
|
|
|
|
case "qwertz_de": return R.xml.qwertz_de;
|
|
|
|
case "qwertz_hu": return R.xml.qwertz_hu;
|
2022-03-14 22:43:19 +01:00
|
|
|
case "ru_jcuken": return R.xml.local_ru_jcuken;
|
2022-04-06 09:43:46 +02:00
|
|
|
default: return R.xml.qwerty; // The config might store an invalid layout, don't crash
|
2021-04-28 00:23:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:47:19 +01:00
|
|
|
private static Config _globalConfig = null;
|
|
|
|
|
2022-11-11 14:27:02 +01:00
|
|
|
public static void initGlobalConfig(SharedPreferences prefs, Resources res,
|
|
|
|
IKeyEventHandler handler)
|
2021-12-28 16:47:19 +01:00
|
|
|
{
|
2022-11-11 14:27:02 +01:00
|
|
|
_globalConfig = new Config(prefs, res, handler);
|
2021-12-28 16:47:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Config globalConfig()
|
|
|
|
{
|
|
|
|
return _globalConfig;
|
|
|
|
}
|
2021-12-28 17:47:18 +01:00
|
|
|
|
|
|
|
public static interface IKeyEventHandler
|
|
|
|
{
|
2022-06-05 01:38:42 +02:00
|
|
|
public void handleKeyUp(KeyValue value, Pointers.Modifiers flags);
|
2021-12-28 17:47:18 +01:00
|
|
|
}
|
2015-10-29 12:49:40 +01:00
|
|
|
}
|