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 final 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 InputMethodManagerCompatWrapper()40 private InputMethodManagerCompatWrapper() { 41 // This wrapper class is not publicly instantiable. 42 } 43 getInstance()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 init(Context context)51 public static void init(Context context) { 52 sInstance.mImm = ImfUtils.getInputMethodManager(context); 53 } 54 getCurrentInputMethodSubtype()55 public InputMethodSubtype getCurrentInputMethodSubtype() { 56 return mImm.getCurrentInputMethodSubtype(); 57 } 58 getLastInputMethodSubtype()59 public InputMethodSubtype getLastInputMethodSubtype() { 60 return mImm.getLastInputMethodSubtype(); 61 } 62 switchToLastInputMethod(IBinder token)63 public boolean switchToLastInputMethod(IBinder token) { 64 return mImm.switchToLastInputMethod(token); 65 } 66 switchToNextInputMethod(IBinder token, boolean onlyCurrentIme)67 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) { 68 return (Boolean)CompatUtils.invoke(mImm, false, METHOD_switchToNextInputMethod, token, 69 onlyCurrentIme); 70 } 71 setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype)72 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) { 73 mImm.setInputMethodAndSubtype(token, id, subtype); 74 } 75 showInputMethodPicker()76 public void showInputMethodPicker() { 77 mImm.showInputMethodPicker(); 78 } 79 } 80