Hide the voice typing key if no suitable IM

Implemented similarly to the IM switching key.
This commit is contained in:
Jules Aguillon 2023-06-03 18:35:16 +02:00
parent 85cdb9b2b5
commit 59b3341eaf
2 changed files with 30 additions and 19 deletions

View File

@ -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:

View File

@ -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<String, InputMethodSubtype> 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
SimpleEntry<String, InputMethodSubtype> 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.getId());
Keyboard2.this.switchInputMethod(im.getKey());
else
Keyboard2.this.switchInputMethod(im.getId(), imst);
}
}
}
Keyboard2.this.switchInputMethod(im.getKey(), im.getValue());
}
public void setPane_emoji()