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 // Dynamically set
public boolean shouldOfferSwitchingToNextInputMethod; public boolean shouldOfferSwitchingToNextInputMethod;
public boolean shouldOfferSwitchingToSecond; public boolean shouldOfferSwitchingToSecond;
public boolean shouldOfferVoiceTyping;
public String actionLabel; // Might be 'null' public String actionLabel; // Might be 'null'
public int actionId; // Meaningful only when 'actionLabel' isn't 'null' public int actionId; // Meaningful only when 'actionLabel' isn't 'null'
public boolean swapEnterActionKey; // Swap the "enter" and "action" keys public boolean swapEnterActionKey; // Swap the "enter" and "action" keys
@ -75,6 +76,7 @@ final class Config
// initialized later // initialized later
shouldOfferSwitchingToNextInputMethod = false; shouldOfferSwitchingToNextInputMethod = false;
shouldOfferSwitchingToSecond = false; shouldOfferSwitchingToSecond = false;
shouldOfferVoiceTyping = false;
actionLabel = null; actionLabel = null;
actionId = 0; actionId = 0;
swapEnterActionKey = false; swapEnterActionKey = false;
@ -211,6 +213,8 @@ final class Config
KeyValue.getKeyByName("enter") : action_key; KeyValue.getKeyByName("enter") : action_key;
case SWITCH_SECOND: case SWITCH_SECOND:
return shouldOfferSwitchingToSecond ? key : null; return shouldOfferSwitchingToSecond ? key : null;
case SWITCH_VOICE_TYPING:
return shouldOfferVoiceTyping ? key : null;
} }
break; break;
case Keyevent: case Keyevent:

View File

@ -15,12 +15,13 @@ import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype; 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.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
public class Keyboard2 extends InputMethodService public class Keyboard2 extends InputMethodService
implements SharedPreferences.OnSharedPreferenceChangeListener implements SharedPreferences.OnSharedPreferenceChangeListener
@ -156,6 +157,7 @@ public class Keyboard2 extends InputMethodService
_config.shouldOfferSwitchingToNextInputMethod = true; _config.shouldOfferSwitchingToNextInputMethod = true;
else else
_config.shouldOfferSwitchingToNextInputMethod = shouldOfferSwitchingToNextInputMethod(); _config.shouldOfferSwitchingToNextInputMethod = shouldOfferSwitchingToNextInputMethod();
_config.shouldOfferVoiceTyping = (get_voice_typing_im(imm) != null);
if (VERSION.SDK_INT < 12) if (VERSION.SDK_INT < 12)
{ {
// Subtypes won't work well under API level 12 (getExtraValueOf) // Subtypes won't work well under API level 12 (getExtraValueOf)
@ -240,6 +242,20 @@ public class Keyboard2 extends InputMethodService
_keyboardView.reset(); _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) private void log_editor_info(EditorInfo info)
{ {
LogPrinter p = new LogPrinter(Log.DEBUG, TAG); LogPrinter p = new LogPrinter(Log.DEBUG, TAG);
@ -401,24 +417,15 @@ public class Keyboard2 extends InputMethodService
public void switch_voice_typing() public void switch_voice_typing()
{ {
if (VERSION.SDK_INT < 11) // Due to InputMethodSubtype
return;
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
for (InputMethodInfo im : imm.getEnabledInputMethodList()) SimpleEntry<String, InputMethodSubtype> im = get_voice_typing_im(imm);
{ if (im == null)
for (InputMethodSubtype imst : imm.getEnabledInputMethodSubtypeList(im, true)) return;
{ // Best-effort. Good enough for triggering Google's voice typing.
// Switch to the first IM that has a subtype of this mode if (VERSION.SDK_INT < 28)
if (imst.getMode().equals("voice")) Keyboard2.this.switchInputMethod(im.getKey());
{ else
// Best-effort. Good enough for triggering Google's voice typing Keyboard2.this.switchInputMethod(im.getKey(), im.getValue());
if (VERSION.SDK_INT < 28)
Keyboard2.this.switchInputMethod(im.getId());
else
Keyboard2.this.switchInputMethod(im.getId(), imst);
}
}
}
} }
public void setPane_emoji() public void setPane_emoji()