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.app.AlertDialog; 20 import android.inputmethodservice.InputMethodService; 21 import android.os.IBinder; 22 import android.view.Window; 23 import android.view.WindowManager; 24 import android.view.inputmethod.InputMethodSubtype; 25 26 import com.android.inputmethod.deprecated.LanguageSwitcherProxy; 27 import com.android.inputmethod.keyboard.KeyboardSwitcher; 28 import com.android.inputmethod.latin.SubtypeSwitcher; 29 30 public class InputMethodServiceCompatWrapper extends InputMethodService { 31 // CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED needs to be false if the API level is 10 32 // or previous. Note that InputMethodSubtype was added in the API level 11. 33 // For the API level 11 or later, LatinIME should override onCurrentInputMethodSubtypeChanged(). 34 // For the API level 10 or previous, we handle the "subtype changed" events by ourselves 35 // without having support from framework -- onCurrentInputMethodSubtypeChanged(). 36 public static final boolean CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED = true; 37 38 private InputMethodManagerCompatWrapper mImm; 39 40 // For compatibility of {@link InputMethodManager#showInputMethodPicker}. 41 // TODO: Move this variable back to LatinIME when this compatibility wrapper is removed. 42 protected AlertDialog mOptionsDialog; 43 44 public void showOptionDialogInternal(AlertDialog dialog) { 45 final IBinder windowToken = KeyboardSwitcher.getInstance().getKeyboardView() 46 .getWindowToken(); 47 if (windowToken == null) return; 48 49 dialog.setCancelable(true); 50 dialog.setCanceledOnTouchOutside(true); 51 52 final Window window = dialog.getWindow(); 53 final WindowManager.LayoutParams lp = window.getAttributes(); 54 lp.token = windowToken; 55 lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG; 56 window.setAttributes(lp); 57 window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 58 59 mOptionsDialog = dialog; 60 dialog.show(); 61 } 62 63 @Override 64 public void onCreate() { 65 super.onCreate(); 66 mImm = InputMethodManagerCompatWrapper.getInstance(); 67 } 68 69 // When the API level is 10 or previous, notifyOnCurrentInputMethodSubtypeChanged should 70 // handle the event the current subtype was changed. LatinIME calls 71 // notifyOnCurrentInputMethodSubtypeChanged every time LatinIME 72 // changes the current subtype. 73 // This call is required to let LatinIME itself know a subtype changed 74 // event when the API level is 10 or previous. 75 @SuppressWarnings("unused") 76 public void notifyOnCurrentInputMethodSubtypeChanged( 77 InputMethodSubtypeCompatWrapper newSubtype) { 78 // Do nothing when the API level is 11 or later 79 // and FORCE_ENABLE_VOICE_EVEN_WITH_NO_VOICE_SUBTYPES is not true 80 if (CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED && !InputMethodManagerCompatWrapper. 81 FORCE_ENABLE_VOICE_EVEN_WITH_NO_VOICE_SUBTYPES) { 82 return; 83 } 84 final InputMethodSubtypeCompatWrapper subtype = (newSubtype == null) 85 ? mImm.getCurrentInputMethodSubtype() 86 : newSubtype; 87 if (subtype != null) { 88 if (!InputMethodManagerCompatWrapper.FORCE_ENABLE_VOICE_EVEN_WITH_NO_VOICE_SUBTYPES 89 && !subtype.isDummy()) return; 90 if (!InputMethodManagerCompatWrapper.SUBTYPE_SUPPORTED) { 91 LanguageSwitcherProxy.getInstance().setLocale(subtype.getLocale()); 92 } 93 SubtypeSwitcher.getInstance().updateSubtype(subtype); 94 } 95 } 96 97 ////////////////////////////////////// 98 // Functions using API v11 or later // 99 ////////////////////////////////////// 100 @Override 101 public void onCurrentInputMethodSubtypeChanged(InputMethodSubtype subtype) { 102 // Do nothing when the API level is 10 or previous 103 if (!CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED) return; 104 SubtypeSwitcher.getInstance().updateSubtype( 105 new InputMethodSubtypeCompatWrapper(subtype)); 106 } 107 108 protected static void setTouchableRegionCompat(InputMethodService.Insets outInsets, 109 int x, int y, int width, int height) { 110 outInsets.touchableInsets = InputMethodService.Insets.TOUCHABLE_INSETS_REGION; 111 outInsets.touchableRegion.set(x, y, width, height); 112 } 113 } 114