@@ -5,11 +5,12 @@ import android.os.VibrationEffect
55import android.os.Vibrator
66import android.text.InputType
77import android.text.TextUtils
8+ import android.util.Log
89import android.view.InputDevice
910import android.view.KeyCharacterMap
1011import android.view.KeyEvent
11- import android.view.View
1212import android.view.inputmethod.EditorInfo
13+ import androidx.preference.PreferenceManager
1314import java.util.Locale
1415import android.inputmethodservice.InputMethodService as AndroidInputMethodService
1516
@@ -39,25 +40,70 @@ fun makeKeyEvent(original: KeyEvent, code: Int, metaState: Int, action: Int, sou
3940 return KeyEvent (original.downTime, original.eventTime, action, code, original.repeatCount, metaState, KeyCharacterMap .VIRTUAL_KEYBOARD , code, 0 , source)
4041}
4142
43+ val templates = hashMapOf(
44+ " fr" to hashMapOf(
45+ KeyEvent .KEYCODE_A to arrayOf(' `' , ' ^' , ' ´' , ' ¨' , ' ~' , MPSUBST_BYPASS ),
46+ KeyEvent .KEYCODE_E to arrayOf(' ´' , ' `' , ' ^' , ' ¨' , MPSUBST_BYPASS ),
47+ KeyEvent .KEYCODE_I to arrayOf(' ^' , ' ´' , ' ¨' , ' `' , MPSUBST_BYPASS ),
48+ KeyEvent .KEYCODE_O to arrayOf(' ^' , ' ´' , ' `' , ' ¨' , ' ~' , MPSUBST_BYPASS ),
49+ KeyEvent .KEYCODE_U to arrayOf(' `' , ' ^' , ' ´' , ' ¨' , MPSUBST_BYPASS ),
50+ KeyEvent .KEYCODE_C to arrayOf(' ç' , MPSUBST_BYPASS ),
51+ KeyEvent .KEYCODE_SPACE to arrayOf(MPSUBST_STR_DOTSPACE )
52+ ),
53+ " es" to hashMapOf(
54+ KeyEvent .KEYCODE_A to arrayOf(' ´' , MPSUBST_BYPASS ),
55+ KeyEvent .KEYCODE_E to arrayOf(' ´' , MPSUBST_BYPASS ),
56+ KeyEvent .KEYCODE_I to arrayOf(' ´' , MPSUBST_BYPASS ),
57+ KeyEvent .KEYCODE_O to arrayOf(' ´' , MPSUBST_BYPASS ),
58+ KeyEvent .KEYCODE_U to arrayOf(' ´' , MPSUBST_BYPASS ),
59+ KeyEvent .KEYCODE_SPACE to arrayOf(MPSUBST_STR_DOTSPACE )
60+ ),
61+ " de" to hashMapOf(
62+ KeyEvent .KEYCODE_A to arrayOf(' ¨' , MPSUBST_BYPASS ),
63+ KeyEvent .KEYCODE_O to arrayOf(' ¨' , MPSUBST_BYPASS ),
64+ KeyEvent .KEYCODE_U to arrayOf(' ¨' , MPSUBST_BYPASS ),
65+ KeyEvent .KEYCODE_SPACE to arrayOf(MPSUBST_STR_DOTSPACE )
66+ ),
67+ " pt" to hashMapOf(
68+ KeyEvent .KEYCODE_A to arrayOf(' ´' , ' ^' , ' `' , ' ~' , MPSUBST_BYPASS ),
69+ KeyEvent .KEYCODE_E to arrayOf(' ´' , ' ^' , MPSUBST_BYPASS ),
70+ KeyEvent .KEYCODE_I to arrayOf(' ´' , MPSUBST_BYPASS ),
71+ KeyEvent .KEYCODE_O to arrayOf(' ´' , ' ^' , ' ~' , MPSUBST_BYPASS ),
72+ KeyEvent .KEYCODE_U to arrayOf(' ´' , MPSUBST_BYPASS ),
73+ KeyEvent .KEYCODE_C to arrayOf(' ç' , MPSUBST_BYPASS ),
74+ KeyEvent .KEYCODE_SPACE to arrayOf(MPSUBST_STR_DOTSPACE )
75+ ),
76+ " order1" to hashMapOf( // áàâäã
77+ KeyEvent .KEYCODE_A to arrayOf(' ´' , ' `' , ' ^' , ' ¨' , ' ~' , MPSUBST_BYPASS ),
78+ KeyEvent .KEYCODE_E to arrayOf(' ´' , ' `' , ' ^' , ' ¨' , ' ~' , MPSUBST_BYPASS ),
79+ KeyEvent .KEYCODE_I to arrayOf(' ´' , ' `' , ' ^' , ' ¨' , ' ~' , MPSUBST_BYPASS ),
80+ KeyEvent .KEYCODE_O to arrayOf(' ´' , ' `' , ' ^' , ' ¨' , ' ~' , MPSUBST_BYPASS ),
81+ KeyEvent .KEYCODE_U to arrayOf(' ´' , ' `' , ' ^' , ' ¨' , ' ~' , MPSUBST_BYPASS ),
82+ KeyEvent .KEYCODE_SPACE to arrayOf(MPSUBST_STR_DOTSPACE )
83+ ),
84+ " order2" to hashMapOf( // àáâäã
85+ KeyEvent .KEYCODE_A to arrayOf(' `' , ' ´' , ' ^' , ' ¨' , ' ~' , MPSUBST_BYPASS ),
86+ KeyEvent .KEYCODE_E to arrayOf(' `' , ' ´' , ' ^' , ' ¨' , ' ~' , MPSUBST_BYPASS ),
87+ KeyEvent .KEYCODE_I to arrayOf(' `' , ' ´' , ' ^' , ' ¨' , ' ~' , MPSUBST_BYPASS ),
88+ KeyEvent .KEYCODE_O to arrayOf(' `' , ' ´' , ' ^' , ' ¨' , ' ~' , MPSUBST_BYPASS ),
89+ KeyEvent .KEYCODE_U to arrayOf(' `' , ' ´' , ' ^' , ' ¨' , ' ~' , MPSUBST_BYPASS ),
90+ KeyEvent .KEYCODE_SPACE to arrayOf(MPSUBST_STR_DOTSPACE )
91+ )
92+ )
93+
4294class InputMethodService : AndroidInputMethodService () {
4395 private lateinit var vibrator: Vibrator
44- private lateinit var inputView: View
4596 private val shift = Modifier ()
4697 private val alt = Modifier ()
4798 private val sym = SimpleModifier ()
4899 private var lastShift = false
49100 private var lastAlt = false
50101 private var lastSym = false
102+
103+ private var autoCapitalize = false
104+
51105 private val multipress = MultipressController (arrayOf(
52- hashMapOf(
53- KeyEvent .KEYCODE_A to arrayOf(' `' , ' ^' , ' ´' , ' ¨' , ' ~' , MPSUBST_BYPASS ),
54- KeyEvent .KEYCODE_E to arrayOf(' ´' , ' `' , ' ^' , ' ¨' , MPSUBST_BYPASS ),
55- KeyEvent .KEYCODE_I to arrayOf(' ^' , ' ´' , ' ¨' , ' `' , MPSUBST_BYPASS ),
56- KeyEvent .KEYCODE_O to arrayOf(' ^' , ' ´' , ' `' , ' ¨' , ' ~' , MPSUBST_BYPASS ),
57- KeyEvent .KEYCODE_U to arrayOf(' `' , ' ^' , ' ´' , ' ¨' , MPSUBST_BYPASS ),
58- KeyEvent .KEYCODE_C to arrayOf(' ç' , MPSUBST_BYPASS ),
59- KeyEvent .KEYCODE_SPACE to arrayOf(MPSUBST_STR_DOTSPACE )
60- ),
106+ templates[" fr" ]!! ,
61107 hashMapOf(
62108 KeyEvent .KEYCODE_Q to arrayOf(MPSUBST_TOGGLE_ALT , ' °' , MPSUBST_TOGGLE_SHIFT , MPSUBST_BYPASS ),
63109 KeyEvent .KEYCODE_W to arrayOf(MPSUBST_TOGGLE_ALT , ' &' , ' ↑' , MPSUBST_TOGGLE_SHIFT , MPSUBST_BYPASS ),
@@ -92,11 +138,19 @@ class InputMethodService : AndroidInputMethodService() {
92138 override fun onCreate () {
93139 super .onCreate()
94140 vibrator = getSystemService(Context .VIBRATOR_SERVICE ) as Vibrator
141+
142+ val preferences = PreferenceManager .getDefaultSharedPreferences(this )
143+ preferences.registerOnSharedPreferenceChangeListener { preferences, key ->
144+ updateFromPreferences()
145+ }
146+ updateFromPreferences()
95147 }
96148
97149 override fun onStartInput (attribute : EditorInfo ? , restarting : Boolean ) {
98150 super .onStartInput(attribute, restarting)
99151
152+ updateFromPreferences()
153+
100154 if (! sym.get()) {
101155 updateAutoCapitalization()
102156 }
@@ -233,16 +287,6 @@ class InputMethodService : AndroidInputMethodService() {
233287 */
234288 private fun onSymKey (event : KeyEvent , pressed : Boolean ): Boolean {
235289
236- // Some keys have special handling
237- // when(event.keyCode) {
238- // in arrayOf(KeyEvent.KEYCODE_ENTER, KeyEvent.KEYCODE_DEL, KeyEvent.KEYCODE_CTRL_LEFT, KeyEvent.KEYCODE_CTRL_RIGHT, KeyEvent.KEYCODE_BACK) -> {
239- // if(pressed) {
240- // super.onKeyDown(event.keyCode, event)
241- // } else {
242- // super.onKeyUp(event.keyCode, event)
243- // }
244- // }
245- // }
246290 // The SPACE key is equivalent to hitting Shift
247291 if (event.keyCode == KeyEvent .KEYCODE_SPACE ) {
248292 if (pressed) {
@@ -388,6 +432,9 @@ class InputMethodService : AndroidInputMethodService() {
388432 * Update the Shift modifier state for auto-capitalization.
389433 */
390434 private fun updateAutoCapitalization () {
435+ if (! autoCapitalize) {
436+ return
437+ }
391438 if (currentInputEditorInfo == null || currentInputConnection == null ) {
392439 return
393440 }
@@ -433,4 +480,31 @@ class InputMethodService : AndroidInputMethodService() {
433480 updateAutoCapitalization()
434481 }
435482 }
483+
484+ /* *
485+ * Update values from the preferences.
486+ */
487+ private fun updateFromPreferences () {
488+ val preferences = PreferenceManager .getDefaultSharedPreferences(this )
489+
490+ autoCapitalize = preferences.getBoolean(" AutoCapitalize" , true )
491+
492+ val lockThreshold = preferences.getInt(" ModifierLockThreshold" , 250 )
493+ shift.lockThreshold = lockThreshold
494+ alt.lockThreshold = lockThreshold
495+ sym.lockThreshold = lockThreshold
496+
497+ val nextThreshold = preferences.getInt(" ModifierNextThreshold" , 350 )
498+ shift.nextThreshold = nextThreshold
499+ alt.nextThreshold = nextThreshold
500+
501+ multipress.multipressThreshold = preferences.getInt(" MultipressThreshold" , 750 )
502+ multipress.ignoreFirstLevel = ! preferences.getBoolean(" UseFirstLevel" , true )
503+ multipress.ignoreDotSpace = ! preferences.getBoolean(" DotSpace" , true )
504+
505+ val templateId = preferences.getString(" FirstLevelTemplate" , " fr" )
506+ if (templates.containsKey(templateId)) {
507+ multipress.substitutions[0 ] = templates[templateId]!!
508+ }
509+ }
436510}
0 commit comments