Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.inputmethod.latin.utils;
     18 
     19 import android.content.Context;
     20 import android.provider.Settings;
     21 import android.view.inputmethod.InputMethodInfo;
     22 import android.view.inputmethod.InputMethodManager;
     23 
     24 /*
     25  * A utility class for {@link InputMethodManager}. Unlike {@link RichInputMethodManager}, this
     26  * class provides synchronous, non-cached access to {@link InputMethodManager}. The setup activity
     27  * is a good example to use this class because {@link InputMethodManagerService} may not be aware of
     28  * this IME immediately after this IME is installed.
     29  */
     30 public final class UncachedInputMethodManagerUtils {
     31     /**
     32      * Check if the IME specified by the context is enabled.
     33      * CAVEAT: This may cause a round trip IPC.
     34      *
     35      * @param context package context of the IME to be checked.
     36      * @param imm the {@link InputMethodManager}.
     37      * @return true if this IME is enabled.
     38      */
     39     public static boolean isThisImeEnabled(final Context context,
     40             final InputMethodManager imm) {
     41         final String packageName = context.getPackageName();
     42         for (final InputMethodInfo imi : imm.getEnabledInputMethodList()) {
     43             if (packageName.equals(imi.getPackageName())) {
     44                 return true;
     45             }
     46         }
     47         return false;
     48     }
     49 
     50     /**
     51      * Check if the IME specified by the context is the current IME.
     52      * CAVEAT: This may cause a round trip IPC.
     53      *
     54      * @param context package context of the IME to be checked.
     55      * @param imm the {@link InputMethodManager}.
     56      * @return true if this IME is the current IME.
     57      */
     58     public static boolean isThisImeCurrent(final Context context,
     59             final InputMethodManager imm) {
     60         final InputMethodInfo imi = getInputMethodInfoOf(context.getPackageName(), imm);
     61         final String currentImeId = Settings.Secure.getString(
     62                 context.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
     63         return imi != null && imi.getId().equals(currentImeId);
     64     }
     65 
     66     /**
     67      * Get {@link InputMethodInfo} of the IME specified by the package name.
     68      * CAVEAT: This may cause a round trip IPC.
     69      *
     70      * @param packageName package name of the IME.
     71      * @param imm the {@link InputMethodManager}.
     72      * @return the {@link InputMethodInfo} of the IME specified by the <code>packageName</code>,
     73      * or null if not found.
     74      */
     75     public static InputMethodInfo getInputMethodInfoOf(final String packageName,
     76             final InputMethodManager imm) {
     77         for (final InputMethodInfo imi : imm.getInputMethodList()) {
     78             if (packageName.equals(imi.getPackageName())) {
     79                 return imi;
     80             }
     81         }
     82         return null;
     83     }
     84 }
     85