@NonNull
 private static Drawable getInputMethodIcon(
     @NonNull final PackageManager packageManager, @NonNull final InputMethodInfo imi) {
   final ServiceInfo si = imi.getServiceInfo();
   final ApplicationInfo ai = si.applicationInfo;
   final String packageName = imi.getPackageName();
   if (si == null || ai == null || packageName == null) {
     return new ColorDrawable(Color.TRANSPARENT);
   }
   // We do not use ServiceInfo#loadLogo() and ServiceInfo#loadIcon here since those methods
   // internally have some fallback rules, which we want to do manually.
   Drawable drawable = loadDrawable(packageManager, packageName, si.logo, ai);
   if (drawable != null) {
     return drawable;
   }
   drawable = loadDrawable(packageManager, packageName, si.icon, ai);
   if (drawable != null) {
     return drawable;
   }
   // We do not use ApplicationInfo#loadLogo() and ApplicationInfo#loadIcon here since those
   // methods internally have some fallback rules, which we want to do manually.
   drawable = loadDrawable(packageManager, packageName, ai.logo, ai);
   if (drawable != null) {
     return drawable;
   }
   drawable = loadDrawable(packageManager, packageName, ai.icon, ai);
   if (drawable != null) {
     return drawable;
   }
   return new ColorDrawable(Color.TRANSPARENT);
 }
 private Drawable getSubtypeIcon(InputMethodInfo imi, InputMethodSubtype subtype) {
   if (imi != null) {
     if (DEBUG) {
       Log.d(TAG, "Update icons of IME: " + imi.getPackageName());
       if (subtype != null) {
         Log.d(TAG, "subtype =" + subtype.getLocale() + "," + subtype.getMode());
       }
     }
     if (subtype != null) {
       return mPackageManager.getDrawable(
           imi.getPackageName(), subtype.getIconResId(), imi.getServiceInfo().applicationInfo);
     } else if (imi.getSubtypeCount() > 0) {
       return mPackageManager.getDrawable(
           imi.getPackageName(),
           imi.getSubtypeAt(0).getIconResId(),
           imi.getServiceInfo().applicationInfo);
     } else {
       try {
         return mPackageManager
             .getApplicationInfo(imi.getPackageName(), 0)
             .loadIcon(mPackageManager);
       } catch (PackageManager.NameNotFoundException e) {
         Log.w(TAG, "IME can't be found: " + imi.getPackageName());
       }
     }
   }
   return null;
 }
Example #3
0
 public static InputMethodInfo getInputMethodInfoOfThisIme(Context context) {
   if (sInputMethodInfoOfThisIme == null) {
     final InputMethodManager imm = getInputMethodManager(context);
     final String packageName = context.getPackageName();
     for (final InputMethodInfo imi : imm.getInputMethodList()) {
       if (imi.getPackageName().equals(packageName)) return imi;
     }
     throw new RuntimeException("Can not find input method id for " + packageName);
   }
   return sInputMethodInfoOfThisIme;
 }
Example #4
0
 public static boolean checkIfSubtypeBelongsToThisIme(Context context, InputMethodSubtype ims) {
   final InputMethodInfo myImi = getInputMethodInfoOfThisIme(context);
   final int count = myImi.getSubtypeCount();
   for (int i = 0; i < count; i++) {
     final InputMethodSubtype subtype = myImi.getSubtypeAt(i);
     if (subtype.equals(ims)) {
       return true;
     }
   }
   return false;
 }
Example #5
0
  public static boolean isInputServiceSelected() {
    InputMethodInfo info = getInputMethodInfo();

    if (info != null) {
      if (info.getId().equals(ApplicationUtilities.getSelectedInputMethodIdentifier())) {
        return true;
      }
    }

    return false;
  }
Example #6
0
  public static InputMethodInfo getInputMethodInfo(Class classObject) {
    String className = classObject.getName();

    for (InputMethodInfo info :
        ApplicationUtilities.getInputMethodManager().getEnabledInputMethodList()) {
      if (info.getComponent().getClassName().equals(className)) {
        return info;
      }
    }

    return null;
  }
