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;
|
2015-10-29 12:49:40 +01:00
|
|
|
import android.preference.PreferenceManager;
|
2021-12-28 16:47:19 +01:00
|
|
|
import android.util.DisplayMetrics;
|
2015-10-29 12:49:40 +01:00
|
|
|
import android.util.TypedValue;
|
|
|
|
|
2021-12-28 16:47:19 +01:00
|
|
|
final class Config
|
2015-10-29 12:49:40 +01:00
|
|
|
{
|
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
|
|
|
|
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
|
2021-12-30 22:22:25 +01:00
|
|
|
private float swipe_dist_dp;
|
|
|
|
public float swipe_dist_px;
|
2021-12-19 19:44:27 +01:00
|
|
|
public boolean vibrateEnabled;
|
|
|
|
public long vibrateDuration;
|
|
|
|
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;
|
2021-04-20 00:34:21 +02:00
|
|
|
public boolean preciseRepeat;
|
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.*
|
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-01-09 12:47:47 +01:00
|
|
|
public int key_flags_to_remove;
|
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
|
2021-04-18 00:55:31 +02:00
|
|
|
|
2021-12-28 17:47:18 +01:00
|
|
|
public final IKeyEventHandler handler;
|
|
|
|
|
|
|
|
private Config(Context context, IKeyEventHandler h)
|
2021-12-19 19:44:27 +01:00
|
|
|
{
|
|
|
|
Resources res = context.getResources();
|
|
|
|
// static values
|
|
|
|
marginTop = res.getDimension(R.dimen.margin_top);
|
|
|
|
keyPadding = res.getDimension(R.dimen.key_padding);
|
|
|
|
// default values
|
2021-04-28 00:23:52 +02:00
|
|
|
layout = -1;
|
2021-12-19 19:44:27 +01:00
|
|
|
vibrateEnabled = true;
|
|
|
|
vibrateDuration = 20;
|
|
|
|
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
|
2021-12-28 16:47:19 +01:00
|
|
|
refresh(context);
|
2021-04-18 00:55:31 +02:00
|
|
|
// initialized later
|
|
|
|
shouldOfferSwitchingToNextInputMethod = false;
|
2022-01-09 12:47:47 +01:00
|
|
|
key_flags_to_remove = 0;
|
2022-01-09 20:26:06 +01:00
|
|
|
actionLabel = null;
|
|
|
|
actionId = 0;
|
2022-01-10 00:27:22 +01:00
|
|
|
swapEnterActionKey = false;
|
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
|
|
|
|
*/
|
2021-12-28 16:47:19 +01:00
|
|
|
public void refresh(Context context)
|
2021-12-19 19:44:27 +01:00
|
|
|
{
|
2021-12-28 16:47:19 +01:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2022-01-30 23:29:50 +01:00
|
|
|
Resources res = context.getResources();
|
|
|
|
DisplayMetrics dm = res.getDisplayMetrics();
|
2022-01-15 10:13:41 +01:00
|
|
|
layout = layoutId_of_string(prefs.getString("layout", "system"));
|
2021-12-30 22:22:25 +01:00
|
|
|
swipe_dist_dp = Float.valueOf(prefs.getString("swipe_dist", "15"));
|
|
|
|
swipe_dist_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, swipe_dist_dp, dm);
|
2021-12-19 19:44:27 +01:00
|
|
|
vibrateEnabled = prefs.getBoolean("vibrate_enabled", vibrateEnabled);
|
|
|
|
vibrateDuration = prefs.getInt("vibrate_duration", (int)vibrateDuration);
|
|
|
|
longPressTimeout = prefs.getInt("longpress_timeout", (int)longPressTimeout);
|
|
|
|
longPressInterval = prefs.getInt("longpress_interval", (int)longPressInterval);
|
2021-12-30 22:22:25 +01:00
|
|
|
marginBottom = getDipPref(dm, prefs, "margin_bottom", marginBottom);
|
2022-01-30 23:55:15 +01:00
|
|
|
keyVerticalInterval = getDipPref(dm, prefs, "key_vertical_space", keyVerticalInterval);
|
|
|
|
keyHorizontalInterval = getDipPref(dm, prefs, "key_horizontal_space", keyHorizontalInterval);
|
2022-01-15 20:24:27 +01:00
|
|
|
// Add keyVerticalInterval to keyHeight because the space between the keys
|
|
|
|
// is removed from the keys during rendering
|
|
|
|
keyHeight = getDipPref(dm, prefs, "key_height", keyHeight) + keyVerticalInterval;
|
2021-12-30 22:22:25 +01:00
|
|
|
horizontalMargin = getDipPref(dm, prefs, "horizontal_margin", horizontalMargin);
|
2021-04-20 00:34:21 +02:00
|
|
|
preciseRepeat = prefs.getBoolean("precise_repeat", preciseRepeat);
|
2022-01-15 10:13:41 +01:00
|
|
|
characterSize = prefs.getFloat("character_size", characterSize);
|
2021-05-09 12:14:56 +02:00
|
|
|
accents = Integer.valueOf(prefs.getString("accents", "1"));
|
2022-01-30 23:29:50 +01:00
|
|
|
theme = getThemeId(res, prefs.getString("theme", ""));
|
2021-12-19 19:44:27 +01:00
|
|
|
}
|
2015-10-29 12:49:40 +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;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
case "azerty": return R.xml.azerty;
|
|
|
|
case "qwerty": return R.xml.qwerty;
|
2022-01-19 09:44:50 +01:00
|
|
|
case "qwerty_lv": return R.xml.qwerty_lv;
|
2022-01-15 20:56:08 +01:00
|
|
|
case "qwertz": return R.xml.qwertz;
|
2022-01-29 19:27:33 +01:00
|
|
|
case "bgph1": return R.xml.local_bgph1;
|
2022-02-07 00:06:49 +01:00
|
|
|
case "dvorak": return R.xml.dvorak;
|
2021-12-19 20:04:17 +01:00
|
|
|
case "system": default: return -1;
|
2021-04-28 00:23:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-09 00:09:10 +02:00
|
|
|
/* Used for the accents option. */
|
2022-01-09 12:47:47 +01:00
|
|
|
public static int extra_key_flag_of_name(String name)
|
2021-05-09 00:09:10 +02:00
|
|
|
{
|
|
|
|
switch (name)
|
|
|
|
{
|
|
|
|
case "aigu": return KeyValue.FLAG_ACCENT2;
|
2022-01-15 10:13:41 +01:00
|
|
|
case "caron": return KeyValue.FLAG_ACCENT_CARON;
|
2021-05-09 00:09:10 +02:00
|
|
|
case "cedille": return KeyValue.FLAG_ACCENT5;
|
2022-01-15 10:13:41 +01:00
|
|
|
case "circonflexe": return KeyValue.FLAG_ACCENT3;
|
|
|
|
case "grave": return KeyValue.FLAG_ACCENT1;
|
|
|
|
case "macron": return KeyValue.FLAG_ACCENT_MACRON;
|
2021-12-11 17:05:49 +01:00
|
|
|
case "ring": return KeyValue.FLAG_ACCENT_RING;
|
2022-01-09 12:49:28 +01:00
|
|
|
case "szlig": return KeyValue.FLAG_LANG_SZLIG;
|
2022-01-15 10:13:41 +01:00
|
|
|
case "tilde": return KeyValue.FLAG_ACCENT4;
|
|
|
|
case "trema": return KeyValue.FLAG_ACCENT6;
|
2021-05-09 00:09:10 +02:00
|
|
|
default: throw new RuntimeException(name);
|
|
|
|
}
|
|
|
|
}
|
2021-12-28 16:47:19 +01:00
|
|
|
|
2021-12-30 00:26:05 +01:00
|
|
|
public static int themeId_of_string(String name)
|
|
|
|
{
|
|
|
|
switch (name)
|
|
|
|
{
|
|
|
|
case "light": return R.style.Light;
|
2021-12-30 00:53:48 +01:00
|
|
|
case "black": return R.style.Black;
|
2021-12-30 00:26:05 +01:00
|
|
|
default: case "dark": return R.style.Dark;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:47:19 +01:00
|
|
|
private static Config _globalConfig = null;
|
|
|
|
|
2021-12-28 17:47:18 +01:00
|
|
|
public static void initGlobalConfig(Context context, IKeyEventHandler handler)
|
2021-12-28 16:47:19 +01:00
|
|
|
{
|
2021-12-28 17:47:18 +01:00
|
|
|
_globalConfig = new Config(context, 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
|
|
|
|
{
|
|
|
|
public void handleKeyUp(KeyValue value, int flags);
|
|
|
|
}
|
2015-10-29 12:49:40 +01:00
|
|
|
}
|