Detect if voice typing is enabled - android

I need to display an alert to the user if they haven't enabled Google voice typing from their settings(Language and input -> Google voice typing). Is there someway to detect that setting status?

So i found my answer. There is no official way of detecting wether voice typing is enabled or not. I have managed to get a list of enabled input methods ( keyboard, voice, etc).
String enabledMethods = Settings.Secure.getString(getActivity().getContentResolver(),Settings.Secure.ENABLED_INPUT_METHODS);
There we can see if Google voice typing is enabled or not and we can alert the user to turn it on, however this applies for the default keyboard. Some users use custom keyboards that have their own implementation of speech to text and it doesn't relly on the users settings for Google voice typing. So it will be a false positive for them.

Given that you know the package name of the IME, you can do something like this:
boolean isImeEnabled(String packageName) {
InputMethodManager imm =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
for (InputMethodInfo imi : imm.getEnabledInputMethodList()) {
if (imi.getPackageName().equals(packageName)) {
return true;
}
}
return false;
}

Related

Autofill service doesn't show up suggestions automatically

I only get autofill suggestions after I make long press on input field and select 'autofill'. As I've read in other answers, this forces requestAutofill. I've enabled my autofill service like so:
First, check if it's available and set autofill hints for input fields:
autoFillService = requireContext().getSystemService(AutofillManager::class.java)
autoFillService?.let { autoFillServiceEnabled = it.isEnabled }
Timber.d("Autofill service is $autoFillServiceEnabled as $autoFillService")
if (autoFillServiceEnabled && autoFillService?.isAutofillSupported == true) {
setAutoFillHints()
}
Hints are set like this. I've also set android:importantForAutofill="yes" in xml:
firstNameEditText.setAutofillHints(HintConstants.AUTOFILL_HINT_PERSON_NAME_GIVEN)
Finally, I request autofill service on input field when onFocus event occurs:
autoFillService?.requestAutofill(fieldView)
Why it doesn't show autofill suggestions automatically? Have I missed something?

How to check if Accessibility Shortcut (Volume key shortcut) is enabled?

How to programmatically check if Volume Key Shortcut is enabled? This shortcut enables accessibility services, which I don't need. I tried this:
AccessibilityManager am = (AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE);
boolean isAccessibilityEnabled = am.isEnabled();
boolean isExploreByTouchEnabled = am.isTouchExplorationEnabled();
But this only returns true if any accessibility service (like TalkBack) is currently enabled, which is slightly different. What I need is to prevent enabling accessibility services when user press&hold volume up and volume down key buttons.
I could achieve this using:
Runtime.getRuntime().exec("adb shell settings get secure accessibility_shortcut_enabled");
but this requires root
So If I know that it is enabled, I'll ask user to turn it off.

Receive barcode scanner Device result in android

I am trying to receive the scanned barcode result from a device paired via (Bluetooth/USB) to an android device.
so many topics said :
most plug-in barcode scanners (that I've seen) are made as HID profile devices so whatever they are plugged into should see them as a Keyboard basically.
source
So I am using this code to receive the result of the scan:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (viewModel.onTriggerScan()) {
//1
char pressedKey = (char) event.getUnicodeChar();
viewModel.addCharToCode(pressedKey);
//2
String fullCode = event.getCharacters();
viewModel.fullCode(fullCode);
//check if the scan is done, received all the chars
if (event.getAction() == EditorInfo.IME_ACTION_DONE) {
//does this work ?
viewModel.gotAllChars();
//3
String fullCode2 = event.getCharacters();
viewModel.fullCode(fullCode2);
}
return true;
} else
return super.dispatchKeyEvent(event);
}
Note: I don't have a barcode scanner device for the test.
which code will receive the result ?? (1 or 2 or 3 ?)
You won't ever see an IME_ACTION_DONE, that's something that's Android only and an external keyboard would never generate.
After that, it's really up to how the scanner works. You may get a full key up and key down for each character. You may not, and may receive multiple characters per event. You may see it finish with a terminator (like \n) you may not- depends on how the scanner is configured. Unless you can configure it yourself or tell the user how to configure it, you need to be prepared for either (which means treating the data as done either after seeing the terminator, or after a second or two once new data stops coming in.
Really you need to buy a configurable scanner model and try it in multiple modes and make ever mode works. Expect it to take a few days in your schedule.
Workaround solution but it works 100%.
the solution is based on clone edittext (hidden from the UI), this edit text just receives the result on it, adds a listener, and when the result arrives gets it and clears the edittext field. An important step, when you try to receive the result(trigger scan) make sure that edittext has the focus otherwise you wil not get the result.
Quick steps:
1- create editText (any text field that receives inputs) in your layout
2- set its visibility to "gone" and clear it.
3- add onValueChangeListener to your edittext.
4- focus your edittext when you start trigger the scan
5- each time you the listener call, get the result and clear edittext
Note: never miss to focus your edittext whenever you start trigger scan.
Note: this method work(99%) for all external scan device and any barcode type.