Example #7
0
 public static InputMethodSubtype findSubtypeByLocaleAndKeyboardLayoutSet(
     Context context, String localeString, String keyboardLayoutSetName) {
   final InputMethodInfo imi = getInputMethodInfoOfThisIme(context);
   final int count = imi.getSubtypeCount();
   for (int i = 0; i < count; i++) {
     final InputMethodSubtype subtype = imi.getSubtypeAt(i);
     final String layoutName = SubtypeLocale.getKeyboardLayoutSetName(subtype);
     if (localeString.equals(subtype.getLocale()) && keyboardLayoutSetName.equals(layoutName)) {
       return subtype;
     }
   }
   return null;
 }
 private CharSequence getSubtypeName(InputMethodInfo imi, InputMethodSubtype subtype) {
   if (imi == null || subtype == null) return null;
   if (DEBUG) {
     Log.d(
         TAG,
         "Get text from: "
             + imi.getPackageName()
             + subtype.getNameResId()
             + imi.getServiceInfo().applicationInfo);
   }
   return subtype.getDisplayName(
       mContext, imi.getPackageName(), imi.getServiceInfo().applicationInfo);
 }
 private void setInputMethodAndSubtype(InputMethodInfo imi, InputMethodSubtype subtype) {
   if (mToken != null) {
     mImm.setInputMethodAndSubtype(mToken, imi.getId(), subtype);
   } else {
     Log.w(TAG, "IME Token is not set yet.");
   }
 }
 /**
  * Initialize internal states of this object.
  *
  * @param context the context for this application.
  * @param prefScreen a PreferenceScreen of PreferenceActivity or PreferenceFragment.
  * @return true if this application is an IME and has two or more subtypes, false otherwise.
  */
 public boolean init(final Context context, final PreferenceScreen prefScreen) {
   mContext = context;
   mImm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
   mImi = getMyImi(context, mImm);
   if (mImi == null || mImi.getSubtypeCount() <= 1) {
     return false;
   }
   mSubtypeEnablerPreference = new Preference(context);
   mSubtypeEnablerPreference.setOnPreferenceClickListener(
       new OnPreferenceClickListener() {
         @Override
         public boolean onPreferenceClick(Preference preference) {
           final CharSequence title = getSubtypeEnablerTitle(context);
           final Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
           intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, mImi.getId());
           if (!TextUtils.isEmpty(title)) {
             intent.putExtra(Intent.EXTRA_TITLE, title);
           }
           intent.setFlags(
               Intent.FLAG_ACTIVITY_NEW_TASK
                   | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
                   | Intent.FLAG_ACTIVITY_CLEAR_TOP);
           context.startActivity(intent);
           return true;
         }
       });
   prefScreen.addPreference(mSubtypeEnablerPreference);
   updateSubtypeEnabler();
   return true;
 }
    public SubtypeLocaleAdapter(Context context) {
      super(context, android.R.layout.simple_spinner_item);
      setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

      final TreeSet<SubtypeLocaleItem> items = new TreeSet<SubtypeLocaleItem>();
      final InputMethodInfo imi = ImfUtils.getInputMethodInfoOfThisIme(context);
      final int count = imi.getSubtypeCount();
      for (int i = 0; i < count; i++) {
        final InputMethodSubtype subtype = imi.getSubtypeAt(i);
        if (subtype.containsExtraValueKey(ASCII_CAPABLE)) {
          items.add(createItem(context, subtype.getLocale()));
        }
      }
      // TODO: Should filter out already existing combinations of locale and layout.
      addAll(items);
    }
 private View createInputMethodItem(final InputMethodInfo imi, final InputMethodSubtype subtype) {
   final CharSequence subtypeName;
   if (subtype == null || subtype.overridesImplicitlyEnabledSubtype()) {
     subtypeName = null;
   } else {
     subtypeName = getSubtypeName(imi, subtype);
   }
   final CharSequence imiName = getIMIName(imi);
   final Drawable icon = getSubtypeIcon(imi, subtype);
   final View view = View.inflate(mContext, R.layout.system_bar_input_methods_item, null);
   final ImageView subtypeIcon = (ImageView) view.findViewById(R.id.item_icon);
   final TextView itemTitle = (TextView) view.findViewById(R.id.item_title);
   final TextView itemSubtitle = (TextView) view.findViewById(R.id.item_subtitle);
   final ImageView settingsIcon = (ImageView) view.findViewById(R.id.item_settings_icon);
   final View subtypeView = view.findViewById(R.id.item_subtype);
   if (subtypeName == null) {
     itemTitle.setText(imiName);
     itemSubtitle.setVisibility(View.GONE);
   } else {
     itemTitle.setText(subtypeName);
     itemSubtitle.setVisibility(View.VISIBLE);
     itemSubtitle.setText(imiName);
   }
   subtypeIcon.setImageDrawable(icon);
   subtypeIcon.setContentDescription(itemTitle.getText());
   final String settingsActivity = imi.getSettingsActivity();
   if (!TextUtils.isEmpty(settingsActivity)) {
     settingsIcon.setOnClickListener(
         new View.OnClickListener() {
           @Override
           public void onClick(View arg0) {
             Intent intent = new Intent(Intent.ACTION_MAIN);
             intent.setClassName(imi.getPackageName(), settingsActivity);
             intent.setFlags(
                 Intent.FLAG_ACTIVITY_NEW_TASK
                     | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
                     | Intent.FLAG_ACTIVITY_CLEAR_TOP);
             startActivity(intent);
             closePanel(true);
           }
         });
   } else {
     // Do not show the settings icon if the IME does not have a settings preference
     view.findViewById(R.id.item_vertical_separator).setVisibility(View.GONE);
     settingsIcon.setVisibility(View.GONE);
   }
   mRadioViewAndImiMap.put(
       subtypeView, new Pair<InputMethodInfo, InputMethodSubtype>(imi, subtype));
   subtypeView.setOnClickListener(
       new View.OnClickListener() {
         @Override
         public void onClick(View v) {
           Pair<InputMethodInfo, InputMethodSubtype> imiAndSubtype = updateRadioButtonsByView(v);
           closePanel(false);
           setInputMethodAndSubtype(imiAndSubtype.first, imiAndSubtype.second);
         }
       });
   return view;
 }
 private static String getEnabledSubtypesLabel(
     Context context, InputMethodManager imm, InputMethodInfo imi) {
   if (context == null || imm == null || imi == null) return null;
   final List<InputMethodSubtype> subtypes = imm.getEnabledInputMethodSubtypeList(imi, true);
   final StringBuilder sb = new StringBuilder();
   final int N = subtypes.size();
   for (int i = 0; i < N; ++i) {
     final InputMethodSubtype subtype = subtypes.get(i);
     if (sb.length() > 0) {
       sb.append(", ");
     }
     sb.append(
         subtype.getDisplayName(
             context, imi.getPackageName(), imi.getServiceInfo().applicationInfo));
   }
   return sb.toString();
 }
