From 153c3849903c3d8489c3af17581d3d1073abb935 Mon Sep 17 00:00:00 2001 From: juloo Date: Sat, 8 Aug 2015 16:47:22 +0200 Subject: [PATCH] Start SettingsActivity --- AndroidManifest.xml | 8 ++++ res/values/strings.xml | 15 ++++++ res/xml/method.xml | 1 + res/xml/settings.xml | 23 +++++++++ srcs/juloo.keyboard2/Keyboard2.java | 40 ++++------------ srcs/juloo.keyboard2/Keyboard2View.java | 55 +++++++++++++++------- srcs/juloo.keyboard2/SettingsActivity.java | 14 ++++++ 7 files changed, 108 insertions(+), 48 deletions(-) create mode 100644 res/xml/settings.xml create mode 100644 srcs/juloo.keyboard2/SettingsActivity.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 5dde156..15b7d34 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -23,6 +23,14 @@ android:resource="@xml/method" /> + + + + + + diff --git a/res/values/strings.xml b/res/values/strings.xml index 9397c8b..e4629be 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -1,4 +1,19 @@ Keyboard 2.0 + + Keyboard 2.0 Settings + + Layout + + Typing + + Vibration + Vibration + Enable/Disable vibrations + Duration + Change vibrations duration + + Other + diff --git a/res/xml/method.xml b/res/xml/method.xml index 7b523f9..63887e3 100644 --- a/res/xml/method.xml +++ b/res/xml/method.xml @@ -1,4 +1,5 @@ diff --git a/res/xml/settings.xml b/res/xml/settings.xml new file mode 100644 index 0000000..e92fb2b --- /dev/null +++ b/res/xml/settings.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + diff --git a/srcs/juloo.keyboard2/Keyboard2.java b/srcs/juloo.keyboard2/Keyboard2.java index 73cdee7..9fb456a 100644 --- a/srcs/juloo.keyboard2/Keyboard2.java +++ b/srcs/juloo.keyboard2/Keyboard2.java @@ -1,35 +1,35 @@ package juloo.keyboard2; +import android.content.SharedPreferences; import android.inputmethodservice.InputMethodService; -import android.util.Log; +import android.preference.PreferenceManager; import android.view.KeyEvent; import android.view.View; public class Keyboard2 extends InputMethodService + implements SharedPreferences.OnSharedPreferenceChangeListener { - private KeyboardData _keyboardData; // TODO: settings private Keyboard2View _inputView = null; @Override public void onCreate() { super.onCreate(); - setKeyboard(R.xml.azerty); + PreferenceManager.setDefaultValues(this, R.xml.settings, false); + _inputView = (Keyboard2View)getLayoutInflater().inflate(R.layout.input, null); + _inputView.reset_prefs(this); } @Override public View onCreateInputView() { - _inputView = (Keyboard2View)getLayoutInflater().inflate(R.layout.input, null); - _inputView.setKeyboard(this, _keyboardData); + _inputView.reset(); return (_inputView); } - private void setKeyboard(int res) + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - _keyboardData = new KeyboardData(getResources().getXml(res)); - if (_inputView != null) - _inputView.setKeyboard(this, _keyboardData); + _inputView.reset_prefs(this); } public void handleKeyUp(KeyValue key, int flags) @@ -38,27 +38,7 @@ public class Keyboard2 extends InputMethodService return ; if (key.getEventCode() == KeyValue.EVENT_CONFIG) { - // TODO improve this shit - final CharSequence layouts[] = new CharSequence[]{"Azerty", "Qwerty"}; - final int layout_res[] = new int[]{R.xml.azerty, R.xml.qwerty}; - final Keyboard2 self = this; - android.app.AlertDialog dialog = new android.app.AlertDialog.Builder(this) - .setTitle("Change keyboard layout") - .setItems(layouts, new android.content.DialogInterface.OnClickListener() - { - @Override - public void onClick(android.content.DialogInterface dialog, int which) - { - self.setKeyboard(layout_res[which]); - } - }) - .create(); - android.view.WindowManager.LayoutParams lp = dialog.getWindow().getAttributes(); - lp.token = _inputView.getWindowToken(); - lp.type = android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG; - dialog.getWindow().setAttributes(lp); - dialog.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); - dialog.show(); + // TODO: go to settings activity } else if ((flags & (KeyValue.FLAG_CTRL | KeyValue.FLAG_ALT)) != 0) handleMetaKeyUp(key, flags); diff --git a/srcs/juloo.keyboard2/Keyboard2View.java b/srcs/juloo.keyboard2/Keyboard2View.java index 1b455af..4e41303 100644 --- a/srcs/juloo.keyboard2/Keyboard2View.java +++ b/srcs/juloo.keyboard2/Keyboard2View.java @@ -1,9 +1,11 @@ package juloo.keyboard2; import android.content.Context; +import android.content.SharedPreferences; import android.graphics.Canvas; import android.graphics.RectF; import android.graphics.Paint; +import android.preference.PreferenceManager; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.os.Handler; @@ -18,15 +20,8 @@ public class Keyboard2View extends View { private static final float KEY_PER_ROW = 10; - private static final float SUB_VALUE_DIST = 7f; // TODO: settings - - private static final boolean VIBRATE_ENABLED = true; // TODO: settings - private static final long VIBRATE_DURATION = 20; // TODO: settings private static final long VIBRATE_MIN_INTERVAL = 100; - private static final long LONGPRESS_TIMEOUT = 600; // TODO: settings - private static final long LONGPRESS_INTERVAL = 65; // TODO: settings - private Keyboard2 _ime; private KeyboardData _keyboard; @@ -42,13 +37,19 @@ public class Keyboard2View extends View private float _horizontalMargin; private float _marginTop; - private float _marginBottom; // TODO: settings private float _keyWidth; - private float _keyHeight; // TODO: settings + private float _keyHeight; private float _keyPadding; private float _keyBgPadding; private float _keyRound; + private float _subValueDist = 7f; + private boolean _vibrateEnabled = true; + private long _vibrateDuration = 20; + private long _longPressTimeout = 600; + private long _longPressInterval = 65; + private float _marginBottom; + private Paint _keyBgPaint = new Paint(); private Paint _keyDownBgPaint = new Paint(); private Paint _keyLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG); @@ -78,13 +79,31 @@ public class Keyboard2View extends View _keySubLabelPaint.setColor(getResources().getColor(R.color.key_sub_label)); _keySubLabelPaint.setTextSize(getResources().getDimension(R.dimen.sublabel_text_size)); setOnTouchListener(this); - requestLayout(); } - public void setKeyboard(Keyboard2 ime, KeyboardData keyboardData) + public void reset_prefs(Keyboard2 ime) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ime); + _ime = ime; - _keyboard = keyboardData; + _subValueDist = prefs.getFloat("sub_value_dist", _subValueDist); + _vibrateEnabled = prefs.getBoolean("vibrate_enabled", _vibrateEnabled); + _vibrateDuration = prefs.getLong("vibrate_duration", _vibrateDuration); + _longPressTimeout = prefs.getLong("longpress_timeout", _longPressTimeout); + _longPressInterval = prefs.getLong("longpress_interval", _longPressInterval); + _marginBottom = prefs.getFloat("margin_bottom", _marginBottom); + + String keyboardLayout = prefs.getString("keyboard_layout", "azerty"); + + if (keyboardLayout.equals("azerty")) // TODO change this + _keyboard = new KeyboardData(ime.getResources().getXml(R.xml.azerty)); + else + _keyboard = new KeyboardData(ime.getResources().getXml(R.xml.qwerty)); + } + + public void reset() + { + _flags = 0; requestLayout(); invalidate(); } @@ -147,7 +166,7 @@ public class Keyboard2View extends View { moveX -= key.downX; moveY -= key.downY; - if ((Math.abs(moveX) + Math.abs(moveY)) < SUB_VALUE_DIST) + if ((Math.abs(moveX) + Math.abs(moveY)) < _subValueDist) newValue = key.key.key0; else if (moveX < 0) newValue = (moveY < 0) ? key.key.key1 : key.key.key3; @@ -160,7 +179,7 @@ public class Keyboard2View extends View if (key.timeoutWhat != -1) { _handler.removeMessages(key.timeoutWhat); - _handler.sendEmptyMessageDelayed(key.timeoutWhat, LONGPRESS_TIMEOUT); + _handler.sendEmptyMessageDelayed(key.timeoutWhat, _longPressTimeout); } key.value = newValue; key.flags = newValue.getFlags(); @@ -202,7 +221,7 @@ public class Keyboard2View extends View else { int what = _currentWhat++; - _handler.sendEmptyMessageDelayed(what, LONGPRESS_TIMEOUT); + _handler.sendEmptyMessageDelayed(what, _longPressTimeout); _downKeys.add(new KeyDown(pointerId, key, touchX, touchY, what)); } vibrate(); @@ -258,7 +277,7 @@ public class Keyboard2View extends View private void vibrate() { - if (!VIBRATE_ENABLED) + if (!_vibrateEnabled) return ; long now = System.currentTimeMillis(); if ((now - _lastVibration) > VIBRATE_MIN_INTERVAL) @@ -266,7 +285,7 @@ public class Keyboard2View extends View _lastVibration = now; try { - _vibratorService.vibrate(VIBRATE_DURATION); + _vibratorService.vibrate(_vibrateDuration); } catch (Exception e) { @@ -284,7 +303,7 @@ public class Keyboard2View extends View { if (key.timeoutWhat == msg.what) { - _handler.sendEmptyMessageDelayed(msg.what, LONGPRESS_INTERVAL); + _handler.sendEmptyMessageDelayed(msg.what, _longPressInterval); _ime.handleKeyUp(key.value, _flags); vibrate(); return (true); diff --git a/srcs/juloo.keyboard2/SettingsActivity.java b/srcs/juloo.keyboard2/SettingsActivity.java new file mode 100644 index 0000000..e9e49c3 --- /dev/null +++ b/srcs/juloo.keyboard2/SettingsActivity.java @@ -0,0 +1,14 @@ +package juloo.keyboard2; + +import android.os.Bundle; +import android.preference.PreferenceActivity; + +public class SettingsActivity extends PreferenceActivity +{ + @Override + public void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.settings); + } +}