Android get available input languages

I'm trying to get the available input devices on Android, in order to do that I'm using the InputMethodManager and using the API of getEnabledInputMethodList() as follows:
InputMethodManager inputMgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
List<InputMethodInfo> inputMethodList = inputMgr.getEnabledInputMethodList();
for (InputMethodInfo method : inputMethodList) {
List<InputMethodSubtype> subMethods = inputMgr.getEnabledInputMethodSubtypeList(method, true);
for (InputMethodSubtype submethod : subMethods) {
if (submethod.getMode().equals("keyboard")) { //Ignore voice input method
String localeString = submethod.getLocale();
Locale locale = new Locale(localeString);
String currentLanguage = locale.getLanguage();
//do something...
}
}
}
However, although I've got many more input languages available on my LG G3 and MEIZU M2, this API returns only 1 input language - English.
It seems that this API works as expected only on Google Nexus phones.
Has anyone tried to do the same and succeeded?
P.S
I've already read the solution on this thread but it doesn't help much:
how to get user keyboard language
There is no way to do this. A keyboard doesn't report to Android the list of languages it supports.
In fact, most keyboards keep the input language separate from the phone's locale, in order to switch without resetting the UI of the entire phone. So the OS has no idea what languages a keyboard can write in or is currently writing in.

Android: switch to a different IME programmatically

http://developer.android.com/guide/topics/text/creating-input-method.html#GeneralDesign
reads:
Because multiple IMEs may be installed on the device, provide a way for the user to switch to a different IME directly from the input method UI.
Let's assume I have the source of two input methods and can modify it.
I want to let the user switch between them quickly and am ready to reserve a button for that.
How do I "switch to a different IME directly from the input method UI"?
Switching to the previous input method from the current input method is:
//final String LATIN = "com.android.inputmethod.latin/.LatinIME";
// 'this' is an InputMethodService
try {
InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
final IBinder token = this.getWindow().getWindow().getAttributes().token;
//imm.setInputMethod(token, LATIN);
imm.switchToLastInputMethod(token);
} catch (Throwable t) { // java.lang.NoSuchMethodError if API_level<11
Log.e(TAG,"cannot set the previous input method:");
t.printStackTrace();
}
If you want to switch to a particular input method whose ID you know, you may do as the commented-out lines suggest.
EDIT #pRaNaY suggested a single .getWindow() in a silent edit (click "edited" below to see the history). I remember that it did not work for Android 2.3; if you consult the docs, you will see that the first call, InputMethodService.getWindow() returns a Dialog (which is not a subclass of Window), and the second call, Dialog.getWindow() returns a Window. There is no Dialog.getAttributes(), so with a single .getWindow() it will not even compile.
You cannot change the user's currently active IME through code for security reasons, sorry.
However, you can show a system provided dialog to allow the user to select one of the other enabled ones.
InputMethodManager imeManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imeManager != null) {
imeManager.showInputMethodPicker();
} else {
Toast.makeText(context ,"Error", Toast.LENGTH_LONG).show();
}
If you have rooted device, you can use /system/bin/ime utility.
List all installed input methods: # ime list -a
Set google's keyboard as default:
ime set com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME

Categories

Resources