Example #14
0
 /**
  * Test if user use a custom keyboard
  *
  * @return
  */
 public boolean isUsingCustomInputMethod() {
   InputMethodManager imm =
       (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
   List<InputMethodInfo> mInputMethodProperties = imm.getEnabledInputMethodList();
   final int N = mInputMethodProperties.size();
   for (int i = 0; i < N; i++) {
     InputMethodInfo imi = mInputMethodProperties.get(i);
     if (imi.getId()
         .equals(
             Settings.Secure.getString(
                 getContext().getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
       if ((imi.getServiceInfo().applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
         return true;
       }
     }
   }
   return false;
 }
  private InputMethodPreference getInputMethodPreference(InputMethodInfo imi, int imiSize) {
    final PackageManager pm = getPackageManager();
    final CharSequence label = imi.loadLabel(pm);
    // IME settings
    final Intent intent;
    final String settingsActivity = imi.getSettingsActivity();
    if (!TextUtils.isEmpty(settingsActivity)) {
      intent = new Intent(Intent.ACTION_MAIN);
      intent.setClassName(imi.getPackageName(), settingsActivity);
    } else {
      intent = null;
    }

    // Add a check box for enabling/disabling IME
    InputMethodPreference pref = new InputMethodPreference(this, intent, mImm, imi, imiSize);
    pref.setKey(imi.getId());
    pref.setTitle(label);
    return pref;
  }
 // Turn on the selected radio button at startup
 private void updateRadioButtonsByImiAndSubtype(InputMethodInfo imi, InputMethodSubtype subtype) {
   if (imi == null) return;
   if (DEBUG) {
     Log.d(TAG, "Update radio buttons by " + imi.getId() + ", " + subtype);
   }
   for (View radioView : mRadioViewAndImiMap.keySet()) {
     RadioButton subtypeRadioButton = (RadioButton) radioView.findViewById(R.id.item_radio);
     if (subtypeRadioButton == null) {
       Log.w(TAG, "RadioButton was not found in the selected subtype view");
       return;
     }
     Pair<InputMethodInfo, InputMethodSubtype> imiAndSubtype = mRadioViewAndImiMap.get(radioView);
     if (imiAndSubtype.first.getId().equals(imi.getId())
         && (imiAndSubtype.second == null || imiAndSubtype.second.equals(subtype))) {
       subtypeRadioButton.setChecked(true);
     } else {
       subtypeRadioButton.setChecked(false);
     }
   }
 }
 private InputMethodInfo getCurrentInputMethodInfo() {
   String curInputMethodId =
       Settings.Secure.getString(
           getContext().getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
   Set<InputMethodInfo> cachedImiSet = mEnabledInputMethodAndSubtypesCache.keySet();
   // 1. Search IMI in cache
   for (InputMethodInfo imi : cachedImiSet) {
     if (imi.getId().equals(curInputMethodId)) {
       return imi;
     }
   }
   // 2. Get current enabled IMEs and search IMI
   cachedImiSet = getEnabledInputMethodAndSubtypeList().keySet();
   for (InputMethodInfo imi : cachedImiSet) {
     if (imi.getId().equals(curInputMethodId)) {
       return imi;
     }
   }
   return null;
 }
 private void updateInputMethodPreferenceViews() {
   mInputMethodSettingValues.refreshAllInputMethodAndSubtypes();
   // Clear existing "InputMethodPreference"s
   mInputMethodPreferenceList.clear();
   List<String> permittedList = mDpm.getPermittedInputMethodsForCurrentUser();
   final Context context = getPrefContext();
   final PackageManager packageManager = getActivity().getPackageManager();
   final List<InputMethodInfo> imis = mInputMethodSettingValues.getInputMethodList();
   final int N = (imis == null ? 0 : imis.size());
   for (int i = 0; i < N; ++i) {
     final InputMethodInfo imi = imis.get(i);
     final boolean isAllowedByOrganization =
         permittedList == null || permittedList.contains(imi.getPackageName());
     final InputMethodPreference pref =
         new InputMethodPreference(context, imi, true, isAllowedByOrganization, this);
     pref.setIcon(getInputMethodIcon(packageManager, imi));
     mInputMethodPreferenceList.add(pref);
   }
   final Collator collator = Collator.getInstance();
   Collections.sort(
       mInputMethodPreferenceList,
       new Comparator<InputMethodPreference>() {
         @Override
         public int compare(InputMethodPreference lhs, InputMethodPreference rhs) {
           return lhs.compareTo(rhs, collator);
         }
       });
   getPreferenceScreen().removeAll();
   for (int i = 0; i < N; ++i) {
     final InputMethodPreference pref = mInputMethodPreferenceList.get(i);
     pref.setOrder(i);
     getPreferenceScreen().addPreference(pref);
     InputMethodAndSubtypeUtil.removeUnnecessaryNonPersistentPreference(pref);
     pref.updatePreferenceViews();
   }
 }
 @Override
 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
   if (imi2 == null) return 0;
   if (imi1 == null) return 1;
   if (mPackageManager == null) {
     return imi1.getId().compareTo(imi2.getId());
   }
   CharSequence imiId1 = imi1.loadLabel(mPackageManager) + "/" + imi1.getId();
   CharSequence imiId2 = imi2.loadLabel(mPackageManager) + "/" + imi2.getId();
   return imiId1.toString().compareTo(imiId2.toString());
 }
 private CharSequence getIMIName(InputMethodInfo imi) {
   if (imi == null) return null;
   return imi.loadLabel(mPackageManager);
 }