diff --git a/srcs/juloo.keyboard2/Config.java b/srcs/juloo.keyboard2/Config.java index 9fa6593..f99a140 100644 --- a/srcs/juloo.keyboard2/Config.java +++ b/srcs/juloo.keyboard2/Config.java @@ -53,6 +53,7 @@ final class Config // Dynamically set public boolean shouldOfferSwitchingToNextInputMethod; public boolean shouldOfferSwitchingToSecond; + public boolean shouldOfferVoiceTyping; public String actionLabel; // Might be 'null' public int actionId; // Meaningful only when 'actionLabel' isn't 'null' public boolean swapEnterActionKey; // Swap the "enter" and "action" keys @@ -75,6 +76,7 @@ final class Config // initialized later shouldOfferSwitchingToNextInputMethod = false; shouldOfferSwitchingToSecond = false; + shouldOfferVoiceTyping = false; actionLabel = null; actionId = 0; swapEnterActionKey = false; @@ -211,6 +213,8 @@ final class Config KeyValue.getKeyByName("enter") : action_key; case SWITCH_SECOND: return shouldOfferSwitchingToSecond ? key : null; + case SWITCH_VOICE_TYPING: + return shouldOfferVoiceTyping ? key : null; } break; case Keyevent: diff --git a/srcs/juloo.keyboard2/Keyboard2.java b/srcs/juloo.keyboard2/Keyboard2.java index e5bd726..5394a6c 100644 --- a/srcs/juloo.keyboard2/Keyboard2.java +++ b/srcs/juloo.keyboard2/Keyboard2.java @@ -15,12 +15,13 @@ import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodSubtype; +import android.widget.FrameLayout; +import android.widget.LinearLayout; +import java.util.AbstractMap.SimpleEntry; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; -import android.widget.FrameLayout; -import android.widget.LinearLayout; public class Keyboard2 extends InputMethodService implements SharedPreferences.OnSharedPreferenceChangeListener @@ -156,6 +157,7 @@ public class Keyboard2 extends InputMethodService _config.shouldOfferSwitchingToNextInputMethod = true; else _config.shouldOfferSwitchingToNextInputMethod = shouldOfferSwitchingToNextInputMethod(); + _config.shouldOfferVoiceTyping = (get_voice_typing_im(imm) != null); if (VERSION.SDK_INT < 12) { // Subtypes won't work well under API level 12 (getExtraValueOf) @@ -240,6 +242,20 @@ public class Keyboard2 extends InputMethodService _keyboardView.reset(); } + /** Returns the id and subtype of the voice typing IM. Returns [null] if none + is installed or if the feature is unsupported. */ + SimpleEntry get_voice_typing_im(InputMethodManager imm) + { + if (VERSION.SDK_INT < 11) // Due to InputMethodSubtype + return null; + for (InputMethodInfo im : imm.getEnabledInputMethodList()) + for (InputMethodSubtype imst : imm.getEnabledInputMethodSubtypeList(im, true)) + // Switch to the first IM that has a subtype of this mode + if (imst.getMode().equals("voice")) + return new SimpleEntry(im.getId(), imst); + return null; + } + private void log_editor_info(EditorInfo info) { LogPrinter p = new LogPrinter(Log.DEBUG, TAG); @@ -401,24 +417,15 @@ public class Keyboard2 extends InputMethodService public void switch_voice_typing() { - if (VERSION.SDK_INT < 11) // Due to InputMethodSubtype - return; InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); - for (InputMethodInfo im : imm.getEnabledInputMethodList()) - { - for (InputMethodSubtype imst : imm.getEnabledInputMethodSubtypeList(im, true)) - { - // Switch to the first IM that has a subtype of this mode - if (imst.getMode().equals("voice")) - { - // Best-effort. Good enough for triggering Google's voice typing - if (VERSION.SDK_INT < 28) - Keyboard2.this.switchInputMethod(im.getId()); - else - Keyboard2.this.switchInputMethod(im.getId(), imst); - } - } - } + SimpleEntry im = get_voice_typing_im(imm); + if (im == null) + return; + // Best-effort. Good enough for triggering Google's voice typing. + if (VERSION.SDK_INT < 28) + Keyboard2.this.switchInputMethod(im.getKey()); + else + Keyboard2.this.switchInputMethod(im.getKey(), im.getValue()); } public void setPane_emoji()