Home | History | Annotate | Download | only in compat
      1 /*
      2  * Copyright (C) 2011 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.compat;
     18 
     19 import android.content.Context;
     20 import android.os.IBinder;
     21 import android.view.inputmethod.InputMethodManager;
     22 import android.view.inputmethod.InputMethodSubtype;
     23 
     24 import com.android.inputmethod.latin.ImfUtils;
     25 
     26 import java.lang.reflect.Method;
     27 
     28 // TODO: Override this class with the concrete implementation if we need to take care of the
     29 // performance.
     30 public class InputMethodManagerCompatWrapper {
     31     private static final String TAG = InputMethodManagerCompatWrapper.class.getSimpleName();
     32     private static final Method METHOD_switchToNextInputMethod = CompatUtils.getMethod(
     33             InputMethodManager.class, "switchToNextInputMethod", IBinder.class, Boolean.TYPE);
     34 
     35     private static final InputMethodManagerCompatWrapper sInstance =
     36             new InputMethodManagerCompatWrapper();
     37 
     38     private InputMethodManager mImm;
     39 
     40     private InputMethodManagerCompatWrapper() {
     41         // This wrapper class is not publicly instantiable.
     42     }
     43 
     44     public static InputMethodManagerCompatWrapper getInstance() {
     45         if (sInstance.mImm == null) {
     46             throw new RuntimeException(TAG + ".getInstance() is called before initialization");
     47         }
     48         return sInstance;
     49     }
     50 
     51     public static void init(Context context) {
     52         sInstance.mImm = ImfUtils.getInputMethodManager(context);
     53     }
     54 
     55     public InputMethodSubtype getLastInputMethodSubtype() {
     56         return mImm.getLastInputMethodSubtype();
     57     }
     58 
     59     public boolean switchToLastInputMethod(IBinder token) {
     60         return mImm.switchToLastInputMethod(token);
     61     }
     62 
     63     public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
     64         return (Boolean)CompatUtils.invoke(mImm, false, METHOD_switchToNextInputMethod, token,
     65                 onlyCurrentIme);
     66     }
     67 
     68     public void showInputMethodPicker() {
     69         mImm.showInputMethodPicker();
     70     }
     71 }
     72