下面列出了android.view.inputmethod.InputMethodManager#getEnabledInputMethodList ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private boolean switchToNextInputMethodAndSubtype(final IBinder token) {
final InputMethodManager imm = mImmWrapper.mImm;
final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
final int currentIndex = getImiIndexInList(getInputMethodInfoOfThisIme(), enabledImis);
if (currentIndex == INDEX_NOT_FOUND) {
Log.w(TAG, "Can't find current IME in enabled IMEs: IME package="
+ getInputMethodInfoOfThisIme().getPackageName());
return false;
}
final InputMethodInfo nextImi = getNextNonAuxiliaryIme(currentIndex, enabledImis);
final List<InputMethodSubtype> enabledSubtypes = getEnabledInputMethodSubtypeList(nextImi,
true /* allowsImplicitlySelectedSubtypes */);
if (enabledSubtypes.isEmpty()) {
// The next IME has no subtype.
imm.setInputMethod(token, nextImi.getId());
return true;
}
final InputMethodSubtype firstSubtype = enabledSubtypes.get(0);
imm.setInputMethodAndSubtype(token, nextImi.getId(), firstSubtype);
return true;
}
public boolean isSystemLocaleSameAsLocaleOfAllEnabledSubtypesOfEnabledImes() {
final Locale systemLocale = mContext.getResources().getConfiguration().locale;
final Set<InputMethodSubtype> enabledSubtypesOfEnabledImes = new HashSet<>();
final InputMethodManager inputMethodManager = getInputMethodManager();
final List<InputMethodInfo> enabledInputMethodInfoList =
inputMethodManager.getEnabledInputMethodList();
for (final InputMethodInfo info : enabledInputMethodInfoList) {
final List<InputMethodSubtype> enabledSubtypes =
inputMethodManager.getEnabledInputMethodSubtypeList(
info, true /* allowsImplicitlySelectedSubtypes */);
if (enabledSubtypes.isEmpty()) {
// An IME with no subtypes is found.
return false;
}
enabledSubtypesOfEnabledImes.addAll(enabledSubtypes);
}
for (final InputMethodSubtype subtype : enabledSubtypesOfEnabledImes) {
if (!subtype.isAuxiliary() && !subtype.getLocale().isEmpty()
&& !systemLocale.equals(SubtypeLocaleUtils.getSubtypeLocale(subtype))) {
return false;
}
}
return true;
}
private boolean switchToNextInputMethodAndSubtype(final IBinder token) {
final InputMethodManager imm = mImmWrapper.mImm;
final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
final int currentIndex = getImiIndexInList(getInputMethodInfoOfThisIme(), enabledImis);
if (currentIndex == INDEX_NOT_FOUND) {
Log.w(TAG, "Can't find current IME in enabled IMEs: IME package="
+ getInputMethodInfoOfThisIme().getPackageName());
return false;
}
final InputMethodInfo nextImi = getNextNonAuxiliaryIme(currentIndex, enabledImis);
final List<InputMethodSubtype> enabledSubtypes = getEnabledInputMethodSubtypeList(nextImi,
true /* allowsImplicitlySelectedSubtypes */);
if (enabledSubtypes.isEmpty()) {
// The next IME has no subtype.
imm.setInputMethod(token, nextImi.getId());
return true;
}
final InputMethodSubtype firstSubtype = enabledSubtypes.get(0);
imm.setInputMethodAndSubtype(token, nextImi.getId(), firstSubtype);
return true;
}
public boolean isSystemLocaleSameAsLocaleOfAllEnabledSubtypesOfEnabledImes() {
final Locale systemLocale = mContext.getResources().getConfiguration().locale;
final Set<InputMethodSubtype> enabledSubtypesOfEnabledImes = new HashSet<>();
final InputMethodManager inputMethodManager = getInputMethodManager();
final List<InputMethodInfo> enabledInputMethodInfoList =
inputMethodManager.getEnabledInputMethodList();
for (final InputMethodInfo info : enabledInputMethodInfoList) {
final List<InputMethodSubtype> enabledSubtypes =
inputMethodManager.getEnabledInputMethodSubtypeList(
info, true /* allowsImplicitlySelectedSubtypes */);
if (enabledSubtypes.isEmpty()) {
// An IME with no subtypes is found.
return false;
}
enabledSubtypesOfEnabledImes.addAll(enabledSubtypes);
}
for (final InputMethodSubtype subtype : enabledSubtypesOfEnabledImes) {
if (!subtype.isAuxiliary() && !subtype.getLocale().isEmpty()
&& !systemLocale.equals(SubtypeLocaleUtils.getSubtypeLocale(subtype))) {
return false;
}
}
return true;
}
/**
* Gets the set of locales supported by the current enabled Input Methods.
* @param context A {@link Context} instance.
* @return A possibly-empty {@link Set} of locale strings.
*/
public static Set<String> getIMELocales(Context context) {
LinkedHashSet<String> locales = new LinkedHashSet<String>();
InputMethodManager imManager =
(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
List<InputMethodInfo> enabledMethods = imManager.getEnabledInputMethodList();
for (int i = 0; i < enabledMethods.size(); i++) {
List<InputMethodSubtype> subtypes =
imManager.getEnabledInputMethodSubtypeList(enabledMethods.get(i), true);
if (subtypes == null) continue;
for (int j = 0; j < subtypes.size(); j++) {
String locale = ApiCompatibilityUtils.getLocale(subtypes.get(j));
if (!TextUtils.isEmpty(locale)) locales.add(locale);
}
}
return locales;
}
private boolean isKeyboardActivated() {
String packageLocal = getPackageName();
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (inputMethodManager == null) return false;
List<InputMethodInfo> list = inputMethodManager.getEnabledInputMethodList();
// check if our keyboard is enabled as input method
for (InputMethodInfo inputMethod : list) {
String packageName = inputMethod.getPackageName();
if (packageName.equals(packageLocal)) {
return true;
}
}
return false;
}
private boolean switchToNextInputMethodAndSubtype(final IBinder token) {
final InputMethodManager imm = mImmWrapper.mImm;
final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
final int currentIndex = getImiIndexInList(getInputMethodInfoOfThisIme(), enabledImis);
if (currentIndex == INDEX_NOT_FOUND) {
Log.w(TAG, "Can't find current IME in enabled IMEs: IME package="
+ getInputMethodInfoOfThisIme().getPackageName());
return false;
}
final InputMethodInfo nextImi = getNextNonAuxiliaryIme(currentIndex, enabledImis);
final List<InputMethodSubtype> enabledSubtypes = getEnabledInputMethodSubtypeList(nextImi,
true /* allowsImplicitlySelectedSubtypes */);
if (enabledSubtypes.isEmpty()) {
// The next IME has no subtype.
imm.setInputMethod(token, nextImi.getId());
return true;
}
final InputMethodSubtype firstSubtype = enabledSubtypes.get(0);
imm.setInputMethodAndSubtype(token, nextImi.getId(), firstSubtype);
return true;
}
public boolean isSystemLocaleSameAsLocaleOfAllEnabledSubtypesOfEnabledImes() {
final Locale systemLocale = mContext.getResources().getConfiguration().locale;
final Set<InputMethodSubtype> enabledSubtypesOfEnabledImes = new HashSet<>();
final InputMethodManager inputMethodManager = getInputMethodManager();
final List<InputMethodInfo> enabledInputMethodInfoList =
inputMethodManager.getEnabledInputMethodList();
for (final InputMethodInfo info : enabledInputMethodInfoList) {
final List<InputMethodSubtype> enabledSubtypes =
inputMethodManager.getEnabledInputMethodSubtypeList(
info, true /* allowsImplicitlySelectedSubtypes */);
if (enabledSubtypes.isEmpty()) {
// An IME with no subtypes is found.
return false;
}
enabledSubtypesOfEnabledImes.addAll(enabledSubtypes);
}
for (final InputMethodSubtype subtype : enabledSubtypesOfEnabledImes) {
if (!subtype.isAuxiliary() && !subtype.getLocale().isEmpty()
&& !systemLocale.equals(SubtypeLocaleUtils.getSubtypeLocale(subtype))) {
return false;
}
}
return true;
}
/**
* Check if the IME specified by the context is enabled.
* CAVEAT: This may cause a round trip IPC.
*
* @param context package context of the IME to be checked.
* @param imm the {@link InputMethodManager}.
* @return true if this IME is enabled.
*/
public static boolean isThisImeEnabled(final Context context,
final InputMethodManager imm) {
final String packageName = context.getPackageName();
for (final InputMethodInfo imi : imm.getEnabledInputMethodList()) {
if (packageName.equals(imi.getPackageName())) {
return true;
}
}
return false;
}
public void setupBtn() {
final InputMethodManager im = (InputMethodManager) getContext().getSystemService(INPUT_METHOD_SERVICE);
final List<InputMethodInfo> inputMethodInfoList = im.getEnabledInputMethodList();
boolean turnedOn = false;
for (InputMethodInfo inputMethodInfo : inputMethodInfoList) {
if (inputMethodInfo.getId().equals("com.bald.uriah.baldphone/.keyboard.BaldInputMethodService")) {
turnedOn = true;
break;
}
}
if (!turnedOn)
bt_set_keyboard.setOnClickListener((v) -> {
v.getContext().startActivity(new Intent("android.settings.INPUT_METHOD_SETTINGS"));
});
else {
bt_set_keyboard.setText(R.string.already_setted);
bt_set_keyboard.setOnClickListener(v -> {
try {
((InputMethodManager) getContext().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE)).showInputMethodPicker();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
BaldToast.error(getContext());
}
});
}
}
public static boolean isEnableIME(Context context){
try {
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
List<InputMethodInfo> inputs = imm.getEnabledInputMethodList();
boolean flag = false;
for(InputMethodInfo input : inputs){
if(input.getPackageName().equals(IMEService.class.getPackage().getName())){
return true;
}
}
}catch (Exception ignored){ }
return false;
}
/**
* Check if the IME specified by the context is enabled.
* CAVEAT: This may cause a round trip IPC.
*
* @param context package context of the IME to be checked.
* @param imm the {@link InputMethodManager}.
* @return true if this IME is enabled.
*/
public static boolean isThisImeEnabled(final Context context,
final InputMethodManager imm) {
final String packageName = context.getPackageName();
for (final InputMethodInfo imi : imm.getEnabledInputMethodList()) {
if (packageName.equals(imi.getPackageName())) {
return true;
}
}
return false;
}
private static void initInputMethods(Context context) {
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
List<InputMethodInfo> inputMethods = inputMethodManager.getEnabledInputMethodList();
final int count = inputMethods == null ? 0 : inputMethods.size();
for (int i = 0; i < count; ++i) {
inputMethodPackages.add(inputMethods.get(i).getPackageName());
}
}
private void recordKeyboardLocaleUma() {
InputMethodManager imm =
(InputMethodManager) mAppContext.getSystemService(Context.INPUT_METHOD_SERVICE);
List<InputMethodInfo> ims = imm.getEnabledInputMethodList();
ArrayList<String> uniqueLanguages = new ArrayList<String>();
for (InputMethodInfo method : ims) {
List<InputMethodSubtype> submethods =
imm.getEnabledInputMethodSubtypeList(method, true);
for (InputMethodSubtype submethod : submethods) {
if (submethod.getMode().equals("keyboard")) {
String language = submethod.getLocale().split("_")[0];
if (!uniqueLanguages.contains(language)) {
uniqueLanguages.add(language);
}
}
}
}
RecordHistogram.recordCountHistogram("InputMethod.ActiveCount", uniqueLanguages.size());
InputMethodSubtype currentSubtype = imm.getCurrentInputMethodSubtype();
Locale systemLocale = Locale.getDefault();
if (currentSubtype != null && currentSubtype.getLocale() != null && systemLocale != null) {
String keyboardLanguage = currentSubtype.getLocale().split("_")[0];
boolean match = systemLocale.getLanguage().equalsIgnoreCase(keyboardLanguage);
RecordHistogram.recordBooleanHistogram("InputMethod.MatchesSystemLanguage", match);
}
}
private void recordKeyboardLocaleUma() {
InputMethodManager imm =
(InputMethodManager) mAppContext.getSystemService(Context.INPUT_METHOD_SERVICE);
List<InputMethodInfo> ims = imm.getEnabledInputMethodList();
ArrayList<String> uniqueLanguages = new ArrayList<>();
for (InputMethodInfo method : ims) {
List<InputMethodSubtype> submethods =
imm.getEnabledInputMethodSubtypeList(method, true);
for (InputMethodSubtype submethod : submethods) {
if (submethod.getMode().equals("keyboard")) {
String language = submethod.getLocale().split("_")[0];
if (!uniqueLanguages.contains(language)) {
uniqueLanguages.add(language);
}
}
}
}
RecordHistogram.recordCountHistogram("InputMethod.ActiveCount", uniqueLanguages.size());
InputMethodSubtype currentSubtype = imm.getCurrentInputMethodSubtype();
Locale systemLocale = Locale.getDefault();
if (currentSubtype != null && currentSubtype.getLocale() != null && systemLocale != null) {
String keyboardLanguage = currentSubtype.getLocale().split("_")[0];
boolean match = systemLocale.getLanguage().equalsIgnoreCase(keyboardLanguage);
RecordHistogram.recordBooleanHistogram("InputMethod.MatchesSystemLanguage", match);
}
}
private void checkCustomFont ()
{
if (Preferences.isLanguageTibetan())
{
CustomTypefaceManager.loadFromAssets(this,true);
}
else
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
List<InputMethodInfo> mInputMethodProperties = imm.getEnabledInputMethodList();
final int N = mInputMethodProperties.size();
boolean loadTibetan = false;
for (int i = 0; i < N; i++) {
InputMethodInfo imi = mInputMethodProperties.get(i);
//imi contains the information about the keyboard you are using
if (imi.getPackageName().equals("org.ironrabbit.bhoboard")) {
// CustomTypefaceManager.loadFromKeyboard(this);
loadTibetan = true;
break;
}
}
CustomTypefaceManager.loadFromAssets(this, loadTibetan);
}
}
private void checkCustomFont ()
{
if (Preferences.getLanguage() != null
&& Preferences.getLanguage().equalsIgnoreCase("bo"))
{
CustomTypefaceManager.loadFromAssets(this,true);
}
else
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
List<InputMethodInfo> mInputMethodProperties = imm.getEnabledInputMethodList();
final int N = mInputMethodProperties.size();
boolean loadTibetan = false;
for (int i = 0; i < N; i++) {
InputMethodInfo imi = mInputMethodProperties.get(i);
//imi contains the information about the keyboard you are using
if (imi.getPackageName().equals("org.ironrabbit.bhoboard")) {
// CustomTypefaceManager.loadFromKeyboard(this);
loadTibetan = true;
break;
}
}
CustomTypefaceManager.loadFromAssets(this,loadTibetan);
}
}
@SuppressWarnings("deprecation") // InputMethodSubtype.getLocale() deprecated in API 24
private void recordKeyboardLocaleUma(Context context) {
InputMethodManager imm =
(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
List<InputMethodInfo> ims = imm.getEnabledInputMethodList();
ArrayList<String> uniqueLanguages = new ArrayList<>();
for (InputMethodInfo method : ims) {
List<InputMethodSubtype> submethods =
imm.getEnabledInputMethodSubtypeList(method, true);
for (InputMethodSubtype submethod : submethods) {
if (submethod.getMode().equals("keyboard")) {
String language = submethod.getLocale().split("_")[0];
if (!uniqueLanguages.contains(language)) {
uniqueLanguages.add(language);
}
}
}
}
RecordHistogram.recordCountHistogram("InputMethod.ActiveCount", uniqueLanguages.size());
InputMethodSubtype currentSubtype = imm.getCurrentInputMethodSubtype();
Locale systemLocale = Locale.getDefault();
if (currentSubtype != null && currentSubtype.getLocale() != null && systemLocale != null) {
String keyboardLanguage = currentSubtype.getLocale().split("_")[0];
boolean match = systemLocale.getLanguage().equalsIgnoreCase(keyboardLanguage);
RecordHistogram.recordBooleanHistogram("InputMethod.MatchesSystemLanguage", match);
}
}
/**
* Check if the IME specified by the context is enabled.
* CAVEAT: This may cause a round trip IPC.
*
* @param context package context of the IME to be checked.
* @param imm the {@link InputMethodManager}.
* @return true if this IME is enabled.
*/
public static boolean isThisImeEnabled(final Context context,
final InputMethodManager imm) {
final String packageName = context.getPackageName();
for (final InputMethodInfo imi : imm.getEnabledInputMethodList()) {
if (packageName.equals(imi.getPackageName())) {
return true;
}
}
return false;
}