From 87d972f3a4b03624b78a4acc519f7dc1655c3336 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 16 Jan 2022 13:51:48 +0100 Subject: [PATCH] show different Enter icon depending on the circumstances --- app/build.gradle | 2 +- .../keyboard/helpers/MyKeyboard.kt | 20 ++++++++++++++++--- .../keyboard/services/SimpleKeyboardIME.kt | 16 +++++++++++---- .../keyboard/views/MyKeyboardView.kt | 4 ++-- build.gradle | 4 ++-- 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 5a13b1a..87ef249 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -42,5 +42,5 @@ android { } dependencies { - implementation 'com.github.SimpleMobileTools:Simple-Commons:02b48c0b4d' + implementation 'com.github.SimpleMobileTools:Simple-Commons:d824a8a174' } diff --git a/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/MyKeyboard.kt b/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/MyKeyboard.kt index 39b1e63..7b05733 100644 --- a/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/MyKeyboard.kt +++ b/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/MyKeyboard.kt @@ -23,6 +23,8 @@ import android.graphics.drawable.Drawable import android.util.SparseArray import android.util.TypedValue import android.util.Xml +import android.view.inputmethod.EditorInfo +import android.view.inputmethod.EditorInfo.IME_ACTION_NONE import androidx.annotation.XmlRes import com.simplemobiletools.keyboard.R import java.util.* @@ -90,6 +92,9 @@ class MyKeyboard { /** Height of the screen */ private var mDisplayHeight = 0 + /** What icon should we show at Enter key */ + var mEnterKeyType = IME_ACTION_NONE + /** Keyboard mode, or zero, if none. */ private var mKeyboardMode = 0 private var mCellWidth = 0 @@ -108,7 +113,7 @@ class MyKeyboard { const val EDGE_BOTTOM = 0x08 const val KEYCODE_SHIFT = -1 const val KEYCODE_MODE_CHANGE = -2 - const val KEYCODE_DONE = -4 + const val KEYCODE_ENTER = -4 const val KEYCODE_DELETE = -5 const val KEYCODE_ALT = -6 @@ -392,7 +397,7 @@ class MyKeyboard { * @param xmlLayoutResId the resource file that contains the keyboard layout and keys. */ @JvmOverloads - constructor(context: Context, @XmlRes xmlLayoutResId: Int, modeId: Int = 0) { + constructor(context: Context, @XmlRes xmlLayoutResId: Int, enterKeyType: Int, modeId: Int = 0) { val dm = context.resources.displayMetrics mDisplayWidth = dm.widthPixels mDisplayHeight = dm.heightPixels @@ -401,6 +406,7 @@ class MyKeyboard { mDefaultVerticalGap = 0 mDefaultHeight = mDefaultWidth mKeys = ArrayList() + mEnterKeyType = enterKeyType mKeyboardMode = modeId loadKeyboard(context, context.resources.getXml(xmlLayoutResId)) } @@ -419,7 +425,7 @@ class MyKeyboard { * for each character. */ constructor(context: Context, layoutTemplateResId: Int, characters: CharSequence) : - this(context, layoutTemplateResId) { + this(context, layoutTemplateResId, 0) { var x = 0 var y = 0 var column = 0 @@ -601,6 +607,14 @@ class MyKeyboard { mModifierKeys.add(key) } else if (key.codes[0] == KEYCODE_ALT) { mModifierKeys.add(key) + } else if (key.codes[0] == KEYCODE_ENTER) { + val enterResourceId = when (mEnterKeyType) { + EditorInfo.IME_ACTION_SEARCH -> R.drawable.ic_search_vector + EditorInfo.IME_ACTION_NEXT, EditorInfo.IME_ACTION_GO -> R.drawable.ic_arrow_right_vector + EditorInfo.IME_ACTION_SEND -> R.drawable.ic_send_vector + else -> R.drawable.ic_enter_vector + } + key.icon = context.resources.getDrawable(enterResourceId, context.theme) } currentRow.mKeys.add(key) } else if (TAG_KEYBOARD == tag) { diff --git a/app/src/main/kotlin/com/simplemobiletools/keyboard/services/SimpleKeyboardIME.kt b/app/src/main/kotlin/com/simplemobiletools/keyboard/services/SimpleKeyboardIME.kt index 83d4cab..9b63fdc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/keyboard/services/SimpleKeyboardIME.kt +++ b/app/src/main/kotlin/com/simplemobiletools/keyboard/services/SimpleKeyboardIME.kt @@ -21,11 +21,17 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL private val KEYBOARD_SYMBOLS = 1 private val KEYBOARD_SYMBOLS_SHIFT = 2 + private val ENTER_DEFAULT = 0 + private val ENTER_SEARCH = 1 + private val ENTER_GO = 2 + private val ENTER_NEXT = 3 + private var keyboard: MyKeyboard? = null private var keyboardView: MyKeyboardView? = null private var lastShiftPressTS = 0L private var keyboardMode = KEYBOARD_LETTERS private var inputType = InputType.TYPE_CLASS_TEXT + private var enterKeyType = ENTER_DEFAULT override fun onCreateInputView(): View { keyboardView = layoutInflater.inflate(R.layout.keyboard_view_keyboard, null) as MyKeyboardView @@ -37,7 +43,8 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL } else -> R.xml.keys_letters } - keyboard = MyKeyboard(this, keyboardXml) + + keyboard = MyKeyboard(this, keyboardXml, enterKeyType) keyboardView!!.setKeyboard(keyboard!!) keyboardView!!.onKeyboardActionListener = this return keyboardView!! @@ -50,6 +57,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL override fun onStartInput(attribute: EditorInfo?, restarting: Boolean) { super.onStartInput(attribute, restarting) inputType = attribute!!.inputType and InputType.TYPE_MASK_CLASS + enterKeyType = attribute.imeOptions and (EditorInfo.IME_MASK_ACTION or EditorInfo.IME_FLAG_NO_ENTER_ACTION) } override fun onKey(primaryCode: Int, keyCodes: IntArray?) { @@ -90,12 +98,12 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL keyboardMode = KEYBOARD_SYMBOLS R.xml.keys_symbols } - keyboard = MyKeyboard(this, keyboardXml) + keyboard = MyKeyboard(this, keyboardXml, enterKeyType) keyboardView!!.setKeyboard(keyboard!!) } keyboardView!!.invalidateAllKeys() } - MyKeyboard.KEYCODE_DONE -> { + MyKeyboard.KEYCODE_ENTER -> { inputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER)) inputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER)) } @@ -107,7 +115,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL keyboardMode = KEYBOARD_LETTERS R.xml.keys_letters } - keyboard = MyKeyboard(this, keyboardXml) + keyboard = MyKeyboard(this, keyboardXml, enterKeyType) keyboardView!!.setKeyboard(keyboard!!) } else -> { diff --git a/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt b/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt index 6acb747..328b14e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt +++ b/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt @@ -899,7 +899,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut onInitializeAccessibilityEvent(event) val text: String = when (code) { MyKeyboard.KEYCODE_DELETE -> context.getString(R.string.keycode_delete) - MyKeyboard.KEYCODE_DONE -> context.getString(R.string.keycode_enter) + MyKeyboard.KEYCODE_ENTER -> context.getString(R.string.keycode_enter) MyKeyboard.KEYCODE_MODE_CHANGE -> context.getString(R.string.keycode_mode_change) MyKeyboard.KEYCODE_SHIFT -> context.getString(R.string.keycode_shift) else -> code.toChar().toString() @@ -1010,7 +1010,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut val keyboard = if (popupKey.popupCharacters != null) { MyKeyboard(context, popupKeyboardId, popupKey.popupCharacters!!) } else { - MyKeyboard(context, popupKeyboardId) + MyKeyboard(context, popupKeyboardId, 0) } mMiniKeyboard!!.setKeyboard(keyboard) diff --git a/build.gradle b/build.gradle index 39e98e8..afe1652 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.5.31' + ext.kotlin_version = '1.6.10' repositories { google() @@ -9,7 +9,7 @@ buildscript { } dependencies { - classpath "com.android.tools.build:gradle:7.0.3" + classpath "com.android.tools.build:gradle:7.0.4" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong