Skip to content

Added Voice input button to the keyboard's toolbar #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class SettingsActivity : SimpleActivity() {
setupShowClipboardContent()
setupSentencesCapitalization()
setupShowNumbersRow()
setupShowVoiceButton()

binding.apply {
updateTextColors(settingsNestedScrollview)
Expand Down Expand Up @@ -200,4 +201,14 @@ class SettingsActivity : SimpleActivity() {
}
}
}

private fun setupShowVoiceButton() {
binding.apply {
settingsShowVoiceButton.isChecked = config.showVoiceButton
settingsShowVoiceButtonHolder.setOnClickListener {
settingsShowVoiceButton.toggle()
config.showVoiceButton = settingsShowVoiceButton.isChecked
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class Config(context: Context) : BaseConfig(context) {
}
set(showNumbersRow) = prefs.edit().putBoolean(SHOW_NUMBERS_ROW, showNumbersRow).apply()

var showVoiceButton: Boolean
get() = prefs.getBoolean(SHOW_VOICE_BUTTON, false)
set(showVoiceButton) = prefs.edit().putBoolean(SHOW_VOICE_BUTTON, showVoiceButton).apply()

private fun getDefaultLanguage(): Int {
val conf = context.resources.configuration
return if (conf.locale.toString().toLowerCase(Locale.getDefault()).startsWith("ru_")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const val KEYBOARD_LANGUAGE = "keyboard_language"
const val HEIGHT_PERCENTAGE = "height_percentage"
const val SHOW_CLIPBOARD_CONTENT = "show_clipboard_content"
const val SHOW_NUMBERS_ROW = "show_numbers_row"
const val SHOW_VOICE_BUTTON = "show_voice_button"

// differentiate current and pinned clips at the keyboards' Clipboard section
const val ITEM_SECTION_LABEL = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import com.simplemobiletools.keyboard.helpers.*
import com.simplemobiletools.keyboard.interfaces.OnKeyboardActionListener
import com.simplemobiletools.keyboard.views.MyKeyboardView
import java.io.ByteArrayOutputStream
import java.util.AbstractMap
import java.util.Locale

// based on https://www.androidauthority.com/lets-build-custom-keyboard-android-832362/
Expand Down Expand Up @@ -71,6 +72,10 @@ class SimpleKeyboardIME : InputMethodService(), OnKeyboardActionListener, Shared
override fun onCreateInputView(): View {
binding = KeyboardViewKeyboardBinding.inflate(layoutInflater)
keyboardView = binding.keyboardView.apply {
val voiceInputButton = binding.voiceInputButton
voiceInputButton.setOnClickListener {
switchToVoiceTypingIME()
}
setKeyboardHolder(binding)
setKeyboard(keyboard!!)
setEditorInfo(currentInputEditorInfo)
Expand Down Expand Up @@ -485,4 +490,21 @@ class SimpleKeyboardIME : InputMethodService(), OnKeyboardActionListener, Shared

return Icon.createWithData(byteArray, 0, byteArray.size)
}

// voice input

private fun getVoiceTypingIm(imm: InputMethodManager): AbstractMap.SimpleEntry<String, InputMethodSubtype>? {
val enabledKeyboards = imm.enabledInputMethodList
for (im in enabledKeyboards) for (imst in imm.getEnabledInputMethodSubtypeList(im, true))
if (imst.mode == "voice") return AbstractMap.SimpleEntry<String, InputMethodSubtype>(im.id, imst)
return null
}

private fun switchToVoiceTypingIME() {
val imm: InputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager;
val im = getVoiceTypingIm(imm)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
switchInputMethod(im?.key, im?.value)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import com.simplemobiletools.keyboard.models.ClipsSectionLabel
import com.simplemobiletools.keyboard.models.ListItem
import java.util.*


@SuppressLint("UseCompatLoadingForDrawables", "ClickableViewAccessibility")
class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: AttributeSet?, defStyleRes: Int = 0) : View(context, attrs, defStyleRes) {

Expand Down Expand Up @@ -259,6 +260,12 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
closeClipboardManager()
closeEmojiPalette()

if (context.config.showVoiceButton) {
keyboardViewBinding?.voiceInputButton?.visibility = VISIBLE
} else {
keyboardViewBinding?.voiceInputButton?.visibility = GONE
}

if (visibility == VISIBLE) {
setupKeyboard(changedView)
}
Expand Down Expand Up @@ -323,6 +330,8 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
toggleClipboardVisibility(false)
}

voiceInputButton.setOnLongClickListener { context.toast(R.string.voice_input_button); true }

suggestionsHolder.addOnLayoutChangeListener(object : OnLayoutChangeListener {
override fun onLayoutChange(v: View?, left: Int, top: Int, right: Int, bottom: Int, oldLeft: Int, oldTop: Int, oldRight: Int, oldBottom: Int) {
updateSuggestionsToolbarLayout()
Expand Down Expand Up @@ -419,6 +428,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
settingsCog.applyColorFilter(mTextColor)
pinnedClipboardItems.applyColorFilter(mTextColor)
clipboardClear.applyColorFilter(mTextColor)
voiceInputButton.applyColorFilter(mTextColor)

mToolbarHolder?.beInvisibleIf(context.isDeviceLocked)

Expand Down
15 changes: 15 additions & 0 deletions app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,21 @@

</RelativeLayout>

<RelativeLayout
android:id="@+id/settings_show_voice_button_holder"
style="@style/SettingsHolderCheckboxStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.simplemobiletools.commons.views.MyAppCompatCheckbox
android:id="@+id/settings_show_voice_button"
style="@style/SettingsCheckboxStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/enable_voice_input_button" />

</RelativeLayout>

<RelativeLayout
android:id="@+id/settings_keyboard_language_holder"
style="@style/SettingsHolderTextViewStyle"
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/layout/keyboard_view_keyboard.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@

</com.simplemobiletools.keyboard.views.InlineContentViewHorizontalScrollView>

<ImageView
android:id="@+id/voice_input_button"
android:layout_width="@dimen/toolbar_icon_height"
android:layout_height="@dimen/toolbar_icon_height"
android:layout_marginEnd="@dimen/medium_margin"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/voice_input_button"
android:padding="@dimen/small_margin"
android:src="@drawable/ic_microphone_vector"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/pinned_clipboard_items"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/pinned_clipboard_items"
android:layout_width="@dimen/toolbar_icon_height"
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@
<string name="start_sentences_capitalized">بدء الجمل بحرف كبير</string>
<!-- Emojis -->
<string name="emojis">الرموز التعبيرية</string>
<!-- Voice Input -->
<string name="voice_input_button">التبديل إلى أسلوب الإدخال الصوتي</string>
<string name="enable_voice_input_button">إظهار زر الإدخال الصوتي</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
-->
</resources>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values-be/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<string name="start_sentences_capitalized">Start sentences with a capital letter</string>
<!-- Emojis -->
<string name="emojis">Эмодзі</string>
<!-- Voice Input -->
<string name="voice_input_button">Switch to voice input method</string>
<string name="enable_voice_input_button">Show voice input button</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-bg/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<string name="start_sentences_capitalized">Start sentences with a capital letter</string>
<!-- Emojis -->
<string name="emojis">Емоджита</string>
<!-- Voice Input -->
<string name="voice_input_button">Switch to voice input method</string>
<string name="enable_voice_input_button">Show voice input button</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/values-ca/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@
<string name="start_sentences_capitalized">Comença les frases amb una lletra majúscula</string>
<!-- Emojis -->
<string name="emojis">Emojis</string>
<!-- Voice Input -->
<string name="voice_input_button">Switch to voice input method</string>
<string name="enable_voice_input_button">Show voice input button</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
-->
</resources>
</resources>
5 changes: 4 additions & 1 deletion app/src/main/res/values-ckb/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@
<string name="start_sentences_capitalized">گەورەکردنی یەکەم پیتی لاتینی</string>
<!-- Emojis -->
<string name="emojis">خەندەکان</string>
</resources>
<!-- Voice Input -->
<string name="voice_input_button">بگۆڕە بۆ شێوازی نوسينی دەنگ</string>
<string name="enable_voice_input_button">دوگمەی نوسينی دەنگ پیشان بدە</string>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values-cs/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<string name="start_sentences_capitalized">Začínat věty velkým písmenem</string>
<!-- Emojis -->
<string name="emojis">Emotikony</string>
<!-- Voice Input -->
<string name="voice_input_button">Switch to voice input method</string>
<string name="enable_voice_input_button">Show voice input button</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-da/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<string name="start_sentences_capitalized">Start sentences with a capital letter</string>
<!-- Emojis -->
<string name="emojis">Emojis</string>
<!-- Voice Input -->
<string name="voice_input_button">Switch to voice input method</string>
<string name="enable_voice_input_button">Show voice input button</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@
<string name="start_sentences_capitalized">Sätze mit einem Großbuchstaben beginnen</string>
<!-- Emojis -->
<string name="emojis">Emojis</string>
<!-- Voice Input -->
<string name="voice_input_button">Switch to voice input method</string>
<string name="enable_voice_input_button">Show voice input button</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
-->
</resources>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values-el/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<string name="start_sentences_capitalized">Αρχίστε τις προτάσεις με κεφαλαίο γράμμα</string>
<!-- Emojis -->
<string name="emojis">Emojis</string>
<!-- Voice Input -->
<string name="voice_input_button">Switch to voice input method</string>
<string name="enable_voice_input_button">Show voice input button</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-eo/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<string name="start_sentences_capitalized">Start sentences with a capital letter</string>
<!-- Emojis -->
<string name="emojis">Emojis</string>
<!-- Voice Input -->
<string name="voice_input_button">Switch to voice input method</string>
<string name="enable_voice_input_button">Show voice input button</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@
<string name="start_sentences_capitalized">Empezar las frases con mayúsculas</string>
<!-- Emojis -->
<string name="emojis">Emoticonos</string>
<!-- Voice Input -->
<string name="voice_input_button">Switch to voice input method</string>
<string name="enable_voice_input_button">Show voice input button</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
-->
</resources>
</resources>
5 changes: 4 additions & 1 deletion app/src/main/res/values-et/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@
<string name="start_sentences_capitalized">Alusta lauseid suurtähega</string>
<!-- Emojis -->
<string name="emojis">Emojid</string>
<!-- Voice Input -->
<string name="voice_input_button">Switch to voice input method</string>
<string name="enable_voice_input_button">Show voice input button</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
-->
</resources>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values-fi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<string name="start_sentences_capitalized">Start sentences with a capital letter</string>
<!-- Emojis -->
<string name="emojis">Emojit</string>
<!-- Voice Input -->
<string name="voice_input_button">Switch to voice input method</string>
<string name="enable_voice_input_button">Show voice input button</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@
<string name="start_sentences_capitalized">Commencer les phrases par une majuscule</string>
<!-- Emojis -->
<string name="emojis">Émojis</string>
<!-- Voice Input -->
<string name="voice_input_button">Switch to voice input method</string>
<string name="enable_voice_input_button">Show voice input button</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
-->
</resources>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values-gl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<string name="start_sentences_capitalized">Start sentences with a capital letter</string>
<!-- Emojis -->
<string name="emojis">Emoticona</string>
<!-- Voice Input -->
<string name="voice_input_button">Switch to voice input method</string>
<string name="enable_voice_input_button">Show voice input button</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-hr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<string name="start_sentences_capitalized">Počni rečenice s velikim slovom</string>
<!-- Emojis -->
<string name="emojis">Emoji</string>
<!-- Voice Input -->
<string name="voice_input_button">Switch to voice input method</string>
<string name="enable_voice_input_button">Show voice input button</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-hu/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<string name="start_sentences_capitalized">Start sentences with a capital letter</string>
<!-- Emojis -->
<string name="emojis">Emojik</string>
<!-- Voice Input -->
<string name="voice_input_button">Switch to voice input method</string>
<string name="enable_voice_input_button">Show voice input button</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/values-in/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@
<string name="start_sentences_capitalized">Mulai kalimat dengan huruf kapital</string>
<!-- Emojis -->
<string name="emojis">Emoji</string>
<!-- Voice Input -->
<string name="voice_input_button">Switch to voice input method</string>
<string name="enable_voice_input_button">Show voice input button</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
-->
</resources>
</resources>
5 changes: 4 additions & 1 deletion app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@
<string name="start_sentences_capitalized">Inizia le frasi con la lettera maiuscola</string>
<!-- Emojis -->
<string name="emojis">Emoji</string>
<!-- Voice Input -->
<string name="voice_input_button">Switch to voice input method</string>
<string name="enable_voice_input_button">Show voice input button</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
-->
</resources>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values-iw/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<string name="start_sentences_capitalized">Start sentences with a capital letter</string>
<!-- Emojis -->
<string name="emojis">Emojis</string>
<!-- Voice Input -->
<string name="voice_input_button">Switch to voice input method</string>
<string name="enable_voice_input_button">Show voice input button</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res
Expand Down
Loading