@@ -32,25 +32,24 @@ class MainKeyboardService : InputMethodService() {
3232 private lateinit var keyboardRoot: FrameLayout
3333 lateinit var mainKeyboardView: LinearLayout
3434 lateinit var phoneNumKeyboardView: LinearLayout
35- private lateinit var englishLayout: EnglishLayout
36- private lateinit var koreanLayout: KoreanLayout
37- private lateinit var specialKeyLayout: SpecialKeyLayout
38- private lateinit var phoneNumberLayout: PhoneNumberLayout
3935
40- private lateinit var languageLayouts: Array <LanguageLayout >
36+ private val languageMode = mapOf (" Special" to 0 , " English" to 1 , " Korean" to 2 )
37+ private lateinit var standardLayouts: Array <KeyboardLayout >
38+ private lateinit var phoneNumberLayout: PhoneNumberLayout
4139
4240 var rapidTextDeleteInterval: Long = 200 // in milliseconds
4341 val gestureMinDist = 120
4442
4543 private val numbers = arrayOf(" 1" , " 2" , " 3" , " 4" , " 5" , " 6" , " 7" , " 8" , " 9" , " 0" )
46- private val numBtnSubTexts = arrayOf(" !" , " @" , " #" , " $" , " %" , " ^" , " &" , " *" , " (" , " )" )
47- private val combinedNums = mutableListOf<SpannableString >()
48-
44+ val numBtnSubTexts = arrayOf(" !" , " @" , " #" , " $" , " %" , " ^" , " &" , " *" , " (" , " )" )
4945 val subTextLetterList = listOf (
50- arrayOf(" ! " , " ( " , " ) " , " \\ " , " |" , " [" , " ]" , " {" , " }" ),
46+ arrayOf(" " , " " , " " , " \\ " , " |" , " [" , " ]" , " {" , " }" ),
5147 arrayOf(" " , " " , " `" , " ~" , " -" , " _" , " :" , " ;" , " \" " , " '" ),
52- arrayOf(" < " , " > " , " = " , " + " , " * " , " /" , " ?" )
48+ arrayOf(" " , " < " , " > " , " = " , " + " , " /" , " ?" )
5349 )
50+ private val combinedNums = mutableListOf<SpannableString >()
51+ lateinit var numberRow: LinearLayout
52+
5453 var subtextColor = 0
5554
5655 private lateinit var numBtns: Array <Button >
@@ -70,6 +69,7 @@ class MainKeyboardService : InputMethodService() {
7069 private var returnKeyImageForward: VectorDrawableCompat ? = null
7170 private var returnKeyImageReturn: VectorDrawableCompat ? = null
7271 private var returnKeyImageTab: VectorDrawableCompat ? = null
72+
7373 var currReturnKeyImage: VectorDrawableCompat ? = null
7474
7575 private val searchIconActionList = arrayOf(
@@ -89,7 +89,7 @@ class MainKeyboardService : InputMethodService() {
8989 )
9090 var currIMEOptions = 0
9191
92- private var mode = 1
92+ private var mode = languageMode[ " English " ] !!
9393 private var lastDownSpacebarX = 0f
9494 private var lastCursorPos = 0
9595
@@ -106,6 +106,12 @@ class MainKeyboardService : InputMethodService() {
106106 mainKeyboardView = layoutInflater.inflate(R .layout.main_keyboardview, null ) as LinearLayout
107107 phoneNumKeyboardView = layoutInflater.inflate(R .layout.phone_number_keyboardview, null ) as LinearLayout
108108
109+ specialKeyBtn = mainKeyboardView.findViewById(R .id.special_key)
110+ commaBtn = mainKeyboardView.findViewById(R .id.comma)
111+ spacebarBtn = mainKeyboardView.findViewById(R .id.spacebar)
112+ fullStopBtn = mainKeyboardView.findViewById(R .id.full_stop)
113+ returnKeyBtn = mainKeyboardView.findViewById(R .id.return_key)
114+
109115 val vm = getSystemService(Context .VIBRATOR_MANAGER_SERVICE ) as VibratorManager
110116 vibrator = vm.defaultVibrator
111117
@@ -120,8 +126,13 @@ class MainKeyboardService : InputMethodService() {
120126 returnKeyImageForward = VectorDrawableCompat .create(resources, R .drawable.ic_outline_arrow_forward_24, null )
121127
122128 // number buttons
123- val numberRow = mainKeyboardView.findViewById<LinearLayout >(R .id.number_row)
124-
129+ numberRow = LinearLayout (mainKeyboardView.context)
130+ numberRow.layoutParams = LinearLayout .LayoutParams (
131+ LinearLayout .LayoutParams .MATCH_PARENT ,
132+ 0 ,
133+ 1f
134+ )
135+ numberRow.orientation = LinearLayout .HORIZONTAL
125136 numBtns = Array (numbers.size) { Button (ContextThemeWrapper (this , R .style.Theme_LetterBtn )) }
126137 numBtnPreviewPopupArr = Array (numbers.size) { PopupWindow (ContextThemeWrapper (this , R .style.Theme_TransparentBackground )) }
127138
@@ -171,7 +182,6 @@ class MainKeyboardService : InputMethodService() {
171182 }
172183
173184 // special key
174- specialKeyBtn = mainKeyboardView.findViewById(R .id.special_key)
175185 specialKeyBtn.setOnClickListener {
176186 vibrate()
177187 if (mode != 0 ) {
@@ -186,8 +196,6 @@ class MainKeyboardService : InputMethodService() {
186196 }
187197
188198 // spacebar
189- spacebarBtn = mainKeyboardView.findViewById(R .id.spacebar)
190-
191199 spacebarBtn.setOnTouchListener { btn, motionEvent ->
192200 val action = motionEvent.action
193201 if (action == MotionEvent .ACTION_DOWN ) {
@@ -224,23 +232,20 @@ class MainKeyboardService : InputMethodService() {
224232 }
225233
226234 // comma
227- commaBtn = mainKeyboardView.findViewById(R .id.comma)
228235 commaBtn.setTextSize(TypedValue .COMPLEX_UNIT_SP , resources.getFloat(R .dimen.default_text_size))
229236 commaBtn.setOnClickListener {
230237 vibrate()
231238 resetAndFinishComposing()
232239 currentInputConnection.commitText(" ," , 1 )
233240 }
234241 // full stop
235- fullStopBtn = mainKeyboardView.findViewById(R .id.full_stop)
236242 fullStopBtn.setTextSize(TypedValue .COMPLEX_UNIT_SP , resources.getFloat(R .dimen.default_text_size))
237243 fullStopBtn.setOnClickListener {
238244 vibrate()
239245 resetAndFinishComposing()
240246 currentInputConnection.commitText(" ." , 1 )
241247 }
242248 // return key
243- returnKeyBtn = mainKeyboardView.findViewById(R .id.return_key)
244249 returnKeyBtn.setOnClickListener {
245250 vibrate()
246251 resetAndFinishComposing()
@@ -252,17 +257,13 @@ class MainKeyboardService : InputMethodService() {
252257 }
253258 }
254259
255- englishLayout = EnglishLayout (this )
256- englishLayout.init ()
257- koreanLayout = KoreanLayout (this )
258- koreanLayout.init ()
259- specialKeyLayout = SpecialKeyLayout (this )
260- specialKeyLayout.init ()
260+ standardLayouts = arrayOf(SpecialKeyLayout (this ), EnglishLayout (this ), KoreanLayout (this ))
261+ for (layout in standardLayouts) {
262+ layout.init ()
263+ }
261264 phoneNumberLayout = PhoneNumberLayout (this )
262265 phoneNumberLayout.init ()
263266
264- languageLayouts = arrayOf(englishLayout, koreanLayout)
265-
266267 // init layout
267268 changeLayout()
268269 }
@@ -292,7 +293,6 @@ class MainKeyboardService : InputMethodService() {
292293 if ((currIMEOptions and EditorInfo .IME_FLAG_NO_ENTER_ACTION ) == EditorInfo .IME_FLAG_NO_ENTER_ACTION ) {
293294 currReturnKeyImage = returnKeyImageReturn
294295 }
295-
296296 returnKeyBtn.setImageDrawable(currReturnKeyImage)
297297
298298 keyboardRoot = if (mainKeyboardView.parent != null ) {
@@ -301,7 +301,7 @@ class MainKeyboardService : InputMethodService() {
301301 phoneNumKeyboardView.parent as FrameLayout
302302 }
303303 // if inputType is phone number
304- if (editorInfo?.inputType?.and (InputType .TYPE_MASK_CLASS ) == InputType .TYPE_CLASS_PHONE ) {
304+ if (editorInfo?.inputType?.and (InputType .TYPE_MASK_CLASS ) == ( InputType .TYPE_CLASS_PHONE or InputType . TYPE_CLASS_NUMBER ) ) {
305305 if (keyboardRoot.children.contains(mainKeyboardView)) {
306306 keyboardRoot.removeView(mainKeyboardView)
307307 phoneNumberLayout.updateReturnKeyImage()
@@ -322,7 +322,7 @@ class MainKeyboardService : InputMethodService() {
322322
323323 override fun onUpdateCursorAnchorInfo (cursorAnchorInfo : CursorAnchorInfo ) {
324324 val currPos = cursorAnchorInfo.selectionStart
325- if ((currPos != lastCursorPos + 1 || ! koreanLayout .hangulAssembler.cursorMovedBySystem) && currPos != lastCursorPos) {
325+ if ((currPos != lastCursorPos + 1 || ! (standardLayouts[languageMode[ " Korean " ] !! ] as KoreanLayout ) .hangulAssembler.cursorMovedBySystem) && currPos != lastCursorPos) {
326326 resetAndFinishComposing()
327327 }
328328 lastCursorPos = currPos
@@ -364,9 +364,8 @@ class MainKeyboardService : InputMethodService() {
364364 }
365365 numBtns[i].text = combinedNums[i]
366366 }
367- for (layout in languageLayouts) {
368- layout.updateSubtextColor()
369- }
367+ (standardLayouts[languageMode[" English" ]!! ] as LanguageLayout ).updateSubtextColor()
368+ (standardLayouts[languageMode[" Korean" ]!! ] as LanguageLayout ).updateSubtextColor()
370369 }
371370
372371 fun deleteByWord (direction : Int ): Boolean {
@@ -426,34 +425,32 @@ class MainKeyboardService : InputMethodService() {
426425
427426 private fun changeLayout () {
428427 resetAndFinishComposing()
429- // delete middle rows
430- if (mainKeyboardView.childCount > 2 ) {
431- for (i in 0 until mainKeyboardView.childCount- 2 ) {
432- mainKeyboardView.removeViewAt(1 )
428+ if (mainKeyboardView.childCount > 1 ) { // check if this is initial layout insert call
429+ // delete remove every row except control row
430+ for (i in 0 until mainKeyboardView.childCount- 1 ) {
431+ mainKeyboardView.removeViewAt(0 )
433432 }
434433 }
434+ standardLayouts[mode].insertLayout()
435435 when (mode) {
436436 // special key layout
437- 0 -> {
438- specialKeyLayout.insertLetterBtns()
437+ languageMode[" Special" ]!! -> {
439438 spacebarBtn.text = getString(R .string.spacebar_text_special_key)
440439 }
441440 // english layout
442- 1 -> {
443- englishLayout.insertLetterBtns()
441+ languageMode[" English" ]!! -> {
444442 spacebarBtn.text = getString(R .string.spacebar_text_english)
445443 }
446444 // korean layout
447- 2 -> {
448- koreanLayout.insertLetterBtns()
445+ languageMode[" Korean" ]!! -> {
449446 spacebarBtn.text = getString(R .string.spacebar_text_korean)
450447 }
451448 }
452449 }
453450
454451 fun resetAndFinishComposing () {
455- if (mode == 2 ) {
456- koreanLayout .hangulAssembler.reset()
452+ if (mode == languageMode[ " Korean " ] ) {
453+ (standardLayouts[languageMode[ " Korean " ] !! ] as KoreanLayout ) .hangulAssembler.reset()
457454 }
458455 if (currentInputConnection != null ) {
459456 currentInputConnection.finishComposingText()
0 commit comments