Unexpected-Keyboard/srcs/juloo.keyboard2/Config.java

162 lines
5.4 KiB
Java
Raw Normal View History

2015-10-29 12:49:40 +01:00
package juloo.keyboard2;
import android.content.Context;
2015-10-29 12:49:40 +01:00
import android.content.res.Resources;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
2015-10-29 12:49:40 +01:00
import android.util.TypedValue;
final class Config
2015-10-29 12:49:40 +01:00
{
// From resources
public final float marginTop;
public final float keyPadding;
public final float keyVerticalInterval;
public final float keyHorizontalInterval;
2015-10-29 12:49:40 +01:00
// From preferences
public int layout; // Or '-1' for the system defaults
private float swipe_dist_dp;
public float swipe_dist_px;
public boolean vibrateEnabled;
public long vibrateDuration;
public long longPressTimeout;
public long longPressInterval;
public float marginBottom;
public float keyHeight;
public float horizontalMargin;
2021-04-20 00:34:21 +02:00
public boolean preciseRepeat;
2021-04-24 23:22:25 +02:00
public float characterSize; // Ratio
public int accents; // Values are R.values.pref_accents_v_*
public int theme; // Values are R.style.*
2015-10-29 12:49:40 +01:00
// Dynamically set
public boolean shouldOfferSwitchingToNextInputMethod;
public int key_flags_to_remove;
public String actionLabel; // Might be 'null'
public int actionId; // Meaningful only when 'actionLabel' isn't 'null'
public boolean swapEnterActionKey; // Swap the "enter" and "action" keys
public final IKeyEventHandler handler;
private Config(Context context, IKeyEventHandler h)
{
Resources res = context.getResources();
// static values
marginTop = res.getDimension(R.dimen.margin_top);
keyPadding = res.getDimension(R.dimen.key_padding);
keyVerticalInterval = res.getDimension(R.dimen.key_vertical_interval);
keyHorizontalInterval = res.getDimension(R.dimen.key_horizontal_interval);
// default values
layout = -1;
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);
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;
// from prefs
refresh(context);
// initialized later
shouldOfferSwitchingToNextInputMethod = false;
key_flags_to_remove = 0;
actionLabel = null;
actionId = 0;
swapEnterActionKey = false;
handler = h;
}
2015-10-29 12:49:40 +01:00
/*
** Reload prefs
*/
public void refresh(Context context)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
DisplayMetrics dm = context.getResources().getDisplayMetrics();
layout = layoutId_of_string(prefs.getString("layout", "system"));
swipe_dist_dp = Float.valueOf(prefs.getString("swipe_dist", "15"));
swipe_dist_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, swipe_dist_dp, dm);
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);
marginBottom = getDipPref(dm, prefs, "margin_bottom", marginBottom);
// 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;
horizontalMargin = getDipPref(dm, prefs, "horizontal_margin", horizontalMargin);
2021-04-20 00:34:21 +02:00
preciseRepeat = prefs.getBoolean("precise_repeat", preciseRepeat);
2021-04-24 23:22:25 +02:00
characterSize = prefs.getFloat("character_size", characterSize);
2021-05-09 12:14:56 +02:00
accents = Integer.valueOf(prefs.getString("accents", "1"));
theme = themeId_of_string(prefs.getString("theme", ""));
}
2015-10-29 12:49:40 +01:00
private float getDipPref(DisplayMetrics dm, SharedPreferences prefs, String pref_name, float def)
{
int value = prefs.getInt(pref_name, -1);
if (value < 0)
return (def);
return (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, dm));
}
public static int layoutId_of_string(String name)
{
switch (name)
{
case "azerty": return R.xml.azerty;
case "qwerty": return R.xml.qwerty;
case "qwertz": return R.xml.qwertz;
case "system": default: return -1;
}
}
/* Used for the accents option. */
public static int extra_key_flag_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;
case "ring": return KeyValue.FLAG_ACCENT_RING;
case "szlig": return KeyValue.FLAG_LANG_SZLIG;
default: throw new RuntimeException(name);
}
}
public static int themeId_of_string(String name)
{
switch (name)
{
case "light": return R.style.Light;
case "black": return R.style.Black;
default: case "dark": return R.style.Dark;
}
}
private static Config _globalConfig = null;
public static void initGlobalConfig(Context context, IKeyEventHandler handler)
{
_globalConfig = new Config(context, handler);
}
public static Config globalConfig()
{
return _globalConfig;
}
public static interface IKeyEventHandler
{
public void handleKeyUp(KeyValue value, int flags);
}
2015-10-29 12:49:40 +01:00
}