| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2 | * |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | * use this file except in compliance with the License. You may obtain a copy of |
| 5 | * the License at |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6 | * |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 8 | * |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | * License for the specific language governing permissions and limitations under |
| 13 | * the License. |
| 14 | */ |
| 15 | |
| 16 | package com.android.server; |
| 17 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 18 | import com.android.internal.content.PackageMonitor; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import com.android.internal.os.HandlerCaller; |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 20 | import com.android.internal.os.SomeArgs; |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 21 | import com.android.internal.util.FastXmlSerializer; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import com.android.internal.view.IInputContext; |
| 23 | import com.android.internal.view.IInputMethod; |
| 24 | import com.android.internal.view.IInputMethodCallback; |
| 25 | import com.android.internal.view.IInputMethodClient; |
| 26 | import com.android.internal.view.IInputMethodManager; |
| 27 | import com.android.internal.view.IInputMethodSession; |
| 28 | import com.android.internal.view.InputBindResult; |
| Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 29 | import com.android.server.EventLogTags; |
| satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 30 | import com.android.server.wm.WindowManagerService; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 32 | import org.xmlpull.v1.XmlPullParser; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import org.xmlpull.v1.XmlPullParserException; |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 34 | import org.xmlpull.v1.XmlSerializer; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
| 36 | import android.app.ActivityManagerNative; |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 37 | import android.app.AppGlobals; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.app.AlertDialog; |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 39 | import android.app.IUserSwitchObserver; |
| satok | f90a33e | 2011-07-19 11:55:52 +0900 | [diff] [blame] | 40 | import android.app.KeyguardManager; |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 41 | import android.app.Notification; |
| 42 | import android.app.NotificationManager; |
| Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 43 | import android.app.PendingIntent; |
| satok | 5b927c43 | 2012-05-01 20:09:34 +0900 | [diff] [blame] | 44 | import android.content.BroadcastReceiver; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.content.ComponentName; |
| 46 | import android.content.ContentResolver; |
| 47 | import android.content.Context; |
| 48 | import android.content.DialogInterface; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.content.DialogInterface.OnCancelListener; |
| 50 | import android.content.Intent; |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 51 | import android.content.IntentFilter; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.content.ServiceConnection; |
| Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 53 | import android.content.pm.ApplicationInfo; |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 54 | import android.content.pm.IPackageManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.content.pm.PackageManager; |
| Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 56 | import android.content.pm.PackageManager.NameNotFoundException; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import android.content.pm.ResolveInfo; |
| 58 | import android.content.pm.ServiceInfo; |
| Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 59 | import android.content.res.Configuration; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.content.res.Resources; |
| 61 | import android.content.res.TypedArray; |
| 62 | import android.database.ContentObserver; |
| Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 63 | import android.inputmethodservice.InputMethodService; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import android.os.Binder; |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 65 | import android.os.Environment; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import android.os.Handler; |
| 67 | import android.os.IBinder; |
| 68 | import android.os.IInterface; |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 69 | import android.os.IRemoteCallback; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | import android.os.Message; |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 71 | import android.os.Process; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | import android.os.Parcel; |
| 73 | import android.os.RemoteException; |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 74 | import android.os.ResultReceiver; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | import android.os.ServiceManager; |
| 76 | import android.os.SystemClock; |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 77 | import android.os.UserHandle; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | import android.provider.Settings; |
| Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 79 | import android.provider.Settings.Secure; |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 80 | import android.provider.Settings.SettingNotFoundException; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | import android.text.TextUtils; |
| satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 82 | import android.text.style.SuggestionSpan; |
| Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 83 | import android.util.AtomicFile; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import android.util.EventLog; |
| satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 85 | import android.util.LruCache; |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 86 | import android.util.Pair; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | import android.util.PrintWriterPrinter; |
| 88 | import android.util.Printer; |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 89 | import android.util.Slog; |
| 90 | import android.util.Xml; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | import android.view.IWindowManager; |
| Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 92 | import android.view.LayoutInflater; |
| 93 | import android.view.View; |
| 94 | import android.view.ViewGroup; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | import android.view.WindowManager; |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 96 | import android.view.inputmethod.EditorInfo; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | import android.view.inputmethod.InputBinding; |
| 98 | import android.view.inputmethod.InputMethod; |
| 99 | import android.view.inputmethod.InputMethodInfo; |
| 100 | import android.view.inputmethod.InputMethodManager; |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 101 | import android.view.inputmethod.InputMethodSubtype; |
| Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 102 | import android.widget.ArrayAdapter; |
| satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 103 | import android.widget.CompoundButton; |
| 104 | import android.widget.CompoundButton.OnCheckedChangeListener; |
| Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 105 | import android.widget.RadioButton; |
| satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 106 | import android.widget.Switch; |
| Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 107 | import android.widget.TextView; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 109 | import java.io.File; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | import java.io.FileDescriptor; |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 111 | import java.io.FileInputStream; |
| 112 | import java.io.FileOutputStream; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | import java.io.IOException; |
| 114 | import java.io.PrintWriter; |
| 115 | import java.util.ArrayList; |
| satok | 688bd47 | 2012-02-09 20:09:17 +0900 | [diff] [blame] | 116 | import java.util.Collections; |
| Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 117 | import java.util.Comparator; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | import java.util.HashMap; |
| satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 119 | import java.util.HashSet; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | import java.util.List; |
| satok | 5b927c43 | 2012-05-01 20:09:34 +0900 | [diff] [blame] | 121 | import java.util.Locale; |
| satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 122 | import java.util.TreeMap; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * This class provides a system service that manages input methods. |
| 126 | */ |
| 127 | public class InputMethodManagerService extends IInputMethodManager.Stub |
| 128 | implements ServiceConnection, Handler.Callback { |
| 129 | static final boolean DEBUG = false; |
| Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 130 | static final String TAG = "InputMethodManagerService"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | |
| 132 | static final int MSG_SHOW_IM_PICKER = 1; |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 133 | static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2; |
| satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 134 | static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3; |
| satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 135 | static final int MSG_SHOW_IM_CONFIG = 4; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 136 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | static final int MSG_UNBIND_INPUT = 1000; |
| 138 | static final int MSG_BIND_INPUT = 1010; |
| 139 | static final int MSG_SHOW_SOFT_INPUT = 1020; |
| 140 | static final int MSG_HIDE_SOFT_INPUT = 1030; |
| 141 | static final int MSG_ATTACH_TOKEN = 1040; |
| 142 | static final int MSG_CREATE_SESSION = 1050; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 143 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | static final int MSG_START_INPUT = 2000; |
| 145 | static final int MSG_RESTART_INPUT = 2010; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 146 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | static final int MSG_UNBIND_METHOD = 3000; |
| 148 | static final int MSG_BIND_METHOD = 3010; |
| Dianne Hackborn | a6e4134 | 2012-05-22 16:30:34 -0700 | [diff] [blame] | 149 | static final int MSG_SET_ACTIVE = 3020; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 150 | |
| satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 151 | static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000; |
| 152 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | static final long TIME_TO_RECONNECT = 10*1000; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 154 | |
| satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 155 | static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20; |
| 156 | |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 157 | private static final int NOT_A_SUBTYPE_ID = -1; |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 158 | private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID); |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 159 | private static final String SUBTYPE_MODE_KEYBOARD = "keyboard"; |
| 160 | private static final String SUBTYPE_MODE_VOICE = "voice"; |
| satok | b635941 | 2011-06-28 17:47:41 +0900 | [diff] [blame] | 161 | private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher"; |
| satok | c369056 | 2012-01-10 20:14:43 +0900 | [diff] [blame] | 162 | private static final String TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE = |
| 163 | "EnabledWhenDefaultIsNotAsciiCapable"; |
| 164 | private static final String TAG_ASCII_CAPABLE = "AsciiCapable"; |
| satok | 5b927c43 | 2012-05-01 20:09:34 +0900 | [diff] [blame] | 165 | private static final Locale ENGLISH_LOCALE = new Locale("en"); |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 166 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | final Context mContext; |
| Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 168 | final Resources mRes; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | final Handler mHandler; |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 170 | final InputMethodSettings mSettings; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | final SettingsObserver mSettingsObserver; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | final IWindowManager mIWindowManager; |
| 173 | final HandlerCaller mCaller; |
| Satoshi Kataoka | 5ade83b | 2012-09-26 22:59:41 +0900 | [diff] [blame] | 174 | private InputMethodFileManager mFileManager; |
| 175 | private InputMethodAndSubtypeListManager mImListManager; |
| satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 176 | private final HardKeyboardListener mHardKeyboardListener; |
| 177 | private final WindowManagerService mWindowManagerService; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 178 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | final InputBindResult mNoBinding = new InputBindResult(null, null, -1); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 180 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | // All known input methods. mMethodMap also serves as the global |
| 182 | // lock for this class. |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 183 | final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>(); |
| 184 | final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>(); |
| satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 185 | private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans = |
| 186 | new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 187 | |
| Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 188 | // Used to bring IME service up to visible adjustment while it is being shown. |
| 189 | final ServiceConnection mVisibleConnection = new ServiceConnection() { |
| 190 | @Override public void onServiceConnected(ComponentName name, IBinder service) { |
| 191 | } |
| 192 | |
| 193 | @Override public void onServiceDisconnected(ComponentName name) { |
| 194 | } |
| 195 | }; |
| 196 | boolean mVisibleBound = false; |
| 197 | |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 198 | // Ongoing notification |
| Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 199 | private NotificationManager mNotificationManager; |
| 200 | private KeyguardManager mKeyguardManager; |
| 201 | private StatusBarManagerService mStatusBar; |
| 202 | private Notification mImeSwitcherNotification; |
| 203 | private PendingIntent mImeSwitchPendingIntent; |
| satok | b858c73 | 2011-07-22 19:54:34 +0900 | [diff] [blame] | 204 | private boolean mShowOngoingImeSwitcherForPhones; |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 205 | private boolean mNotificationShown; |
| satok | 0a1bcf4 | 2012-05-16 19:26:31 +0900 | [diff] [blame] | 206 | private final boolean mImeSelectedOnBoot; |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 207 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | class SessionState { |
| 209 | final ClientState client; |
| 210 | final IInputMethod method; |
| 211 | final IInputMethodSession session; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 212 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | @Override |
| 214 | public String toString() { |
| 215 | return "SessionState{uid " + client.uid + " pid " + client.pid |
| 216 | + " method " + Integer.toHexString( |
| 217 | System.identityHashCode(method)) |
| 218 | + " session " + Integer.toHexString( |
| 219 | System.identityHashCode(session)) |
| 220 | + "}"; |
| 221 | } |
| 222 | |
| 223 | SessionState(ClientState _client, IInputMethod _method, |
| 224 | IInputMethodSession _session) { |
| 225 | client = _client; |
| 226 | method = _method; |
| 227 | session = _session; |
| 228 | } |
| 229 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 230 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | class ClientState { |
| 232 | final IInputMethodClient client; |
| 233 | final IInputContext inputContext; |
| 234 | final int uid; |
| 235 | final int pid; |
| 236 | final InputBinding binding; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 237 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | boolean sessionRequested; |
| 239 | SessionState curSession; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 240 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | @Override |
| 242 | public String toString() { |
| 243 | return "ClientState{" + Integer.toHexString( |
| 244 | System.identityHashCode(this)) + " uid " + uid |
| 245 | + " pid " + pid + "}"; |
| 246 | } |
| 247 | |
| 248 | ClientState(IInputMethodClient _client, IInputContext _inputContext, |
| 249 | int _uid, int _pid) { |
| 250 | client = _client; |
| 251 | inputContext = _inputContext; |
| 252 | uid = _uid; |
| 253 | pid = _pid; |
| 254 | binding = new InputBinding(null, inputContext.asBinder(), uid, pid); |
| 255 | } |
| 256 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 257 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | final HashMap<IBinder, ClientState> mClients |
| 259 | = new HashMap<IBinder, ClientState>(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 260 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | /** |
| Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 262 | * Set once the system is ready to run third party code. |
| 263 | */ |
| 264 | boolean mSystemReady; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 265 | |
| Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 266 | /** |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | * Id of the currently selected input method. |
| 268 | */ |
| 269 | String mCurMethodId; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 270 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | /** |
| 272 | * The current binding sequence number, incremented every time there is |
| 273 | * a new bind performed. |
| 274 | */ |
| 275 | int mCurSeq; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 276 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | /** |
| 278 | * The client that is currently bound to an input method. |
| 279 | */ |
| 280 | ClientState mCurClient; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 281 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | /** |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 283 | * The last window token that gained focus. |
| 284 | */ |
| 285 | IBinder mCurFocusedWindow; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 286 | |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 287 | /** |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | * The input context last provided by the current client. |
| 289 | */ |
| 290 | IInputContext mCurInputContext; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 291 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | /** |
| 293 | * The attributes last provided by the current client. |
| 294 | */ |
| 295 | EditorInfo mCurAttribute; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 296 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | /** |
| 298 | * The input method ID of the input method service that we are currently |
| 299 | * connected to or in the process of connecting to. |
| 300 | */ |
| 301 | String mCurId; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 302 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | /** |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 304 | * The current subtype of the current input method. |
| 305 | */ |
| 306 | private InputMethodSubtype mCurrentSubtype; |
| 307 | |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 308 | // This list contains the pairs of InputMethodInfo and InputMethodSubtype. |
| satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 309 | private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>> |
| 310 | mShortcutInputMethodsAndSubtypes = |
| 311 | new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>(); |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 312 | |
| 313 | /** |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | * Set to true if our ServiceConnection is currently actively bound to |
| 315 | * a service (whether or not we have gotten its IBinder back yet). |
| 316 | */ |
| 317 | boolean mHaveConnection; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 318 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | /** |
| 320 | * Set if the client has asked for the input method to be shown. |
| 321 | */ |
| 322 | boolean mShowRequested; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 323 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | /** |
| 325 | * Set if we were explicitly told to show the input method. |
| 326 | */ |
| 327 | boolean mShowExplicitlyRequested; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 328 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | /** |
| 330 | * Set if we were forced to be shown. |
| 331 | */ |
| 332 | boolean mShowForced; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 333 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | /** |
| 335 | * Set if we last told the input method to show itself. |
| 336 | */ |
| 337 | boolean mInputShown; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 338 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | /** |
| 340 | * The Intent used to connect to the current input method. |
| 341 | */ |
| 342 | Intent mCurIntent; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 343 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | /** |
| 345 | * The token we have made for the currently active input method, to |
| 346 | * identify it in the future. |
| 347 | */ |
| 348 | IBinder mCurToken; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 349 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | /** |
| 351 | * If non-null, this is the input method service we are currently connected |
| 352 | * to. |
| 353 | */ |
| 354 | IInputMethod mCurMethod; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 355 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | /** |
| 357 | * Time that we last initiated a bind to the input method, to determine |
| 358 | * if we should try to disconnect and reconnect to it. |
| 359 | */ |
| 360 | long mLastBindTime; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 361 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | /** |
| 363 | * Have we called mCurMethod.bindInput()? |
| 364 | */ |
| 365 | boolean mBoundToMethod; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 366 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | /** |
| 368 | * Currently enabled session. Only touched by service thread, not |
| 369 | * protected by a lock. |
| 370 | */ |
| 371 | SessionState mEnabledSession; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 372 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | /** |
| 374 | * True if the screen is on. The value is true initially. |
| 375 | */ |
| 376 | boolean mScreenOn = true; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 377 | |
| Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 378 | int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT; |
| 379 | int mImeWindowVis; |
| 380 | |
| Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 381 | private AlertDialog.Builder mDialogBuilder; |
| 382 | private AlertDialog mSwitchingDialog; |
| satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 383 | private View mSwitchingDialogTitleView; |
| Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 384 | private InputMethodInfo[] mIms; |
| 385 | private int[] mSubtypeIds; |
| satok | 5b927c43 | 2012-05-01 20:09:34 +0900 | [diff] [blame] | 386 | private Locale mLastSystemLocale; |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 387 | private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor(); |
| 388 | private final IPackageManager mIPackageManager; |
| John Spurlock | bc7b6fc | 2012-11-14 08:51:07 -0500 | [diff] [blame^] | 389 | private boolean mInputBoundToKeyguard; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 390 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | class SettingsObserver extends ContentObserver { |
| 392 | SettingsObserver(Handler handler) { |
| 393 | super(handler); |
| 394 | ContentResolver resolver = mContext.getContentResolver(); |
| 395 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 396 | Settings.Secure.DEFAULT_INPUT_METHOD), false, this); |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 397 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| satok | b6109bb | 2011-02-03 22:24:54 +0900 | [diff] [blame] | 398 | Settings.Secure.ENABLED_INPUT_METHODS), false, this); |
| 399 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 400 | Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 402 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | @Override public void onChange(boolean selfChange) { |
| 404 | synchronized (mMethodMap) { |
| 405 | updateFromSettingsLocked(); |
| 406 | } |
| 407 | } |
| 408 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 409 | |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 410 | class ImmsBroadcastReceiver extends android.content.BroadcastReceiver { |
| 411 | private void updateActive() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | // Inform the current client of the change in active status |
| Dianne Hackborn | a6e4134 | 2012-05-22 16:30:34 -0700 | [diff] [blame] | 413 | if (mCurClient != null && mCurClient.client != null) { |
| 414 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 415 | MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | } |
| 417 | } |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 418 | |
| 419 | @Override |
| 420 | public void onReceive(Context context, Intent intent) { |
| 421 | final String action = intent.getAction(); |
| 422 | if (Intent.ACTION_SCREEN_ON.equals(action)) { |
| 423 | mScreenOn = true; |
| 424 | refreshImeWindowVisibilityLocked(); |
| 425 | updateActive(); |
| 426 | return; |
| 427 | } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { |
| 428 | mScreenOn = false; |
| 429 | setImeWindowVisibilityStatusHiddenLocked(); |
| 430 | updateActive(); |
| 431 | return; |
| 432 | } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) { |
| 433 | hideInputMethodMenu(); |
| 434 | // No need to updateActive |
| 435 | return; |
| 436 | } else { |
| 437 | Slog.w(TAG, "Unexpected intent " + intent); |
| 438 | } |
| 439 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 441 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 442 | class MyPackageMonitor extends PackageMonitor { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 443 | private boolean isChangingPackagesOfCurrentUser() { |
| 444 | final int userId = getChangingUserId(); |
| 445 | final boolean retval = userId == mSettings.getCurrentUserId(); |
| 446 | if (DEBUG) { |
| 447 | Slog.d(TAG, "--- ignore this call back from a background user: " + userId); |
| 448 | } |
| 449 | return retval; |
| 450 | } |
| 451 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | @Override |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 453 | public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 454 | if (!isChangingPackagesOfCurrentUser()) { |
| 455 | return false; |
| 456 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | synchronized (mMethodMap) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 458 | String curInputMethodId = mSettings.getSelectedInputMethod(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | final int N = mMethodList.size(); |
| 460 | if (curInputMethodId != null) { |
| 461 | for (int i=0; i<N; i++) { |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 462 | InputMethodInfo imi = mMethodList.get(i); |
| 463 | if (imi.getId().equals(curInputMethodId)) { |
| 464 | for (String pkg : packages) { |
| 465 | if (imi.getPackageName().equals(pkg)) { |
| 466 | if (!doit) { |
| 467 | return true; |
| 468 | } |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 469 | resetSelectedInputMethodAndSubtypeLocked(""); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 470 | chooseNewDefaultIMELocked(); |
| 471 | return true; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | return false; |
| 479 | } |
| 480 | |
| 481 | @Override |
| 482 | public void onSomePackagesChanged() { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 483 | if (!isChangingPackagesOfCurrentUser()) { |
| 484 | return; |
| 485 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 486 | synchronized (mMethodMap) { |
| 487 | InputMethodInfo curIm = null; |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 488 | String curInputMethodId = mSettings.getSelectedInputMethod(); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 489 | final int N = mMethodList.size(); |
| 490 | if (curInputMethodId != null) { |
| 491 | for (int i=0; i<N; i++) { |
| 492 | InputMethodInfo imi = mMethodList.get(i); |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 493 | final String imiId = imi.getId(); |
| 494 | if (imiId.equals(curInputMethodId)) { |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 495 | curIm = imi; |
| 496 | } |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 497 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 498 | int change = isPackageDisappearing(imi.getPackageName()); |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 499 | if (isPackageModified(imi.getPackageName())) { |
| 500 | mFileManager.deleteAllInputMethodSubtypes(imiId); |
| 501 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 502 | if (change == PACKAGE_TEMPORARY_CHANGE |
| 503 | || change == PACKAGE_PERMANENT_CHANGE) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 504 | Slog.i(TAG, "Input method uninstalled, disabling: " |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 505 | + imi.getComponent()); |
| 506 | setInputMethodEnabledLocked(imi.getId(), false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 510 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 511 | buildInputMethodListLocked(mMethodList, mMethodMap); |
| 512 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | boolean changed = false; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 514 | |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 515 | if (curIm != null) { |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 516 | int change = isPackageDisappearing(curIm.getPackageName()); |
| 517 | if (change == PACKAGE_TEMPORARY_CHANGE |
| 518 | || change == PACKAGE_PERMANENT_CHANGE) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 519 | ServiceInfo si = null; |
| 520 | try { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 521 | si = mIPackageManager.getServiceInfo( |
| 522 | curIm.getComponent(), 0, mSettings.getCurrentUserId()); |
| 523 | } catch (RemoteException ex) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 524 | } |
| 525 | if (si == null) { |
| 526 | // Uh oh, current input method is no longer around! |
| 527 | // Pick another one... |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 528 | Slog.i(TAG, "Current input method removed: " + curInputMethodId); |
| satok | 15452a4 | 2011-10-28 17:58:28 +0900 | [diff] [blame] | 529 | setImeWindowVisibilityStatusHiddenLocked(); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 530 | if (!chooseNewDefaultIMELocked()) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 531 | changed = true; |
| 532 | curIm = null; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 533 | Slog.i(TAG, "Unsetting current input method"); |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 534 | resetSelectedInputMethodAndSubtypeLocked(""); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 535 | } |
| 536 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 537 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 538 | } |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 539 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 540 | if (curIm == null) { |
| 541 | // We currently don't have a default input method... is |
| 542 | // one now available? |
| 543 | changed = chooseNewDefaultIMELocked(); |
| 544 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 545 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 546 | if (changed) { |
| 547 | updateFromSettingsLocked(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | } |
| 551 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 552 | |
| Jean Chalard | e0d32a6 | 2011-10-20 20:36:07 +0900 | [diff] [blame] | 553 | private static class MethodCallback extends IInputMethodCallback.Stub { |
| 554 | private final IInputMethod mMethod; |
| 555 | private final InputMethodManagerService mParentIMMS; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 556 | |
| Jean Chalard | e0d32a6 | 2011-10-20 20:36:07 +0900 | [diff] [blame] | 557 | MethodCallback(final IInputMethod method, final InputMethodManagerService imms) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | mMethod = method; |
| Jean Chalard | e0d32a6 | 2011-10-20 20:36:07 +0900 | [diff] [blame] | 559 | mParentIMMS = imms; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 561 | |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 562 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | public void finishedEvent(int seq, boolean handled) throws RemoteException { |
| 564 | } |
| 565 | |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 566 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | public void sessionCreated(IInputMethodSession session) throws RemoteException { |
| Jean Chalard | e0d32a6 | 2011-10-20 20:36:07 +0900 | [diff] [blame] | 568 | mParentIMMS.onSessionCreated(mMethod, session); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | } |
| 570 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 571 | |
| satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 572 | private class HardKeyboardListener |
| 573 | implements WindowManagerService.OnHardKeyboardStatusChangeListener { |
| 574 | @Override |
| 575 | public void onHardKeyboardStatusChange(boolean available, boolean enabled) { |
| 576 | mHandler.sendMessage(mHandler.obtainMessage( |
| 577 | MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0)); |
| 578 | } |
| 579 | |
| 580 | public void handleHardKeyboardStatusChange(boolean available, boolean enabled) { |
| 581 | if (DEBUG) { |
| 582 | Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = " |
| 583 | + enabled); |
| 584 | } |
| 585 | synchronized(mMethodMap) { |
| 586 | if (mSwitchingDialog != null && mSwitchingDialogTitleView != null |
| 587 | && mSwitchingDialog.isShowing()) { |
| 588 | mSwitchingDialogTitleView.findViewById( |
| 589 | com.android.internal.R.id.hard_keyboard_section).setVisibility( |
| 590 | available ? View.VISIBLE : View.GONE); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | public InputMethodManagerService(Context context, WindowManagerService windowManager) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 597 | mIPackageManager = AppGlobals.getPackageManager(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | mContext = context; |
| Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 599 | mRes = context.getResources(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | mHandler = new Handler(this); |
| 601 | mIWindowManager = IWindowManager.Stub.asInterface( |
| 602 | ServiceManager.getService(Context.WINDOW_SERVICE)); |
| 603 | mCaller = new HandlerCaller(context, new HandlerCaller.Callback() { |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 604 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | public void executeMessage(Message msg) { |
| 606 | handleMessage(msg); |
| 607 | } |
| 608 | }); |
| satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 609 | mWindowManagerService = windowManager; |
| 610 | mHardKeyboardListener = new HardKeyboardListener(); |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 611 | |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 612 | mImeSwitcherNotification = new Notification(); |
| 613 | mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default; |
| 614 | mImeSwitcherNotification.when = 0; |
| 615 | mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT; |
| 616 | mImeSwitcherNotification.tickerText = null; |
| 617 | mImeSwitcherNotification.defaults = 0; // please be quiet |
| 618 | mImeSwitcherNotification.sound = null; |
| 619 | mImeSwitcherNotification.vibrate = null; |
| Daniel Sandler | 590d515 | 2012-06-14 16:10:13 -0400 | [diff] [blame] | 620 | |
| 621 | // Tag this notification specially so SystemUI knows it's important |
| 622 | mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" }; |
| 623 | |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 624 | Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER); |
| satok | 683e238 | 2011-07-12 08:28:52 +0900 | [diff] [blame] | 625 | mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0); |
| satok | b858c73 | 2011-07-22 19:54:34 +0900 | [diff] [blame] | 626 | |
| 627 | mShowOngoingImeSwitcherForPhones = false; |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 628 | |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 629 | final IntentFilter broadcastFilter = new IntentFilter(); |
| 630 | broadcastFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 631 | broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF); |
| 632 | broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
| 633 | mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 634 | |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 635 | mNotificationShown = false; |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 636 | int userId = 0; |
| 637 | try { |
| 638 | ActivityManagerNative.getDefault().registerUserSwitchObserver( |
| 639 | new IUserSwitchObserver.Stub() { |
| 640 | @Override |
| 641 | public void onUserSwitching(int newUserId, IRemoteCallback reply) { |
| Satoshi Kataoka | 5ade83b | 2012-09-26 22:59:41 +0900 | [diff] [blame] | 642 | synchronized(mMethodMap) { |
| 643 | switchUserLocked(newUserId); |
| 644 | } |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 645 | if (reply != null) { |
| 646 | try { |
| 647 | reply.sendResult(null); |
| 648 | } catch (RemoteException e) { |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | @Override |
| 654 | public void onUserSwitchComplete(int newUserId) throws RemoteException { |
| 655 | } |
| 656 | }); |
| 657 | userId = ActivityManagerNative.getDefault().getCurrentUser().id; |
| 658 | } catch (RemoteException e) { |
| 659 | Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e); |
| 660 | } |
| 661 | mMyPackageMonitor.register(mContext, null, true); |
| satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 662 | |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 663 | // mSettings should be created before buildInputMethodListLocked |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 664 | mSettings = new InputMethodSettings( |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 665 | mRes, context.getContentResolver(), mMethodMap, mMethodList, userId); |
| Satoshi Kataoka | 5ade83b | 2012-09-26 22:59:41 +0900 | [diff] [blame] | 666 | mFileManager = new InputMethodFileManager(mMethodMap, userId); |
| 667 | mImListManager = new InputMethodAndSubtypeListManager(context, this); |
| satok | 0a1bcf4 | 2012-05-16 19:26:31 +0900 | [diff] [blame] | 668 | |
| 669 | // Just checking if defaultImiId is empty or not |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 670 | final String defaultImiId = mSettings.getSelectedInputMethod(); |
| satok | 0a1bcf4 | 2012-05-16 19:26:31 +0900 | [diff] [blame] | 671 | mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId); |
| 672 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 673 | buildInputMethodListLocked(mMethodList, mMethodMap); |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 674 | mSettings.enableAllIMEsIfThereIsNoEnabledIME(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | |
| satok | 0a1bcf4 | 2012-05-16 19:26:31 +0900 | [diff] [blame] | 676 | if (!mImeSelectedOnBoot) { |
| 677 | Slog.w(TAG, "No IME selected. Choose the most applicable IME."); |
| satok | 5b927c43 | 2012-05-01 20:09:34 +0900 | [diff] [blame] | 678 | resetDefaultImeLocked(context); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 679 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 680 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | mSettingsObserver = new SettingsObserver(mHandler); |
| 682 | updateFromSettingsLocked(); |
| satok | 5b927c43 | 2012-05-01 20:09:34 +0900 | [diff] [blame] | 683 | |
| 684 | // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME |
| 685 | // according to the new system locale. |
| 686 | final IntentFilter filter = new IntentFilter(); |
| 687 | filter.addAction(Intent.ACTION_LOCALE_CHANGED); |
| 688 | mContext.registerReceiver( |
| 689 | new BroadcastReceiver() { |
| 690 | @Override |
| 691 | public void onReceive(Context context, Intent intent) { |
| 692 | synchronized(mMethodMap) { |
| 693 | checkCurrentLocaleChangedLocked(); |
| 694 | } |
| 695 | } |
| 696 | }, filter); |
| 697 | } |
| 698 | |
| satok | 5b927c43 | 2012-05-01 20:09:34 +0900 | [diff] [blame] | 699 | private void resetDefaultImeLocked(Context context) { |
| 700 | // Do not reset the default (current) IME when it is a 3rd-party IME |
| 701 | if (mCurMethodId != null && !isSystemIme(mMethodMap.get(mCurMethodId))) { |
| 702 | return; |
| 703 | } |
| 704 | |
| 705 | InputMethodInfo defIm = null; |
| 706 | for (InputMethodInfo imi : mMethodList) { |
| 707 | if (defIm == null) { |
| 708 | if (isValidSystemDefaultIme(imi, context)) { |
| 709 | defIm = imi; |
| 710 | Slog.i(TAG, "Selected default: " + imi.getId()); |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | if (defIm == null && mMethodList.size() > 0) { |
| 715 | defIm = getMostApplicableDefaultIMELocked(); |
| 716 | Slog.i(TAG, "No default found, using " + defIm.getId()); |
| 717 | } |
| 718 | if (defIm != null) { |
| 719 | setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false); |
| 720 | } |
| 721 | } |
| 722 | |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 723 | private void resetAllInternalStateLocked(boolean updateOnlyWhenLocaleChanged) { |
| 724 | if (!mSystemReady) { |
| 725 | // not system ready |
| 726 | return; |
| 727 | } |
| 728 | final Locale newLocale = mRes.getConfiguration().locale; |
| 729 | if (!updateOnlyWhenLocaleChanged |
| 730 | || (newLocale != null && !newLocale.equals(mLastSystemLocale))) { |
| 731 | if (!updateOnlyWhenLocaleChanged) { |
| 732 | hideCurrentInputLocked(0, null); |
| 733 | mCurMethodId = null; |
| Dianne Hackborn | 2ea9bae | 2012-11-02 18:43:48 -0700 | [diff] [blame] | 734 | unbindCurrentMethodLocked(true, false); |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 735 | } |
| 736 | if (DEBUG) { |
| 737 | Slog.i(TAG, "Locale has been changed to " + newLocale); |
| 738 | } |
| Satoshi Kataoka | 5ade83b | 2012-09-26 22:59:41 +0900 | [diff] [blame] | 739 | // InputMethodAndSubtypeListManager should be reset when the locale is changed. |
| 740 | mImListManager = new InputMethodAndSubtypeListManager(mContext, this); |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 741 | buildInputMethodListLocked(mMethodList, mMethodMap); |
| 742 | if (!updateOnlyWhenLocaleChanged) { |
| 743 | final String selectedImiId = mSettings.getSelectedInputMethod(); |
| 744 | if (TextUtils.isEmpty(selectedImiId)) { |
| 745 | // This is the first time of the user switch and |
| 746 | // set the current ime to the proper one. |
| 747 | resetDefaultImeLocked(mContext); |
| 748 | } |
| Satoshi Kataoka | d08a923 | 2012-09-28 15:59:58 +0900 | [diff] [blame] | 749 | } else { |
| 750 | // If the locale is changed, needs to reset the default ime |
| 751 | resetDefaultImeLocked(mContext); |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 752 | } |
| 753 | updateFromSettingsLocked(); |
| 754 | mLastSystemLocale = newLocale; |
| 755 | if (!updateOnlyWhenLocaleChanged) { |
| 756 | try { |
| 757 | startInputInnerLocked(); |
| 758 | } catch (RuntimeException e) { |
| 759 | Slog.w(TAG, "Unexpected exception", e); |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | private void checkCurrentLocaleChangedLocked() { |
| 766 | resetAllInternalStateLocked(true); |
| 767 | } |
| 768 | |
| Satoshi Kataoka | 5ade83b | 2012-09-26 22:59:41 +0900 | [diff] [blame] | 769 | private void switchUserLocked(int newUserId) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 770 | mSettings.setCurrentUserId(newUserId); |
| Satoshi Kataoka | 5ade83b | 2012-09-26 22:59:41 +0900 | [diff] [blame] | 771 | // InputMethodFileManager should be reset when the user is changed |
| 772 | mFileManager = new InputMethodFileManager(mMethodMap, newUserId); |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 773 | resetAllInternalStateLocked(false); |
| 774 | } |
| 775 | |
| satok | 0a1bcf4 | 2012-05-16 19:26:31 +0900 | [diff] [blame] | 776 | private boolean isValidSystemDefaultIme(InputMethodInfo imi, Context context) { |
| 777 | if (!mSystemReady) { |
| 778 | return false; |
| 779 | } |
| satok | 5b927c43 | 2012-05-01 20:09:34 +0900 | [diff] [blame] | 780 | if (!isSystemIme(imi)) { |
| 781 | return false; |
| 782 | } |
| 783 | if (imi.getIsDefaultResourceId() != 0) { |
| 784 | try { |
| 785 | Resources res = context.createPackageContext( |
| 786 | imi.getPackageName(), 0).getResources(); |
| 787 | if (res.getBoolean(imi.getIsDefaultResourceId()) |
| 788 | && containsSubtypeOf(imi, context.getResources().getConfiguration(). |
| 789 | locale.getLanguage())) { |
| 790 | return true; |
| 791 | } |
| 792 | } catch (PackageManager.NameNotFoundException ex) { |
| 793 | } catch (Resources.NotFoundException ex) { |
| 794 | } |
| 795 | } |
| 796 | if (imi.getSubtypeCount() == 0) { |
| 797 | Slog.w(TAG, "Found no subtypes in a system IME: " + imi.getPackageName()); |
| 798 | } |
| 799 | return false; |
| 800 | } |
| 801 | |
| 802 | private static boolean isSystemImeThatHasEnglishSubtype(InputMethodInfo imi) { |
| 803 | if (!isSystemIme(imi)) { |
| 804 | return false; |
| 805 | } |
| 806 | return containsSubtypeOf(imi, ENGLISH_LOCALE.getLanguage()); |
| 807 | } |
| 808 | |
| 809 | private static boolean containsSubtypeOf(InputMethodInfo imi, String language) { |
| 810 | final int N = imi.getSubtypeCount(); |
| 811 | for (int i = 0; i < N; ++i) { |
| 812 | if (imi.getSubtypeAt(i).getLocale().startsWith(language)) { |
| 813 | return true; |
| 814 | } |
| 815 | } |
| 816 | return false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | @Override |
| 820 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 821 | throws RemoteException { |
| 822 | try { |
| 823 | return super.onTransact(code, data, reply, flags); |
| 824 | } catch (RuntimeException e) { |
| 825 | // The input method manager only throws security exceptions, so let's |
| 826 | // log all others. |
| 827 | if (!(e instanceof SecurityException)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 828 | Slog.e(TAG, "Input Method Manager Crash", e); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | } |
| 830 | throw e; |
| 831 | } |
| 832 | } |
| 833 | |
| Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 834 | public void systemReady(StatusBarManagerService statusBar) { |
| Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 835 | synchronized (mMethodMap) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 836 | if (DEBUG) { |
| 837 | Slog.d(TAG, "--- systemReady"); |
| 838 | } |
| Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 839 | if (!mSystemReady) { |
| 840 | mSystemReady = true; |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 841 | mKeyguardManager = |
| 842 | (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); |
| Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 843 | mNotificationManager = (NotificationManager) |
| 844 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 845 | mStatusBar = statusBar; |
| 846 | statusBar.setIconVisibility("ime", false); |
| 847 | updateImeWindowStatusLocked(); |
| satok | b858c73 | 2011-07-22 19:54:34 +0900 | [diff] [blame] | 848 | mShowOngoingImeSwitcherForPhones = mRes.getBoolean( |
| 849 | com.android.internal.R.bool.show_ongoing_ime_switcher); |
| satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 850 | if (mShowOngoingImeSwitcherForPhones) { |
| 851 | mWindowManagerService.setOnHardKeyboardStatusChangeListener( |
| 852 | mHardKeyboardListener); |
| 853 | } |
| satok | 0a1bcf4 | 2012-05-16 19:26:31 +0900 | [diff] [blame] | 854 | buildInputMethodListLocked(mMethodList, mMethodMap); |
| 855 | if (!mImeSelectedOnBoot) { |
| 856 | Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here."); |
| 857 | checkCurrentLocaleChangedLocked(); |
| 858 | } |
| 859 | mLastSystemLocale = mRes.getConfiguration().locale; |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 860 | try { |
| 861 | startInputInnerLocked(); |
| 862 | } catch (RuntimeException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 863 | Slog.w(TAG, "Unexpected exception", e); |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 864 | } |
| Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 865 | } |
| 866 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 868 | |
| satok | 15452a4 | 2011-10-28 17:58:28 +0900 | [diff] [blame] | 869 | private void setImeWindowVisibilityStatusHiddenLocked() { |
| 870 | mImeWindowVis = 0; |
| 871 | updateImeWindowStatusLocked(); |
| 872 | } |
| 873 | |
| satok | 3afd6c0 | 2011-11-18 08:38:19 +0900 | [diff] [blame] | 874 | private void refreshImeWindowVisibilityLocked() { |
| 875 | final Configuration conf = mRes.getConfiguration(); |
| 876 | final boolean haveHardKeyboard = conf.keyboard |
| 877 | != Configuration.KEYBOARD_NOKEYS; |
| 878 | final boolean hardKeyShown = haveHardKeyboard |
| 879 | && conf.hardKeyboardHidden |
| 880 | != Configuration.HARDKEYBOARDHIDDEN_YES; |
| John Spurlock | bc7b6fc | 2012-11-14 08:51:07 -0500 | [diff] [blame^] | 881 | final boolean isScreenLocked = |
| 882 | mKeyguardManager != null && mKeyguardManager.isKeyguardLocked(); |
| 883 | final boolean isScreenSecurelyLocked = |
| 884 | isScreenLocked && mKeyguardManager.isKeyguardSecure(); |
| 885 | final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard); |
| 886 | mImeWindowVis = (!isScreenSecurelyLocked && (inputShown || hardKeyShown)) ? |
| satok | 3afd6c0 | 2011-11-18 08:38:19 +0900 | [diff] [blame] | 887 | (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0; |
| 888 | updateImeWindowStatusLocked(); |
| 889 | } |
| 890 | |
| satok | 15452a4 | 2011-10-28 17:58:28 +0900 | [diff] [blame] | 891 | private void updateImeWindowStatusLocked() { |
| satok | dbf2950 | 2011-08-25 15:28:23 +0900 | [diff] [blame] | 892 | setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition); |
| Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 895 | // --------------------------------------------------------------------------------------- |
| 896 | // Check whether or not this is a valid IPC. Assumes an IPC is valid when either |
| 897 | // 1) it comes from the system process |
| 898 | // 2) the calling process' user id is identical to the current user id IMMS thinks. |
| 899 | private boolean calledFromValidUser() { |
| 900 | final int uid = Binder.getCallingUid(); |
| 901 | final int userId = UserHandle.getUserId(uid); |
| 902 | if (DEBUG) { |
| 903 | Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? " |
| 904 | + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID |
| 905 | + " calling userId = " + userId + ", foreground user id = " |
| Satoshi Kataoka | c86884c | 2012-10-09 15:20:29 +0900 | [diff] [blame] | 906 | + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()); |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 907 | } |
| 908 | if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) { |
| 909 | return true; |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 910 | } |
| Satoshi Kataoka | 135e5fb | 2012-09-28 18:25:06 +0900 | [diff] [blame] | 911 | |
| 912 | // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the |
| 913 | // foreground user, not for the user of that process. Accordingly InputMethodManagerService |
| 914 | // must not manage background users' states in any functions. |
| 915 | // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded |
| 916 | // by a token. |
| 917 | if (mContext.checkCallingOrSelfPermission( |
| 918 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 919 | == PackageManager.PERMISSION_GRANTED) { |
| 920 | if (DEBUG) { |
| 921 | Slog.d(TAG, "--- Access granted because the calling process has " |
| 922 | + "the INTERACT_ACROSS_USERS_FULL permission"); |
| 923 | } |
| 924 | return true; |
| 925 | } |
| 926 | Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace()); |
| 927 | return false; |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | private boolean bindCurrentInputMethodService( |
| 931 | Intent service, ServiceConnection conn, int flags) { |
| 932 | if (service == null || conn == null) { |
| 933 | Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn); |
| 934 | return false; |
| 935 | } |
| 936 | return mContext.bindService(service, conn, flags, mSettings.getCurrentUserId()); |
| 937 | } |
| 938 | |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 939 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 940 | public List<InputMethodInfo> getInputMethodList() { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 941 | // TODO: Make this work even for non-current users? |
| 942 | if (!calledFromValidUser()) { |
| 943 | return Collections.emptyList(); |
| 944 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | synchronized (mMethodMap) { |
| 946 | return new ArrayList<InputMethodInfo>(mMethodList); |
| 947 | } |
| 948 | } |
| 949 | |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 950 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 951 | public List<InputMethodInfo> getEnabledInputMethodList() { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 952 | // TODO: Make this work even for non-current users? |
| 953 | if (!calledFromValidUser()) { |
| 954 | return Collections.emptyList(); |
| 955 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | synchronized (mMethodMap) { |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 957 | return mSettings.getEnabledInputMethodListLocked(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | |
| satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 961 | private HashMap<InputMethodInfo, List<InputMethodSubtype>> |
| 962 | getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() { |
| 963 | HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes = |
| 964 | new HashMap<InputMethodInfo, List<InputMethodSubtype>>(); |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 965 | for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) { |
| satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 966 | enabledInputMethodAndSubtypes.put( |
| 967 | imi, getEnabledInputMethodSubtypeListLocked(imi, true)); |
| 968 | } |
| 969 | return enabledInputMethodAndSubtypes; |
| 970 | } |
| 971 | |
| 972 | public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi, |
| 973 | boolean allowsImplicitlySelectedSubtypes) { |
| 974 | if (imi == null && mCurMethodId != null) { |
| 975 | imi = mMethodMap.get(mCurMethodId); |
| 976 | } |
| satok | 7265d9b | 2011-02-14 15:47:30 +0900 | [diff] [blame] | 977 | List<InputMethodSubtype> enabledSubtypes = |
| satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 978 | mSettings.getEnabledInputMethodSubtypeListLocked(imi); |
| satok | 7265d9b | 2011-02-14 15:47:30 +0900 | [diff] [blame] | 979 | if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) { |
| satok | a86f5e4 | 2011-09-02 17:12:42 +0900 | [diff] [blame] | 980 | enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi); |
| satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 981 | } |
| satok | 7265d9b | 2011-02-14 15:47:30 +0900 | [diff] [blame] | 982 | return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes); |
| satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 983 | } |
| 984 | |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 985 | @Override |
| satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 986 | public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi, |
| 987 | boolean allowsImplicitlySelectedSubtypes) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 988 | // TODO: Make this work even for non-current users? |
| 989 | if (!calledFromValidUser()) { |
| 990 | return Collections.emptyList(); |
| 991 | } |
| satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 992 | synchronized (mMethodMap) { |
| satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 993 | return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes); |
| satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 997 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 998 | public void addClient(IInputMethodClient client, |
| 999 | IInputContext inputContext, int uid, int pid) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1000 | if (!calledFromValidUser()) { |
| 1001 | return; |
| 1002 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1003 | synchronized (mMethodMap) { |
| 1004 | mClients.put(client.asBinder(), new ClientState(client, |
| 1005 | inputContext, uid, pid)); |
| 1006 | } |
| 1007 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1008 | |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1009 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1010 | public void removeClient(IInputMethodClient client) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1011 | if (!calledFromValidUser()) { |
| 1012 | return; |
| 1013 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1014 | synchronized (mMethodMap) { |
| 1015 | mClients.remove(client.asBinder()); |
| 1016 | } |
| 1017 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1018 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1019 | void executeOrSendMessage(IInterface target, Message msg) { |
| 1020 | if (target.asBinder() instanceof Binder) { |
| 1021 | mCaller.sendMessage(msg); |
| 1022 | } else { |
| 1023 | handleMessage(msg); |
| 1024 | msg.recycle(); |
| 1025 | } |
| 1026 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1027 | |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1028 | void unbindCurrentClientLocked() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1029 | if (mCurClient != null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1030 | if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = " |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1031 | + mCurClient.client.asBinder()); |
| 1032 | if (mBoundToMethod) { |
| 1033 | mBoundToMethod = false; |
| 1034 | if (mCurMethod != null) { |
| 1035 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageO( |
| 1036 | MSG_UNBIND_INPUT, mCurMethod)); |
| 1037 | } |
| 1038 | } |
| Dianne Hackborn | a6e4134 | 2012-05-22 16:30:34 -0700 | [diff] [blame] | 1039 | |
| 1040 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 1041 | MSG_SET_ACTIVE, 0, mCurClient)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1042 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 1043 | MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); |
| 1044 | mCurClient.sessionRequested = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | mCurClient = null; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1046 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1047 | hideInputMethodMenuLocked(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1048 | } |
| 1049 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1050 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1051 | private int getImeShowFlags() { |
| 1052 | int flags = 0; |
| 1053 | if (mShowForced) { |
| 1054 | flags |= InputMethod.SHOW_FORCED |
| 1055 | | InputMethod.SHOW_EXPLICIT; |
| 1056 | } else if (mShowExplicitlyRequested) { |
| 1057 | flags |= InputMethod.SHOW_EXPLICIT; |
| 1058 | } |
| 1059 | return flags; |
| 1060 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1061 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1062 | private int getAppShowFlags() { |
| 1063 | int flags = 0; |
| 1064 | if (mShowForced) { |
| 1065 | flags |= InputMethodManager.SHOW_FORCED; |
| 1066 | } else if (!mShowExplicitlyRequested) { |
| 1067 | flags |= InputMethodManager.SHOW_IMPLICIT; |
| 1068 | } |
| 1069 | return flags; |
| 1070 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1071 | |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1072 | InputBindResult attachNewInputLocked(boolean initial) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | if (!mBoundToMethod) { |
| 1074 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 1075 | MSG_BIND_INPUT, mCurMethod, mCurClient.binding)); |
| 1076 | mBoundToMethod = true; |
| 1077 | } |
| 1078 | final SessionState session = mCurClient.curSession; |
| 1079 | if (initial) { |
| 1080 | executeOrSendMessage(session.method, mCaller.obtainMessageOOO( |
| 1081 | MSG_START_INPUT, session, mCurInputContext, mCurAttribute)); |
| 1082 | } else { |
| 1083 | executeOrSendMessage(session.method, mCaller.obtainMessageOOO( |
| 1084 | MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute)); |
| 1085 | } |
| 1086 | if (mShowRequested) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1087 | if (DEBUG) Slog.v(TAG, "Attach new input asks to show input"); |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1088 | showCurrentInputLocked(getAppShowFlags(), null); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1089 | } |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1090 | return new InputBindResult(session.session, mCurId, mCurSeq); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1091 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1092 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1093 | InputBindResult startInputLocked(IInputMethodClient client, |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1094 | IInputContext inputContext, EditorInfo attribute, int controlFlags) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1095 | // If no method is currently selected, do nothing. |
| 1096 | if (mCurMethodId == null) { |
| 1097 | return mNoBinding; |
| 1098 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1099 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1100 | ClientState cs = mClients.get(client.asBinder()); |
| 1101 | if (cs == null) { |
| 1102 | throw new IllegalArgumentException("unknown client " |
| 1103 | + client.asBinder()); |
| 1104 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1105 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1106 | try { |
| 1107 | if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) { |
| 1108 | // Check with the window manager to make sure this client actually |
| 1109 | // has a window with focus. If not, reject. This is thread safe |
| 1110 | // because if the focus changes some time before or after, the |
| 1111 | // next client receiving focus that has any interest in input will |
| 1112 | // be calling through here after that change happens. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1113 | Slog.w(TAG, "Starting input on non-focused client " + cs.client |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1114 | + " (uid=" + cs.uid + " pid=" + cs.pid + ")"); |
| 1115 | return null; |
| 1116 | } |
| 1117 | } catch (RemoteException e) { |
| 1118 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1119 | |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1120 | return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags); |
| 1121 | } |
| 1122 | |
| 1123 | InputBindResult startInputUncheckedLocked(ClientState cs, |
| 1124 | IInputContext inputContext, EditorInfo attribute, int controlFlags) { |
| 1125 | // If no method is currently selected, do nothing. |
| 1126 | if (mCurMethodId == null) { |
| 1127 | return mNoBinding; |
| 1128 | } |
| 1129 | |
| John Spurlock | bc7b6fc | 2012-11-14 08:51:07 -0500 | [diff] [blame^] | 1130 | if (mCurClient == null) { |
| 1131 | mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked(); |
| 1132 | if (DEBUG) { |
| 1133 | Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1137 | if (mCurClient != cs) { |
| 1138 | // If the client is changing, we need to switch over to the new |
| 1139 | // one. |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1140 | unbindCurrentClientLocked(); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1141 | if (DEBUG) Slog.v(TAG, "switching to client: client = " |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1142 | + cs.client.asBinder()); |
| 1143 | |
| 1144 | // If the screen is on, inform the new client it is active |
| 1145 | if (mScreenOn) { |
| Dianne Hackborn | a6e4134 | 2012-05-22 16:30:34 -0700 | [diff] [blame] | 1146 | executeOrSendMessage(cs.client, mCaller.obtainMessageIO( |
| 1147 | MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1148 | } |
| 1149 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1150 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1151 | // Bump up the sequence for this client and attach it. |
| 1152 | mCurSeq++; |
| 1153 | if (mCurSeq <= 0) mCurSeq = 1; |
| 1154 | mCurClient = cs; |
| 1155 | mCurInputContext = inputContext; |
| 1156 | mCurAttribute = attribute; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1157 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1158 | // Check if the input method is changing. |
| 1159 | if (mCurId != null && mCurId.equals(mCurMethodId)) { |
| 1160 | if (cs.curSession != null) { |
| 1161 | // Fast case: if we are already connected to the input method, |
| 1162 | // then just return it. |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1163 | return attachNewInputLocked( |
| 1164 | (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1165 | } |
| 1166 | if (mHaveConnection) { |
| 1167 | if (mCurMethod != null) { |
| 1168 | if (!cs.sessionRequested) { |
| 1169 | cs.sessionRequested = true; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1170 | if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1171 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 1172 | MSG_CREATE_SESSION, mCurMethod, |
| Jean Chalard | e0d32a6 | 2011-10-20 20:36:07 +0900 | [diff] [blame] | 1173 | new MethodCallback(mCurMethod, this))); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1174 | } |
| 1175 | // Return to client, and we will get back with it when |
| 1176 | // we have had a session made for it. |
| 1177 | return new InputBindResult(null, mCurId, mCurSeq); |
| 1178 | } else if (SystemClock.uptimeMillis() |
| 1179 | < (mLastBindTime+TIME_TO_RECONNECT)) { |
| 1180 | // In this case we have connected to the service, but |
| 1181 | // don't yet have its interface. If it hasn't been too |
| 1182 | // long since we did the connection, we'll return to |
| 1183 | // the client and wait to get the service interface so |
| 1184 | // we can report back. If it has been too long, we want |
| 1185 | // to fall through so we can try a disconnect/reconnect |
| 1186 | // to see if we can get back in touch with the service. |
| 1187 | return new InputBindResult(null, mCurId, mCurSeq); |
| 1188 | } else { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1189 | EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, |
| 1190 | mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1191 | } |
| 1192 | } |
| 1193 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1194 | |
| Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 1195 | return startInputInnerLocked(); |
| 1196 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1197 | |
| Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 1198 | InputBindResult startInputInnerLocked() { |
| 1199 | if (mCurMethodId == null) { |
| 1200 | return mNoBinding; |
| 1201 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1202 | |
| Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 1203 | if (!mSystemReady) { |
| 1204 | // If the system is not yet ready, we shouldn't be running third |
| 1205 | // party code. |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 1206 | return new InputBindResult(null, mCurMethodId, mCurSeq); |
| Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 1207 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1208 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1209 | InputMethodInfo info = mMethodMap.get(mCurMethodId); |
| 1210 | if (info == null) { |
| 1211 | throw new IllegalArgumentException("Unknown id: " + mCurMethodId); |
| 1212 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1213 | |
| Dianne Hackborn | 2ea9bae | 2012-11-02 18:43:48 -0700 | [diff] [blame] | 1214 | unbindCurrentMethodLocked(false, true); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1215 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1216 | mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE); |
| 1217 | mCurIntent.setComponent(info.getComponent()); |
| Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 1218 | mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL, |
| 1219 | com.android.internal.R.string.input_method_binding_label); |
| 1220 | mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity( |
| 1221 | mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0)); |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1222 | if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE |
| Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 1223 | | Context.BIND_NOT_VISIBLE)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1224 | mLastBindTime = SystemClock.uptimeMillis(); |
| 1225 | mHaveConnection = true; |
| 1226 | mCurId = info.getId(); |
| 1227 | mCurToken = new Binder(); |
| 1228 | try { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1229 | if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1230 | mIWindowManager.addWindowToken(mCurToken, |
| 1231 | WindowManager.LayoutParams.TYPE_INPUT_METHOD); |
| 1232 | } catch (RemoteException e) { |
| 1233 | } |
| 1234 | return new InputBindResult(null, mCurId, mCurSeq); |
| 1235 | } else { |
| 1236 | mCurIntent = null; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1237 | Slog.w(TAG, "Failure connecting to input method service: " |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1238 | + mCurIntent); |
| 1239 | } |
| 1240 | return null; |
| 1241 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1242 | |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1243 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1244 | public InputBindResult startInput(IInputMethodClient client, |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1245 | IInputContext inputContext, EditorInfo attribute, int controlFlags) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1246 | if (!calledFromValidUser()) { |
| 1247 | return null; |
| 1248 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1249 | synchronized (mMethodMap) { |
| 1250 | final long ident = Binder.clearCallingIdentity(); |
| 1251 | try { |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1252 | return startInputLocked(client, inputContext, attribute, controlFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1253 | } finally { |
| 1254 | Binder.restoreCallingIdentity(ident); |
| 1255 | } |
| 1256 | } |
| 1257 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1258 | |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1259 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1260 | public void finishInput(IInputMethodClient client) { |
| 1261 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1262 | |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1263 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1264 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 1265 | synchronized (mMethodMap) { |
| 1266 | if (mCurIntent != null && name.equals(mCurIntent.getComponent())) { |
| 1267 | mCurMethod = IInputMethod.Stub.asInterface(service); |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 1268 | if (mCurToken == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1269 | Slog.w(TAG, "Service connected without a token!"); |
| Dianne Hackborn | 2ea9bae | 2012-11-02 18:43:48 -0700 | [diff] [blame] | 1270 | unbindCurrentMethodLocked(false, false); |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 1271 | return; |
| 1272 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1273 | if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken); |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 1274 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 1275 | MSG_ATTACH_TOKEN, mCurMethod, mCurToken)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1276 | if (mCurClient != null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1277 | if (DEBUG) Slog.v(TAG, "Creating first session while with client " |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 1278 | + mCurClient); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1279 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 1280 | MSG_CREATE_SESSION, mCurMethod, |
| Jean Chalard | e0d32a6 | 2011-10-20 20:36:07 +0900 | [diff] [blame] | 1281 | new MethodCallback(mCurMethod, this))); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | void onSessionCreated(IInputMethod method, IInputMethodSession session) { |
| 1288 | synchronized (mMethodMap) { |
| 1289 | if (mCurMethod != null && method != null |
| 1290 | && mCurMethod.asBinder() == method.asBinder()) { |
| 1291 | if (mCurClient != null) { |
| 1292 | mCurClient.curSession = new SessionState(mCurClient, |
| 1293 | method, session); |
| 1294 | mCurClient.sessionRequested = false; |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1295 | InputBindResult res = attachNewInputLocked(true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1296 | if (res.method != null) { |
| 1297 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO( |
| 1298 | MSG_BIND_METHOD, mCurClient.client, res)); |
| 1299 | } |
| 1300 | } |
| 1301 | } |
| 1302 | } |
| 1303 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1304 | |
| Dianne Hackborn | 2ea9bae | 2012-11-02 18:43:48 -0700 | [diff] [blame] | 1305 | void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) { |
| Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 1306 | if (mVisibleBound) { |
| 1307 | mContext.unbindService(mVisibleConnection); |
| 1308 | mVisibleBound = false; |
| 1309 | } |
| 1310 | |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1311 | if (mHaveConnection) { |
| 1312 | mContext.unbindService(this); |
| 1313 | mHaveConnection = false; |
| 1314 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1315 | |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1316 | if (mCurToken != null) { |
| 1317 | try { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1318 | if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken); |
| Dianne Hackborn | 2ea9bae | 2012-11-02 18:43:48 -0700 | [diff] [blame] | 1319 | if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) { |
| satok | e0a9941 | 2012-05-10 02:22:58 +0900 | [diff] [blame] | 1320 | // The current IME is shown. Hence an IME switch (transition) is happening. |
| 1321 | mWindowManagerService.saveLastInputMethodWindowForTransition(); |
| 1322 | } |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1323 | mIWindowManager.removeWindowToken(mCurToken); |
| 1324 | } catch (RemoteException e) { |
| 1325 | } |
| 1326 | mCurToken = null; |
| 1327 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1328 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1329 | mCurId = null; |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1330 | clearCurMethodLocked(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1331 | |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1332 | if (reportToClient && mCurClient != null) { |
| 1333 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 1334 | MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); |
| 1335 | } |
| 1336 | } |
| 1337 | |
| Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 1338 | private void finishSession(SessionState sessionState) { |
| 1339 | if (sessionState != null && sessionState.session != null) { |
| 1340 | try { |
| 1341 | sessionState.session.finishSession(); |
| 1342 | } catch (RemoteException e) { |
| Jean-Baptiste Queru | 9d0f6df | 2010-03-29 12:55:09 -0700 | [diff] [blame] | 1343 | Slog.w(TAG, "Session failed to close due to remote exception", e); |
| satok | 15452a4 | 2011-10-28 17:58:28 +0900 | [diff] [blame] | 1344 | setImeWindowVisibilityStatusHiddenLocked(); |
| Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 1345 | } |
| 1346 | } |
| 1347 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1348 | |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1349 | void clearCurMethodLocked() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1350 | if (mCurMethod != null) { |
| 1351 | for (ClientState cs : mClients.values()) { |
| 1352 | cs.sessionRequested = false; |
| Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 1353 | finishSession(cs.curSession); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1354 | cs.curSession = null; |
| 1355 | } |
| Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 1356 | |
| 1357 | finishSession(mEnabledSession); |
| 1358 | mEnabledSession = null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1359 | mCurMethod = null; |
| 1360 | } |
| Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 1361 | if (mStatusBar != null) { |
| 1362 | mStatusBar.setIconVisibility("ime", false); |
| 1363 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1364 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1365 | |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1366 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1367 | public void onServiceDisconnected(ComponentName name) { |
| 1368 | synchronized (mMethodMap) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1369 | if (DEBUG) Slog.v(TAG, "Service disconnected: " + name |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1370 | + " mCurIntent=" + mCurIntent); |
| 1371 | if (mCurMethod != null && mCurIntent != null |
| 1372 | && name.equals(mCurIntent.getComponent())) { |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1373 | clearCurMethodLocked(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1374 | // We consider this to be a new bind attempt, since the system |
| 1375 | // should now try to restart the service for us. |
| 1376 | mLastBindTime = SystemClock.uptimeMillis(); |
| 1377 | mShowRequested = mInputShown; |
| 1378 | mInputShown = false; |
| 1379 | if (mCurClient != null) { |
| 1380 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 1381 | MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); |
| 1382 | } |
| 1383 | } |
| 1384 | } |
| 1385 | } |
| 1386 | |
| satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1387 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1388 | public void updateStatusIcon(IBinder token, String packageName, int iconId) { |
| Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1389 | int uid = Binder.getCallingUid(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1390 | long ident = Binder.clearCallingIdentity(); |
| 1391 | try { |
| 1392 | if (token == null || mCurToken != token) { |
| Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1393 | Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1394 | return; |
| 1395 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1396 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1397 | synchronized (mMethodMap) { |
| 1398 | if (iconId == 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1399 | if (DEBUG) Slog.d(TAG, "hide the small icon for the input method"); |
| Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 1400 | if (mStatusBar != null) { |
| 1401 | mStatusBar.setIconVisibility("ime", false); |
| 1402 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1403 | } else if (packageName != null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1404 | if (DEBUG) Slog.d(TAG, "show a small icon for the input method"); |
| Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 1405 | CharSequence contentDescription = null; |
| 1406 | try { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1407 | // Use PackageManager to load label |
| 1408 | final PackageManager packageManager = mContext.getPackageManager(); |
| Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 1409 | contentDescription = packageManager.getApplicationLabel( |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1410 | mIPackageManager.getApplicationInfo(packageName, 0, |
| 1411 | mSettings.getCurrentUserId())); |
| 1412 | } catch (RemoteException e) { |
| Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 1413 | /* ignore */ |
| 1414 | } |
| Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 1415 | if (mStatusBar != null) { |
| 1416 | mStatusBar.setIcon("ime", packageName, iconId, 0, |
| 1417 | contentDescription != null |
| 1418 | ? contentDescription.toString() : null); |
| 1419 | mStatusBar.setIconVisibility("ime", true); |
| 1420 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1421 | } |
| 1422 | } |
| 1423 | } finally { |
| 1424 | Binder.restoreCallingIdentity(ident); |
| 1425 | } |
| 1426 | } |
| 1427 | |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1428 | private boolean needsToShowImeSwitchOngoingNotification() { |
| 1429 | if (!mShowOngoingImeSwitcherForPhones) return false; |
| satok | 2c93efc | 2012-04-02 19:33:47 +0900 | [diff] [blame] | 1430 | if (isScreenLocked()) return false; |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1431 | synchronized (mMethodMap) { |
| 1432 | List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked(); |
| 1433 | final int N = imis.size(); |
| satok | b635941 | 2011-06-28 17:47:41 +0900 | [diff] [blame] | 1434 | if (N > 2) return true; |
| 1435 | if (N < 1) return false; |
| 1436 | int nonAuxCount = 0; |
| 1437 | int auxCount = 0; |
| 1438 | InputMethodSubtype nonAuxSubtype = null; |
| 1439 | InputMethodSubtype auxSubtype = null; |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1440 | for(int i = 0; i < N; ++i) { |
| 1441 | final InputMethodInfo imi = imis.get(i); |
| 1442 | final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked( |
| 1443 | imi, true); |
| 1444 | final int subtypeCount = subtypes.size(); |
| 1445 | if (subtypeCount == 0) { |
| satok | b635941 | 2011-06-28 17:47:41 +0900 | [diff] [blame] | 1446 | ++nonAuxCount; |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1447 | } else { |
| 1448 | for (int j = 0; j < subtypeCount; ++j) { |
| satok | b635941 | 2011-06-28 17:47:41 +0900 | [diff] [blame] | 1449 | final InputMethodSubtype subtype = subtypes.get(j); |
| 1450 | if (!subtype.isAuxiliary()) { |
| 1451 | ++nonAuxCount; |
| 1452 | nonAuxSubtype = subtype; |
| 1453 | } else { |
| 1454 | ++auxCount; |
| 1455 | auxSubtype = subtype; |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1456 | } |
| 1457 | } |
| 1458 | } |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1459 | } |
| satok | b635941 | 2011-06-28 17:47:41 +0900 | [diff] [blame] | 1460 | if (nonAuxCount > 1 || auxCount > 1) { |
| 1461 | return true; |
| 1462 | } else if (nonAuxCount == 1 && auxCount == 1) { |
| 1463 | if (nonAuxSubtype != null && auxSubtype != null |
| satok | 9747f89 | 2011-09-12 15:56:40 +0900 | [diff] [blame] | 1464 | && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale()) |
| 1465 | || auxSubtype.overridesImplicitlyEnabledSubtype() |
| 1466 | || nonAuxSubtype.overridesImplicitlyEnabledSubtype()) |
| satok | b635941 | 2011-06-28 17:47:41 +0900 | [diff] [blame] | 1467 | && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) { |
| 1468 | return false; |
| 1469 | } |
| 1470 | return true; |
| 1471 | } |
| 1472 | return false; |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1473 | } |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1474 | } |
| 1475 | |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1476 | // Caution! This method is called in this class. Handle multi-user carefully |
| satok | dbf2950 | 2011-08-25 15:28:23 +0900 | [diff] [blame] | 1477 | @SuppressWarnings("deprecation") |
| satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1478 | @Override |
| Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 1479 | public void setImeWindowStatus(IBinder token, int vis, int backDisposition) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1480 | final long ident = Binder.clearCallingIdentity(); |
| satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 1481 | try { |
| 1482 | if (token == null || mCurToken != token) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1483 | int uid = Binder.getCallingUid(); |
| Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 1484 | Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token); |
| satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 1485 | return; |
| 1486 | } |
| 1487 | |
| 1488 | synchronized (mMethodMap) { |
| Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 1489 | mImeWindowVis = vis; |
| 1490 | mBackDisposition = backDisposition; |
| Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 1491 | if (mStatusBar != null) { |
| 1492 | mStatusBar.setImeWindowStatus(token, vis, backDisposition); |
| 1493 | } |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1494 | final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0; |
| satok | 5bc8e73 | 2011-07-22 21:07:23 +0900 | [diff] [blame] | 1495 | final InputMethodInfo imi = mMethodMap.get(mCurMethodId); |
| 1496 | if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1497 | // Used to load label |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1498 | final PackageManager pm = mContext.getPackageManager(); |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1499 | final CharSequence title = mRes.getText( |
| 1500 | com.android.internal.R.string.select_input_method); |
| satok | 5bc8e73 | 2011-07-22 21:07:23 +0900 | [diff] [blame] | 1501 | final CharSequence imiLabel = imi.loadLabel(pm); |
| 1502 | final CharSequence summary = mCurrentSubtype != null |
| 1503 | ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext, |
| 1504 | imi.getPackageName(), imi.getServiceInfo().applicationInfo), |
| 1505 | (TextUtils.isEmpty(imiLabel) ? |
| Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 1506 | "" : " - " + imiLabel)) |
| satok | 5bc8e73 | 2011-07-22 21:07:23 +0900 | [diff] [blame] | 1507 | : imiLabel; |
| 1508 | |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1509 | mImeSwitcherNotification.setLatestEventInfo( |
| satok | 5bc8e73 | 2011-07-22 21:07:23 +0900 | [diff] [blame] | 1510 | mContext, title, summary, mImeSwitchPendingIntent); |
| Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 1511 | if (mNotificationManager != null) { |
| Satoshi Kataoka | 135e5fb | 2012-09-28 18:25:06 +0900 | [diff] [blame] | 1512 | if (DEBUG) { |
| 1513 | Slog.d(TAG, "--- show notification: label = " + imiLabel |
| 1514 | + ", summary = " + summary); |
| 1515 | } |
| 1516 | mNotificationManager.notifyAsUser(null, |
| Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 1517 | com.android.internal.R.string.select_input_method, |
| Satoshi Kataoka | 135e5fb | 2012-09-28 18:25:06 +0900 | [diff] [blame] | 1518 | mImeSwitcherNotification, UserHandle.ALL); |
| Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 1519 | mNotificationShown = true; |
| 1520 | } |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1521 | } else { |
| Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 1522 | if (mNotificationShown && mNotificationManager != null) { |
| Satoshi Kataoka | 135e5fb | 2012-09-28 18:25:06 +0900 | [diff] [blame] | 1523 | if (DEBUG) { |
| 1524 | Slog.d(TAG, "--- hide notification"); |
| 1525 | } |
| 1526 | mNotificationManager.cancelAsUser(null, |
| 1527 | com.android.internal.R.string.select_input_method, UserHandle.ALL); |
| satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1528 | mNotificationShown = false; |
| 1529 | } |
| 1530 | } |
| satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 1531 | } |
| 1532 | } finally { |
| 1533 | Binder.restoreCallingIdentity(ident); |
| 1534 | } |
| 1535 | } |
| 1536 | |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1537 | @Override |
| satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1538 | public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1539 | if (!calledFromValidUser()) { |
| 1540 | return; |
| 1541 | } |
| satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1542 | synchronized (mMethodMap) { |
| 1543 | final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId); |
| 1544 | for (int i = 0; i < spans.length; ++i) { |
| 1545 | SuggestionSpan ss = spans[i]; |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1546 | if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) { |
| satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1547 | mSecureSuggestionSpans.put(ss, currentImi); |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1548 | final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss); |
| satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1549 | } |
| 1550 | } |
| 1551 | } |
| 1552 | } |
| 1553 | |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1554 | @Override |
| satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1555 | public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1556 | if (!calledFromValidUser()) { |
| 1557 | return false; |
| 1558 | } |
| satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1559 | synchronized (mMethodMap) { |
| 1560 | final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span); |
| 1561 | // TODO: Do not send the intent if the process of the targetImi is already dead. |
| 1562 | if (targetImi != null) { |
| 1563 | final String[] suggestions = span.getSuggestions(); |
| 1564 | if (index < 0 || index >= suggestions.length) return false; |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1565 | final String className = span.getNotificationTargetClassName(); |
| satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1566 | final Intent intent = new Intent(); |
| 1567 | // Ensures that only a class in the original IME package will receive the |
| 1568 | // notification. |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1569 | intent.setClassName(targetImi.getPackageName(), className); |
| satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1570 | intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED); |
| 1571 | intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString); |
| 1572 | intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]); |
| 1573 | intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode()); |
| Amith Yamasani | f043de9 | 2012-10-24 06:42:40 -0700 | [diff] [blame] | 1574 | final long ident = Binder.clearCallingIdentity(); |
| 1575 | try { |
| 1576 | mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT); |
| 1577 | } finally { |
| 1578 | Binder.restoreCallingIdentity(ident); |
| 1579 | } |
| satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1580 | return true; |
| 1581 | } |
| 1582 | } |
| 1583 | return false; |
| 1584 | } |
| 1585 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1586 | void updateFromSettingsLocked() { |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1587 | // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and |
| 1588 | // ENABLED_INPUT_METHODS is taking care of keeping them correctly in |
| 1589 | // sync, so we will never have a DEFAULT_INPUT_METHOD that is not |
| 1590 | // enabled. |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1591 | String id = mSettings.getSelectedInputMethod(); |
| satok | 03eb319a | 2010-11-11 18:17:42 +0900 | [diff] [blame] | 1592 | // There is no input method selected, try to choose new applicable input method. |
| 1593 | if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1594 | id = mSettings.getSelectedInputMethod(); |
| satok | 03eb319a | 2010-11-11 18:17:42 +0900 | [diff] [blame] | 1595 | } |
| 1596 | if (!TextUtils.isEmpty(id)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1597 | try { |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1598 | setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1599 | } catch (IllegalArgumentException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1600 | Slog.w(TAG, "Unknown input method from prefs: " + id, e); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1601 | mCurMethodId = null; |
| Dianne Hackborn | 2ea9bae | 2012-11-02 18:43:48 -0700 | [diff] [blame] | 1602 | unbindCurrentMethodLocked(true, false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1603 | } |
| satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 1604 | mShortcutInputMethodsAndSubtypes.clear(); |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1605 | } else { |
| 1606 | // There is no longer an input method set, so stop any current one. |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1607 | mCurMethodId = null; |
| Dianne Hackborn | 2ea9bae | 2012-11-02 18:43:48 -0700 | [diff] [blame] | 1608 | unbindCurrentMethodLocked(true, false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1609 | } |
| 1610 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1611 | |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1612 | /* package */ void setInputMethodLocked(String id, int subtypeId) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1613 | InputMethodInfo info = mMethodMap.get(id); |
| 1614 | if (info == null) { |
| satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 1615 | throw new IllegalArgumentException("Unknown id: " + id); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1616 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1617 | |
| satok | d81e950 | 2012-05-21 12:58:45 +0900 | [diff] [blame] | 1618 | // See if we need to notify a subtype change within the same IME. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1619 | if (id.equals(mCurMethodId)) { |
| satok | d81e950 | 2012-05-21 12:58:45 +0900 | [diff] [blame] | 1620 | final int subtypeCount = info.getSubtypeCount(); |
| 1621 | if (subtypeCount <= 0) { |
| 1622 | return; |
| satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 1623 | } |
| satok | d81e950 | 2012-05-21 12:58:45 +0900 | [diff] [blame] | 1624 | final InputMethodSubtype oldSubtype = mCurrentSubtype; |
| 1625 | final InputMethodSubtype newSubtype; |
| 1626 | if (subtypeId >= 0 && subtypeId < subtypeCount) { |
| 1627 | newSubtype = info.getSubtypeAt(subtypeId); |
| 1628 | } else { |
| 1629 | // If subtype is null, try to find the most applicable one from |
| 1630 | // getCurrentInputMethodSubtype. |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1631 | newSubtype = getCurrentInputMethodSubtypeLocked(); |
| satok | d81e950 | 2012-05-21 12:58:45 +0900 | [diff] [blame] | 1632 | } |
| 1633 | if (newSubtype == null || oldSubtype == null) { |
| 1634 | Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype |
| 1635 | + ", new subtype = " + newSubtype); |
| 1636 | return; |
| 1637 | } |
| 1638 | if (newSubtype != oldSubtype) { |
| 1639 | setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true); |
| 1640 | if (mCurMethod != null) { |
| 1641 | try { |
| 1642 | refreshImeWindowVisibilityLocked(); |
| 1643 | mCurMethod.changeInputMethodSubtype(newSubtype); |
| 1644 | } catch (RemoteException e) { |
| 1645 | Slog.w(TAG, "Failed to call changeInputMethodSubtype"); |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1646 | } |
| 1647 | } |
| 1648 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1649 | return; |
| 1650 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1651 | |
| satok | d81e950 | 2012-05-21 12:58:45 +0900 | [diff] [blame] | 1652 | // Changing to a different IME. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1653 | final long ident = Binder.clearCallingIdentity(); |
| 1654 | try { |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1655 | // Set a subtype to this input method. |
| 1656 | // subtypeId the name of a subtype which will be set. |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 1657 | setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false); |
| 1658 | // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked() |
| 1659 | // because mCurMethodId is stored as a history in |
| 1660 | // setSelectedInputMethodAndSubtypeLocked(). |
| 1661 | mCurMethodId = id; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1662 | |
| 1663 | if (ActivityManagerNative.isSystemReady()) { |
| 1664 | Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED); |
| Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1665 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1666 | intent.putExtra("input_method_id", id); |
| Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1667 | mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1668 | } |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1669 | unbindCurrentClientLocked(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1670 | } finally { |
| 1671 | Binder.restoreCallingIdentity(ident); |
| 1672 | } |
| 1673 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1674 | |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1675 | @Override |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1676 | public boolean showSoftInput(IInputMethodClient client, int flags, |
| 1677 | ResultReceiver resultReceiver) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1678 | if (!calledFromValidUser()) { |
| 1679 | return false; |
| 1680 | } |
| Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1681 | int uid = Binder.getCallingUid(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1682 | long ident = Binder.clearCallingIdentity(); |
| 1683 | try { |
| 1684 | synchronized (mMethodMap) { |
| 1685 | if (mCurClient == null || client == null |
| 1686 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1687 | try { |
| 1688 | // We need to check if this is the current client with |
| 1689 | // focus in the window manager, to allow this call to |
| 1690 | // be made before input is started in it. |
| 1691 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
| Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1692 | Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client); |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1693 | return false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1694 | } |
| 1695 | } catch (RemoteException e) { |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1696 | return false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1697 | } |
| 1698 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1699 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1700 | if (DEBUG) Slog.v(TAG, "Client requesting input be shown"); |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1701 | return showCurrentInputLocked(flags, resultReceiver); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1702 | } |
| 1703 | } finally { |
| 1704 | Binder.restoreCallingIdentity(ident); |
| 1705 | } |
| 1706 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1707 | |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1708 | boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1709 | mShowRequested = true; |
| 1710 | if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) { |
| 1711 | mShowExplicitlyRequested = true; |
| 1712 | } |
| 1713 | if ((flags&InputMethodManager.SHOW_FORCED) != 0) { |
| 1714 | mShowExplicitlyRequested = true; |
| 1715 | mShowForced = true; |
| 1716 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1717 | |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 1718 | if (!mSystemReady) { |
| 1719 | return false; |
| 1720 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1721 | |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1722 | boolean res = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1723 | if (mCurMethod != null) { |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1724 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO( |
| 1725 | MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod, |
| 1726 | resultReceiver)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1727 | mInputShown = true; |
| Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 1728 | if (mHaveConnection && !mVisibleBound) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1729 | bindCurrentInputMethodService( |
| 1730 | mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE); |
| Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 1731 | mVisibleBound = true; |
| 1732 | } |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1733 | res = true; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1734 | } else if (mHaveConnection && SystemClock.uptimeMillis() |
| satok | 59b424c | 2011-09-30 17:21:46 +0900 | [diff] [blame] | 1735 | >= (mLastBindTime+TIME_TO_RECONNECT)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1736 | // The client has asked to have the input method shown, but |
| 1737 | // we have been sitting here too long with a connection to the |
| 1738 | // service and no interface received, so let's disconnect/connect |
| 1739 | // to try to prod things along. |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1740 | EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1741 | SystemClock.uptimeMillis()-mLastBindTime,1); |
| satok | 59b424c | 2011-09-30 17:21:46 +0900 | [diff] [blame] | 1742 | Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1743 | mContext.unbindService(this); |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1744 | bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE |
| Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 1745 | | Context.BIND_NOT_VISIBLE); |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1746 | } else { |
| 1747 | if (DEBUG) { |
| 1748 | Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = " |
| 1749 | + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis())); |
| 1750 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1751 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1752 | |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1753 | return res; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1754 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1755 | |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1756 | @Override |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1757 | public boolean hideSoftInput(IInputMethodClient client, int flags, |
| 1758 | ResultReceiver resultReceiver) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1759 | if (!calledFromValidUser()) { |
| 1760 | return false; |
| 1761 | } |
| Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1762 | int uid = Binder.getCallingUid(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1763 | long ident = Binder.clearCallingIdentity(); |
| 1764 | try { |
| 1765 | synchronized (mMethodMap) { |
| 1766 | if (mCurClient == null || client == null |
| 1767 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1768 | try { |
| 1769 | // We need to check if this is the current client with |
| 1770 | // focus in the window manager, to allow this call to |
| 1771 | // be made before input is started in it. |
| 1772 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
| Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1773 | if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid " |
| 1774 | + uid + ": " + client); |
| satok | 15452a4 | 2011-10-28 17:58:28 +0900 | [diff] [blame] | 1775 | setImeWindowVisibilityStatusHiddenLocked(); |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1776 | return false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1777 | } |
| 1778 | } catch (RemoteException e) { |
| satok | 15452a4 | 2011-10-28 17:58:28 +0900 | [diff] [blame] | 1779 | setImeWindowVisibilityStatusHiddenLocked(); |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1780 | return false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1781 | } |
| 1782 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1783 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1784 | if (DEBUG) Slog.v(TAG, "Client requesting input be hidden"); |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1785 | return hideCurrentInputLocked(flags, resultReceiver); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1786 | } |
| 1787 | } finally { |
| 1788 | Binder.restoreCallingIdentity(ident); |
| 1789 | } |
| 1790 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1791 | |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1792 | boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1793 | if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0 |
| 1794 | && (mShowExplicitlyRequested || mShowForced)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1795 | if (DEBUG) Slog.v(TAG, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1796 | "Not hiding: explicit show not cancelled by non-explicit hide"); |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1797 | return false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1798 | } |
| 1799 | if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1800 | if (DEBUG) Slog.v(TAG, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1801 | "Not hiding: forced show not cancelled by not-always hide"); |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1802 | return false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1803 | } |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1804 | boolean res; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1805 | if (mInputShown && mCurMethod != null) { |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1806 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 1807 | MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver)); |
| 1808 | res = true; |
| 1809 | } else { |
| 1810 | res = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1811 | } |
| Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 1812 | if (mHaveConnection && mVisibleBound) { |
| 1813 | mContext.unbindService(mVisibleConnection); |
| 1814 | mVisibleBound = false; |
| 1815 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1816 | mInputShown = false; |
| 1817 | mShowRequested = false; |
| 1818 | mShowExplicitlyRequested = false; |
| 1819 | mShowForced = false; |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1820 | return res; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1821 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1822 | |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1823 | @Override |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1824 | public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken, |
| 1825 | int controlFlags, int softInputMode, int windowFlags, |
| 1826 | EditorInfo attribute, IInputContext inputContext) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1827 | if (!calledFromValidUser()) { |
| 1828 | return null; |
| 1829 | } |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1830 | InputBindResult res = null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1831 | long ident = Binder.clearCallingIdentity(); |
| 1832 | try { |
| 1833 | synchronized (mMethodMap) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1834 | if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder() |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1835 | + " controlFlags=#" + Integer.toHexString(controlFlags) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1836 | + " softInputMode=#" + Integer.toHexString(softInputMode) |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1837 | + " windowFlags=#" + Integer.toHexString(windowFlags)); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1838 | |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1839 | ClientState cs = mClients.get(client.asBinder()); |
| 1840 | if (cs == null) { |
| 1841 | throw new IllegalArgumentException("unknown client " |
| 1842 | + client.asBinder()); |
| 1843 | } |
| 1844 | |
| 1845 | try { |
| 1846 | if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) { |
| 1847 | // Check with the window manager to make sure this client actually |
| 1848 | // has a window with focus. If not, reject. This is thread safe |
| 1849 | // because if the focus changes some time before or after, the |
| 1850 | // next client receiving focus that has any interest in input will |
| 1851 | // be calling through here after that change happens. |
| 1852 | Slog.w(TAG, "Focus gain on non-focused client " + cs.client |
| 1853 | + " (uid=" + cs.uid + " pid=" + cs.pid + ")"); |
| 1854 | return null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1855 | } |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1856 | } catch (RemoteException e) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1857 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1858 | |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1859 | if (mCurFocusedWindow == windowToken) { |
| Dianne Hackborn | ac92087 | 2012-05-22 11:49:49 -0700 | [diff] [blame] | 1860 | Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client |
| Satoshi Kataoka | 3573950 | 2012-10-02 19:00:26 +0900 | [diff] [blame] | 1861 | + " attribute=" + attribute + ", token = " + windowToken); |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1862 | if (attribute != null) { |
| 1863 | return startInputUncheckedLocked(cs, inputContext, attribute, |
| 1864 | controlFlags); |
| 1865 | } |
| 1866 | return null; |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1867 | } |
| 1868 | mCurFocusedWindow = windowToken; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1869 | |
| Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1870 | // Should we auto-show the IME even if the caller has not |
| 1871 | // specified what should be done with it? |
| 1872 | // We only do this automatically if the window can resize |
| 1873 | // to accommodate the IME (so what the user sees will give |
| 1874 | // them good context without input information being obscured |
| 1875 | // by the IME) or if running on a large screen where there |
| 1876 | // is more room for the target window + IME. |
| 1877 | final boolean doAutoShow = |
| 1878 | (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
| 1879 | == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
| 1880 | || mRes.getConfiguration().isLayoutSizeAtLeast( |
| 1881 | Configuration.SCREENLAYOUT_SIZE_LARGE); |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1882 | final boolean isTextEditor = |
| 1883 | (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0; |
| 1884 | |
| 1885 | // We want to start input before showing the IME, but after closing |
| 1886 | // it. We want to do this after closing it to help the IME disappear |
| 1887 | // more quickly (not get stuck behind it initializing itself for the |
| 1888 | // new focused input, even if its window wants to hide the IME). |
| 1889 | boolean didStart = false; |
| Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1890 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1891 | switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) { |
| 1892 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED: |
| Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1893 | if (!isTextEditor || !doAutoShow) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1894 | if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) { |
| 1895 | // There is no focus view, and this window will |
| 1896 | // be behind any soft input window, so hide the |
| 1897 | // soft input window if it is shown. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1898 | if (DEBUG) Slog.v(TAG, "Unspecified window will hide input"); |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1899 | hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1900 | } |
| Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1901 | } else if (isTextEditor && doAutoShow && (softInputMode & |
| 1902 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1903 | // There is a focus view, and we are navigating forward |
| 1904 | // into the window, so show the input window for the user. |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1905 | // We only do this automatically if the window can resize |
| 1906 | // to accommodate the IME (so what the user sees will give |
| Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1907 | // them good context without input information being obscured |
| 1908 | // by the IME) or if running on a large screen where there |
| 1909 | // is more room for the target window + IME. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1910 | if (DEBUG) Slog.v(TAG, "Unspecified window will show input"); |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1911 | if (attribute != null) { |
| 1912 | res = startInputUncheckedLocked(cs, inputContext, attribute, |
| 1913 | controlFlags); |
| 1914 | didStart = true; |
| 1915 | } |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1916 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1917 | } |
| 1918 | break; |
| 1919 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED: |
| 1920 | // Do nothing. |
| 1921 | break; |
| 1922 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN: |
| 1923 | if ((softInputMode & |
| 1924 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1925 | if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward"); |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1926 | hideCurrentInputLocked(0, null); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1927 | } |
| 1928 | break; |
| 1929 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN: |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1930 | if (DEBUG) Slog.v(TAG, "Window asks to hide input"); |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1931 | hideCurrentInputLocked(0, null); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1932 | break; |
| 1933 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE: |
| 1934 | if ((softInputMode & |
| 1935 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1936 | if (DEBUG) Slog.v(TAG, "Window asks to show input going forward"); |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1937 | if (attribute != null) { |
| 1938 | res = startInputUncheckedLocked(cs, inputContext, attribute, |
| 1939 | controlFlags); |
| 1940 | didStart = true; |
| 1941 | } |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1942 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1943 | } |
| 1944 | break; |
| 1945 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE: |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1946 | if (DEBUG) Slog.v(TAG, "Window asks to always show input"); |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1947 | if (attribute != null) { |
| 1948 | res = startInputUncheckedLocked(cs, inputContext, attribute, |
| 1949 | controlFlags); |
| 1950 | didStart = true; |
| 1951 | } |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1952 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1953 | break; |
| 1954 | } |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1955 | |
| 1956 | if (!didStart && attribute != null) { |
| 1957 | res = startInputUncheckedLocked(cs, inputContext, attribute, |
| 1958 | controlFlags); |
| 1959 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1960 | } |
| 1961 | } finally { |
| 1962 | Binder.restoreCallingIdentity(ident); |
| 1963 | } |
| Dianne Hackborn | 7663d80 | 2012-02-24 13:08:49 -0800 | [diff] [blame] | 1964 | |
| 1965 | return res; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1966 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1967 | |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1968 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1969 | public void showInputMethodPickerFromClient(IInputMethodClient client) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1970 | if (!calledFromValidUser()) { |
| 1971 | return; |
| 1972 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1973 | synchronized (mMethodMap) { |
| 1974 | if (mCurClient == null || client == null |
| 1975 | || mCurClient.client.asBinder() != client.asBinder()) { |
| satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1976 | Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid " |
| Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1977 | + Binder.getCallingUid() + ": " + client); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1978 | } |
| 1979 | |
| satok | 440aab5 | 2010-11-25 09:43:11 +0900 | [diff] [blame] | 1980 | // Always call subtype picker, because subtype picker is a superset of input method |
| 1981 | // picker. |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1982 | mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER); |
| 1983 | } |
| 1984 | } |
| 1985 | |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1986 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1987 | public void setInputMethod(IBinder token, String id) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1988 | if (!calledFromValidUser()) { |
| 1989 | return; |
| 1990 | } |
| satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 1991 | setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID); |
| 1992 | } |
| 1993 | |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1994 | @Override |
| satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 1995 | public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 1996 | if (!calledFromValidUser()) { |
| 1997 | return; |
| 1998 | } |
| satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 1999 | synchronized (mMethodMap) { |
| 2000 | if (subtype != null) { |
| 2001 | setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode( |
| 2002 | mMethodMap.get(id), subtype.hashCode())); |
| 2003 | } else { |
| 2004 | setInputMethod(token, id); |
| 2005 | } |
| 2006 | } |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2007 | } |
| 2008 | |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2009 | @Override |
| satok | b416a71e | 2010-11-25 20:42:14 +0900 | [diff] [blame] | 2010 | public void showInputMethodAndSubtypeEnablerFromClient( |
| satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 2011 | IInputMethodClient client, String inputMethodId) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2012 | if (!calledFromValidUser()) { |
| 2013 | return; |
| 2014 | } |
| satok | b416a71e | 2010-11-25 20:42:14 +0900 | [diff] [blame] | 2015 | synchronized (mMethodMap) { |
| 2016 | if (mCurClient == null || client == null |
| 2017 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 2018 | Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client); |
| 2019 | } |
| satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 2020 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageO( |
| 2021 | MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId)); |
| satok | b416a71e | 2010-11-25 20:42:14 +0900 | [diff] [blame] | 2022 | } |
| 2023 | } |
| 2024 | |
| satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 2025 | @Override |
| satok | 735cf38 | 2010-11-11 20:40:09 +0900 | [diff] [blame] | 2026 | public boolean switchToLastInputMethod(IBinder token) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2027 | if (!calledFromValidUser()) { |
| 2028 | return false; |
| 2029 | } |
| satok | 735cf38 | 2010-11-11 20:40:09 +0900 | [diff] [blame] | 2030 | synchronized (mMethodMap) { |
| satok | c445bcd | 2011-01-25 18:57:24 +0900 | [diff] [blame] | 2031 | final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked(); |
| satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 2032 | final InputMethodInfo lastImi; |
| satok | 208d563 | 2011-05-20 22:13:38 +0900 | [diff] [blame] | 2033 | if (lastIme != null) { |
| satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 2034 | lastImi = mMethodMap.get(lastIme.first); |
| 2035 | } else { |
| 2036 | lastImi = null; |
| satok | 735cf38 | 2010-11-11 20:40:09 +0900 | [diff] [blame] | 2037 | } |
| satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 2038 | String targetLastImiId = null; |
| 2039 | int subtypeId = NOT_A_SUBTYPE_ID; |
| 2040 | if (lastIme != null && lastImi != null) { |
| 2041 | final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId); |
| 2042 | final int lastSubtypeHash = Integer.valueOf(lastIme.second); |
| 2043 | final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID |
| 2044 | : mCurrentSubtype.hashCode(); |
| 2045 | // If the last IME is the same as the current IME and the last subtype is not |
| 2046 | // defined, there is no need to switch to the last IME. |
| 2047 | if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) { |
| 2048 | targetLastImiId = lastIme.first; |
| 2049 | subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash); |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) { |
| 2054 | // This is a safety net. If the currentSubtype can't be added to the history |
| 2055 | // and the framework couldn't find the last ime, we will make the last ime be |
| 2056 | // the most applicable enabled keyboard subtype of the system imes. |
| 2057 | final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked(); |
| 2058 | if (enabled != null) { |
| 2059 | final int N = enabled.size(); |
| 2060 | final String locale = mCurrentSubtype == null |
| 2061 | ? mRes.getConfiguration().locale.toString() |
| 2062 | : mCurrentSubtype.getLocale(); |
| 2063 | for (int i = 0; i < N; ++i) { |
| 2064 | final InputMethodInfo imi = enabled.get(i); |
| 2065 | if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) { |
| 2066 | InputMethodSubtype keyboardSubtype = |
| 2067 | findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi), |
| 2068 | SUBTYPE_MODE_KEYBOARD, locale, true); |
| 2069 | if (keyboardSubtype != null) { |
| 2070 | targetLastImiId = imi.getId(); |
| 2071 | subtypeId = getSubtypeIdFromHashCode( |
| 2072 | imi, keyboardSubtype.hashCode()); |
| 2073 | if(keyboardSubtype.getLocale().equals(locale)) { |
| 2074 | break; |
| 2075 | } |
| 2076 | } |
| 2077 | } |
| 2078 | } |
| 2079 | } |
| 2080 | } |
| 2081 | |
| 2082 | if (!TextUtils.isEmpty(targetLastImiId)) { |
| 2083 | if (DEBUG) { |
| 2084 | Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second |
| 2085 | + ", from: " + mCurMethodId + ", " + subtypeId); |
| 2086 | } |
| 2087 | setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId); |
| 2088 | return true; |
| 2089 | } else { |
| 2090 | return false; |
| 2091 | } |
| satok | 735cf38 | 2010-11-11 20:40:09 +0900 | [diff] [blame] | 2092 | } |
| 2093 | } |
| 2094 | |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 2095 | @Override |
| satok | 688bd47 | 2012-02-09 20:09:17 +0900 | [diff] [blame] | 2096 | public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2097 | if (!calledFromValidUser()) { |
| 2098 | return false; |
| 2099 | } |
| satok | 688bd47 | 2012-02-09 20:09:17 +0900 | [diff] [blame] | 2100 | synchronized (mMethodMap) { |
| 2101 | final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod( |
| 2102 | onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype); |
| 2103 | if (nextSubtype == null) { |
| 2104 | return false; |
| 2105 | } |
| 2106 | setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId); |
| 2107 | return true; |
| 2108 | } |
| 2109 | } |
| 2110 | |
| 2111 | @Override |
| satok | 68f1b78 | 2011-04-11 14:26:04 +0900 | [diff] [blame] | 2112 | public InputMethodSubtype getLastInputMethodSubtype() { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2113 | if (!calledFromValidUser()) { |
| 2114 | return null; |
| 2115 | } |
| satok | 68f1b78 | 2011-04-11 14:26:04 +0900 | [diff] [blame] | 2116 | synchronized (mMethodMap) { |
| 2117 | final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked(); |
| 2118 | // TODO: Handle the case of the last IME with no subtypes |
| 2119 | if (lastIme == null || TextUtils.isEmpty(lastIme.first) |
| 2120 | || TextUtils.isEmpty(lastIme.second)) return null; |
| 2121 | final InputMethodInfo lastImi = mMethodMap.get(lastIme.first); |
| 2122 | if (lastImi == null) return null; |
| 2123 | try { |
| 2124 | final int lastSubtypeHash = Integer.valueOf(lastIme.second); |
| satok | 0e7d7d6 | 2011-07-05 13:28:06 +0900 | [diff] [blame] | 2125 | final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash); |
| 2126 | if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) { |
| 2127 | return null; |
| 2128 | } |
| 2129 | return lastImi.getSubtypeAt(lastSubtypeId); |
| satok | 68f1b78 | 2011-04-11 14:26:04 +0900 | [diff] [blame] | 2130 | } catch (NumberFormatException e) { |
| 2131 | return null; |
| 2132 | } |
| 2133 | } |
| 2134 | } |
| 2135 | |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 2136 | @Override |
| satok | ee5e77c | 2011-09-02 18:50:15 +0900 | [diff] [blame] | 2137 | public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2138 | if (!calledFromValidUser()) { |
| 2139 | return; |
| 2140 | } |
| satok | 91e8812 | 2011-07-18 11:11:42 +0900 | [diff] [blame] | 2141 | // By this IPC call, only a process which shares the same uid with the IME can add |
| 2142 | // additional input method subtypes to the IME. |
| satok | ee5e77c | 2011-09-02 18:50:15 +0900 | [diff] [blame] | 2143 | if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return; |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 2144 | synchronized (mMethodMap) { |
| satok | 91e8812 | 2011-07-18 11:11:42 +0900 | [diff] [blame] | 2145 | final InputMethodInfo imi = mMethodMap.get(imiId); |
| satok | ee5e77c | 2011-09-02 18:50:15 +0900 | [diff] [blame] | 2146 | if (imi == null) return; |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2147 | final String[] packageInfos; |
| 2148 | try { |
| 2149 | packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid()); |
| 2150 | } catch (RemoteException e) { |
| 2151 | Slog.e(TAG, "Failed to get package infos"); |
| 2152 | return; |
| 2153 | } |
| satok | 91e8812 | 2011-07-18 11:11:42 +0900 | [diff] [blame] | 2154 | if (packageInfos != null) { |
| 2155 | final int packageNum = packageInfos.length; |
| 2156 | for (int i = 0; i < packageNum; ++i) { |
| 2157 | if (packageInfos[i].equals(imi.getPackageName())) { |
| 2158 | mFileManager.addInputMethodSubtypes(imi, subtypes); |
| satok | c593380 | 2011-08-31 21:26:04 +0900 | [diff] [blame] | 2159 | final long ident = Binder.clearCallingIdentity(); |
| 2160 | try { |
| 2161 | buildInputMethodListLocked(mMethodList, mMethodMap); |
| 2162 | } finally { |
| 2163 | Binder.restoreCallingIdentity(ident); |
| 2164 | } |
| satok | ee5e77c | 2011-09-02 18:50:15 +0900 | [diff] [blame] | 2165 | return; |
| satok | 91e8812 | 2011-07-18 11:11:42 +0900 | [diff] [blame] | 2166 | } |
| 2167 | } |
| 2168 | } |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 2169 | } |
| satok | ee5e77c | 2011-09-02 18:50:15 +0900 | [diff] [blame] | 2170 | return; |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 2171 | } |
| 2172 | |
| satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 2173 | private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2174 | synchronized (mMethodMap) { |
| 2175 | if (token == null) { |
| 2176 | if (mContext.checkCallingOrSelfPermission( |
| 2177 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 2178 | != PackageManager.PERMISSION_GRANTED) { |
| 2179 | throw new SecurityException( |
| 2180 | "Using null token requires permission " |
| 2181 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 2182 | } |
| 2183 | } else if (mCurToken != token) { |
| Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 2184 | Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid() |
| 2185 | + " token: " + token); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2186 | return; |
| 2187 | } |
| 2188 | |
| satok | c593380 | 2011-08-31 21:26:04 +0900 | [diff] [blame] | 2189 | final long ident = Binder.clearCallingIdentity(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2190 | try { |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2191 | setInputMethodLocked(id, subtypeId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2192 | } finally { |
| 2193 | Binder.restoreCallingIdentity(ident); |
| 2194 | } |
| 2195 | } |
| 2196 | } |
| 2197 | |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2198 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2199 | public void hideMySoftInput(IBinder token, int flags) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2200 | if (!calledFromValidUser()) { |
| 2201 | return; |
| 2202 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2203 | synchronized (mMethodMap) { |
| 2204 | if (token == null || mCurToken != token) { |
| Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 2205 | if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid " |
| 2206 | + Binder.getCallingUid() + " token: " + token); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2207 | return; |
| 2208 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2209 | long ident = Binder.clearCallingIdentity(); |
| 2210 | try { |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 2211 | hideCurrentInputLocked(flags, null); |
| 2212 | } finally { |
| 2213 | Binder.restoreCallingIdentity(ident); |
| 2214 | } |
| 2215 | } |
| 2216 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2217 | |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2218 | @Override |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 2219 | public void showMySoftInput(IBinder token, int flags) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2220 | if (!calledFromValidUser()) { |
| 2221 | return; |
| 2222 | } |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 2223 | synchronized (mMethodMap) { |
| 2224 | if (token == null || mCurToken != token) { |
| Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 2225 | Slog.w(TAG, "Ignoring showMySoftInput of uid " |
| 2226 | + Binder.getCallingUid() + " token: " + token); |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 2227 | return; |
| 2228 | } |
| 2229 | long ident = Binder.clearCallingIdentity(); |
| 2230 | try { |
| 2231 | showCurrentInputLocked(flags, null); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2232 | } finally { |
| 2233 | Binder.restoreCallingIdentity(ident); |
| 2234 | } |
| 2235 | } |
| 2236 | } |
| 2237 | |
| 2238 | void setEnabledSessionInMainThread(SessionState session) { |
| 2239 | if (mEnabledSession != session) { |
| 2240 | if (mEnabledSession != null) { |
| 2241 | try { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2242 | if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2243 | mEnabledSession.method.setSessionEnabled( |
| 2244 | mEnabledSession.session, false); |
| 2245 | } catch (RemoteException e) { |
| 2246 | } |
| 2247 | } |
| 2248 | mEnabledSession = session; |
| 2249 | try { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2250 | if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2251 | session.method.setSessionEnabled( |
| 2252 | session.session, true); |
| 2253 | } catch (RemoteException e) { |
| 2254 | } |
| 2255 | } |
| 2256 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2257 | |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2258 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2259 | public boolean handleMessage(Message msg) { |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2260 | SomeArgs args; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2261 | switch (msg.what) { |
| 2262 | case MSG_SHOW_IM_PICKER: |
| 2263 | showInputMethodMenu(); |
| 2264 | return true; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2265 | |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2266 | case MSG_SHOW_IM_SUBTYPE_PICKER: |
| 2267 | showInputMethodSubtypeMenu(); |
| 2268 | return true; |
| 2269 | |
| satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 2270 | case MSG_SHOW_IM_SUBTYPE_ENABLER: |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2271 | args = (SomeArgs)msg.obj; |
| satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 2272 | showInputMethodAndSubtypeEnabler((String)args.arg1); |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2273 | args.recycle(); |
| satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 2274 | return true; |
| 2275 | |
| 2276 | case MSG_SHOW_IM_CONFIG: |
| 2277 | showConfigureInputMethods(); |
| satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 2278 | return true; |
| 2279 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2280 | // --------------------------------------------------------- |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2281 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2282 | case MSG_UNBIND_INPUT: |
| 2283 | try { |
| 2284 | ((IInputMethod)msg.obj).unbindInput(); |
| 2285 | } catch (RemoteException e) { |
| 2286 | // There is nothing interesting about the method dying. |
| 2287 | } |
| 2288 | return true; |
| 2289 | case MSG_BIND_INPUT: |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2290 | args = (SomeArgs)msg.obj; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2291 | try { |
| 2292 | ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2); |
| 2293 | } catch (RemoteException e) { |
| 2294 | } |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2295 | args.recycle(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2296 | return true; |
| 2297 | case MSG_SHOW_SOFT_INPUT: |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2298 | args = (SomeArgs)msg.obj; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2299 | try { |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 2300 | ((IInputMethod)args.arg1).showSoftInput(msg.arg1, |
| 2301 | (ResultReceiver)args.arg2); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2302 | } catch (RemoteException e) { |
| 2303 | } |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2304 | args.recycle(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2305 | return true; |
| 2306 | case MSG_HIDE_SOFT_INPUT: |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2307 | args = (SomeArgs)msg.obj; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2308 | try { |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 2309 | ((IInputMethod)args.arg1).hideSoftInput(0, |
| 2310 | (ResultReceiver)args.arg2); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2311 | } catch (RemoteException e) { |
| 2312 | } |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2313 | args.recycle(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2314 | return true; |
| 2315 | case MSG_ATTACH_TOKEN: |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2316 | args = (SomeArgs)msg.obj; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2317 | try { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2318 | if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2319 | ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2); |
| 2320 | } catch (RemoteException e) { |
| 2321 | } |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2322 | args.recycle(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2323 | return true; |
| 2324 | case MSG_CREATE_SESSION: |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2325 | args = (SomeArgs)msg.obj; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2326 | try { |
| 2327 | ((IInputMethod)args.arg1).createSession( |
| 2328 | (IInputMethodCallback)args.arg2); |
| 2329 | } catch (RemoteException e) { |
| 2330 | } |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2331 | args.recycle(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2332 | return true; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2333 | // --------------------------------------------------------- |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2334 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2335 | case MSG_START_INPUT: |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2336 | args = (SomeArgs)msg.obj; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2337 | try { |
| 2338 | SessionState session = (SessionState)args.arg1; |
| 2339 | setEnabledSessionInMainThread(session); |
| 2340 | session.method.startInput((IInputContext)args.arg2, |
| 2341 | (EditorInfo)args.arg3); |
| 2342 | } catch (RemoteException e) { |
| 2343 | } |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2344 | args.recycle(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2345 | return true; |
| 2346 | case MSG_RESTART_INPUT: |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2347 | args = (SomeArgs)msg.obj; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2348 | try { |
| 2349 | SessionState session = (SessionState)args.arg1; |
| 2350 | setEnabledSessionInMainThread(session); |
| 2351 | session.method.restartInput((IInputContext)args.arg2, |
| 2352 | (EditorInfo)args.arg3); |
| 2353 | } catch (RemoteException e) { |
| 2354 | } |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2355 | args.recycle(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2356 | return true; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2357 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2358 | // --------------------------------------------------------- |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2359 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2360 | case MSG_UNBIND_METHOD: |
| 2361 | try { |
| 2362 | ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1); |
| 2363 | } catch (RemoteException e) { |
| 2364 | // There is nothing interesting about the last client dying. |
| 2365 | } |
| 2366 | return true; |
| 2367 | case MSG_BIND_METHOD: |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2368 | args = (SomeArgs)msg.obj; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2369 | try { |
| 2370 | ((IInputMethodClient)args.arg1).onBindMethod( |
| 2371 | (InputBindResult)args.arg2); |
| 2372 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2373 | Slog.w(TAG, "Client died receiving input method " + args.arg2); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2374 | } |
| Svetoslav Ganov | 758143e | 2012-08-06 16:40:27 -0700 | [diff] [blame] | 2375 | args.recycle(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2376 | return true; |
| Dianne Hackborn | a6e4134 | 2012-05-22 16:30:34 -0700 | [diff] [blame] | 2377 | case MSG_SET_ACTIVE: |
| 2378 | try { |
| 2379 | ((ClientState)msg.obj).client.setActive(msg.arg1 != 0); |
| 2380 | } catch (RemoteException e) { |
| 2381 | Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid " |
| 2382 | + ((ClientState)msg.obj).pid + " uid " |
| 2383 | + ((ClientState)msg.obj).uid); |
| 2384 | } |
| 2385 | return true; |
| satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 2386 | |
| 2387 | // -------------------------------------------------------------- |
| 2388 | case MSG_HARD_KEYBOARD_SWITCH_CHANGED: |
| 2389 | mHardKeyboardListener.handleHardKeyboardStatusChange( |
| 2390 | msg.arg1 == 1, msg.arg2 == 1); |
| 2391 | return true; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2392 | } |
| 2393 | return false; |
| 2394 | } |
| 2395 | |
| satok | 5b927c43 | 2012-05-01 20:09:34 +0900 | [diff] [blame] | 2396 | private static boolean isSystemIme(InputMethodInfo inputMethod) { |
| Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 2397 | return (inputMethod.getServiceInfo().applicationInfo.flags |
| 2398 | & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 2399 | } |
| 2400 | |
| Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2401 | private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) { |
| 2402 | ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>(); |
| 2403 | final int subtypeCount = imi.getSubtypeCount(); |
| 2404 | for (int i = 0; i < subtypeCount; ++i) { |
| 2405 | subtypes.add(imi.getSubtypeAt(i)); |
| 2406 | } |
| 2407 | return subtypes; |
| 2408 | } |
| 2409 | |
| satok | a86f5e4 | 2011-09-02 17:12:42 +0900 | [diff] [blame] | 2410 | private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes( |
| 2411 | InputMethodInfo imi, String mode) { |
| 2412 | ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>(); |
| 2413 | final int subtypeCount = imi.getSubtypeCount(); |
| 2414 | for (int i = 0; i < subtypeCount; ++i) { |
| 2415 | final InputMethodSubtype subtype = imi.getSubtypeAt(i); |
| 2416 | if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) { |
| 2417 | subtypes.add(subtype); |
| 2418 | } |
| 2419 | } |
| 2420 | return subtypes; |
| 2421 | } |
| 2422 | |
| satok | dc9ddae | 2011-10-06 12:22:36 +0900 | [diff] [blame] | 2423 | private InputMethodInfo getMostApplicableDefaultIMELocked() { |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2424 | List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked(); |
| Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 2425 | if (enabled != null && enabled.size() > 0) { |
| Dianne Hackborn | 83e48f5 | 2010-03-23 23:03:25 -0700 | [diff] [blame] | 2426 | // We'd prefer to fall back on a system IME, since that is safer. |
| satok | 0a1bcf4 | 2012-05-16 19:26:31 +0900 | [diff] [blame] | 2427 | int i = enabled.size(); |
| 2428 | int firstFoundSystemIme = -1; |
| Dianne Hackborn | 83e48f5 | 2010-03-23 23:03:25 -0700 | [diff] [blame] | 2429 | while (i > 0) { |
| 2430 | i--; |
| satok | dc9ddae | 2011-10-06 12:22:36 +0900 | [diff] [blame] | 2431 | final InputMethodInfo imi = enabled.get(i); |
| satok | 0a1bcf4 | 2012-05-16 19:26:31 +0900 | [diff] [blame] | 2432 | if (isSystemImeThatHasEnglishSubtype(imi) && !imi.isAuxiliaryIme()) { |
| 2433 | return imi; |
| 2434 | } |
| 2435 | if (firstFoundSystemIme < 0 && isSystemIme(imi) && !imi.isAuxiliaryIme()) { |
| 2436 | firstFoundSystemIme = i; |
| Dianne Hackborn | 83e48f5 | 2010-03-23 23:03:25 -0700 | [diff] [blame] | 2437 | } |
| 2438 | } |
| satok | 0a1bcf4 | 2012-05-16 19:26:31 +0900 | [diff] [blame] | 2439 | return enabled.get(Math.max(firstFoundSystemIme, 0)); |
| satok | dc9ddae | 2011-10-06 12:22:36 +0900 | [diff] [blame] | 2440 | } |
| 2441 | return null; |
| 2442 | } |
| 2443 | |
| 2444 | private boolean chooseNewDefaultIMELocked() { |
| 2445 | final InputMethodInfo imi = getMostApplicableDefaultIMELocked(); |
| 2446 | if (imi != null) { |
| satok | 03eb319a | 2010-11-11 18:17:42 +0900 | [diff] [blame] | 2447 | if (DEBUG) { |
| 2448 | Slog.d(TAG, "New default IME was selected: " + imi.getId()); |
| 2449 | } |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2450 | resetSelectedInputMethodAndSubtypeLocked(imi.getId()); |
| Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 2451 | return true; |
| 2452 | } |
| 2453 | |
| 2454 | return false; |
| 2455 | } |
| 2456 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2457 | void buildInputMethodListLocked(ArrayList<InputMethodInfo> list, |
| 2458 | HashMap<String, InputMethodInfo> map) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2459 | if (DEBUG) { |
| 2460 | Slog.d(TAG, "--- re-buildInputMethodList " + ", \n ------ \n" + getStackTrace()); |
| 2461 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2462 | list.clear(); |
| 2463 | map.clear(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2464 | |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2465 | // Use for queryIntentServicesAsUser |
| 2466 | final PackageManager pm = mContext.getPackageManager(); |
| Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 2467 | final Configuration config = mRes.getConfiguration(); |
| Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 2468 | final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY; |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2469 | String disabledSysImes = mSettings.getDisabledSystemInputMethods(); |
| Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 2470 | if (disabledSysImes == null) disabledSysImes = ""; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2471 | |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2472 | final List<ResolveInfo> services = pm.queryIntentServicesAsUser( |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2473 | new Intent(InputMethod.SERVICE_INTERFACE), |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2474 | PackageManager.GET_META_DATA, mSettings.getCurrentUserId()); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2475 | |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 2476 | final HashMap<String, List<InputMethodSubtype>> additionalSubtypes = |
| 2477 | mFileManager.getAllAdditionalInputMethodSubtypes(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2478 | for (int i = 0; i < services.size(); ++i) { |
| 2479 | ResolveInfo ri = services.get(i); |
| 2480 | ServiceInfo si = ri.serviceInfo; |
| 2481 | ComponentName compName = new ComponentName(si.packageName, si.name); |
| 2482 | if (!android.Manifest.permission.BIND_INPUT_METHOD.equals( |
| 2483 | si.permission)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2484 | Slog.w(TAG, "Skipping input method " + compName |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2485 | + ": it does not require the permission " |
| 2486 | + android.Manifest.permission.BIND_INPUT_METHOD); |
| 2487 | continue; |
| 2488 | } |
| 2489 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2490 | if (DEBUG) Slog.d(TAG, "Checking " + compName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2491 | |
| 2492 | try { |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 2493 | InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2494 | list.add(p); |
| Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 2495 | final String id = p.getId(); |
| 2496 | map.put(id, p); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2497 | |
| satok | 5b927c43 | 2012-05-01 20:09:34 +0900 | [diff] [blame] | 2498 | // Valid system default IMEs and IMEs that have English subtypes are enabled |
| Satoshi Kataoka | 15451bd | 2012-11-16 13:17:54 +0900 | [diff] [blame] | 2499 | // by default |
| 2500 | if ((isValidSystemDefaultIme(p, mContext) || isSystemImeThatHasEnglishSubtype(p))) { |
| Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 2501 | setInputMethodEnabledLocked(id, true); |
| Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 2502 | } |
| 2503 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2504 | if (DEBUG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2505 | Slog.d(TAG, "Found a third-party input method " + p); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2506 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2507 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2508 | } catch (XmlPullParserException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2509 | Slog.w(TAG, "Unable to load input method " + compName, e); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2510 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2511 | Slog.w(TAG, "Unable to load input method " + compName, e); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2512 | } |
| 2513 | } |
| Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 2514 | |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2515 | final String defaultImiId = mSettings.getSelectedInputMethod(); |
| satok | 0a1bcf4 | 2012-05-16 19:26:31 +0900 | [diff] [blame] | 2516 | if (!TextUtils.isEmpty(defaultImiId)) { |
| 2517 | if (!map.containsKey(defaultImiId)) { |
| 2518 | Slog.w(TAG, "Default IME is uninstalled. Choose new default IME."); |
| 2519 | if (chooseNewDefaultIMELocked()) { |
| 2520 | updateFromSettingsLocked(); |
| 2521 | } |
| 2522 | } else { |
| 2523 | // Double check that the default IME is certainly enabled. |
| 2524 | setInputMethodEnabledLocked(defaultImiId, true); |
| Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 2525 | } |
| 2526 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2527 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2528 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2529 | // ---------------------------------------------------------------------- |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2530 | |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2531 | private void showInputMethodMenu() { |
| 2532 | showInputMethodMenuInternal(false); |
| 2533 | } |
| 2534 | |
| 2535 | private void showInputMethodSubtypeMenu() { |
| 2536 | showInputMethodMenuInternal(true); |
| 2537 | } |
| 2538 | |
| satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 2539 | private void showInputMethodAndSubtypeEnabler(String inputMethodId) { |
| Tadashi G. Takaoka | f49688f | 2011-01-20 17:56:13 +0900 | [diff] [blame] | 2540 | Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS); |
| satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 2541 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| satok | 86417ea | 2010-10-27 14:11:03 +0900 | [diff] [blame] | 2542 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
| 2543 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 2544 | if (!TextUtils.isEmpty(inputMethodId)) { |
| Tadashi G. Takaoka | 2548020 | 2011-01-20 23:13:02 +0900 | [diff] [blame] | 2545 | intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId); |
| satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 2546 | } |
| Satoshi Kataoka | 3ba439d | 2012-10-05 18:30:13 +0900 | [diff] [blame] | 2547 | mContext.startActivityAsUser(intent, null, UserHandle.CURRENT); |
| satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 2548 | } |
| 2549 | |
| 2550 | private void showConfigureInputMethods() { |
| 2551 | Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS); |
| 2552 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 2553 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
| 2554 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| Satoshi Kataoka | 3ba439d | 2012-10-05 18:30:13 +0900 | [diff] [blame] | 2555 | mContext.startActivityAsUser(intent, null, UserHandle.CURRENT); |
| satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 2556 | } |
| 2557 | |
| satok | 2c93efc | 2012-04-02 19:33:47 +0900 | [diff] [blame] | 2558 | private boolean isScreenLocked() { |
| 2559 | return mKeyguardManager != null |
| 2560 | && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure(); |
| 2561 | } |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2562 | private void showInputMethodMenuInternal(boolean showSubtypes) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2563 | if (DEBUG) Slog.v(TAG, "Show switching menu"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2564 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2565 | final Context context = mContext; |
| satok | 2c93efc | 2012-04-02 19:33:47 +0900 | [diff] [blame] | 2566 | final boolean isScreenLocked = isScreenLocked(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2567 | |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2568 | final String lastInputMethodId = mSettings.getSelectedInputMethod(); |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2569 | int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2570 | if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2571 | |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2572 | synchronized (mMethodMap) { |
| satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2573 | final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis = |
| 2574 | getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(); |
| satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2575 | if (immis == null || immis.size() == 0) { |
| 2576 | return; |
| 2577 | } |
| 2578 | |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2579 | hideInputMethodMenuLocked(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2580 | |
| satok | 688bd47 | 2012-02-09 20:09:17 +0900 | [diff] [blame] | 2581 | final List<ImeSubtypeListItem> imList = |
| 2582 | mImListManager.getSortedInputMethodAndSubtypeList( |
| 2583 | showSubtypes, mInputShown, isScreenLocked); |
| satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 2584 | |
| satok | c369056 | 2012-01-10 20:14:43 +0900 | [diff] [blame] | 2585 | if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2586 | final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked(); |
| satok | c369056 | 2012-01-10 20:14:43 +0900 | [diff] [blame] | 2587 | if (currentSubtype != null) { |
| 2588 | final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId); |
| 2589 | lastInputMethodSubtypeId = |
| 2590 | getSubtypeIdFromHashCode(currentImi, currentSubtype.hashCode()); |
| 2591 | } |
| 2592 | } |
| 2593 | |
| Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 2594 | final int N = imList.size(); |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2595 | mIms = new InputMethodInfo[N]; |
| 2596 | mSubtypeIds = new int[N]; |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2597 | int checkedItem = 0; |
| 2598 | for (int i = 0; i < N; ++i) { |
| Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 2599 | final ImeSubtypeListItem item = imList.get(i); |
| 2600 | mIms[i] = item.mImi; |
| 2601 | mSubtypeIds[i] = item.mSubtypeId; |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2602 | if (mIms[i].getId().equals(lastInputMethodId)) { |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2603 | int subtypeId = mSubtypeIds[i]; |
| 2604 | if ((subtypeId == NOT_A_SUBTYPE_ID) |
| 2605 | || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0) |
| 2606 | || (subtypeId == lastInputMethodSubtypeId)) { |
| 2607 | checkedItem = i; |
| 2608 | } |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2609 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2610 | } |
| Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 2611 | final TypedArray a = context.obtainStyledAttributes(null, |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2612 | com.android.internal.R.styleable.DialogPreference, |
| 2613 | com.android.internal.R.attr.alertDialogStyle, 0); |
| 2614 | mDialogBuilder = new AlertDialog.Builder(context) |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2615 | .setOnCancelListener(new OnCancelListener() { |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2616 | @Override |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2617 | public void onCancel(DialogInterface dialog) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2618 | hideInputMethodMenu(); |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2619 | } |
| 2620 | }) |
| 2621 | .setIcon(a.getDrawable( |
| 2622 | com.android.internal.R.styleable.DialogPreference_dialogTitle)); |
| 2623 | a.recycle(); |
| satok | 0103849 | 2012-04-09 21:08:27 +0900 | [diff] [blame] | 2624 | final LayoutInflater inflater = |
| 2625 | (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 2626 | final View tv = inflater.inflate( |
| 2627 | com.android.internal.R.layout.input_method_switch_dialog_title, null); |
| 2628 | mDialogBuilder.setCustomTitle(tv); |
| 2629 | |
| 2630 | // Setup layout for a toggle switch of the hardware keyboard |
| 2631 | mSwitchingDialogTitleView = tv; |
| 2632 | mSwitchingDialogTitleView.findViewById( |
| 2633 | com.android.internal.R.id.hard_keyboard_section).setVisibility( |
| 2634 | mWindowManagerService.isHardKeyboardAvailable() ? |
| 2635 | View.VISIBLE : View.GONE); |
| 2636 | final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById( |
| 2637 | com.android.internal.R.id.hard_keyboard_switch)); |
| 2638 | hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled()); |
| 2639 | hardKeySwitch.setOnCheckedChangeListener( |
| 2640 | new OnCheckedChangeListener() { |
| 2641 | @Override |
| 2642 | public void onCheckedChanged( |
| 2643 | CompoundButton buttonView, boolean isChecked) { |
| 2644 | mWindowManagerService.setHardKeyboardEnabled(isChecked); |
| 2645 | } |
| 2646 | }); |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2647 | |
| Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 2648 | final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context, |
| 2649 | com.android.internal.R.layout.simple_list_item_2_single_choice, imList, |
| 2650 | checkedItem); |
| 2651 | |
| 2652 | mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2653 | new AlertDialog.OnClickListener() { |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2654 | @Override |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2655 | public void onClick(DialogInterface dialog, int which) { |
| 2656 | synchronized (mMethodMap) { |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2657 | if (mIms == null || mIms.length <= which |
| 2658 | || mSubtypeIds == null || mSubtypeIds.length <= which) { |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2659 | return; |
| 2660 | } |
| 2661 | InputMethodInfo im = mIms[which]; |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2662 | int subtypeId = mSubtypeIds[which]; |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2663 | hideInputMethodMenu(); |
| 2664 | if (im != null) { |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2665 | if ((subtypeId < 0) |
| Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2666 | || (subtypeId >= im.getSubtypeCount())) { |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2667 | subtypeId = NOT_A_SUBTYPE_ID; |
| 2668 | } |
| 2669 | setInputMethodLocked(im.getId(), subtypeId); |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2670 | } |
| Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 2671 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2672 | } |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2673 | }); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2674 | |
| satok | bc81b69 | 2011-08-26 16:22:22 +0900 | [diff] [blame] | 2675 | if (showSubtypes && !isScreenLocked) { |
| satok | 82beadf | 2010-12-27 19:03:06 +0900 | [diff] [blame] | 2676 | mDialogBuilder.setPositiveButton( |
| 2677 | com.android.internal.R.string.configure_input_methods, |
| satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2678 | new DialogInterface.OnClickListener() { |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2679 | @Override |
| satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2680 | public void onClick(DialogInterface dialog, int whichButton) { |
| satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 2681 | showConfigureInputMethods(); |
| satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2682 | } |
| 2683 | }); |
| 2684 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2685 | mSwitchingDialog = mDialogBuilder.create(); |
| Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 2686 | mSwitchingDialog.setCanceledOnTouchOutside(true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2687 | mSwitchingDialog.getWindow().setType( |
| 2688 | WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG); |
| Satoshi Kataoka | c86884c | 2012-10-09 15:20:29 +0900 | [diff] [blame] | 2689 | mSwitchingDialog.getWindow().getAttributes().privateFlags |= |
| 2690 | WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS; |
| Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 2691 | mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2692 | mSwitchingDialog.show(); |
| 2693 | } |
| 2694 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2695 | |
| satok | 93d744d | 2012-05-09 17:14:08 +0900 | [diff] [blame] | 2696 | private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> { |
| Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 2697 | public final CharSequence mImeName; |
| 2698 | public final CharSequence mSubtypeName; |
| 2699 | public final InputMethodInfo mImi; |
| 2700 | public final int mSubtypeId; |
| satok | 93d744d | 2012-05-09 17:14:08 +0900 | [diff] [blame] | 2701 | private final boolean mIsSystemLocale; |
| 2702 | private final boolean mIsSystemLanguage; |
| 2703 | |
| Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 2704 | public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName, |
| satok | 93d744d | 2012-05-09 17:14:08 +0900 | [diff] [blame] | 2705 | InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) { |
| Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 2706 | mImeName = imeName; |
| 2707 | mSubtypeName = subtypeName; |
| 2708 | mImi = imi; |
| 2709 | mSubtypeId = subtypeId; |
| satok | 93d744d | 2012-05-09 17:14:08 +0900 | [diff] [blame] | 2710 | if (TextUtils.isEmpty(subtypeLocale)) { |
| 2711 | mIsSystemLocale = false; |
| 2712 | mIsSystemLanguage = false; |
| 2713 | } else { |
| 2714 | mIsSystemLocale = subtypeLocale.equals(systemLocale); |
| 2715 | mIsSystemLanguage = mIsSystemLocale |
| 2716 | || subtypeLocale.startsWith(systemLocale.substring(0, 2)); |
| 2717 | } |
| 2718 | } |
| 2719 | |
| 2720 | @Override |
| 2721 | public int compareTo(ImeSubtypeListItem other) { |
| 2722 | if (TextUtils.isEmpty(mImeName)) { |
| 2723 | return 1; |
| 2724 | } |
| 2725 | if (TextUtils.isEmpty(other.mImeName)) { |
| 2726 | return -1; |
| 2727 | } |
| 2728 | if (!TextUtils.equals(mImeName, other.mImeName)) { |
| 2729 | return mImeName.toString().compareTo(other.mImeName.toString()); |
| 2730 | } |
| 2731 | if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) { |
| 2732 | return 0; |
| 2733 | } |
| 2734 | if (mIsSystemLocale) { |
| 2735 | return -1; |
| 2736 | } |
| 2737 | if (other.mIsSystemLocale) { |
| 2738 | return 1; |
| 2739 | } |
| 2740 | if (mIsSystemLanguage) { |
| 2741 | return -1; |
| 2742 | } |
| 2743 | if (other.mIsSystemLanguage) { |
| 2744 | return 1; |
| 2745 | } |
| 2746 | if (TextUtils.isEmpty(mSubtypeName)) { |
| 2747 | return 1; |
| 2748 | } |
| 2749 | if (TextUtils.isEmpty(other.mSubtypeName)) { |
| 2750 | return -1; |
| 2751 | } |
| 2752 | return mSubtypeName.toString().compareTo(other.mSubtypeName.toString()); |
| Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 2753 | } |
| 2754 | } |
| 2755 | |
| 2756 | private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> { |
| 2757 | private final LayoutInflater mInflater; |
| 2758 | private final int mTextViewResourceId; |
| 2759 | private final List<ImeSubtypeListItem> mItemsList; |
| 2760 | private final int mCheckedItem; |
| 2761 | public ImeSubtypeListAdapter(Context context, int textViewResourceId, |
| 2762 | List<ImeSubtypeListItem> itemsList, int checkedItem) { |
| 2763 | super(context, textViewResourceId, itemsList); |
| 2764 | mTextViewResourceId = textViewResourceId; |
| 2765 | mItemsList = itemsList; |
| 2766 | mCheckedItem = checkedItem; |
| 2767 | mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 2768 | } |
| 2769 | |
| 2770 | @Override |
| 2771 | public View getView(int position, View convertView, ViewGroup parent) { |
| 2772 | final View view = convertView != null ? convertView |
| 2773 | : mInflater.inflate(mTextViewResourceId, null); |
| 2774 | if (position < 0 || position >= mItemsList.size()) return view; |
| 2775 | final ImeSubtypeListItem item = mItemsList.get(position); |
| 2776 | final CharSequence imeName = item.mImeName; |
| 2777 | final CharSequence subtypeName = item.mSubtypeName; |
| 2778 | final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1); |
| 2779 | final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2); |
| 2780 | if (TextUtils.isEmpty(subtypeName)) { |
| 2781 | firstTextView.setText(imeName); |
| 2782 | secondTextView.setVisibility(View.GONE); |
| 2783 | } else { |
| 2784 | firstTextView.setText(subtypeName); |
| 2785 | secondTextView.setText(imeName); |
| 2786 | secondTextView.setVisibility(View.VISIBLE); |
| 2787 | } |
| 2788 | final RadioButton radioButton = |
| 2789 | (RadioButton)view.findViewById(com.android.internal.R.id.radio); |
| 2790 | radioButton.setChecked(position == mCheckedItem); |
| 2791 | return view; |
| 2792 | } |
| 2793 | } |
| 2794 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2795 | void hideInputMethodMenu() { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2796 | synchronized (mMethodMap) { |
| 2797 | hideInputMethodMenuLocked(); |
| 2798 | } |
| 2799 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2800 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2801 | void hideInputMethodMenuLocked() { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2802 | if (DEBUG) Slog.v(TAG, "Hide switching menu"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2803 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2804 | if (mSwitchingDialog != null) { |
| 2805 | mSwitchingDialog.dismiss(); |
| 2806 | mSwitchingDialog = null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2807 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2808 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2809 | mDialogBuilder = null; |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2810 | mIms = null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2811 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2812 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2813 | // ---------------------------------------------------------------------- |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2814 | |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2815 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2816 | public boolean setInputMethodEnabled(String id, boolean enabled) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2817 | // TODO: Make this work even for non-current users? |
| 2818 | if (!calledFromValidUser()) { |
| 2819 | return false; |
| 2820 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2821 | synchronized (mMethodMap) { |
| 2822 | if (mContext.checkCallingOrSelfPermission( |
| 2823 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 2824 | != PackageManager.PERMISSION_GRANTED) { |
| 2825 | throw new SecurityException( |
| 2826 | "Requires permission " |
| 2827 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 2828 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2829 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2830 | long ident = Binder.clearCallingIdentity(); |
| 2831 | try { |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2832 | return setInputMethodEnabledLocked(id, enabled); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2833 | } finally { |
| 2834 | Binder.restoreCallingIdentity(ident); |
| 2835 | } |
| 2836 | } |
| 2837 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2838 | |
| 2839 | boolean setInputMethodEnabledLocked(String id, boolean enabled) { |
| 2840 | // Make sure this is a valid input method. |
| 2841 | InputMethodInfo imm = mMethodMap.get(id); |
| 2842 | if (imm == null) { |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2843 | throw new IllegalArgumentException("Unknown id: " + mCurMethodId); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2844 | } |
| 2845 | |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2846 | List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings |
| 2847 | .getEnabledInputMethodsAndSubtypeListLocked(); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2848 | |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2849 | if (enabled) { |
| 2850 | for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) { |
| 2851 | if (pair.first.equals(id)) { |
| 2852 | // We are enabling this input method, but it is already enabled. |
| 2853 | // Nothing to do. The previous state was enabled. |
| 2854 | return true; |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2855 | } |
| 2856 | } |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2857 | mSettings.appendAndPutEnabledInputMethodLocked(id, false); |
| 2858 | // Previous state was disabled. |
| 2859 | return false; |
| 2860 | } else { |
| 2861 | StringBuilder builder = new StringBuilder(); |
| 2862 | if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked( |
| 2863 | builder, enabledInputMethodsList, id)) { |
| 2864 | // Disabled input method is currently selected, switch to another one. |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2865 | final String selId = mSettings.getSelectedInputMethod(); |
| satok | 03eb319a | 2010-11-11 18:17:42 +0900 | [diff] [blame] | 2866 | if (id.equals(selId) && !chooseNewDefaultIMELocked()) { |
| 2867 | Slog.i(TAG, "Can't find new IME, unsetting the current input method."); |
| 2868 | resetSelectedInputMethodAndSubtypeLocked(""); |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2869 | } |
| 2870 | // Previous state was enabled. |
| 2871 | return true; |
| 2872 | } else { |
| 2873 | // We are disabling the input method but it is already disabled. |
| 2874 | // Nothing to do. The previous state was disabled. |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2875 | return false; |
| 2876 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2877 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2878 | } |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 2879 | |
| satok | 57ffc00 | 2011-01-25 00:11:47 +0900 | [diff] [blame] | 2880 | private boolean canAddToLastInputMethod(InputMethodSubtype subtype) { |
| 2881 | if (subtype == null) return true; |
| satok | 9b41579 | 2011-05-30 12:37:52 +0900 | [diff] [blame] | 2882 | return !subtype.isAuxiliary(); |
| satok | 57ffc00 | 2011-01-25 00:11:47 +0900 | [diff] [blame] | 2883 | } |
| 2884 | |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2885 | private void saveCurrentInputMethodAndSubtypeToHistory() { |
| 2886 | String subtypeId = NOT_A_SUBTYPE_ID_STR; |
| 2887 | if (mCurrentSubtype != null) { |
| 2888 | subtypeId = String.valueOf(mCurrentSubtype.hashCode()); |
| 2889 | } |
| satok | 57ffc00 | 2011-01-25 00:11:47 +0900 | [diff] [blame] | 2890 | if (canAddToLastInputMethod(mCurrentSubtype)) { |
| 2891 | mSettings.addSubtypeToHistory(mCurMethodId, subtypeId); |
| 2892 | } |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2893 | } |
| 2894 | |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2895 | private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId, |
| 2896 | boolean setSubtypeOnly) { |
| 2897 | // Update the history of InputMethod and Subtype |
| 2898 | saveCurrentInputMethodAndSubtypeToHistory(); |
| 2899 | |
| 2900 | // Set Subtype here |
| 2901 | if (imi == null || subtypeId < 0) { |
| 2902 | mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID); |
| Tadashi G. Takaoka | 0ba75bb | 2010-11-09 12:19:32 -0800 | [diff] [blame] | 2903 | mCurrentSubtype = null; |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2904 | } else { |
| Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2905 | if (subtypeId < imi.getSubtypeCount()) { |
| 2906 | InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId); |
| 2907 | mSettings.putSelectedSubtype(subtype.hashCode()); |
| 2908 | mCurrentSubtype = subtype; |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2909 | } else { |
| 2910 | mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID); |
| satok | d81e950 | 2012-05-21 12:58:45 +0900 | [diff] [blame] | 2911 | // If the subtype is not specified, choose the most applicable one |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2912 | mCurrentSubtype = getCurrentInputMethodSubtypeLocked(); |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2913 | } |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2914 | } |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2915 | |
| satok | 4c0e715 | 2012-06-20 20:08:44 +0900 | [diff] [blame] | 2916 | // Workaround. |
| 2917 | // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked |
| 2918 | // IMEs are not recognized and considered uninstalled. |
| 2919 | // Actually, we can't move everything after SystemReady because |
| 2920 | // IMMS needs to run in the encryption lock screen. So, we just skip changing |
| 2921 | // the default IME here and try cheking the default IME again in systemReady(). |
| 2922 | // TODO: Do nothing before system ready and implement a separated logic for |
| 2923 | // the encryption lock screen. |
| 2924 | // TODO: ASEC should be ready before IMMS is instantiated. |
| 2925 | if (mSystemReady && !setSubtypeOnly) { |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2926 | // Set InputMethod here |
| 2927 | mSettings.putSelectedInputMethod(imi != null ? imi.getId() : ""); |
| 2928 | } |
| 2929 | } |
| 2930 | |
| 2931 | private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) { |
| 2932 | InputMethodInfo imi = mMethodMap.get(newDefaultIme); |
| 2933 | int lastSubtypeId = NOT_A_SUBTYPE_ID; |
| 2934 | // newDefaultIme is empty when there is no candidate for the selected IME. |
| 2935 | if (imi != null && !TextUtils.isEmpty(newDefaultIme)) { |
| 2936 | String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme); |
| 2937 | if (subtypeHashCode != null) { |
| 2938 | try { |
| 2939 | lastSubtypeId = getSubtypeIdFromHashCode( |
| 2940 | imi, Integer.valueOf(subtypeHashCode)); |
| 2941 | } catch (NumberFormatException e) { |
| 2942 | Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e); |
| 2943 | } |
| 2944 | } |
| 2945 | } |
| 2946 | setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false); |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | private int getSelectedInputMethodSubtypeId(String id) { |
| 2950 | InputMethodInfo imi = mMethodMap.get(id); |
| 2951 | if (imi == null) { |
| 2952 | return NOT_A_SUBTYPE_ID; |
| 2953 | } |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 2954 | final int subtypeHashCode = mSettings.getSelectedInputMethodSubtypeHashCode(); |
| 2955 | return getSubtypeIdFromHashCode(imi, subtypeHashCode); |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2956 | } |
| 2957 | |
| satok | fdf419e | 2012-05-08 16:52:08 +0900 | [diff] [blame] | 2958 | private static boolean isValidSubtypeId(InputMethodInfo imi, int subtypeHashCode) { |
| 2959 | return getSubtypeIdFromHashCode(imi, subtypeHashCode) != NOT_A_SUBTYPE_ID; |
| 2960 | } |
| 2961 | |
| 2962 | private static int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) { |
| satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 2963 | if (imi != null) { |
| Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2964 | final int subtypeCount = imi.getSubtypeCount(); |
| 2965 | for (int i = 0; i < subtypeCount; ++i) { |
| 2966 | InputMethodSubtype ims = imi.getSubtypeAt(i); |
| satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 2967 | if (subtypeHashCode == ims.hashCode()) { |
| 2968 | return i; |
| 2969 | } |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2970 | } |
| 2971 | } |
| 2972 | return NOT_A_SUBTYPE_ID; |
| 2973 | } |
| 2974 | |
| satok | a86f5e4 | 2011-09-02 17:12:42 +0900 | [diff] [blame] | 2975 | private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked( |
| 2976 | Resources res, InputMethodInfo imi) { |
| 2977 | final List<InputMethodSubtype> subtypes = getSubtypes(imi); |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2978 | final String systemLocale = res.getConfiguration().locale.toString(); |
| satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2979 | if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>(); |
| satok | 4a553e3 | 2011-10-03 17:05:50 +0900 | [diff] [blame] | 2980 | final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap = |
| satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2981 | new HashMap<String, InputMethodSubtype>(); |
| satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2982 | final int N = subtypes.size(); |
| satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2983 | for (int i = 0; i < N; ++i) { |
| satok | a86f5e4 | 2011-09-02 17:12:42 +0900 | [diff] [blame] | 2984 | // scan overriding implicitly enabled subtypes. |
| 2985 | InputMethodSubtype subtype = subtypes.get(i); |
| 2986 | if (subtype.overridesImplicitlyEnabledSubtype()) { |
| 2987 | final String mode = subtype.getMode(); |
| 2988 | if (!applicableModeAndSubtypesMap.containsKey(mode)) { |
| 2989 | applicableModeAndSubtypesMap.put(mode, subtype); |
| 2990 | } |
| 2991 | } |
| 2992 | } |
| 2993 | if (applicableModeAndSubtypesMap.size() > 0) { |
| 2994 | return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values()); |
| 2995 | } |
| 2996 | for (int i = 0; i < N; ++i) { |
| satok | 4a553e3 | 2011-10-03 17:05:50 +0900 | [diff] [blame] | 2997 | final InputMethodSubtype subtype = subtypes.get(i); |
| satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2998 | final String locale = subtype.getLocale(); |
| 2999 | final String mode = subtype.getMode(); |
| 3000 | // When system locale starts with subtype's locale, that subtype will be applicable |
| 3001 | // for system locale |
| 3002 | // For instance, it's clearly applicable for cases like system locale = en_US and |
| 3003 | // subtype = en, but it is not necessarily considered applicable for cases like system |
| 3004 | // locale = en and subtype = en_US. |
| 3005 | // We just call systemLocale.startsWith(locale) in this function because there is no |
| 3006 | // need to find applicable subtypes aggressively unlike |
| 3007 | // findLastResortApplicableSubtypeLocked. |
| 3008 | if (systemLocale.startsWith(locale)) { |
| satok | 4a553e3 | 2011-10-03 17:05:50 +0900 | [diff] [blame] | 3009 | final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode); |
| satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 3010 | // If more applicable subtypes are contained, skip. |
| satok | 4a553e3 | 2011-10-03 17:05:50 +0900 | [diff] [blame] | 3011 | if (applicableSubtype != null) { |
| 3012 | if (systemLocale.equals(applicableSubtype.getLocale())) continue; |
| 3013 | if (!systemLocale.equals(locale)) continue; |
| 3014 | } |
| satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 3015 | applicableModeAndSubtypesMap.put(mode, subtype); |
| satok | c369056 | 2012-01-10 20:14:43 +0900 | [diff] [blame] | 3016 | } |
| 3017 | } |
| 3018 | final InputMethodSubtype keyboardSubtype |
| 3019 | = applicableModeAndSubtypesMap.get(SUBTYPE_MODE_KEYBOARD); |
| 3020 | final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>( |
| 3021 | applicableModeAndSubtypesMap.values()); |
| 3022 | if (keyboardSubtype != null && !keyboardSubtype.containsExtraValueKey(TAG_ASCII_CAPABLE)) { |
| 3023 | for (int i = 0; i < N; ++i) { |
| 3024 | final InputMethodSubtype subtype = subtypes.get(i); |
| 3025 | final String mode = subtype.getMode(); |
| 3026 | if (SUBTYPE_MODE_KEYBOARD.equals(mode) && subtype.containsExtraValueKey( |
| 3027 | TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE)) { |
| 3028 | applicableSubtypes.add(subtype); |
| satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 3029 | } |
| 3030 | } |
| 3031 | } |
| satok | c369056 | 2012-01-10 20:14:43 +0900 | [diff] [blame] | 3032 | if (keyboardSubtype == null) { |
| satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 3033 | InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked( |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3034 | res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true); |
| satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 3035 | if (lastResortKeyboardSubtype != null) { |
| 3036 | applicableSubtypes.add(lastResortKeyboardSubtype); |
| 3037 | } |
| 3038 | } |
| 3039 | return applicableSubtypes; |
| 3040 | } |
| 3041 | |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3042 | /** |
| 3043 | * If there are no selected subtypes, tries finding the most applicable one according to the |
| 3044 | * given locale. |
| 3045 | * @param subtypes this function will search the most applicable subtype in subtypes |
| 3046 | * @param mode subtypes will be filtered by mode |
| 3047 | * @param locale subtypes will be filtered by locale |
| satok | 7599a7fb | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 3048 | * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype, |
| 3049 | * it will return the first subtype matched with mode |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3050 | * @return the most applicable subtypeId |
| 3051 | */ |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3052 | private static InputMethodSubtype findLastResortApplicableSubtypeLocked( |
| 3053 | Resources res, List<InputMethodSubtype> subtypes, String mode, String locale, |
| satok | 7599a7fb | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 3054 | boolean canIgnoreLocaleAsLastResort) { |
| satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 3055 | if (subtypes == null || subtypes.size() == 0) { |
| satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 3056 | return null; |
| satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 3057 | } |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3058 | if (TextUtils.isEmpty(locale)) { |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3059 | locale = res.getConfiguration().locale.toString(); |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3060 | } |
| satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 3061 | final String language = locale.substring(0, 2); |
| 3062 | boolean partialMatchFound = false; |
| satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 3063 | InputMethodSubtype applicableSubtype = null; |
| satok | 7599a7fb | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 3064 | InputMethodSubtype firstMatchedModeSubtype = null; |
| satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 3065 | final int N = subtypes.size(); |
| 3066 | for (int i = 0; i < N; ++i) { |
| satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 3067 | InputMethodSubtype subtype = subtypes.get(i); |
| 3068 | final String subtypeLocale = subtype.getLocale(); |
| satok | d871343 | 2011-01-18 00:55:13 +0900 | [diff] [blame] | 3069 | // An applicable subtype should match "mode". If mode is null, mode will be ignored, |
| 3070 | // and all subtypes with all modes can be candidates. |
| 3071 | if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) { |
| satok | 7599a7fb | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 3072 | if (firstMatchedModeSubtype == null) { |
| 3073 | firstMatchedModeSubtype = subtype; |
| 3074 | } |
| satok | 9ef0283 | 2010-11-04 21:17:48 +0900 | [diff] [blame] | 3075 | if (locale.equals(subtypeLocale)) { |
| 3076 | // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US") |
| satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 3077 | applicableSubtype = subtype; |
| satok | 9ef0283 | 2010-11-04 21:17:48 +0900 | [diff] [blame] | 3078 | break; |
| 3079 | } else if (!partialMatchFound && subtypeLocale.startsWith(language)) { |
| 3080 | // Partial match (e.g. system locale is "en_US" and subtype locale is "en") |
| satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 3081 | applicableSubtype = subtype; |
| satok | 9ef0283 | 2010-11-04 21:17:48 +0900 | [diff] [blame] | 3082 | partialMatchFound = true; |
| 3083 | } |
| satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 3084 | } |
| 3085 | } |
| 3086 | |
| satok | 7599a7fb | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 3087 | if (applicableSubtype == null && canIgnoreLocaleAsLastResort) { |
| 3088 | return firstMatchedModeSubtype; |
| satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 3089 | } |
| 3090 | |
| satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 3091 | // The first subtype applicable to the system locale will be defined as the most applicable |
| 3092 | // subtype. |
| 3093 | if (DEBUG) { |
| satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 3094 | if (applicableSubtype != null) { |
| 3095 | Slog.d(TAG, "Applicable InputMethodSubtype was found: " |
| 3096 | + applicableSubtype.getMode() + "," + applicableSubtype.getLocale()); |
| 3097 | } |
| satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 3098 | } |
| satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 3099 | return applicableSubtype; |
| satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 3100 | } |
| 3101 | |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3102 | // If there are no selected shortcuts, tries finding the most applicable ones. |
| 3103 | private Pair<InputMethodInfo, InputMethodSubtype> |
| 3104 | findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) { |
| 3105 | List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked(); |
| 3106 | InputMethodInfo mostApplicableIMI = null; |
| satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 3107 | InputMethodSubtype mostApplicableSubtype = null; |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3108 | boolean foundInSystemIME = false; |
| 3109 | |
| 3110 | // Search applicable subtype for each InputMethodInfo |
| 3111 | for (InputMethodInfo imi: imis) { |
| satok | 7599a7fb | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 3112 | final String imiId = imi.getId(); |
| 3113 | if (foundInSystemIME && !imiId.equals(mCurMethodId)) { |
| 3114 | continue; |
| 3115 | } |
| satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 3116 | InputMethodSubtype subtype = null; |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3117 | final List<InputMethodSubtype> enabledSubtypes = |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3118 | getEnabledInputMethodSubtypeListLocked(imi, true); |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3119 | // 1. Search by the current subtype's locale from enabledSubtypes. |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3120 | if (mCurrentSubtype != null) { |
| satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 3121 | subtype = findLastResortApplicableSubtypeLocked( |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3122 | mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false); |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3123 | } |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3124 | // 2. Search by the system locale from enabledSubtypes. |
| 3125 | // 3. Search the first enabled subtype matched with mode from enabledSubtypes. |
| satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 3126 | if (subtype == null) { |
| 3127 | subtype = findLastResortApplicableSubtypeLocked( |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3128 | mRes, enabledSubtypes, mode, null, true); |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3129 | } |
| satok | a86f5e4 | 2011-09-02 17:12:42 +0900 | [diff] [blame] | 3130 | final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes = |
| 3131 | getOverridingImplicitlyEnabledSubtypes(imi, mode); |
| 3132 | final ArrayList<InputMethodSubtype> subtypesForSearch = |
| 3133 | overridingImplicitlyEnabledSubtypes.isEmpty() |
| 3134 | ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes; |
| satok | 7599a7fb | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 3135 | // 4. Search by the current subtype's locale from all subtypes. |
| 3136 | if (subtype == null && mCurrentSubtype != null) { |
| 3137 | subtype = findLastResortApplicableSubtypeLocked( |
| satok | a86f5e4 | 2011-09-02 17:12:42 +0900 | [diff] [blame] | 3138 | mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false); |
| satok | 7599a7fb | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 3139 | } |
| 3140 | // 5. Search by the system locale from all subtypes. |
| 3141 | // 6. Search the first enabled subtype matched with mode from all subtypes. |
| satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 3142 | if (subtype == null) { |
| satok | 7599a7fb | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 3143 | subtype = findLastResortApplicableSubtypeLocked( |
| satok | a86f5e4 | 2011-09-02 17:12:42 +0900 | [diff] [blame] | 3144 | mRes, subtypesForSearch, mode, null, true); |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3145 | } |
| satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 3146 | if (subtype != null) { |
| satok | 7599a7fb | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 3147 | if (imiId.equals(mCurMethodId)) { |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3148 | // The current input method is the most applicable IME. |
| 3149 | mostApplicableIMI = imi; |
| satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 3150 | mostApplicableSubtype = subtype; |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3151 | break; |
| satok | 7599a7fb | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 3152 | } else if (!foundInSystemIME) { |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3153 | // The system input method is 2nd applicable IME. |
| 3154 | mostApplicableIMI = imi; |
| satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 3155 | mostApplicableSubtype = subtype; |
| satok | 7599a7fb | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 3156 | if ((imi.getServiceInfo().applicationInfo.flags |
| 3157 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 3158 | foundInSystemIME = true; |
| 3159 | } |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3160 | } |
| 3161 | } |
| 3162 | } |
| 3163 | if (DEBUG) { |
| satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 3164 | if (mostApplicableIMI != null) { |
| 3165 | Slog.w(TAG, "Most applicable shortcut input method was:" |
| 3166 | + mostApplicableIMI.getId()); |
| 3167 | if (mostApplicableSubtype != null) { |
| 3168 | Slog.w(TAG, "Most applicable shortcut input method subtype was:" |
| 3169 | + "," + mostApplicableSubtype.getMode() + "," |
| 3170 | + mostApplicableSubtype.getLocale()); |
| 3171 | } |
| 3172 | } |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3173 | } |
| satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 3174 | if (mostApplicableIMI != null) { |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3175 | return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI, |
| satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 3176 | mostApplicableSubtype); |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3177 | } else { |
| 3178 | return null; |
| 3179 | } |
| 3180 | } |
| 3181 | |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 3182 | /** |
| 3183 | * @return Return the current subtype of this input method. |
| 3184 | */ |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 3185 | @Override |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 3186 | public InputMethodSubtype getCurrentInputMethodSubtype() { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3187 | // TODO: Make this work even for non-current users? |
| 3188 | if (!calledFromValidUser()) { |
| 3189 | return null; |
| 3190 | } |
| 3191 | synchronized (mMethodMap) { |
| 3192 | return getCurrentInputMethodSubtypeLocked(); |
| 3193 | } |
| 3194 | } |
| 3195 | |
| 3196 | private InputMethodSubtype getCurrentInputMethodSubtypeLocked() { |
| satok | fdf419e | 2012-05-08 16:52:08 +0900 | [diff] [blame] | 3197 | if (mCurMethodId == null) { |
| 3198 | return null; |
| 3199 | } |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3200 | final boolean subtypeIsSelected = |
| 3201 | mSettings.getSelectedInputMethodSubtypeHashCode() != NOT_A_SUBTYPE_ID; |
| 3202 | final InputMethodInfo imi = mMethodMap.get(mCurMethodId); |
| 3203 | if (imi == null || imi.getSubtypeCount() == 0) { |
| 3204 | return null; |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3205 | } |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3206 | if (!subtypeIsSelected || mCurrentSubtype == null |
| 3207 | || !isValidSubtypeId(imi, mCurrentSubtype.hashCode())) { |
| 3208 | int subtypeId = getSelectedInputMethodSubtypeId(mCurMethodId); |
| 3209 | if (subtypeId == NOT_A_SUBTYPE_ID) { |
| 3210 | // If there are no selected subtypes, the framework will try to find |
| 3211 | // the most applicable subtype from explicitly or implicitly enabled |
| 3212 | // subtypes. |
| 3213 | List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes = |
| 3214 | getEnabledInputMethodSubtypeListLocked(imi, true); |
| 3215 | // If there is only one explicitly or implicitly enabled subtype, |
| 3216 | // just returns it. |
| 3217 | if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) { |
| 3218 | mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0); |
| 3219 | } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) { |
| 3220 | mCurrentSubtype = findLastResortApplicableSubtypeLocked( |
| 3221 | mRes, explicitlyOrImplicitlyEnabledSubtypes, |
| 3222 | SUBTYPE_MODE_KEYBOARD, null, true); |
| 3223 | if (mCurrentSubtype == null) { |
| satok | fdf419e | 2012-05-08 16:52:08 +0900 | [diff] [blame] | 3224 | mCurrentSubtype = findLastResortApplicableSubtypeLocked( |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3225 | mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null, |
| 3226 | true); |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3227 | } |
| satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 3228 | } |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3229 | } else { |
| 3230 | mCurrentSubtype = getSubtypes(imi).get(subtypeId); |
| satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 3231 | } |
| 3232 | } |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3233 | return mCurrentSubtype; |
| satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 3234 | } |
| 3235 | |
| satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 3236 | private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi, |
| 3237 | InputMethodSubtype subtype) { |
| 3238 | if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) { |
| 3239 | mShortcutInputMethodsAndSubtypes.get(imi).add(subtype); |
| 3240 | } else { |
| 3241 | ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>(); |
| 3242 | subtypes.add(subtype); |
| 3243 | mShortcutInputMethodsAndSubtypes.put(imi, subtypes); |
| 3244 | } |
| 3245 | } |
| 3246 | |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3247 | // TODO: We should change the return type from List to List<Parcelable> |
| satok | dbf2950 | 2011-08-25 15:28:23 +0900 | [diff] [blame] | 3248 | @SuppressWarnings("rawtypes") |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3249 | @Override |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3250 | public List getShortcutInputMethodsAndSubtypes() { |
| 3251 | synchronized (mMethodMap) { |
| satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 3252 | ArrayList<Object> ret = new ArrayList<Object>(); |
| satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 3253 | if (mShortcutInputMethodsAndSubtypes.size() == 0) { |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3254 | // If there are no selected shortcut subtypes, the framework will try to find |
| 3255 | // the most applicable subtype from all subtypes whose mode is |
| 3256 | // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode. |
| satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 3257 | Pair<InputMethodInfo, InputMethodSubtype> info = |
| 3258 | findLastResortApplicableShortcutInputMethodAndSubtypeLocked( |
| 3259 | SUBTYPE_MODE_VOICE); |
| satok | 7599a7fb | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 3260 | if (info != null) { |
| satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 3261 | ret.add(info.first); |
| 3262 | ret.add(info.second); |
| satok | 7599a7fb | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 3263 | } |
| satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 3264 | return ret; |
| satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 3265 | } |
| satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 3266 | for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) { |
| 3267 | ret.add(imi); |
| 3268 | for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) { |
| 3269 | ret.add(subtype); |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3270 | } |
| 3271 | } |
| satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 3272 | return ret; |
| satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 3273 | } |
| 3274 | } |
| 3275 | |
| satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 3276 | @Override |
| satok | b66d287 | 2010-11-10 01:04:04 +0900 | [diff] [blame] | 3277 | public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3278 | // TODO: Make this work even for non-current users? |
| 3279 | if (!calledFromValidUser()) { |
| 3280 | return false; |
| 3281 | } |
| satok | b66d287 | 2010-11-10 01:04:04 +0900 | [diff] [blame] | 3282 | synchronized (mMethodMap) { |
| 3283 | if (subtype != null && mCurMethodId != null) { |
| 3284 | InputMethodInfo imi = mMethodMap.get(mCurMethodId); |
| 3285 | int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode()); |
| 3286 | if (subtypeId != NOT_A_SUBTYPE_ID) { |
| 3287 | setInputMethodLocked(mCurMethodId, subtypeId); |
| 3288 | return true; |
| 3289 | } |
| 3290 | } |
| 3291 | return false; |
| 3292 | } |
| 3293 | } |
| 3294 | |
| satok | 688bd47 | 2012-02-09 20:09:17 +0900 | [diff] [blame] | 3295 | private static class InputMethodAndSubtypeListManager { |
| 3296 | private final Context mContext; |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3297 | // Used to load label |
| satok | 688bd47 | 2012-02-09 20:09:17 +0900 | [diff] [blame] | 3298 | private final PackageManager mPm; |
| 3299 | private final InputMethodManagerService mImms; |
| satok | 93d744d | 2012-05-09 17:14:08 +0900 | [diff] [blame] | 3300 | private final String mSystemLocaleStr; |
| satok | 688bd47 | 2012-02-09 20:09:17 +0900 | [diff] [blame] | 3301 | public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) { |
| 3302 | mContext = context; |
| 3303 | mPm = context.getPackageManager(); |
| 3304 | mImms = imms; |
| satok | 0a1bcf4 | 2012-05-16 19:26:31 +0900 | [diff] [blame] | 3305 | final Locale locale = context.getResources().getConfiguration().locale; |
| 3306 | mSystemLocaleStr = locale != null ? locale.toString() : ""; |
| satok | 688bd47 | 2012-02-09 20:09:17 +0900 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis = |
| 3310 | new TreeMap<InputMethodInfo, List<InputMethodSubtype>>( |
| 3311 | new Comparator<InputMethodInfo>() { |
| 3312 | @Override |
| 3313 | public int compare(InputMethodInfo imi1, InputMethodInfo imi2) { |
| 3314 | if (imi2 == null) return 0; |
| 3315 | if (imi1 == null) return 1; |
| 3316 | if (mPm == null) { |
| 3317 | return imi1.getId().compareTo(imi2.getId()); |
| 3318 | } |
| 3319 | CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId(); |
| 3320 | CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId(); |
| 3321 | return imiId1.toString().compareTo(imiId2.toString()); |
| 3322 | } |
| 3323 | }); |
| 3324 | |
| 3325 | public ImeSubtypeListItem getNextInputMethod( |
| 3326 | boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) { |
| 3327 | if (imi == null) { |
| 3328 | return null; |
| 3329 | } |
| 3330 | final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList(); |
| 3331 | if (imList.size() <= 1) { |
| 3332 | return null; |
| 3333 | } |
| 3334 | final int N = imList.size(); |
| 3335 | final int currentSubtypeId = subtype != null |
| satok | fdf419e | 2012-05-08 16:52:08 +0900 | [diff] [blame] | 3336 | ? getSubtypeIdFromHashCode(imi, subtype.hashCode()) |
| satok | 688bd47 | 2012-02-09 20:09:17 +0900 | [diff] [blame] | 3337 | : NOT_A_SUBTYPE_ID; |
| 3338 | for (int i = 0; i < N; ++i) { |
| 3339 | final ImeSubtypeListItem isli = imList.get(i); |
| 3340 | if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) { |
| 3341 | if (!onlyCurrentIme) { |
| 3342 | return imList.get((i + 1) % N); |
| 3343 | } |
| 3344 | for (int j = 0; j < N - 1; ++j) { |
| 3345 | final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N); |
| 3346 | if (candidate.mImi.equals(imi)) { |
| 3347 | return candidate; |
| 3348 | } |
| 3349 | } |
| 3350 | return null; |
| 3351 | } |
| 3352 | } |
| 3353 | return null; |
| 3354 | } |
| 3355 | |
| 3356 | public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() { |
| 3357 | return getSortedInputMethodAndSubtypeList(true, false, false); |
| 3358 | } |
| 3359 | |
| 3360 | public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes, |
| 3361 | boolean inputShown, boolean isScreenLocked) { |
| 3362 | final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>(); |
| 3363 | final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis = |
| 3364 | mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(); |
| 3365 | if (immis == null || immis.size() == 0) { |
| 3366 | return Collections.emptyList(); |
| 3367 | } |
| 3368 | mSortedImmis.clear(); |
| 3369 | mSortedImmis.putAll(immis); |
| 3370 | for (InputMethodInfo imi : mSortedImmis.keySet()) { |
| 3371 | if (imi == null) continue; |
| 3372 | List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi); |
| 3373 | HashSet<String> enabledSubtypeSet = new HashSet<String>(); |
| 3374 | for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) { |
| 3375 | enabledSubtypeSet.add(String.valueOf(subtype.hashCode())); |
| 3376 | } |
| 3377 | ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi); |
| 3378 | final CharSequence imeLabel = imi.loadLabel(mPm); |
| 3379 | if (showSubtypes && enabledSubtypeSet.size() > 0) { |
| 3380 | final int subtypeCount = imi.getSubtypeCount(); |
| 3381 | if (DEBUG) { |
| 3382 | Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId()); |
| 3383 | } |
| 3384 | for (int j = 0; j < subtypeCount; ++j) { |
| 3385 | final InputMethodSubtype subtype = imi.getSubtypeAt(j); |
| 3386 | final String subtypeHashCode = String.valueOf(subtype.hashCode()); |
| 3387 | // We show all enabled IMEs and subtypes when an IME is shown. |
| 3388 | if (enabledSubtypeSet.contains(subtypeHashCode) |
| 3389 | && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) { |
| 3390 | final CharSequence subtypeLabel = |
| 3391 | subtype.overridesImplicitlyEnabledSubtype() ? null |
| 3392 | : subtype.getDisplayName(mContext, imi.getPackageName(), |
| 3393 | imi.getServiceInfo().applicationInfo); |
| satok | 93d744d | 2012-05-09 17:14:08 +0900 | [diff] [blame] | 3394 | imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j, |
| 3395 | subtype.getLocale(), mSystemLocaleStr)); |
| satok | 688bd47 | 2012-02-09 20:09:17 +0900 | [diff] [blame] | 3396 | |
| 3397 | // Removing this subtype from enabledSubtypeSet because we no longer |
| 3398 | // need to add an entry of this subtype to imList to avoid duplicated |
| 3399 | // entries. |
| 3400 | enabledSubtypeSet.remove(subtypeHashCode); |
| 3401 | } |
| 3402 | } |
| 3403 | } else { |
| satok | 93d744d | 2012-05-09 17:14:08 +0900 | [diff] [blame] | 3404 | imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID, |
| 3405 | null, mSystemLocaleStr)); |
| satok | 688bd47 | 2012-02-09 20:09:17 +0900 | [diff] [blame] | 3406 | } |
| 3407 | } |
| satok | 93d744d | 2012-05-09 17:14:08 +0900 | [diff] [blame] | 3408 | Collections.sort(imList); |
| satok | 688bd47 | 2012-02-09 20:09:17 +0900 | [diff] [blame] | 3409 | return imList; |
| 3410 | } |
| 3411 | } |
| 3412 | |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 3413 | /** |
| 3414 | * Utility class for putting and getting settings for InputMethod |
| 3415 | * TODO: Move all putters and getters of settings to this class. |
| 3416 | */ |
| 3417 | private static class InputMethodSettings { |
| 3418 | // The string for enabled input method is saved as follows: |
| 3419 | // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0") |
| 3420 | private static final char INPUT_METHOD_SEPARATER = ':'; |
| 3421 | private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';'; |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3422 | private final TextUtils.SimpleStringSplitter mInputMethodSplitter = |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 3423 | new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER); |
| 3424 | |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3425 | private final TextUtils.SimpleStringSplitter mSubtypeSplitter = |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 3426 | new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER); |
| 3427 | |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3428 | private final Resources mRes; |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 3429 | private final ContentResolver mResolver; |
| 3430 | private final HashMap<String, InputMethodInfo> mMethodMap; |
| 3431 | private final ArrayList<InputMethodInfo> mMethodList; |
| 3432 | |
| 3433 | private String mEnabledInputMethodsStrCache; |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3434 | private int mCurrentUserId; |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 3435 | |
| 3436 | private static void buildEnabledInputMethodsSettingString( |
| 3437 | StringBuilder builder, Pair<String, ArrayList<String>> pair) { |
| 3438 | String id = pair.first; |
| 3439 | ArrayList<String> subtypes = pair.second; |
| 3440 | builder.append(id); |
| satok | 57c767c | 2010-11-01 22:34:08 +0900 | [diff] [blame] | 3441 | // Inputmethod and subtypes are saved in the settings as follows: |
| 3442 | // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1 |
| 3443 | for (String subtypeId: subtypes) { |
| 3444 | builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId); |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 3445 | } |
| 3446 | } |
| 3447 | |
| 3448 | public InputMethodSettings( |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3449 | Resources res, ContentResolver resolver, |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3450 | HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList, |
| 3451 | int userId) { |
| 3452 | setCurrentUserId(userId); |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3453 | mRes = res; |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 3454 | mResolver = resolver; |
| 3455 | mMethodMap = methodMap; |
| 3456 | mMethodList = methodList; |
| 3457 | } |
| 3458 | |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3459 | public void setCurrentUserId(int userId) { |
| 3460 | if (DEBUG) { |
| 3461 | Slog.d(TAG, "--- Swtich the current user from " + mCurrentUserId + " to " |
| 3462 | + userId + ", new ime = " + getSelectedInputMethod()); |
| 3463 | } |
| 3464 | // IMMS settings are kept per user, so keep track of current user |
| 3465 | mCurrentUserId = userId; |
| 3466 | } |
| 3467 | |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 3468 | public List<InputMethodInfo> getEnabledInputMethodListLocked() { |
| 3469 | return createEnabledInputMethodListLocked( |
| 3470 | getEnabledInputMethodsAndSubtypeListLocked()); |
| 3471 | } |
| 3472 | |
| satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 3473 | public List<Pair<InputMethodInfo, ArrayList<String>>> |
| satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 3474 | getEnabledInputMethodAndSubtypeHashCodeListLocked() { |
| 3475 | return createEnabledInputMethodAndSubtypeHashCodeListLocked( |
| satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 3476 | getEnabledInputMethodsAndSubtypeListLocked()); |
| 3477 | } |
| 3478 | |
| satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 3479 | public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked( |
| 3480 | InputMethodInfo imi) { |
| 3481 | List<Pair<String, ArrayList<String>>> imsList = |
| 3482 | getEnabledInputMethodsAndSubtypeListLocked(); |
| 3483 | ArrayList<InputMethodSubtype> enabledSubtypes = |
| 3484 | new ArrayList<InputMethodSubtype>(); |
| satok | 884ef9a | 2010-11-18 10:39:46 +0900 | [diff] [blame] | 3485 | if (imi != null) { |
| 3486 | for (Pair<String, ArrayList<String>> imsPair : imsList) { |
| 3487 | InputMethodInfo info = mMethodMap.get(imsPair.first); |
| 3488 | if (info != null && info.getId().equals(imi.getId())) { |
| Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 3489 | final int subtypeCount = info.getSubtypeCount(); |
| 3490 | for (int i = 0; i < subtypeCount; ++i) { |
| 3491 | InputMethodSubtype ims = info.getSubtypeAt(i); |
| satok | 884ef9a | 2010-11-18 10:39:46 +0900 | [diff] [blame] | 3492 | for (String s: imsPair.second) { |
| 3493 | if (String.valueOf(ims.hashCode()).equals(s)) { |
| 3494 | enabledSubtypes.add(ims); |
| 3495 | } |
| satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 3496 | } |
| 3497 | } |
| satok | 884ef9a | 2010-11-18 10:39:46 +0900 | [diff] [blame] | 3498 | break; |
| satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 3499 | } |
| satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 3500 | } |
| 3501 | } |
| 3502 | return enabledSubtypes; |
| 3503 | } |
| 3504 | |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 3505 | // At the initial boot, the settings for input methods are not set, |
| 3506 | // so we need to enable IME in that case. |
| 3507 | public void enableAllIMEsIfThereIsNoEnabledIME() { |
| 3508 | if (TextUtils.isEmpty(getEnabledInputMethodsStr())) { |
| 3509 | StringBuilder sb = new StringBuilder(); |
| 3510 | final int N = mMethodList.size(); |
| 3511 | for (int i = 0; i < N; i++) { |
| 3512 | InputMethodInfo imi = mMethodList.get(i); |
| 3513 | Slog.i(TAG, "Adding: " + imi.getId()); |
| 3514 | if (i > 0) sb.append(':'); |
| 3515 | sb.append(imi.getId()); |
| 3516 | } |
| 3517 | putEnabledInputMethodsStr(sb.toString()); |
| 3518 | } |
| 3519 | } |
| 3520 | |
| satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 3521 | private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() { |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 3522 | ArrayList<Pair<String, ArrayList<String>>> imsList |
| 3523 | = new ArrayList<Pair<String, ArrayList<String>>>(); |
| 3524 | final String enabledInputMethodsStr = getEnabledInputMethodsStr(); |
| 3525 | if (TextUtils.isEmpty(enabledInputMethodsStr)) { |
| 3526 | return imsList; |
| 3527 | } |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3528 | mInputMethodSplitter.setString(enabledInputMethodsStr); |
| 3529 | while (mInputMethodSplitter.hasNext()) { |
| 3530 | String nextImsStr = mInputMethodSplitter.next(); |
| 3531 | mSubtypeSplitter.setString(nextImsStr); |
| 3532 | if (mSubtypeSplitter.hasNext()) { |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 3533 | ArrayList<String> subtypeHashes = new ArrayList<String>(); |
| 3534 | // The first element is ime id. |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3535 | String imeId = mSubtypeSplitter.next(); |
| 3536 | while (mSubtypeSplitter.hasNext()) { |
| 3537 | subtypeHashes.add(mSubtypeSplitter.next()); |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 3538 | } |
| 3539 | imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes)); |
| 3540 | } |
| 3541 | } |
| 3542 | return imsList; |
| 3543 | } |
| 3544 | |
| 3545 | public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) { |
| 3546 | if (reloadInputMethodStr) { |
| 3547 | getEnabledInputMethodsStr(); |
| 3548 | } |
| 3549 | if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) { |
| 3550 | // Add in the newly enabled input method. |
| 3551 | putEnabledInputMethodsStr(id); |
| 3552 | } else { |
| 3553 | putEnabledInputMethodsStr( |
| 3554 | mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id); |
| 3555 | } |
| 3556 | } |
| 3557 | |
| 3558 | /** |
| 3559 | * Build and put a string of EnabledInputMethods with removing specified Id. |
| 3560 | * @return the specified id was removed or not. |
| 3561 | */ |
| 3562 | public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked( |
| 3563 | StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) { |
| 3564 | boolean isRemoved = false; |
| 3565 | boolean needsAppendSeparator = false; |
| 3566 | for (Pair<String, ArrayList<String>> ims: imsList) { |
| 3567 | String curId = ims.first; |
| 3568 | if (curId.equals(id)) { |
| 3569 | // We are disabling this input method, and it is |
| 3570 | // currently enabled. Skip it to remove from the |
| 3571 | // new list. |
| 3572 | isRemoved = true; |
| 3573 | } else { |
| 3574 | if (needsAppendSeparator) { |
| 3575 | builder.append(INPUT_METHOD_SEPARATER); |
| 3576 | } else { |
| 3577 | needsAppendSeparator = true; |
| 3578 | } |
| 3579 | buildEnabledInputMethodsSettingString(builder, ims); |
| 3580 | } |
| 3581 | } |
| 3582 | if (isRemoved) { |
| 3583 | // Update the setting with the new list of input methods. |
| 3584 | putEnabledInputMethodsStr(builder.toString()); |
| 3585 | } |
| 3586 | return isRemoved; |
| 3587 | } |
| 3588 | |
| 3589 | private List<InputMethodInfo> createEnabledInputMethodListLocked( |
| 3590 | List<Pair<String, ArrayList<String>>> imsList) { |
| 3591 | final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>(); |
| 3592 | for (Pair<String, ArrayList<String>> ims: imsList) { |
| 3593 | InputMethodInfo info = mMethodMap.get(ims.first); |
| 3594 | if (info != null) { |
| 3595 | res.add(info); |
| 3596 | } |
| 3597 | } |
| 3598 | return res; |
| 3599 | } |
| 3600 | |
| satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 3601 | private List<Pair<InputMethodInfo, ArrayList<String>>> |
| satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 3602 | createEnabledInputMethodAndSubtypeHashCodeListLocked( |
| satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 3603 | List<Pair<String, ArrayList<String>>> imsList) { |
| 3604 | final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res |
| 3605 | = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>(); |
| 3606 | for (Pair<String, ArrayList<String>> ims : imsList) { |
| 3607 | InputMethodInfo info = mMethodMap.get(ims.first); |
| 3608 | if (info != null) { |
| 3609 | res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second)); |
| 3610 | } |
| 3611 | } |
| 3612 | return res; |
| 3613 | } |
| 3614 | |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 3615 | private void putEnabledInputMethodsStr(String str) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3616 | Settings.Secure.putStringForUser( |
| 3617 | mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str, mCurrentUserId); |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 3618 | mEnabledInputMethodsStrCache = str; |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3619 | if (DEBUG) { |
| 3620 | Slog.d(TAG, "putEnabledInputMethodStr: " + str); |
| 3621 | } |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 3622 | } |
| 3623 | |
| 3624 | private String getEnabledInputMethodsStr() { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3625 | mEnabledInputMethodsStrCache = Settings.Secure.getStringForUser( |
| 3626 | mResolver, Settings.Secure.ENABLED_INPUT_METHODS, mCurrentUserId); |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3627 | if (DEBUG) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3628 | Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache |
| 3629 | + ", " + mCurrentUserId); |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3630 | } |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 3631 | return mEnabledInputMethodsStrCache; |
| 3632 | } |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3633 | |
| 3634 | private void saveSubtypeHistory( |
| 3635 | List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) { |
| 3636 | StringBuilder builder = new StringBuilder(); |
| 3637 | boolean isImeAdded = false; |
| 3638 | if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) { |
| 3639 | builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append( |
| 3640 | newSubtypeId); |
| 3641 | isImeAdded = true; |
| 3642 | } |
| 3643 | for (Pair<String, String> ime: savedImes) { |
| 3644 | String imeId = ime.first; |
| 3645 | String subtypeId = ime.second; |
| 3646 | if (TextUtils.isEmpty(subtypeId)) { |
| 3647 | subtypeId = NOT_A_SUBTYPE_ID_STR; |
| 3648 | } |
| 3649 | if (isImeAdded) { |
| 3650 | builder.append(INPUT_METHOD_SEPARATER); |
| 3651 | } else { |
| 3652 | isImeAdded = true; |
| 3653 | } |
| 3654 | builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append( |
| 3655 | subtypeId); |
| 3656 | } |
| 3657 | // Remove the last INPUT_METHOD_SEPARATER |
| 3658 | putSubtypeHistoryStr(builder.toString()); |
| 3659 | } |
| 3660 | |
| 3661 | public void addSubtypeToHistory(String imeId, String subtypeId) { |
| 3662 | List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked(); |
| 3663 | for (Pair<String, String> ime: subtypeHistory) { |
| 3664 | if (ime.first.equals(imeId)) { |
| 3665 | if (DEBUG) { |
| satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 3666 | Slog.v(TAG, "Subtype found in the history: " + imeId + ", " |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3667 | + ime.second); |
| 3668 | } |
| 3669 | // We should break here |
| 3670 | subtypeHistory.remove(ime); |
| 3671 | break; |
| 3672 | } |
| 3673 | } |
| satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 3674 | if (DEBUG) { |
| 3675 | Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId); |
| 3676 | } |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3677 | saveSubtypeHistory(subtypeHistory, imeId, subtypeId); |
| 3678 | } |
| 3679 | |
| 3680 | private void putSubtypeHistoryStr(String str) { |
| 3681 | if (DEBUG) { |
| 3682 | Slog.d(TAG, "putSubtypeHistoryStr: " + str); |
| 3683 | } |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3684 | Settings.Secure.putStringForUser( |
| 3685 | mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str, mCurrentUserId); |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3686 | } |
| 3687 | |
| 3688 | public Pair<String, String> getLastInputMethodAndSubtypeLocked() { |
| 3689 | // Gets the first one from the history |
| 3690 | return getLastSubtypeForInputMethodLockedInternal(null); |
| 3691 | } |
| 3692 | |
| 3693 | public String getLastSubtypeForInputMethodLocked(String imeId) { |
| 3694 | Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId); |
| 3695 | if (ime != null) { |
| 3696 | return ime.second; |
| 3697 | } else { |
| 3698 | return null; |
| 3699 | } |
| 3700 | } |
| 3701 | |
| 3702 | private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) { |
| 3703 | List<Pair<String, ArrayList<String>>> enabledImes = |
| 3704 | getEnabledInputMethodsAndSubtypeListLocked(); |
| 3705 | List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked(); |
| satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 3706 | for (Pair<String, String> imeAndSubtype : subtypeHistory) { |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3707 | final String imeInTheHistory = imeAndSubtype.first; |
| 3708 | // If imeId is empty, returns the first IME and subtype in the history |
| 3709 | if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) { |
| 3710 | final String subtypeInTheHistory = imeAndSubtype.second; |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3711 | final String subtypeHashCode = |
| 3712 | getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked( |
| 3713 | enabledImes, imeInTheHistory, subtypeInTheHistory); |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3714 | if (!TextUtils.isEmpty(subtypeHashCode)) { |
| 3715 | if (DEBUG) { |
| satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 3716 | Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode); |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3717 | } |
| 3718 | return new Pair<String, String>(imeInTheHistory, subtypeHashCode); |
| 3719 | } |
| 3720 | } |
| 3721 | } |
| 3722 | if (DEBUG) { |
| 3723 | Slog.d(TAG, "No enabled IME found in the history"); |
| 3724 | } |
| 3725 | return null; |
| 3726 | } |
| 3727 | |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3728 | private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String, |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3729 | ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) { |
| 3730 | for (Pair<String, ArrayList<String>> enabledIme: enabledImes) { |
| 3731 | if (enabledIme.first.equals(imeId)) { |
| satok | f6cafb6 | 2011-01-17 16:29:02 +0900 | [diff] [blame] | 3732 | final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second; |
| satok | fdf419e | 2012-05-08 16:52:08 +0900 | [diff] [blame] | 3733 | final InputMethodInfo imi = mMethodMap.get(imeId); |
| satok | f6cafb6 | 2011-01-17 16:29:02 +0900 | [diff] [blame] | 3734 | if (explicitlyEnabledSubtypes.size() == 0) { |
| 3735 | // If there are no explicitly enabled subtypes, applicable subtypes are |
| 3736 | // enabled implicitly. |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3737 | // If IME is enabled and no subtypes are enabled, applicable subtypes |
| 3738 | // are enabled implicitly, so needs to treat them to be enabled. |
| satok | a86f5e4 | 2011-09-02 17:12:42 +0900 | [diff] [blame] | 3739 | if (imi != null && imi.getSubtypeCount() > 0) { |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3740 | List<InputMethodSubtype> implicitlySelectedSubtypes = |
| satok | a86f5e4 | 2011-09-02 17:12:42 +0900 | [diff] [blame] | 3741 | getImplicitlyApplicableSubtypesLocked(mRes, imi); |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3742 | if (implicitlySelectedSubtypes != null) { |
| 3743 | final int N = implicitlySelectedSubtypes.size(); |
| 3744 | for (int i = 0; i < N; ++i) { |
| 3745 | final InputMethodSubtype st = implicitlySelectedSubtypes.get(i); |
| 3746 | if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) { |
| 3747 | return subtypeHashCode; |
| 3748 | } |
| 3749 | } |
| 3750 | } |
| 3751 | } |
| 3752 | } else { |
| satok | f6cafb6 | 2011-01-17 16:29:02 +0900 | [diff] [blame] | 3753 | for (String s: explicitlyEnabledSubtypes) { |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3754 | if (s.equals(subtypeHashCode)) { |
| 3755 | // If both imeId and subtypeId are enabled, return subtypeId. |
| satok | fdf419e | 2012-05-08 16:52:08 +0900 | [diff] [blame] | 3756 | try { |
| 3757 | final int hashCode = Integer.valueOf(subtypeHashCode); |
| 3758 | // Check whether the subtype id is valid or not |
| 3759 | if (isValidSubtypeId(imi, hashCode)) { |
| 3760 | return s; |
| 3761 | } else { |
| 3762 | return NOT_A_SUBTYPE_ID_STR; |
| 3763 | } |
| 3764 | } catch (NumberFormatException e) { |
| 3765 | return NOT_A_SUBTYPE_ID_STR; |
| 3766 | } |
| satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3767 | } |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3768 | } |
| 3769 | } |
| 3770 | // If imeId was enabled but subtypeId was disabled. |
| 3771 | return NOT_A_SUBTYPE_ID_STR; |
| 3772 | } |
| 3773 | } |
| 3774 | // If both imeId and subtypeId are disabled, return null |
| 3775 | return null; |
| 3776 | } |
| 3777 | |
| 3778 | private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() { |
| 3779 | ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>(); |
| 3780 | final String subtypeHistoryStr = getSubtypeHistoryStr(); |
| 3781 | if (TextUtils.isEmpty(subtypeHistoryStr)) { |
| 3782 | return imsList; |
| 3783 | } |
| 3784 | mInputMethodSplitter.setString(subtypeHistoryStr); |
| 3785 | while (mInputMethodSplitter.hasNext()) { |
| 3786 | String nextImsStr = mInputMethodSplitter.next(); |
| 3787 | mSubtypeSplitter.setString(nextImsStr); |
| 3788 | if (mSubtypeSplitter.hasNext()) { |
| 3789 | String subtypeId = NOT_A_SUBTYPE_ID_STR; |
| 3790 | // The first element is ime id. |
| 3791 | String imeId = mSubtypeSplitter.next(); |
| 3792 | while (mSubtypeSplitter.hasNext()) { |
| 3793 | subtypeId = mSubtypeSplitter.next(); |
| 3794 | break; |
| 3795 | } |
| 3796 | imsList.add(new Pair<String, String>(imeId, subtypeId)); |
| 3797 | } |
| 3798 | } |
| 3799 | return imsList; |
| 3800 | } |
| 3801 | |
| 3802 | private String getSubtypeHistoryStr() { |
| 3803 | if (DEBUG) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3804 | Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getStringForUser( |
| 3805 | mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, mCurrentUserId)); |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3806 | } |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3807 | return Settings.Secure.getStringForUser( |
| 3808 | mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, mCurrentUserId); |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3809 | } |
| 3810 | |
| 3811 | public void putSelectedInputMethod(String imeId) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3812 | if (DEBUG) { |
| 3813 | Slog.d(TAG, "putSelectedInputMethodStr: " + imeId + ", " |
| 3814 | + mCurrentUserId); |
| 3815 | } |
| 3816 | Settings.Secure.putStringForUser( |
| 3817 | mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId, mCurrentUserId); |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3818 | } |
| 3819 | |
| 3820 | public void putSelectedSubtype(int subtypeId) { |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 3821 | if (DEBUG) { |
| 3822 | Slog.d(TAG, "putSelectedInputMethodSubtypeStr: " + subtypeId + ", " |
| 3823 | + mCurrentUserId); |
| 3824 | } |
| 3825 | Settings.Secure.putIntForUser(mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, |
| 3826 | subtypeId, mCurrentUserId); |
| 3827 | } |
| 3828 | |
| 3829 | public String getDisabledSystemInputMethods() { |
| 3830 | return Settings.Secure.getStringForUser( |
| 3831 | mResolver, Settings.Secure.DISABLED_SYSTEM_INPUT_METHODS, mCurrentUserId); |
| 3832 | } |
| 3833 | |
| 3834 | public String getSelectedInputMethod() { |
| 3835 | if (DEBUG) { |
| 3836 | Slog.d(TAG, "getSelectedInputMethodStr: " + Settings.Secure.getStringForUser( |
| 3837 | mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, mCurrentUserId) |
| 3838 | + ", " + mCurrentUserId); |
| 3839 | } |
| 3840 | return Settings.Secure.getStringForUser( |
| 3841 | mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, mCurrentUserId); |
| 3842 | } |
| 3843 | |
| 3844 | public int getSelectedInputMethodSubtypeHashCode() { |
| 3845 | try { |
| 3846 | return Settings.Secure.getIntForUser( |
| 3847 | mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, mCurrentUserId); |
| 3848 | } catch (SettingNotFoundException e) { |
| 3849 | return NOT_A_SUBTYPE_ID; |
| 3850 | } |
| 3851 | } |
| 3852 | |
| 3853 | public int getCurrentUserId() { |
| 3854 | return mCurrentUserId; |
| satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3855 | } |
| satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 3856 | } |
| 3857 | |
| Satoshi Kataoka | 5ade83b | 2012-09-26 22:59:41 +0900 | [diff] [blame] | 3858 | // TODO: Cache the state for each user and reset when the cached user is removed. |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3859 | private static class InputMethodFileManager { |
| 3860 | private static final String SYSTEM_PATH = "system"; |
| 3861 | private static final String INPUT_METHOD_PATH = "inputmethod"; |
| 3862 | private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml"; |
| 3863 | private static final String NODE_SUBTYPES = "subtypes"; |
| 3864 | private static final String NODE_SUBTYPE = "subtype"; |
| 3865 | private static final String NODE_IMI = "imi"; |
| 3866 | private static final String ATTR_ID = "id"; |
| 3867 | private static final String ATTR_LABEL = "label"; |
| 3868 | private static final String ATTR_ICON = "icon"; |
| 3869 | private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale"; |
| 3870 | private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode"; |
| 3871 | private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue"; |
| 3872 | private static final String ATTR_IS_AUXILIARY = "isAuxiliary"; |
| 3873 | private final AtomicFile mAdditionalInputMethodSubtypeFile; |
| 3874 | private final HashMap<String, InputMethodInfo> mMethodMap; |
| Satoshi Kataoka | e62e6d8 | 2012-07-02 18:45:43 +0900 | [diff] [blame] | 3875 | private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap = |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3876 | new HashMap<String, List<InputMethodSubtype>>(); |
| Satoshi Kataoka | 5ade83b | 2012-09-26 22:59:41 +0900 | [diff] [blame] | 3877 | public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) { |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3878 | if (methodMap == null) { |
| 3879 | throw new NullPointerException("methodMap is null"); |
| 3880 | } |
| 3881 | mMethodMap = methodMap; |
| Satoshi Kataoka | 5ade83b | 2012-09-26 22:59:41 +0900 | [diff] [blame] | 3882 | final File systemDir = userId == UserHandle.USER_OWNER |
| 3883 | ? new File(Environment.getDataDirectory(), SYSTEM_PATH) |
| 3884 | : Environment.getUserSystemDirectory(userId); |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3885 | final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH); |
| 3886 | if (!inputMethodDir.mkdirs()) { |
| 3887 | Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath()); |
| 3888 | } |
| 3889 | final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME); |
| 3890 | mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile); |
| 3891 | if (!subtypeFile.exists()) { |
| 3892 | // If "subtypes.xml" doesn't exist, create a blank file. |
| Satoshi Kataoka | e62e6d8 | 2012-07-02 18:45:43 +0900 | [diff] [blame] | 3893 | writeAdditionalInputMethodSubtypes( |
| 3894 | mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap); |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3895 | } else { |
| Satoshi Kataoka | e62e6d8 | 2012-07-02 18:45:43 +0900 | [diff] [blame] | 3896 | readAdditionalInputMethodSubtypes( |
| 3897 | mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile); |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3898 | } |
| 3899 | } |
| 3900 | |
| 3901 | private void deleteAllInputMethodSubtypes(String imiId) { |
| 3902 | synchronized (mMethodMap) { |
| Satoshi Kataoka | e62e6d8 | 2012-07-02 18:45:43 +0900 | [diff] [blame] | 3903 | mAdditionalSubtypesMap.remove(imiId); |
| 3904 | writeAdditionalInputMethodSubtypes( |
| 3905 | mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap); |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3906 | } |
| 3907 | } |
| 3908 | |
| 3909 | public void addInputMethodSubtypes( |
| satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame] | 3910 | InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) { |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3911 | synchronized (mMethodMap) { |
| 3912 | final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>(); |
| 3913 | final int N = additionalSubtypes.length; |
| 3914 | for (int i = 0; i < N; ++i) { |
| 3915 | final InputMethodSubtype subtype = additionalSubtypes[i]; |
| satok | ed2b24e | 2011-08-31 18:03:21 +0900 | [diff] [blame] | 3916 | if (!subtypes.contains(subtype)) { |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3917 | subtypes.add(subtype); |
| Satoshi Kataoka | e62e6d8 | 2012-07-02 18:45:43 +0900 | [diff] [blame] | 3918 | } else { |
| 3919 | Slog.w(TAG, "Duplicated subtype definition found: " |
| 3920 | + subtype.getLocale() + ", " + subtype.getMode()); |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3921 | } |
| 3922 | } |
| Satoshi Kataoka | e62e6d8 | 2012-07-02 18:45:43 +0900 | [diff] [blame] | 3923 | mAdditionalSubtypesMap.put(imi.getId(), subtypes); |
| 3924 | writeAdditionalInputMethodSubtypes( |
| 3925 | mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap); |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3926 | } |
| 3927 | } |
| 3928 | |
| 3929 | public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() { |
| 3930 | synchronized (mMethodMap) { |
| Satoshi Kataoka | e62e6d8 | 2012-07-02 18:45:43 +0900 | [diff] [blame] | 3931 | return mAdditionalSubtypesMap; |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3932 | } |
| 3933 | } |
| 3934 | |
| 3935 | private static void writeAdditionalInputMethodSubtypes( |
| 3936 | HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile, |
| 3937 | HashMap<String, InputMethodInfo> methodMap) { |
| 3938 | // Safety net for the case that this function is called before methodMap is set. |
| 3939 | final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0; |
| 3940 | FileOutputStream fos = null; |
| 3941 | try { |
| 3942 | fos = subtypesFile.startWrite(); |
| 3943 | final XmlSerializer out = new FastXmlSerializer(); |
| 3944 | out.setOutput(fos, "utf-8"); |
| 3945 | out.startDocument(null, true); |
| 3946 | out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 3947 | out.startTag(null, NODE_SUBTYPES); |
| 3948 | for (String imiId : allSubtypes.keySet()) { |
| 3949 | if (isSetMethodMap && !methodMap.containsKey(imiId)) { |
| 3950 | Slog.w(TAG, "IME uninstalled or not valid.: " + imiId); |
| 3951 | continue; |
| 3952 | } |
| 3953 | out.startTag(null, NODE_IMI); |
| 3954 | out.attribute(null, ATTR_ID, imiId); |
| 3955 | final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId); |
| 3956 | final int N = subtypesList.size(); |
| 3957 | for (int i = 0; i < N; ++i) { |
| 3958 | final InputMethodSubtype subtype = subtypesList.get(i); |
| 3959 | out.startTag(null, NODE_SUBTYPE); |
| 3960 | out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId())); |
| 3961 | out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId())); |
| 3962 | out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale()); |
| 3963 | out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode()); |
| 3964 | out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue()); |
| 3965 | out.attribute(null, ATTR_IS_AUXILIARY, |
| 3966 | String.valueOf(subtype.isAuxiliary() ? 1 : 0)); |
| 3967 | out.endTag(null, NODE_SUBTYPE); |
| 3968 | } |
| 3969 | out.endTag(null, NODE_IMI); |
| 3970 | } |
| 3971 | out.endTag(null, NODE_SUBTYPES); |
| 3972 | out.endDocument(); |
| 3973 | subtypesFile.finishWrite(fos); |
| 3974 | } catch (java.io.IOException e) { |
| 3975 | Slog.w(TAG, "Error writing subtypes", e); |
| 3976 | if (fos != null) { |
| 3977 | subtypesFile.failWrite(fos); |
| 3978 | } |
| 3979 | } |
| 3980 | } |
| 3981 | |
| 3982 | private static void readAdditionalInputMethodSubtypes( |
| 3983 | HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) { |
| 3984 | if (allSubtypes == null || subtypesFile == null) return; |
| 3985 | allSubtypes.clear(); |
| 3986 | FileInputStream fis = null; |
| 3987 | try { |
| 3988 | fis = subtypesFile.openRead(); |
| 3989 | final XmlPullParser parser = Xml.newPullParser(); |
| 3990 | parser.setInput(fis, null); |
| 3991 | int type = parser.getEventType(); |
| 3992 | // Skip parsing until START_TAG |
| 3993 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 3994 | && type != XmlPullParser.END_DOCUMENT) {} |
| 3995 | String firstNodeName = parser.getName(); |
| 3996 | if (!NODE_SUBTYPES.equals(firstNodeName)) { |
| 3997 | throw new XmlPullParserException("Xml doesn't start with subtypes"); |
| 3998 | } |
| 3999 | final int depth =parser.getDepth(); |
| 4000 | String currentImiId = null; |
| 4001 | ArrayList<InputMethodSubtype> tempSubtypesArray = null; |
| 4002 | while (((type = parser.next()) != XmlPullParser.END_TAG |
| 4003 | || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { |
| 4004 | if (type != XmlPullParser.START_TAG) |
| 4005 | continue; |
| 4006 | final String nodeName = parser.getName(); |
| 4007 | if (NODE_IMI.equals(nodeName)) { |
| 4008 | currentImiId = parser.getAttributeValue(null, ATTR_ID); |
| 4009 | if (TextUtils.isEmpty(currentImiId)) { |
| 4010 | Slog.w(TAG, "Invalid imi id found in subtypes.xml"); |
| 4011 | continue; |
| 4012 | } |
| 4013 | tempSubtypesArray = new ArrayList<InputMethodSubtype>(); |
| 4014 | allSubtypes.put(currentImiId, tempSubtypesArray); |
| 4015 | } else if (NODE_SUBTYPE.equals(nodeName)) { |
| 4016 | if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) { |
| 4017 | Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId); |
| 4018 | continue; |
| 4019 | } |
| 4020 | final int icon = Integer.valueOf( |
| 4021 | parser.getAttributeValue(null, ATTR_ICON)); |
| 4022 | final int label = Integer.valueOf( |
| 4023 | parser.getAttributeValue(null, ATTR_LABEL)); |
| 4024 | final String imeSubtypeLocale = |
| 4025 | parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE); |
| 4026 | final String imeSubtypeMode = |
| 4027 | parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE); |
| 4028 | final String imeSubtypeExtraValue = |
| 4029 | parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE); |
| satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame] | 4030 | final boolean isAuxiliary = "1".equals(String.valueOf( |
| 4031 | parser.getAttributeValue(null, ATTR_IS_AUXILIARY))); |
| satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 4032 | final InputMethodSubtype subtype = |
| 4033 | new InputMethodSubtype(label, icon, imeSubtypeLocale, |
| 4034 | imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary); |
| 4035 | tempSubtypesArray.add(subtype); |
| 4036 | } |
| 4037 | } |
| 4038 | } catch (XmlPullParserException e) { |
| 4039 | Slog.w(TAG, "Error reading subtypes: " + e); |
| 4040 | return; |
| 4041 | } catch (java.io.IOException e) { |
| 4042 | Slog.w(TAG, "Error reading subtypes: " + e); |
| 4043 | return; |
| 4044 | } catch (NumberFormatException e) { |
| 4045 | Slog.w(TAG, "Error reading subtypes: " + e); |
| 4046 | return; |
| 4047 | } finally { |
| 4048 | if (fis != null) { |
| 4049 | try { |
| 4050 | fis.close(); |
| 4051 | } catch (java.io.IOException e1) { |
| 4052 | Slog.w(TAG, "Failed to close."); |
| 4053 | } |
| 4054 | } |
| 4055 | } |
| 4056 | } |
| 4057 | } |
| 4058 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4059 | // ---------------------------------------------------------------------- |
| Satoshi Kataoka | 4e1ab15 | 2012-09-13 21:34:20 +0900 | [diff] [blame] | 4060 | // Utilities for debug |
| 4061 | private static String getStackTrace() { |
| 4062 | final StringBuilder sb = new StringBuilder(); |
| 4063 | try { |
| 4064 | throw new RuntimeException(); |
| 4065 | } catch (RuntimeException e) { |
| 4066 | final StackTraceElement[] frames = e.getStackTrace(); |
| 4067 | // Start at 1 because the first frame is here and we don't care about it |
| 4068 | for (int j = 1; j < frames.length; ++j) { |
| 4069 | sb.append(frames[j].toString() + "\n"); |
| 4070 | } |
| 4071 | } |
| 4072 | return sb.toString(); |
| 4073 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4074 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4075 | @Override |
| 4076 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 4077 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 4078 | != PackageManager.PERMISSION_GRANTED) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4079 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4080 | pw.println("Permission Denial: can't dump InputMethodManager from from pid=" |
| 4081 | + Binder.getCallingPid() |
| 4082 | + ", uid=" + Binder.getCallingUid()); |
| 4083 | return; |
| 4084 | } |
| 4085 | |
| 4086 | IInputMethod method; |
| 4087 | ClientState client; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4088 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4089 | final Printer p = new PrintWriterPrinter(pw); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4090 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4091 | synchronized (mMethodMap) { |
| 4092 | p.println("Current Input Method Manager state:"); |
| 4093 | int N = mMethodList.size(); |
| 4094 | p.println(" Input Methods:"); |
| 4095 | for (int i=0; i<N; i++) { |
| 4096 | InputMethodInfo info = mMethodList.get(i); |
| 4097 | p.println(" InputMethod #" + i + ":"); |
| 4098 | info.dump(p, " "); |
| 4099 | } |
| 4100 | p.println(" Clients:"); |
| 4101 | for (ClientState ci : mClients.values()) { |
| 4102 | p.println(" Client " + ci + ":"); |
| 4103 | p.println(" client=" + ci.client); |
| 4104 | p.println(" inputContext=" + ci.inputContext); |
| 4105 | p.println(" sessionRequested=" + ci.sessionRequested); |
| 4106 | p.println(" curSession=" + ci.curSession); |
| 4107 | } |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4108 | p.println(" mCurMethodId=" + mCurMethodId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4109 | client = mCurClient; |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4110 | p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq); |
| 4111 | p.println(" mCurFocusedWindow=" + mCurFocusedWindow); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4112 | p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection |
| 4113 | + " mBoundToMethod=" + mBoundToMethod); |
| 4114 | p.println(" mCurToken=" + mCurToken); |
| 4115 | p.println(" mCurIntent=" + mCurIntent); |
| 4116 | method = mCurMethod; |
| 4117 | p.println(" mCurMethod=" + mCurMethod); |
| 4118 | p.println(" mEnabledSession=" + mEnabledSession); |
| 4119 | p.println(" mShowRequested=" + mShowRequested |
| 4120 | + " mShowExplicitlyRequested=" + mShowExplicitlyRequested |
| 4121 | + " mShowForced=" + mShowForced |
| 4122 | + " mInputShown=" + mInputShown); |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 4123 | p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4124 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4125 | |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4126 | p.println(" "); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4127 | if (client != null) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4128 | pw.flush(); |
| 4129 | try { |
| 4130 | client.client.asBinder().dump(fd, args); |
| 4131 | } catch (RemoteException e) { |
| 4132 | p.println("Input method client dead: " + e); |
| 4133 | } |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4134 | } else { |
| 4135 | p.println("No input method client."); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4136 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4137 | |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4138 | p.println(" "); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4139 | if (method != null) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4140 | pw.flush(); |
| 4141 | try { |
| 4142 | method.asBinder().dump(fd, args); |
| 4143 | } catch (RemoteException e) { |
| 4144 | p.println("Input method service dead: " + e); |
| 4145 | } |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4146 | } else { |
| 4147 | p.println("No input method service."); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4148 | } |
| 4149 | } |
| 4150 | } |