diff --git a/srcs/juloo.keyboard2/Config.java b/srcs/juloo.keyboard2/Config.java index 6f4fdf9..e7a793a 100644 --- a/srcs/juloo.keyboard2/Config.java +++ b/srcs/juloo.keyboard2/Config.java @@ -1,14 +1,14 @@ package juloo.keyboard2; +import android.content.Context; import android.content.res.Resources; import android.content.SharedPreferences; import android.preference.PreferenceManager; +import android.util.DisplayMetrics; import android.util.TypedValue; -class Config +final class Config { - private Keyboard2 _context; - // From resources public final float marginTop; public final float keyPadding; @@ -34,11 +34,9 @@ class Config public boolean shouldOfferSwitchingToNextInputMethod; public int accent_flags_to_remove; - public Config(Keyboard2 context) + private Config(Context context) { Resources res = context.getResources(); - - _context = context; // static values marginTop = res.getDimension(R.dimen.margin_top); keyPadding = res.getDimension(R.dimen.key_padding); @@ -59,7 +57,7 @@ class Config characterSize = 1.f; accents = 1; // from prefs - refresh(); + refresh(context); // initialized later shouldOfferSwitchingToNextInputMethod = false; accent_flags_to_remove = 0; @@ -68,32 +66,30 @@ class Config /* ** Reload prefs */ - public void refresh() + public void refresh(Context context) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(_context); - + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + DisplayMetrics dm = context.getResources().getDisplayMetrics(); layout = layoutId_of_string(prefs.getString("layout", "system")); subValueDist = prefs.getFloat("sub_value_dist", subValueDist); 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(prefs, "margin_bottom", marginBottom); - keyHeight = getDipPref(prefs, "key_height", keyHeight); - horizontalMargin = getDipPref(prefs, "horizontal_margin", horizontalMargin); + marginBottom = getDipPref(dm, prefs, "margin_bottom", marginBottom); + keyHeight = getDipPref(dm, prefs, "key_height", keyHeight); + horizontalMargin = getDipPref(dm, prefs, "horizontal_margin", horizontalMargin); preciseRepeat = prefs.getBoolean("precise_repeat", preciseRepeat); characterSize = prefs.getFloat("character_size", characterSize); accents = Integer.valueOf(prefs.getString("accents", "1")); } - private float getDipPref(SharedPreferences prefs, String pref_name, float def) + 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, - _context.getResources().getDisplayMetrics())); + return (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, dm)); } public static int layoutId_of_string(String name) @@ -121,4 +117,16 @@ class Config default: throw new RuntimeException(name); } } + + private static Config _globalConfig = null; + + public static void initGlobalConfig(Context context) + { + _globalConfig = new Config(context); + } + + public static Config globalConfig() + { + return _globalConfig; + } } diff --git a/srcs/juloo.keyboard2/Keyboard2.java b/srcs/juloo.keyboard2/Keyboard2.java index b1bafbd..7a57def 100644 --- a/srcs/juloo.keyboard2/Keyboard2.java +++ b/srcs/juloo.keyboard2/Keyboard2.java @@ -54,16 +54,12 @@ public class Keyboard2 extends InputMethodService _specialKeyFont = Typeface.createFromAsset(getAssets(), "fonts/keys.ttf"); PreferenceManager.setDefaultValues(this, R.xml.settings, false); PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this); - _config = new Config(this); + Config.initGlobalConfig(this); + _config = Config.globalConfig(); _keyboardView = (Keyboard2View)getLayoutInflater().inflate(R.layout.keyboard, null); _keyboardView.reset(); } - public Config getConfig() - { - return (_config); - } - public Typeface getSpecialKeyFont() { return (_specialKeyFont); @@ -186,9 +182,9 @@ public class Keyboard2 extends InputMethodService @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - _config.refresh(); + _config.refresh(this); refreshSubtypeImm(); - _keyboardView.refreshConfig(_config, getLayout(_currentTextLayout)); + _keyboardView.refreshConfig(getLayout(_currentTextLayout)); } @Override diff --git a/srcs/juloo.keyboard2/Keyboard2View.java b/srcs/juloo.keyboard2/Keyboard2View.java index c85af4d..4430df4 100644 --- a/srcs/juloo.keyboard2/Keyboard2View.java +++ b/srcs/juloo.keyboard2/Keyboard2View.java @@ -45,14 +45,14 @@ public class Keyboard2View extends View _vibratorService = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE); _handler = new Handler(this); _theme = new Theme(getContext(), attrs); - refreshConfig(((Keyboard2)context).getConfig(), null); + _config = Config.globalConfig(); + refreshConfig(null); setOnTouchListener(this); } /* Internally calls [reset()]. */ - public void refreshConfig(Config config, KeyboardData kw) + public void refreshConfig(KeyboardData kw) { - _config = config; if (kw != null) setKeyboard(kw); // handle layout options then calls reset(). }