The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006-2008 The Android Open Source Project |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * the License at |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 9 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 19 | import com.android.internal.content.PackageMonitor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import com.android.internal.os.HandlerCaller; |
| 21 | import com.android.internal.view.IInputContext; |
| 22 | import com.android.internal.view.IInputMethod; |
| 23 | import com.android.internal.view.IInputMethodCallback; |
| 24 | import com.android.internal.view.IInputMethodClient; |
| 25 | import com.android.internal.view.IInputMethodManager; |
| 26 | import com.android.internal.view.IInputMethodSession; |
| 27 | import com.android.internal.view.InputBindResult; |
| 28 | |
Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 29 | import com.android.server.EventLogTags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
| 31 | import org.xmlpull.v1.XmlPullParserException; |
| 32 | |
| 33 | import android.app.ActivityManagerNative; |
| 34 | import android.app.AlertDialog; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 35 | import android.app.PendingIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.content.ComponentName; |
| 37 | import android.content.ContentResolver; |
| 38 | import android.content.Context; |
| 39 | import android.content.DialogInterface; |
| 40 | import android.content.IntentFilter; |
| 41 | import android.content.DialogInterface.OnCancelListener; |
| 42 | import android.content.Intent; |
| 43 | import android.content.ServiceConnection; |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 44 | import android.content.pm.ApplicationInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.content.pm.PackageManager; |
| 46 | import android.content.pm.ResolveInfo; |
| 47 | import android.content.pm.ServiceInfo; |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 48 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.content.res.Resources; |
| 50 | import android.content.res.TypedArray; |
| 51 | import android.database.ContentObserver; |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 52 | import android.inputmethodservice.InputMethodService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.os.Binder; |
| 54 | import android.os.Handler; |
| 55 | import android.os.IBinder; |
| 56 | import android.os.IInterface; |
| 57 | import android.os.Message; |
| 58 | import android.os.Parcel; |
| 59 | import android.os.RemoteException; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 60 | import android.os.ResultReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | import android.os.ServiceManager; |
| 62 | import android.os.SystemClock; |
| 63 | import android.provider.Settings; |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 64 | import android.provider.Settings.Secure; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 65 | import android.provider.Settings.SettingNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import android.text.TextUtils; |
| 67 | import android.util.EventLog; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 68 | import android.util.Pair; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 69 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | import android.util.PrintWriterPrinter; |
| 71 | import android.util.Printer; |
| 72 | import android.view.IWindowManager; |
| 73 | import android.view.WindowManager; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 74 | import android.view.inputmethod.EditorInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | import android.view.inputmethod.InputBinding; |
| 76 | import android.view.inputmethod.InputMethod; |
| 77 | import android.view.inputmethod.InputMethodInfo; |
| 78 | import android.view.inputmethod.InputMethodManager; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 79 | import android.view.inputmethod.InputMethodSubtype; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | |
| 81 | import java.io.FileDescriptor; |
| 82 | import java.io.IOException; |
| 83 | import java.io.PrintWriter; |
| 84 | import java.util.ArrayList; |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 85 | import java.util.Comparator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | import java.util.HashMap; |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 87 | import java.util.HashSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | import java.util.List; |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 89 | import java.util.Map; |
| 90 | import java.util.TreeMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * This class provides a system service that manages input methods. |
| 94 | */ |
| 95 | public class InputMethodManagerService extends IInputMethodManager.Stub |
| 96 | implements ServiceConnection, Handler.Callback { |
| 97 | static final boolean DEBUG = false; |
| 98 | static final String TAG = "InputManagerService"; |
| 99 | |
| 100 | static final int MSG_SHOW_IM_PICKER = 1; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 101 | static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2; |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 102 | static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3; |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 103 | static final int MSG_SHOW_IM_CONFIG = 4; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | static final int MSG_UNBIND_INPUT = 1000; |
| 106 | static final int MSG_BIND_INPUT = 1010; |
| 107 | static final int MSG_SHOW_SOFT_INPUT = 1020; |
| 108 | static final int MSG_HIDE_SOFT_INPUT = 1030; |
| 109 | static final int MSG_ATTACH_TOKEN = 1040; |
| 110 | static final int MSG_CREATE_SESSION = 1050; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 111 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | static final int MSG_START_INPUT = 2000; |
| 113 | static final int MSG_RESTART_INPUT = 2010; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 114 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | static final int MSG_UNBIND_METHOD = 3000; |
| 116 | static final int MSG_BIND_METHOD = 3010; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 117 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | static final long TIME_TO_RECONNECT = 10*1000; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 119 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 120 | private static final int NOT_A_SUBTYPE_ID = -1; |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 121 | 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] | 122 | private static final String SUBTYPE_MODE_KEYBOARD = "keyboard"; |
| 123 | private static final String SUBTYPE_MODE_VOICE = "voice"; |
| 124 | |
satok | 57ffc00 | 2011-01-25 00:11:47 +0900 | [diff] [blame] | 125 | // TODO: Will formalize this value as API |
| 126 | private static final String SUBTYPE_EXTRAVALUE_EXCLUDE_FROM_LAST_IME = |
| 127 | "excludeFromLastInputMethod"; |
| 128 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | final Context mContext; |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 130 | final Resources mRes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | final Handler mHandler; |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 132 | final InputMethodSettings mSettings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | final SettingsObserver mSettingsObserver; |
Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 134 | final StatusBarManagerService mStatusBar; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | final IWindowManager mIWindowManager; |
| 136 | final HandlerCaller mCaller; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 137 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | final InputBindResult mNoBinding = new InputBindResult(null, null, -1); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 139 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | // All known input methods. mMethodMap also serves as the global |
| 141 | // lock for this class. |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 142 | final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>(); |
| 143 | final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 144 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | class SessionState { |
| 146 | final ClientState client; |
| 147 | final IInputMethod method; |
| 148 | final IInputMethodSession session; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 149 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | @Override |
| 151 | public String toString() { |
| 152 | return "SessionState{uid " + client.uid + " pid " + client.pid |
| 153 | + " method " + Integer.toHexString( |
| 154 | System.identityHashCode(method)) |
| 155 | + " session " + Integer.toHexString( |
| 156 | System.identityHashCode(session)) |
| 157 | + "}"; |
| 158 | } |
| 159 | |
| 160 | SessionState(ClientState _client, IInputMethod _method, |
| 161 | IInputMethodSession _session) { |
| 162 | client = _client; |
| 163 | method = _method; |
| 164 | session = _session; |
| 165 | } |
| 166 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 167 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | class ClientState { |
| 169 | final IInputMethodClient client; |
| 170 | final IInputContext inputContext; |
| 171 | final int uid; |
| 172 | final int pid; |
| 173 | final InputBinding binding; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 174 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | boolean sessionRequested; |
| 176 | SessionState curSession; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 177 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | @Override |
| 179 | public String toString() { |
| 180 | return "ClientState{" + Integer.toHexString( |
| 181 | System.identityHashCode(this)) + " uid " + uid |
| 182 | + " pid " + pid + "}"; |
| 183 | } |
| 184 | |
| 185 | ClientState(IInputMethodClient _client, IInputContext _inputContext, |
| 186 | int _uid, int _pid) { |
| 187 | client = _client; |
| 188 | inputContext = _inputContext; |
| 189 | uid = _uid; |
| 190 | pid = _pid; |
| 191 | binding = new InputBinding(null, inputContext.asBinder(), uid, pid); |
| 192 | } |
| 193 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 194 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | final HashMap<IBinder, ClientState> mClients |
| 196 | = new HashMap<IBinder, ClientState>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 197 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | /** |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 199 | * Set once the system is ready to run third party code. |
| 200 | */ |
| 201 | boolean mSystemReady; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 202 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 203 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | * Id of the currently selected input method. |
| 205 | */ |
| 206 | String mCurMethodId; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 207 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | /** |
| 209 | * The current binding sequence number, incremented every time there is |
| 210 | * a new bind performed. |
| 211 | */ |
| 212 | int mCurSeq; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | /** |
| 215 | * The client that is currently bound to an input method. |
| 216 | */ |
| 217 | ClientState mCurClient; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 218 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | /** |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 220 | * The last window token that gained focus. |
| 221 | */ |
| 222 | IBinder mCurFocusedWindow; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 223 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 224 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | * The input context last provided by the current client. |
| 226 | */ |
| 227 | IInputContext mCurInputContext; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 228 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | /** |
| 230 | * The attributes last provided by the current client. |
| 231 | */ |
| 232 | EditorInfo mCurAttribute; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 233 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | /** |
| 235 | * The input method ID of the input method service that we are currently |
| 236 | * connected to or in the process of connecting to. |
| 237 | */ |
| 238 | String mCurId; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 239 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | /** |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 241 | * The current subtype of the current input method. |
| 242 | */ |
| 243 | private InputMethodSubtype mCurrentSubtype; |
| 244 | |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 245 | // This list contains the pairs of InputMethodInfo and InputMethodSubtype. |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 246 | private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>> |
| 247 | mShortcutInputMethodsAndSubtypes = |
| 248 | new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>(); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 249 | |
| 250 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | * Set to true if our ServiceConnection is currently actively bound to |
| 252 | * a service (whether or not we have gotten its IBinder back yet). |
| 253 | */ |
| 254 | boolean mHaveConnection; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | /** |
| 257 | * Set if the client has asked for the input method to be shown. |
| 258 | */ |
| 259 | boolean mShowRequested; |
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 | /** |
| 262 | * Set if we were explicitly told to show the input method. |
| 263 | */ |
| 264 | boolean mShowExplicitlyRequested; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 265 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | /** |
| 267 | * Set if we were forced to be shown. |
| 268 | */ |
| 269 | boolean mShowForced; |
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 | * Set if we last told the input method to show itself. |
| 273 | */ |
| 274 | boolean mInputShown; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 275 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | /** |
| 277 | * The Intent used to connect to the current input method. |
| 278 | */ |
| 279 | Intent mCurIntent; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 280 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | /** |
| 282 | * The token we have made for the currently active input method, to |
| 283 | * identify it in the future. |
| 284 | */ |
| 285 | IBinder mCurToken; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 286 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | /** |
| 288 | * If non-null, this is the input method service we are currently connected |
| 289 | * to. |
| 290 | */ |
| 291 | IInputMethod mCurMethod; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 292 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | /** |
| 294 | * Time that we last initiated a bind to the input method, to determine |
| 295 | * if we should try to disconnect and reconnect to it. |
| 296 | */ |
| 297 | long mLastBindTime; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 298 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | /** |
| 300 | * Have we called mCurMethod.bindInput()? |
| 301 | */ |
| 302 | boolean mBoundToMethod; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 303 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | /** |
| 305 | * Currently enabled session. Only touched by service thread, not |
| 306 | * protected by a lock. |
| 307 | */ |
| 308 | SessionState mEnabledSession; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 309 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | /** |
| 311 | * True if the screen is on. The value is true initially. |
| 312 | */ |
| 313 | boolean mScreenOn = true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 314 | |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 315 | int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT; |
| 316 | int mImeWindowVis; |
| 317 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | AlertDialog.Builder mDialogBuilder; |
| 319 | AlertDialog mSwitchingDialog; |
| 320 | InputMethodInfo[] mIms; |
| 321 | CharSequence[] mItems; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 322 | int[] mSubtypeIds; |
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 | class SettingsObserver extends ContentObserver { |
| 325 | SettingsObserver(Handler handler) { |
| 326 | super(handler); |
| 327 | ContentResolver resolver = mContext.getContentResolver(); |
| 328 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 329 | Settings.Secure.DEFAULT_INPUT_METHOD), false, this); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 330 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
satok | b6109bb | 2011-02-03 22:24:54 +0900 | [diff] [blame] | 331 | Settings.Secure.ENABLED_INPUT_METHODS), false, this); |
| 332 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 333 | Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 335 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | @Override public void onChange(boolean selfChange) { |
| 337 | synchronized (mMethodMap) { |
| 338 | updateFromSettingsLocked(); |
| 339 | } |
| 340 | } |
| 341 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 342 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | class ScreenOnOffReceiver extends android.content.BroadcastReceiver { |
| 344 | @Override |
| 345 | public void onReceive(Context context, Intent intent) { |
| 346 | if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { |
| 347 | mScreenOn = true; |
| 348 | } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { |
| 349 | mScreenOn = false; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 350 | } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { |
| 351 | hideInputMethodMenu(); |
| 352 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 354 | Slog.w(TAG, "Unexpected intent " + intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | // Inform the current client of the change in active status |
| 358 | try { |
| 359 | if (mCurClient != null && mCurClient.client != null) { |
| 360 | mCurClient.client.setActive(mScreenOn); |
| 361 | } |
| 362 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 363 | Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | + mCurClient.pid + " uid " + mCurClient.uid); |
| 365 | } |
| 366 | } |
| 367 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 368 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 369 | class MyPackageMonitor extends PackageMonitor { |
| 370 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | @Override |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 372 | public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | synchronized (mMethodMap) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 374 | String curInputMethodId = Settings.Secure.getString(mContext |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
| 376 | final int N = mMethodList.size(); |
| 377 | if (curInputMethodId != null) { |
| 378 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 379 | InputMethodInfo imi = mMethodList.get(i); |
| 380 | if (imi.getId().equals(curInputMethodId)) { |
| 381 | for (String pkg : packages) { |
| 382 | if (imi.getPackageName().equals(pkg)) { |
| 383 | if (!doit) { |
| 384 | return true; |
| 385 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 386 | resetSelectedInputMethodAndSubtypeLocked(""); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 387 | chooseNewDefaultIMELocked(); |
| 388 | return true; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | @Override |
| 399 | public void onSomePackagesChanged() { |
| 400 | synchronized (mMethodMap) { |
| 401 | InputMethodInfo curIm = null; |
| 402 | String curInputMethodId = Settings.Secure.getString(mContext |
| 403 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
| 404 | final int N = mMethodList.size(); |
| 405 | if (curInputMethodId != null) { |
| 406 | for (int i=0; i<N; i++) { |
| 407 | InputMethodInfo imi = mMethodList.get(i); |
| 408 | if (imi.getId().equals(curInputMethodId)) { |
| 409 | curIm = imi; |
| 410 | } |
| 411 | int change = isPackageDisappearing(imi.getPackageName()); |
| 412 | if (change == PACKAGE_TEMPORARY_CHANGE |
| 413 | || change == PACKAGE_PERMANENT_CHANGE) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 414 | Slog.i(TAG, "Input method uninstalled, disabling: " |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 415 | + imi.getComponent()); |
| 416 | setInputMethodEnabledLocked(imi.getId(), false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 420 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 421 | buildInputMethodListLocked(mMethodList, mMethodMap); |
| 422 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | boolean changed = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 424 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 425 | if (curIm != null) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 426 | int change = isPackageDisappearing(curIm.getPackageName()); |
| 427 | if (change == PACKAGE_TEMPORARY_CHANGE |
| 428 | || change == PACKAGE_PERMANENT_CHANGE) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 429 | ServiceInfo si = null; |
| 430 | try { |
| 431 | si = mContext.getPackageManager().getServiceInfo( |
| 432 | curIm.getComponent(), 0); |
| 433 | } catch (PackageManager.NameNotFoundException ex) { |
| 434 | } |
| 435 | if (si == null) { |
| 436 | // Uh oh, current input method is no longer around! |
| 437 | // Pick another one... |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 438 | Slog.i(TAG, "Current input method removed: " + curInputMethodId); |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 439 | mImeWindowVis = 0; |
| 440 | mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, |
| 441 | mBackDisposition); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 442 | if (!chooseNewDefaultIMELocked()) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 443 | changed = true; |
| 444 | curIm = null; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 445 | Slog.i(TAG, "Unsetting current input method"); |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 446 | resetSelectedInputMethodAndSubtypeLocked(""); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 447 | } |
| 448 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 449 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 450 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 451 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 452 | if (curIm == null) { |
| 453 | // We currently don't have a default input method... is |
| 454 | // one now available? |
| 455 | changed = chooseNewDefaultIMELocked(); |
| 456 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 457 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 458 | if (changed) { |
| 459 | updateFromSettingsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | } |
| 463 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 464 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | class MethodCallback extends IInputMethodCallback.Stub { |
| 466 | final IInputMethod mMethod; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 467 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | MethodCallback(IInputMethod method) { |
| 469 | mMethod = method; |
| 470 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 471 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | public void finishedEvent(int seq, boolean handled) throws RemoteException { |
| 473 | } |
| 474 | |
| 475 | public void sessionCreated(IInputMethodSession session) throws RemoteException { |
| 476 | onSessionCreated(mMethod, session); |
| 477 | } |
| 478 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 479 | |
Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 480 | public InputMethodManagerService(Context context, StatusBarManagerService statusBar) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | mContext = context; |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 482 | mRes = context.getResources(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | mHandler = new Handler(this); |
| 484 | mIWindowManager = IWindowManager.Stub.asInterface( |
| 485 | ServiceManager.getService(Context.WINDOW_SERVICE)); |
| 486 | mCaller = new HandlerCaller(context, new HandlerCaller.Callback() { |
| 487 | public void executeMessage(Message msg) { |
| 488 | handleMessage(msg); |
| 489 | } |
| 490 | }); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 491 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 492 | (new MyPackageMonitor()).register(mContext, true); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 493 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | IntentFilter screenOnOffFilt = new IntentFilter(); |
| 495 | screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON); |
| 496 | screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 497 | screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 499 | |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 500 | mStatusBar = statusBar; |
| 501 | statusBar.setIconVisibility("ime", false); |
| 502 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 503 | // mSettings should be created before buildInputMethodListLocked |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 504 | mSettings = new InputMethodSettings( |
| 505 | mRes, context.getContentResolver(), mMethodMap, mMethodList); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | buildInputMethodListLocked(mMethodList, mMethodMap); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 507 | mSettings.enableAllIMEsIfThereIsNoEnabledIME(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 509 | if (TextUtils.isEmpty(Settings.Secure.getString( |
| 510 | mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | InputMethodInfo defIm = null; |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 512 | for (InputMethodInfo imi: mMethodList) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | if (defIm == null && imi.getIsDefaultResourceId() != 0) { |
| 514 | try { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 515 | Resources res = context.createPackageContext( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | imi.getPackageName(), 0).getResources(); |
| 517 | if (res.getBoolean(imi.getIsDefaultResourceId())) { |
| 518 | defIm = imi; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 519 | Slog.i(TAG, "Selected default: " + imi.getId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | } |
| 521 | } catch (PackageManager.NameNotFoundException ex) { |
| 522 | } catch (Resources.NotFoundException ex) { |
| 523 | } |
| 524 | } |
| 525 | } |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 526 | if (defIm == null && mMethodList.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | defIm = mMethodList.get(0); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 528 | Slog.i(TAG, "No default found, using " + defIm.getId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 530 | if (defIm != null) { |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 531 | setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | } |
| 533 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 534 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | mSettingsObserver = new SettingsObserver(mHandler); |
| 536 | updateFromSettingsLocked(); |
| 537 | } |
| 538 | |
| 539 | @Override |
| 540 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 541 | throws RemoteException { |
| 542 | try { |
| 543 | return super.onTransact(code, data, reply, flags); |
| 544 | } catch (RuntimeException e) { |
| 545 | // The input method manager only throws security exceptions, so let's |
| 546 | // log all others. |
| 547 | if (!(e instanceof SecurityException)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 548 | Slog.e(TAG, "Input Method Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | } |
| 550 | throw e; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | public void systemReady() { |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 555 | synchronized (mMethodMap) { |
| 556 | if (!mSystemReady) { |
| 557 | mSystemReady = true; |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 558 | try { |
| 559 | startInputInnerLocked(); |
| 560 | } catch (RuntimeException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 561 | Slog.w(TAG, "Unexpected exception", e); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 562 | } |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 563 | } |
| 564 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 566 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | public List<InputMethodInfo> getInputMethodList() { |
| 568 | synchronized (mMethodMap) { |
| 569 | return new ArrayList<InputMethodInfo>(mMethodList); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | public List<InputMethodInfo> getEnabledInputMethodList() { |
| 574 | synchronized (mMethodMap) { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 575 | return mSettings.getEnabledInputMethodListLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 579 | private HashMap<InputMethodInfo, List<InputMethodSubtype>> |
| 580 | getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() { |
| 581 | HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes = |
| 582 | new HashMap<InputMethodInfo, List<InputMethodSubtype>>(); |
| 583 | for (InputMethodInfo imi: getEnabledInputMethodList()) { |
| 584 | enabledInputMethodAndSubtypes.put( |
| 585 | imi, getEnabledInputMethodSubtypeListLocked(imi, true)); |
| 586 | } |
| 587 | return enabledInputMethodAndSubtypes; |
| 588 | } |
| 589 | |
| 590 | public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi, |
| 591 | boolean allowsImplicitlySelectedSubtypes) { |
| 592 | if (imi == null && mCurMethodId != null) { |
| 593 | imi = mMethodMap.get(mCurMethodId); |
| 594 | } |
satok | 7265d9b | 2011-02-14 15:47:30 +0900 | [diff] [blame] | 595 | List<InputMethodSubtype> enabledSubtypes = |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 596 | mSettings.getEnabledInputMethodSubtypeListLocked(imi); |
satok | 7265d9b | 2011-02-14 15:47:30 +0900 | [diff] [blame] | 597 | if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) { |
| 598 | enabledSubtypes = getApplicableSubtypesLocked(mRes, getSubtypes(imi)); |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 599 | } |
satok | 7265d9b | 2011-02-14 15:47:30 +0900 | [diff] [blame] | 600 | return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes); |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 601 | } |
| 602 | |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 603 | public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi, |
| 604 | boolean allowsImplicitlySelectedSubtypes) { |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 605 | synchronized (mMethodMap) { |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 606 | return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes); |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | public void addClient(IInputMethodClient client, |
| 611 | IInputContext inputContext, int uid, int pid) { |
| 612 | synchronized (mMethodMap) { |
| 613 | mClients.put(client.asBinder(), new ClientState(client, |
| 614 | inputContext, uid, pid)); |
| 615 | } |
| 616 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 617 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | public void removeClient(IInputMethodClient client) { |
| 619 | synchronized (mMethodMap) { |
| 620 | mClients.remove(client.asBinder()); |
| 621 | } |
| 622 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 623 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | void executeOrSendMessage(IInterface target, Message msg) { |
| 625 | if (target.asBinder() instanceof Binder) { |
| 626 | mCaller.sendMessage(msg); |
| 627 | } else { |
| 628 | handleMessage(msg); |
| 629 | msg.recycle(); |
| 630 | } |
| 631 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 632 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 633 | void unbindCurrentClientLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | if (mCurClient != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 635 | if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 636 | + mCurClient.client.asBinder()); |
| 637 | if (mBoundToMethod) { |
| 638 | mBoundToMethod = false; |
| 639 | if (mCurMethod != null) { |
| 640 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageO( |
| 641 | MSG_UNBIND_INPUT, mCurMethod)); |
| 642 | } |
| 643 | } |
| 644 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 645 | MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); |
| 646 | mCurClient.sessionRequested = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 647 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | // Call setActive(false) on the old client |
| 649 | try { |
| 650 | mCurClient.client.setActive(false); |
| 651 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 652 | Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 653 | + mCurClient.pid + " uid " + mCurClient.uid); |
| 654 | } |
| 655 | mCurClient = null; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 656 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 657 | hideInputMethodMenuLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 658 | } |
| 659 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 660 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 661 | private int getImeShowFlags() { |
| 662 | int flags = 0; |
| 663 | if (mShowForced) { |
| 664 | flags |= InputMethod.SHOW_FORCED |
| 665 | | InputMethod.SHOW_EXPLICIT; |
| 666 | } else if (mShowExplicitlyRequested) { |
| 667 | flags |= InputMethod.SHOW_EXPLICIT; |
| 668 | } |
| 669 | return flags; |
| 670 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 671 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | private int getAppShowFlags() { |
| 673 | int flags = 0; |
| 674 | if (mShowForced) { |
| 675 | flags |= InputMethodManager.SHOW_FORCED; |
| 676 | } else if (!mShowExplicitlyRequested) { |
| 677 | flags |= InputMethodManager.SHOW_IMPLICIT; |
| 678 | } |
| 679 | return flags; |
| 680 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 681 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 682 | InputBindResult attachNewInputLocked(boolean initial, boolean needResult) { |
| 683 | if (!mBoundToMethod) { |
| 684 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 685 | MSG_BIND_INPUT, mCurMethod, mCurClient.binding)); |
| 686 | mBoundToMethod = true; |
| 687 | } |
| 688 | final SessionState session = mCurClient.curSession; |
| 689 | if (initial) { |
| 690 | executeOrSendMessage(session.method, mCaller.obtainMessageOOO( |
| 691 | MSG_START_INPUT, session, mCurInputContext, mCurAttribute)); |
| 692 | } else { |
| 693 | executeOrSendMessage(session.method, mCaller.obtainMessageOOO( |
| 694 | MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute)); |
| 695 | } |
| 696 | if (mShowRequested) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 697 | 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] | 698 | showCurrentInputLocked(getAppShowFlags(), null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 699 | } |
| 700 | return needResult |
| 701 | ? new InputBindResult(session.session, mCurId, mCurSeq) |
| 702 | : null; |
| 703 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 704 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 705 | InputBindResult startInputLocked(IInputMethodClient client, |
| 706 | IInputContext inputContext, EditorInfo attribute, |
| 707 | boolean initial, boolean needResult) { |
| 708 | // If no method is currently selected, do nothing. |
| 709 | if (mCurMethodId == null) { |
| 710 | return mNoBinding; |
| 711 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 712 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 713 | ClientState cs = mClients.get(client.asBinder()); |
| 714 | if (cs == null) { |
| 715 | throw new IllegalArgumentException("unknown client " |
| 716 | + client.asBinder()); |
| 717 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 718 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | try { |
| 720 | if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) { |
| 721 | // Check with the window manager to make sure this client actually |
| 722 | // has a window with focus. If not, reject. This is thread safe |
| 723 | // because if the focus changes some time before or after, the |
| 724 | // next client receiving focus that has any interest in input will |
| 725 | // be calling through here after that change happens. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 726 | 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] | 727 | + " (uid=" + cs.uid + " pid=" + cs.pid + ")"); |
| 728 | return null; |
| 729 | } |
| 730 | } catch (RemoteException e) { |
| 731 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 732 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 733 | if (mCurClient != cs) { |
| 734 | // If the client is changing, we need to switch over to the new |
| 735 | // one. |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 736 | unbindCurrentClientLocked(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 737 | if (DEBUG) Slog.v(TAG, "switching to client: client = " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | + cs.client.asBinder()); |
| 739 | |
| 740 | // If the screen is on, inform the new client it is active |
| 741 | if (mScreenOn) { |
| 742 | try { |
| 743 | cs.client.setActive(mScreenOn); |
| 744 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 745 | Slog.w(TAG, "Got RemoteException sending setActive notification to pid " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | + cs.pid + " uid " + cs.uid); |
| 747 | } |
| 748 | } |
| 749 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 750 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | // Bump up the sequence for this client and attach it. |
| 752 | mCurSeq++; |
| 753 | if (mCurSeq <= 0) mCurSeq = 1; |
| 754 | mCurClient = cs; |
| 755 | mCurInputContext = inputContext; |
| 756 | mCurAttribute = attribute; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 757 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | // Check if the input method is changing. |
| 759 | if (mCurId != null && mCurId.equals(mCurMethodId)) { |
| 760 | if (cs.curSession != null) { |
| 761 | // Fast case: if we are already connected to the input method, |
| 762 | // then just return it. |
| 763 | return attachNewInputLocked(initial, needResult); |
| 764 | } |
| 765 | if (mHaveConnection) { |
| 766 | if (mCurMethod != null) { |
| 767 | if (!cs.sessionRequested) { |
| 768 | cs.sessionRequested = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 769 | 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] | 770 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 771 | MSG_CREATE_SESSION, mCurMethod, |
| 772 | new MethodCallback(mCurMethod))); |
| 773 | } |
| 774 | // Return to client, and we will get back with it when |
| 775 | // we have had a session made for it. |
| 776 | return new InputBindResult(null, mCurId, mCurSeq); |
| 777 | } else if (SystemClock.uptimeMillis() |
| 778 | < (mLastBindTime+TIME_TO_RECONNECT)) { |
| 779 | // In this case we have connected to the service, but |
| 780 | // don't yet have its interface. If it hasn't been too |
| 781 | // long since we did the connection, we'll return to |
| 782 | // the client and wait to get the service interface so |
| 783 | // we can report back. If it has been too long, we want |
| 784 | // to fall through so we can try a disconnect/reconnect |
| 785 | // to see if we can get back in touch with the service. |
| 786 | return new InputBindResult(null, mCurId, mCurSeq); |
| 787 | } else { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 788 | EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, |
| 789 | mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 793 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 794 | return startInputInnerLocked(); |
| 795 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 796 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 797 | InputBindResult startInputInnerLocked() { |
| 798 | if (mCurMethodId == null) { |
| 799 | return mNoBinding; |
| 800 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 801 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 802 | if (!mSystemReady) { |
| 803 | // If the system is not yet ready, we shouldn't be running third |
| 804 | // party code. |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 805 | return new InputBindResult(null, mCurMethodId, mCurSeq); |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 806 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 807 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 808 | InputMethodInfo info = mMethodMap.get(mCurMethodId); |
| 809 | if (info == null) { |
| 810 | throw new IllegalArgumentException("Unknown id: " + mCurMethodId); |
| 811 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 812 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 813 | unbindCurrentMethodLocked(false); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 814 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE); |
| 816 | mCurIntent.setComponent(info.getComponent()); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 817 | mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL, |
| 818 | com.android.internal.R.string.input_method_binding_label); |
| 819 | mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity( |
| 820 | mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) { |
| 822 | mLastBindTime = SystemClock.uptimeMillis(); |
| 823 | mHaveConnection = true; |
| 824 | mCurId = info.getId(); |
| 825 | mCurToken = new Binder(); |
| 826 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 827 | if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 828 | mIWindowManager.addWindowToken(mCurToken, |
| 829 | WindowManager.LayoutParams.TYPE_INPUT_METHOD); |
| 830 | } catch (RemoteException e) { |
| 831 | } |
| 832 | return new InputBindResult(null, mCurId, mCurSeq); |
| 833 | } else { |
| 834 | mCurIntent = null; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 835 | Slog.w(TAG, "Failure connecting to input method service: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 836 | + mCurIntent); |
| 837 | } |
| 838 | return null; |
| 839 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 840 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 841 | public InputBindResult startInput(IInputMethodClient client, |
| 842 | IInputContext inputContext, EditorInfo attribute, |
| 843 | boolean initial, boolean needResult) { |
| 844 | synchronized (mMethodMap) { |
| 845 | final long ident = Binder.clearCallingIdentity(); |
| 846 | try { |
| 847 | return startInputLocked(client, inputContext, attribute, |
| 848 | initial, needResult); |
| 849 | } finally { |
| 850 | Binder.restoreCallingIdentity(ident); |
| 851 | } |
| 852 | } |
| 853 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 854 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 855 | public void finishInput(IInputMethodClient client) { |
| 856 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 857 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 859 | synchronized (mMethodMap) { |
| 860 | if (mCurIntent != null && name.equals(mCurIntent.getComponent())) { |
| 861 | mCurMethod = IInputMethod.Stub.asInterface(service); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 862 | if (mCurToken == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 863 | Slog.w(TAG, "Service connected without a token!"); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 864 | unbindCurrentMethodLocked(false); |
| 865 | return; |
| 866 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 867 | if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 868 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 869 | MSG_ATTACH_TOKEN, mCurMethod, mCurToken)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 870 | if (mCurClient != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 871 | if (DEBUG) Slog.v(TAG, "Creating first session while with client " |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 872 | + mCurClient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 873 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 874 | MSG_CREATE_SESSION, mCurMethod, |
| 875 | new MethodCallback(mCurMethod))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | void onSessionCreated(IInputMethod method, IInputMethodSession session) { |
| 882 | synchronized (mMethodMap) { |
| 883 | if (mCurMethod != null && method != null |
| 884 | && mCurMethod.asBinder() == method.asBinder()) { |
| 885 | if (mCurClient != null) { |
| 886 | mCurClient.curSession = new SessionState(mCurClient, |
| 887 | method, session); |
| 888 | mCurClient.sessionRequested = false; |
| 889 | InputBindResult res = attachNewInputLocked(true, true); |
| 890 | if (res.method != null) { |
| 891 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO( |
| 892 | MSG_BIND_METHOD, mCurClient.client, res)); |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | } |
| 897 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 898 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 899 | void unbindCurrentMethodLocked(boolean reportToClient) { |
| 900 | if (mHaveConnection) { |
| 901 | mContext.unbindService(this); |
| 902 | mHaveConnection = false; |
| 903 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 904 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 905 | if (mCurToken != null) { |
| 906 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 907 | if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 908 | mIWindowManager.removeWindowToken(mCurToken); |
| 909 | } catch (RemoteException e) { |
| 910 | } |
| 911 | mCurToken = null; |
| 912 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 913 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 914 | mCurId = null; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 915 | clearCurMethodLocked(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 916 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 917 | if (reportToClient && mCurClient != null) { |
| 918 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 919 | MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); |
| 920 | } |
| 921 | } |
| 922 | |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 923 | private void finishSession(SessionState sessionState) { |
| 924 | if (sessionState != null && sessionState.session != null) { |
| 925 | try { |
| 926 | sessionState.session.finishSession(); |
| 927 | } catch (RemoteException e) { |
Jean-Baptiste Queru | 9d0f6df | 2010-03-29 12:55:09 -0700 | [diff] [blame] | 928 | Slog.w(TAG, "Session failed to close due to remote exception", e); |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 932 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 933 | void clearCurMethodLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | if (mCurMethod != null) { |
| 935 | for (ClientState cs : mClients.values()) { |
| 936 | cs.sessionRequested = false; |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 937 | finishSession(cs.curSession); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 938 | cs.curSession = null; |
| 939 | } |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 940 | |
| 941 | finishSession(mEnabledSession); |
| 942 | mEnabledSession = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 943 | mCurMethod = null; |
| 944 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 945 | mStatusBar.setIconVisibility("ime", false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 946 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 947 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 948 | public void onServiceDisconnected(ComponentName name) { |
| 949 | synchronized (mMethodMap) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 950 | if (DEBUG) Slog.v(TAG, "Service disconnected: " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 951 | + " mCurIntent=" + mCurIntent); |
| 952 | if (mCurMethod != null && mCurIntent != null |
| 953 | && name.equals(mCurIntent.getComponent())) { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 954 | clearCurMethodLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 955 | // We consider this to be a new bind attempt, since the system |
| 956 | // should now try to restart the service for us. |
| 957 | mLastBindTime = SystemClock.uptimeMillis(); |
| 958 | mShowRequested = mInputShown; |
| 959 | mInputShown = false; |
| 960 | if (mCurClient != null) { |
| 961 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 962 | MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | public void updateStatusIcon(IBinder token, String packageName, int iconId) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 969 | int uid = Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 970 | long ident = Binder.clearCallingIdentity(); |
| 971 | try { |
| 972 | if (token == null || mCurToken != token) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 973 | 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] | 974 | return; |
| 975 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 976 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 977 | synchronized (mMethodMap) { |
| 978 | if (iconId == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 979 | if (DEBUG) Slog.d(TAG, "hide the small icon for the input method"); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 980 | mStatusBar.setIconVisibility("ime", false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 981 | } else if (packageName != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 982 | if (DEBUG) Slog.d(TAG, "show a small icon for the input method"); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 983 | mStatusBar.setIcon("ime", packageName, iconId, 0); |
| 984 | mStatusBar.setIconVisibility("ime", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | } finally { |
| 988 | Binder.restoreCallingIdentity(ident); |
| 989 | } |
| 990 | } |
| 991 | |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 992 | public void setImeWindowStatus(IBinder token, int vis, int backDisposition) { |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 993 | int uid = Binder.getCallingUid(); |
| 994 | long ident = Binder.clearCallingIdentity(); |
| 995 | try { |
| 996 | if (token == null || mCurToken != token) { |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 997 | Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token); |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 998 | return; |
| 999 | } |
| 1000 | |
| 1001 | synchronized (mMethodMap) { |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 1002 | mImeWindowVis = vis; |
| 1003 | mBackDisposition = backDisposition; |
| 1004 | mStatusBar.setImeWindowStatus(token, vis, backDisposition); |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 1005 | } |
| 1006 | } finally { |
| 1007 | Binder.restoreCallingIdentity(ident); |
| 1008 | } |
| 1009 | } |
| 1010 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1011 | void updateFromSettingsLocked() { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1012 | // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and |
| 1013 | // ENABLED_INPUT_METHODS is taking care of keeping them correctly in |
| 1014 | // sync, so we will never have a DEFAULT_INPUT_METHOD that is not |
| 1015 | // enabled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1016 | String id = Settings.Secure.getString(mContext.getContentResolver(), |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1017 | Settings.Secure.DEFAULT_INPUT_METHOD); |
satok | 03eb319a | 2010-11-11 18:17:42 +0900 | [diff] [blame] | 1018 | // There is no input method selected, try to choose new applicable input method. |
| 1019 | if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) { |
| 1020 | id = Settings.Secure.getString(mContext.getContentResolver(), |
| 1021 | Settings.Secure.DEFAULT_INPUT_METHOD); |
| 1022 | } |
| 1023 | if (!TextUtils.isEmpty(id)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | try { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1025 | setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1026 | } catch (IllegalArgumentException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1027 | 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] | 1028 | mCurMethodId = null; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1029 | unbindCurrentMethodLocked(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1030 | } |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 1031 | mShortcutInputMethodsAndSubtypes.clear(); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1032 | } else { |
| 1033 | // 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] | 1034 | mCurMethodId = null; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1035 | unbindCurrentMethodLocked(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1036 | } |
| 1037 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1038 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1039 | /* package */ void setInputMethodLocked(String id, int subtypeId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | InputMethodInfo info = mMethodMap.get(id); |
| 1041 | if (info == null) { |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 1042 | throw new IllegalArgumentException("Unknown id: " + id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1043 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1044 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | if (id.equals(mCurMethodId)) { |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 1046 | InputMethodSubtype subtype = null; |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 1047 | if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) { |
| 1048 | subtype = info.getSubtypeAt(subtypeId); |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 1049 | } |
| 1050 | if (subtype != mCurrentSubtype) { |
| 1051 | synchronized (mMethodMap) { |
satok | ca83021 | 2011-01-13 21:15:04 +0900 | [diff] [blame] | 1052 | if (subtype != null) { |
| 1053 | setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true); |
| 1054 | } |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 1055 | if (mCurMethod != null) { |
| 1056 | try { |
satok | e40dea0 | 2011-01-30 01:14:02 +0900 | [diff] [blame] | 1057 | final Configuration conf = mRes.getConfiguration(); |
| 1058 | final boolean haveHardKeyboard = conf.keyboard |
| 1059 | != Configuration.KEYBOARD_NOKEYS; |
| 1060 | final boolean hardKeyShown = haveHardKeyboard |
Ken Wakasa | 8710e76 | 2011-01-30 11:02:09 +0900 | [diff] [blame] | 1061 | && conf.hardKeyboardHidden |
| 1062 | != Configuration.HARDKEYBOARDHIDDEN_YES; |
satok | e40dea0 | 2011-01-30 01:14:02 +0900 | [diff] [blame] | 1063 | mImeWindowVis = (mInputShown || hardKeyShown) ? ( |
| 1064 | InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) |
| 1065 | : 0; |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 1066 | mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, |
| 1067 | mBackDisposition); |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 1068 | // If subtype is null, try to find the most applicable one from |
| 1069 | // getCurrentInputMethodSubtype. |
| 1070 | if (subtype == null) { |
| 1071 | subtype = getCurrentInputMethodSubtype(); |
| 1072 | } |
| 1073 | mCurMethod.changeInputMethodSubtype(subtype); |
| 1074 | } catch (RemoteException e) { |
| 1075 | return; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | return; |
| 1081 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1082 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1083 | final long ident = Binder.clearCallingIdentity(); |
| 1084 | try { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1085 | // Set a subtype to this input method. |
| 1086 | // subtypeId the name of a subtype which will be set. |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 1087 | setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false); |
| 1088 | // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked() |
| 1089 | // because mCurMethodId is stored as a history in |
| 1090 | // setSelectedInputMethodAndSubtypeLocked(). |
| 1091 | mCurMethodId = id; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1092 | |
| 1093 | if (ActivityManagerNative.isSystemReady()) { |
| 1094 | Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1095 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1096 | intent.putExtra("input_method_id", id); |
| 1097 | mContext.sendBroadcast(intent); |
| 1098 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1099 | unbindCurrentClientLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1100 | } finally { |
| 1101 | Binder.restoreCallingIdentity(ident); |
| 1102 | } |
| 1103 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1104 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1105 | public boolean showSoftInput(IInputMethodClient client, int flags, |
| 1106 | ResultReceiver resultReceiver) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1107 | int uid = Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1108 | long ident = Binder.clearCallingIdentity(); |
| 1109 | try { |
| 1110 | synchronized (mMethodMap) { |
| 1111 | if (mCurClient == null || client == null |
| 1112 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1113 | try { |
| 1114 | // We need to check if this is the current client with |
| 1115 | // focus in the window manager, to allow this call to |
| 1116 | // be made before input is started in it. |
| 1117 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1118 | Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1119 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1120 | } |
| 1121 | } catch (RemoteException e) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1122 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1123 | } |
| 1124 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1125 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1126 | 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] | 1127 | return showCurrentInputLocked(flags, resultReceiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1128 | } |
| 1129 | } finally { |
| 1130 | Binder.restoreCallingIdentity(ident); |
| 1131 | } |
| 1132 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1133 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1134 | boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1135 | mShowRequested = true; |
| 1136 | if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) { |
| 1137 | mShowExplicitlyRequested = true; |
| 1138 | } |
| 1139 | if ((flags&InputMethodManager.SHOW_FORCED) != 0) { |
| 1140 | mShowExplicitlyRequested = true; |
| 1141 | mShowForced = true; |
| 1142 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1143 | |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 1144 | if (!mSystemReady) { |
| 1145 | return false; |
| 1146 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1147 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1148 | boolean res = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1149 | if (mCurMethod != null) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1150 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO( |
| 1151 | MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod, |
| 1152 | resultReceiver)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1153 | mInputShown = true; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1154 | res = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1155 | } else if (mHaveConnection && SystemClock.uptimeMillis() |
| 1156 | < (mLastBindTime+TIME_TO_RECONNECT)) { |
| 1157 | // The client has asked to have the input method shown, but |
| 1158 | // we have been sitting here too long with a connection to the |
| 1159 | // service and no interface received, so let's disconnect/connect |
| 1160 | // to try to prod things along. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1161 | EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1162 | SystemClock.uptimeMillis()-mLastBindTime,1); |
| 1163 | mContext.unbindService(this); |
| 1164 | mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE); |
| 1165 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1166 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1167 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1168 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1169 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1170 | public boolean hideSoftInput(IInputMethodClient client, int flags, |
| 1171 | ResultReceiver resultReceiver) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1172 | int uid = Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1173 | long ident = Binder.clearCallingIdentity(); |
| 1174 | try { |
| 1175 | synchronized (mMethodMap) { |
| 1176 | if (mCurClient == null || client == null |
| 1177 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1178 | try { |
| 1179 | // We need to check if this is the current client with |
| 1180 | // focus in the window manager, to allow this call to |
| 1181 | // be made before input is started in it. |
| 1182 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1183 | if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid " |
| 1184 | + uid + ": " + client); |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 1185 | mImeWindowVis = 0; |
| 1186 | mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, |
| 1187 | mBackDisposition); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1188 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1189 | } |
| 1190 | } catch (RemoteException e) { |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 1191 | mImeWindowVis = 0; |
| 1192 | mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1193 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1194 | } |
| 1195 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1196 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1197 | 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] | 1198 | return hideCurrentInputLocked(flags, resultReceiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1199 | } |
| 1200 | } finally { |
| 1201 | Binder.restoreCallingIdentity(ident); |
| 1202 | } |
| 1203 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1204 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1205 | boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1206 | if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0 |
| 1207 | && (mShowExplicitlyRequested || mShowForced)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1208 | if (DEBUG) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1209 | "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] | 1210 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1211 | } |
| 1212 | if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1213 | if (DEBUG) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1214 | "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] | 1215 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1216 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1217 | boolean res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1218 | if (mInputShown && mCurMethod != null) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1219 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 1220 | MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver)); |
| 1221 | res = true; |
| 1222 | } else { |
| 1223 | res = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1224 | } |
| 1225 | mInputShown = false; |
| 1226 | mShowRequested = false; |
| 1227 | mShowExplicitlyRequested = false; |
| 1228 | mShowForced = false; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1229 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1230 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1231 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1232 | public void windowGainedFocus(IInputMethodClient client, IBinder windowToken, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1233 | boolean viewHasFocus, boolean isTextEditor, int softInputMode, |
| 1234 | boolean first, int windowFlags) { |
| 1235 | long ident = Binder.clearCallingIdentity(); |
| 1236 | try { |
| 1237 | synchronized (mMethodMap) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1238 | if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1239 | + " viewHasFocus=" + viewHasFocus |
| 1240 | + " isTextEditor=" + isTextEditor |
| 1241 | + " softInputMode=#" + Integer.toHexString(softInputMode) |
| 1242 | + " first=" + first + " flags=#" |
| 1243 | + Integer.toHexString(windowFlags)); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1244 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1245 | if (mCurClient == null || client == null |
| 1246 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1247 | try { |
| 1248 | // We need to check if this is the current client with |
| 1249 | // focus in the window manager, to allow this call to |
| 1250 | // be made before input is started in it. |
| 1251 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1252 | Slog.w(TAG, "Client not active, ignoring focus gain of: " + client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1253 | return; |
| 1254 | } |
| 1255 | } catch (RemoteException e) { |
| 1256 | } |
| 1257 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1258 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1259 | if (mCurFocusedWindow == windowToken) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1260 | Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1261 | return; |
| 1262 | } |
| 1263 | mCurFocusedWindow = windowToken; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1264 | |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1265 | // Should we auto-show the IME even if the caller has not |
| 1266 | // specified what should be done with it? |
| 1267 | // We only do this automatically if the window can resize |
| 1268 | // to accommodate the IME (so what the user sees will give |
| 1269 | // them good context without input information being obscured |
| 1270 | // by the IME) or if running on a large screen where there |
| 1271 | // is more room for the target window + IME. |
| 1272 | final boolean doAutoShow = |
| 1273 | (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
| 1274 | == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
| 1275 | || mRes.getConfiguration().isLayoutSizeAtLeast( |
| 1276 | Configuration.SCREENLAYOUT_SIZE_LARGE); |
| 1277 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1278 | switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) { |
| 1279 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED: |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1280 | if (!isTextEditor || !doAutoShow) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1281 | if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) { |
| 1282 | // There is no focus view, and this window will |
| 1283 | // be behind any soft input window, so hide the |
| 1284 | // soft input window if it is shown. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1285 | 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] | 1286 | hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1287 | } |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1288 | } else if (isTextEditor && doAutoShow && (softInputMode & |
| 1289 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1290 | // There is a focus view, and we are navigating forward |
| 1291 | // into the window, so show the input window for the user. |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1292 | // We only do this automatically if the window an resize |
| 1293 | // to accomodate the IME (so what the user sees will give |
| 1294 | // them good context without input information being obscured |
| 1295 | // by the IME) or if running on a large screen where there |
| 1296 | // is more room for the target window + IME. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1297 | if (DEBUG) Slog.v(TAG, "Unspecified window will show input"); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1298 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1299 | } |
| 1300 | break; |
| 1301 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED: |
| 1302 | // Do nothing. |
| 1303 | break; |
| 1304 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN: |
| 1305 | if ((softInputMode & |
| 1306 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1307 | 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] | 1308 | hideCurrentInputLocked(0, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1309 | } |
| 1310 | break; |
| 1311 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN: |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1312 | 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] | 1313 | hideCurrentInputLocked(0, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1314 | break; |
| 1315 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE: |
| 1316 | if ((softInputMode & |
| 1317 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1318 | if (DEBUG) Slog.v(TAG, "Window asks to show input going forward"); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1319 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1320 | } |
| 1321 | break; |
| 1322 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE: |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1323 | if (DEBUG) Slog.v(TAG, "Window asks to always show input"); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1324 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1325 | break; |
| 1326 | } |
| 1327 | } |
| 1328 | } finally { |
| 1329 | Binder.restoreCallingIdentity(ident); |
| 1330 | } |
| 1331 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1332 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1333 | public void showInputMethodPickerFromClient(IInputMethodClient client) { |
| 1334 | synchronized (mMethodMap) { |
| 1335 | if (mCurClient == null || client == null |
| 1336 | || mCurClient.client.asBinder() != client.asBinder()) { |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1337 | Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid " |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1338 | + Binder.getCallingUid() + ": " + client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1339 | } |
| 1340 | |
satok | 440aab5 | 2010-11-25 09:43:11 +0900 | [diff] [blame] | 1341 | // Always call subtype picker, because subtype picker is a superset of input method |
| 1342 | // picker. |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1343 | mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER); |
| 1344 | } |
| 1345 | } |
| 1346 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1347 | public void setInputMethod(IBinder token, String id) { |
satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 1348 | setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID); |
| 1349 | } |
| 1350 | |
| 1351 | public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) { |
| 1352 | synchronized (mMethodMap) { |
| 1353 | if (subtype != null) { |
| 1354 | setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode( |
| 1355 | mMethodMap.get(id), subtype.hashCode())); |
| 1356 | } else { |
| 1357 | setInputMethod(token, id); |
| 1358 | } |
| 1359 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1360 | } |
| 1361 | |
satok | b416a71 | 2010-11-25 20:42:14 +0900 | [diff] [blame] | 1362 | public void showInputMethodAndSubtypeEnablerFromClient( |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 1363 | IInputMethodClient client, String inputMethodId) { |
satok | b416a71 | 2010-11-25 20:42:14 +0900 | [diff] [blame] | 1364 | synchronized (mMethodMap) { |
| 1365 | if (mCurClient == null || client == null |
| 1366 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1367 | Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client); |
| 1368 | } |
satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 1369 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageO( |
| 1370 | MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId)); |
satok | b416a71 | 2010-11-25 20:42:14 +0900 | [diff] [blame] | 1371 | } |
| 1372 | } |
| 1373 | |
satok | 735cf38 | 2010-11-11 20:40:09 +0900 | [diff] [blame] | 1374 | public boolean switchToLastInputMethod(IBinder token) { |
| 1375 | synchronized (mMethodMap) { |
satok | c445bcd | 2011-01-25 18:57:24 +0900 | [diff] [blame] | 1376 | final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked(); |
| 1377 | if (lastIme == null) return false; |
| 1378 | final InputMethodInfo lastImi = mMethodMap.get(lastIme.first); |
| 1379 | if (lastImi == null) return false; |
| 1380 | |
| 1381 | final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId); |
| 1382 | final int lastSubtypeHash = Integer.valueOf(lastIme.second); |
| 1383 | // If the last IME is the same as the current IME and the last subtype is not defined, |
| 1384 | // there is no need to switch to the last IME. |
| 1385 | if (imiIdIsSame && lastSubtypeHash == NOT_A_SUBTYPE_ID) return false; |
| 1386 | |
| 1387 | int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID |
| 1388 | : mCurrentSubtype.hashCode(); |
| 1389 | if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) { |
| 1390 | if (DEBUG) { |
| 1391 | Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second + ", from: " |
| 1392 | + mCurMethodId + ", " + currentSubtypeHash); |
satok | 735cf38 | 2010-11-11 20:40:09 +0900 | [diff] [blame] | 1393 | } |
satok | c445bcd | 2011-01-25 18:57:24 +0900 | [diff] [blame] | 1394 | setInputMethodWithSubtypeId(token, lastIme.first, getSubtypeIdFromHashCode( |
| 1395 | lastImi, lastSubtypeHash)); |
| 1396 | return true; |
satok | 735cf38 | 2010-11-11 20:40:09 +0900 | [diff] [blame] | 1397 | } |
| 1398 | return false; |
| 1399 | } |
| 1400 | } |
| 1401 | |
satok | 68f1b78 | 2011-04-11 14:26:04 +0900 | [diff] [blame] | 1402 | public InputMethodSubtype getLastInputMethodSubtype() { |
| 1403 | synchronized (mMethodMap) { |
| 1404 | final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked(); |
| 1405 | // TODO: Handle the case of the last IME with no subtypes |
| 1406 | if (lastIme == null || TextUtils.isEmpty(lastIme.first) |
| 1407 | || TextUtils.isEmpty(lastIme.second)) return null; |
| 1408 | final InputMethodInfo lastImi = mMethodMap.get(lastIme.first); |
| 1409 | if (lastImi == null) return null; |
| 1410 | try { |
| 1411 | final int lastSubtypeHash = Integer.valueOf(lastIme.second); |
| 1412 | return lastImi.getSubtypeAt(getSubtypeIdFromHashCode( |
| 1413 | lastImi, lastSubtypeHash)); |
| 1414 | } catch (NumberFormatException e) { |
| 1415 | return null; |
| 1416 | } |
| 1417 | } |
| 1418 | } |
| 1419 | |
satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 1420 | private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1421 | synchronized (mMethodMap) { |
| 1422 | if (token == null) { |
| 1423 | if (mContext.checkCallingOrSelfPermission( |
| 1424 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1425 | != PackageManager.PERMISSION_GRANTED) { |
| 1426 | throw new SecurityException( |
| 1427 | "Using null token requires permission " |
| 1428 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 1429 | } |
| 1430 | } else if (mCurToken != token) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1431 | Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid() |
| 1432 | + " token: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1433 | return; |
| 1434 | } |
| 1435 | |
| 1436 | long ident = Binder.clearCallingIdentity(); |
| 1437 | try { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1438 | setInputMethodLocked(id, subtypeId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1439 | } finally { |
| 1440 | Binder.restoreCallingIdentity(ident); |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | public void hideMySoftInput(IBinder token, int flags) { |
| 1446 | synchronized (mMethodMap) { |
| 1447 | if (token == null || mCurToken != token) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1448 | if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid " |
| 1449 | + Binder.getCallingUid() + " token: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1450 | return; |
| 1451 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1452 | long ident = Binder.clearCallingIdentity(); |
| 1453 | try { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1454 | hideCurrentInputLocked(flags, null); |
| 1455 | } finally { |
| 1456 | Binder.restoreCallingIdentity(ident); |
| 1457 | } |
| 1458 | } |
| 1459 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1460 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1461 | public void showMySoftInput(IBinder token, int flags) { |
| 1462 | synchronized (mMethodMap) { |
| 1463 | if (token == null || mCurToken != token) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1464 | Slog.w(TAG, "Ignoring showMySoftInput of uid " |
| 1465 | + Binder.getCallingUid() + " token: " + token); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1466 | return; |
| 1467 | } |
| 1468 | long ident = Binder.clearCallingIdentity(); |
| 1469 | try { |
| 1470 | showCurrentInputLocked(flags, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1471 | } finally { |
| 1472 | Binder.restoreCallingIdentity(ident); |
| 1473 | } |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | void setEnabledSessionInMainThread(SessionState session) { |
| 1478 | if (mEnabledSession != session) { |
| 1479 | if (mEnabledSession != null) { |
| 1480 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1481 | if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1482 | mEnabledSession.method.setSessionEnabled( |
| 1483 | mEnabledSession.session, false); |
| 1484 | } catch (RemoteException e) { |
| 1485 | } |
| 1486 | } |
| 1487 | mEnabledSession = session; |
| 1488 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1489 | if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1490 | session.method.setSessionEnabled( |
| 1491 | session.session, true); |
| 1492 | } catch (RemoteException e) { |
| 1493 | } |
| 1494 | } |
| 1495 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1496 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1497 | public boolean handleMessage(Message msg) { |
| 1498 | HandlerCaller.SomeArgs args; |
| 1499 | switch (msg.what) { |
| 1500 | case MSG_SHOW_IM_PICKER: |
| 1501 | showInputMethodMenu(); |
| 1502 | return true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1503 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1504 | case MSG_SHOW_IM_SUBTYPE_PICKER: |
| 1505 | showInputMethodSubtypeMenu(); |
| 1506 | return true; |
| 1507 | |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1508 | case MSG_SHOW_IM_SUBTYPE_ENABLER: |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 1509 | args = (HandlerCaller.SomeArgs)msg.obj; |
satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 1510 | showInputMethodAndSubtypeEnabler((String)args.arg1); |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 1511 | return true; |
| 1512 | |
| 1513 | case MSG_SHOW_IM_CONFIG: |
| 1514 | showConfigureInputMethods(); |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1515 | return true; |
| 1516 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1517 | // --------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1518 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1519 | case MSG_UNBIND_INPUT: |
| 1520 | try { |
| 1521 | ((IInputMethod)msg.obj).unbindInput(); |
| 1522 | } catch (RemoteException e) { |
| 1523 | // There is nothing interesting about the method dying. |
| 1524 | } |
| 1525 | return true; |
| 1526 | case MSG_BIND_INPUT: |
| 1527 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1528 | try { |
| 1529 | ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2); |
| 1530 | } catch (RemoteException e) { |
| 1531 | } |
| 1532 | return true; |
| 1533 | case MSG_SHOW_SOFT_INPUT: |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1534 | args = (HandlerCaller.SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1535 | try { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1536 | ((IInputMethod)args.arg1).showSoftInput(msg.arg1, |
| 1537 | (ResultReceiver)args.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1538 | } catch (RemoteException e) { |
| 1539 | } |
| 1540 | return true; |
| 1541 | case MSG_HIDE_SOFT_INPUT: |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1542 | args = (HandlerCaller.SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1543 | try { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1544 | ((IInputMethod)args.arg1).hideSoftInput(0, |
| 1545 | (ResultReceiver)args.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1546 | } catch (RemoteException e) { |
| 1547 | } |
| 1548 | return true; |
| 1549 | case MSG_ATTACH_TOKEN: |
| 1550 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1551 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1552 | 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] | 1553 | ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2); |
| 1554 | } catch (RemoteException e) { |
| 1555 | } |
| 1556 | return true; |
| 1557 | case MSG_CREATE_SESSION: |
| 1558 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1559 | try { |
| 1560 | ((IInputMethod)args.arg1).createSession( |
| 1561 | (IInputMethodCallback)args.arg2); |
| 1562 | } catch (RemoteException e) { |
| 1563 | } |
| 1564 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1565 | // --------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1566 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1567 | case MSG_START_INPUT: |
| 1568 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1569 | try { |
| 1570 | SessionState session = (SessionState)args.arg1; |
| 1571 | setEnabledSessionInMainThread(session); |
| 1572 | session.method.startInput((IInputContext)args.arg2, |
| 1573 | (EditorInfo)args.arg3); |
| 1574 | } catch (RemoteException e) { |
| 1575 | } |
| 1576 | return true; |
| 1577 | case MSG_RESTART_INPUT: |
| 1578 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1579 | try { |
| 1580 | SessionState session = (SessionState)args.arg1; |
| 1581 | setEnabledSessionInMainThread(session); |
| 1582 | session.method.restartInput((IInputContext)args.arg2, |
| 1583 | (EditorInfo)args.arg3); |
| 1584 | } catch (RemoteException e) { |
| 1585 | } |
| 1586 | return true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1587 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1588 | // --------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1589 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1590 | case MSG_UNBIND_METHOD: |
| 1591 | try { |
| 1592 | ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1); |
| 1593 | } catch (RemoteException e) { |
| 1594 | // There is nothing interesting about the last client dying. |
| 1595 | } |
| 1596 | return true; |
| 1597 | case MSG_BIND_METHOD: |
| 1598 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1599 | try { |
| 1600 | ((IInputMethodClient)args.arg1).onBindMethod( |
| 1601 | (InputBindResult)args.arg2); |
| 1602 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1603 | 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] | 1604 | } |
| 1605 | return true; |
| 1606 | } |
| 1607 | return false; |
| 1608 | } |
| 1609 | |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1610 | private boolean isSystemIme(InputMethodInfo inputMethod) { |
| 1611 | return (inputMethod.getServiceInfo().applicationInfo.flags |
| 1612 | & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 1613 | } |
| 1614 | |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 1615 | private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) { |
| 1616 | ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>(); |
| 1617 | final int subtypeCount = imi.getSubtypeCount(); |
| 1618 | for (int i = 0; i < subtypeCount; ++i) { |
| 1619 | subtypes.add(imi.getSubtypeAt(i)); |
| 1620 | } |
| 1621 | return subtypes; |
| 1622 | } |
| 1623 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1624 | private boolean chooseNewDefaultIMELocked() { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1625 | List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked(); |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1626 | if (enabled != null && enabled.size() > 0) { |
Dianne Hackborn | 83e48f5 | 2010-03-23 23:03:25 -0700 | [diff] [blame] | 1627 | // We'd prefer to fall back on a system IME, since that is safer. |
| 1628 | int i=enabled.size(); |
| 1629 | while (i > 0) { |
| 1630 | i--; |
| 1631 | if ((enabled.get(i).getServiceInfo().applicationInfo.flags |
| 1632 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 1633 | break; |
| 1634 | } |
| 1635 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1636 | InputMethodInfo imi = enabled.get(i); |
satok | 03eb319a | 2010-11-11 18:17:42 +0900 | [diff] [blame] | 1637 | if (DEBUG) { |
| 1638 | Slog.d(TAG, "New default IME was selected: " + imi.getId()); |
| 1639 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 1640 | resetSelectedInputMethodAndSubtypeLocked(imi.getId()); |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1641 | return true; |
| 1642 | } |
| 1643 | |
| 1644 | return false; |
| 1645 | } |
| 1646 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1647 | void buildInputMethodListLocked(ArrayList<InputMethodInfo> list, |
| 1648 | HashMap<String, InputMethodInfo> map) { |
| 1649 | list.clear(); |
| 1650 | map.clear(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1651 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1652 | PackageManager pm = mContext.getPackageManager(); |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1653 | final Configuration config = mRes.getConfiguration(); |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 1654 | final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY; |
| 1655 | String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(), |
| 1656 | Secure.DISABLED_SYSTEM_INPUT_METHODS); |
| 1657 | if (disabledSysImes == null) disabledSysImes = ""; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1658 | |
| 1659 | List<ResolveInfo> services = pm.queryIntentServices( |
| 1660 | new Intent(InputMethod.SERVICE_INTERFACE), |
| 1661 | PackageManager.GET_META_DATA); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1662 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1663 | for (int i = 0; i < services.size(); ++i) { |
| 1664 | ResolveInfo ri = services.get(i); |
| 1665 | ServiceInfo si = ri.serviceInfo; |
| 1666 | ComponentName compName = new ComponentName(si.packageName, si.name); |
| 1667 | if (!android.Manifest.permission.BIND_INPUT_METHOD.equals( |
| 1668 | si.permission)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1669 | Slog.w(TAG, "Skipping input method " + compName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1670 | + ": it does not require the permission " |
| 1671 | + android.Manifest.permission.BIND_INPUT_METHOD); |
| 1672 | continue; |
| 1673 | } |
| 1674 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1675 | if (DEBUG) Slog.d(TAG, "Checking " + compName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1676 | |
| 1677 | try { |
| 1678 | InputMethodInfo p = new InputMethodInfo(mContext, ri); |
| 1679 | list.add(p); |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 1680 | final String id = p.getId(); |
| 1681 | map.put(id, p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1682 | |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 1683 | // System IMEs are enabled by default, unless there's a hard keyboard |
| 1684 | // and the system IME was explicitly disabled |
| 1685 | if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) { |
| 1686 | setInputMethodEnabledLocked(id, true); |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1687 | } |
| 1688 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1689 | if (DEBUG) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1690 | 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] | 1691 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1692 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1693 | } catch (XmlPullParserException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1694 | 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] | 1695 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1696 | 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] | 1697 | } |
| 1698 | } |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1699 | |
| 1700 | String defaultIme = Settings.Secure.getString(mContext |
| 1701 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 1702 | if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1703 | if (chooseNewDefaultIMELocked()) { |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1704 | updateFromSettingsLocked(); |
| 1705 | } |
| 1706 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1707 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1708 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1709 | // ---------------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1710 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1711 | private void showInputMethodMenu() { |
| 1712 | showInputMethodMenuInternal(false); |
| 1713 | } |
| 1714 | |
| 1715 | private void showInputMethodSubtypeMenu() { |
| 1716 | showInputMethodMenuInternal(true); |
| 1717 | } |
| 1718 | |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 1719 | private void showInputMethodAndSubtypeEnabler(String inputMethodId) { |
Tadashi G. Takaoka | f49688f | 2011-01-20 17:56:13 +0900 | [diff] [blame] | 1720 | Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS); |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1721 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
satok | 86417ea | 2010-10-27 14:11:03 +0900 | [diff] [blame] | 1722 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
| 1723 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 1724 | if (!TextUtils.isEmpty(inputMethodId)) { |
Tadashi G. Takaoka | 2548020 | 2011-01-20 23:13:02 +0900 | [diff] [blame] | 1725 | intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId); |
satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 1726 | } |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 1727 | mContext.startActivity(intent); |
| 1728 | } |
| 1729 | |
| 1730 | private void showConfigureInputMethods() { |
| 1731 | Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS); |
| 1732 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 1733 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
| 1734 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1735 | mContext.startActivity(intent); |
| 1736 | } |
| 1737 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1738 | private void showInputMethodMenuInternal(boolean showSubtypes) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1739 | if (DEBUG) Slog.v(TAG, "Show switching menu"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1740 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1741 | final Context context = mContext; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1742 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1743 | final PackageManager pm = context.getPackageManager(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1744 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1745 | String lastInputMethodId = Settings.Secure.getString(context |
| 1746 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1747 | int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1748 | if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1749 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1750 | synchronized (mMethodMap) { |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 1751 | final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis = |
| 1752 | getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1753 | if (immis == null || immis.size() == 0) { |
| 1754 | return; |
| 1755 | } |
| 1756 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1757 | hideInputMethodMenuLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1758 | |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 1759 | final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis = |
| 1760 | new TreeMap<InputMethodInfo, List<InputMethodSubtype>>( |
| 1761 | new Comparator<InputMethodInfo>() { |
| 1762 | @Override |
| 1763 | public int compare(InputMethodInfo imi1, InputMethodInfo imi2) { |
| 1764 | if (imi2 == null) return 0; |
| 1765 | if (imi1 == null) return 1; |
| 1766 | if (pm == null) { |
| 1767 | return imi1.getId().compareTo(imi2.getId()); |
| 1768 | } |
| 1769 | CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId(); |
| 1770 | CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId(); |
| 1771 | return imiId1.toString().compareTo(imiId2.toString()); |
| 1772 | } |
| 1773 | }); |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 1774 | |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 1775 | sortedImmis.putAll(immis); |
| 1776 | |
| 1777 | final ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>> imList = |
| 1778 | new ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>>(); |
| 1779 | |
| 1780 | for (InputMethodInfo imi : sortedImmis.keySet()) { |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 1781 | if (imi == null) continue; |
| 1782 | List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1783 | HashSet<String> enabledSubtypeSet = new HashSet<String>(); |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 1784 | for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) { |
| 1785 | enabledSubtypeSet.add(String.valueOf(subtype.hashCode())); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1786 | } |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 1787 | ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi); |
Ken Wakasa | 81f6e3d | 2011-03-04 09:59:52 +0900 | [diff] [blame] | 1788 | final CharSequence label = imi.loadLabel(pm); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1789 | if (showSubtypes && enabledSubtypeSet.size() > 0) { |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 1790 | final int subtypeCount = imi.getSubtypeCount(); |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 1791 | for (int j = 0; j < subtypeCount; ++j) { |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 1792 | InputMethodSubtype subtype = imi.getSubtypeAt(j); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1793 | if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) { |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 1794 | final CharSequence title; |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1795 | int nameResId = subtype.getNameResId(); |
satok | 9ef0283 | 2010-11-04 21:17:48 +0900 | [diff] [blame] | 1796 | String mode = subtype.getMode(); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1797 | if (nameResId != 0) { |
Ken Wakasa | 81f6e3d | 2011-03-04 09:59:52 +0900 | [diff] [blame] | 1798 | title = TextUtils.concat(pm.getText(imi.getPackageName(), |
| 1799 | nameResId, imi.getServiceInfo().applicationInfo), |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 1800 | (TextUtils.isEmpty(label) ? "" : " (" + label + ")")); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1801 | } else { |
| 1802 | CharSequence language = subtype.getLocale(); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1803 | // TODO: Use more friendly Title and UI |
| 1804 | title = label + "," + (mode == null ? "" : mode) + "," |
| 1805 | + (language == null ? "" : language); |
| 1806 | } |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 1807 | imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>( |
| 1808 | title, new Pair<InputMethodInfo, Integer>(imi, j))); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1809 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1810 | } |
| 1811 | } else { |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 1812 | imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>( |
| 1813 | label, new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID))); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1814 | } |
Dianne Hackborn | 97106ab | 2010-03-03 00:08:31 -0800 | [diff] [blame] | 1815 | } |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 1816 | |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 1817 | final int N = imList.size(); |
| 1818 | mItems = new CharSequence[N]; |
| 1819 | for (int i = 0; i < N; ++i) { |
| 1820 | mItems[i] = imList.get(i).first; |
| 1821 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1822 | mIms = new InputMethodInfo[N]; |
| 1823 | mSubtypeIds = new int[N]; |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1824 | int checkedItem = 0; |
| 1825 | for (int i = 0; i < N; ++i) { |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 1826 | Pair<InputMethodInfo, Integer> value = imList.get(i).second; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1827 | mIms[i] = value.first; |
| 1828 | mSubtypeIds[i] = value.second; |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1829 | if (mIms[i].getId().equals(lastInputMethodId)) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1830 | int subtypeId = mSubtypeIds[i]; |
| 1831 | if ((subtypeId == NOT_A_SUBTYPE_ID) |
| 1832 | || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0) |
| 1833 | || (subtypeId == lastInputMethodSubtypeId)) { |
| 1834 | checkedItem = i; |
| 1835 | } |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1836 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1837 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1838 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1839 | AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() { |
| 1840 | public void onClick(DialogInterface dialog, int which) { |
| 1841 | hideInputMethodMenu(); |
| 1842 | } |
| 1843 | }; |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1844 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1845 | TypedArray a = context.obtainStyledAttributes(null, |
| 1846 | com.android.internal.R.styleable.DialogPreference, |
| 1847 | com.android.internal.R.attr.alertDialogStyle, 0); |
| 1848 | mDialogBuilder = new AlertDialog.Builder(context) |
| 1849 | .setTitle(com.android.internal.R.string.select_input_method) |
| 1850 | .setOnCancelListener(new OnCancelListener() { |
| 1851 | public void onCancel(DialogInterface dialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1852 | hideInputMethodMenu(); |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1853 | } |
| 1854 | }) |
| 1855 | .setIcon(a.getDrawable( |
| 1856 | com.android.internal.R.styleable.DialogPreference_dialogTitle)); |
| 1857 | a.recycle(); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1858 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1859 | mDialogBuilder.setSingleChoiceItems(mItems, checkedItem, |
| 1860 | new AlertDialog.OnClickListener() { |
| 1861 | public void onClick(DialogInterface dialog, int which) { |
| 1862 | synchronized (mMethodMap) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1863 | if (mIms == null || mIms.length <= which |
| 1864 | || mSubtypeIds == null || mSubtypeIds.length <= which) { |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1865 | return; |
| 1866 | } |
| 1867 | InputMethodInfo im = mIms[which]; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1868 | int subtypeId = mSubtypeIds[which]; |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1869 | hideInputMethodMenu(); |
| 1870 | if (im != null) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1871 | if ((subtypeId < 0) |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 1872 | || (subtypeId >= im.getSubtypeCount())) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1873 | subtypeId = NOT_A_SUBTYPE_ID; |
| 1874 | } |
| 1875 | setInputMethodLocked(im.getId(), subtypeId); |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1876 | } |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1877 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1878 | } |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1879 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1880 | |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1881 | if (showSubtypes) { |
satok | 82beadf | 2010-12-27 19:03:06 +0900 | [diff] [blame] | 1882 | mDialogBuilder.setPositiveButton( |
| 1883 | com.android.internal.R.string.configure_input_methods, |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1884 | new DialogInterface.OnClickListener() { |
| 1885 | public void onClick(DialogInterface dialog, int whichButton) { |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 1886 | showConfigureInputMethods(); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1887 | } |
| 1888 | }); |
| 1889 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1890 | mSwitchingDialog = mDialogBuilder.create(); |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1891 | mSwitchingDialog.setCanceledOnTouchOutside(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1892 | mSwitchingDialog.getWindow().setType( |
| 1893 | WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG); |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1894 | mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1895 | mSwitchingDialog.show(); |
| 1896 | } |
| 1897 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1898 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1899 | void hideInputMethodMenu() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1900 | synchronized (mMethodMap) { |
| 1901 | hideInputMethodMenuLocked(); |
| 1902 | } |
| 1903 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1904 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1905 | void hideInputMethodMenuLocked() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1906 | if (DEBUG) Slog.v(TAG, "Hide switching menu"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1907 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1908 | if (mSwitchingDialog != null) { |
| 1909 | mSwitchingDialog.dismiss(); |
| 1910 | mSwitchingDialog = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1911 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1912 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1913 | mDialogBuilder = null; |
| 1914 | mItems = null; |
| 1915 | mIms = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1916 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1917 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1918 | // ---------------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1919 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1920 | public boolean setInputMethodEnabled(String id, boolean enabled) { |
| 1921 | synchronized (mMethodMap) { |
| 1922 | if (mContext.checkCallingOrSelfPermission( |
| 1923 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1924 | != PackageManager.PERMISSION_GRANTED) { |
| 1925 | throw new SecurityException( |
| 1926 | "Requires permission " |
| 1927 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 1928 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1929 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1930 | long ident = Binder.clearCallingIdentity(); |
| 1931 | try { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1932 | return setInputMethodEnabledLocked(id, enabled); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1933 | } finally { |
| 1934 | Binder.restoreCallingIdentity(ident); |
| 1935 | } |
| 1936 | } |
| 1937 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1938 | |
| 1939 | boolean setInputMethodEnabledLocked(String id, boolean enabled) { |
| 1940 | // Make sure this is a valid input method. |
| 1941 | InputMethodInfo imm = mMethodMap.get(id); |
| 1942 | if (imm == null) { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1943 | throw new IllegalArgumentException("Unknown id: " + mCurMethodId); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1944 | } |
| 1945 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1946 | List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings |
| 1947 | .getEnabledInputMethodsAndSubtypeListLocked(); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1948 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1949 | if (enabled) { |
| 1950 | for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) { |
| 1951 | if (pair.first.equals(id)) { |
| 1952 | // We are enabling this input method, but it is already enabled. |
| 1953 | // Nothing to do. The previous state was enabled. |
| 1954 | return true; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1955 | } |
| 1956 | } |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1957 | mSettings.appendAndPutEnabledInputMethodLocked(id, false); |
| 1958 | // Previous state was disabled. |
| 1959 | return false; |
| 1960 | } else { |
| 1961 | StringBuilder builder = new StringBuilder(); |
| 1962 | if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked( |
| 1963 | builder, enabledInputMethodsList, id)) { |
| 1964 | // Disabled input method is currently selected, switch to another one. |
| 1965 | String selId = Settings.Secure.getString(mContext.getContentResolver(), |
| 1966 | Settings.Secure.DEFAULT_INPUT_METHOD); |
satok | 03eb319a | 2010-11-11 18:17:42 +0900 | [diff] [blame] | 1967 | if (id.equals(selId) && !chooseNewDefaultIMELocked()) { |
| 1968 | Slog.i(TAG, "Can't find new IME, unsetting the current input method."); |
| 1969 | resetSelectedInputMethodAndSubtypeLocked(""); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1970 | } |
| 1971 | // Previous state was enabled. |
| 1972 | return true; |
| 1973 | } else { |
| 1974 | // We are disabling the input method but it is already disabled. |
| 1975 | // Nothing to do. The previous state was disabled. |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1976 | return false; |
| 1977 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1978 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1979 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1980 | |
satok | 57ffc00 | 2011-01-25 00:11:47 +0900 | [diff] [blame] | 1981 | private boolean canAddToLastInputMethod(InputMethodSubtype subtype) { |
| 1982 | if (subtype == null) return true; |
Ken Wakasa | a5e9ffa | 2011-02-16 11:55:23 +0900 | [diff] [blame] | 1983 | return !subtype.containsExtraValueKey(SUBTYPE_EXTRAVALUE_EXCLUDE_FROM_LAST_IME); |
satok | 57ffc00 | 2011-01-25 00:11:47 +0900 | [diff] [blame] | 1984 | } |
| 1985 | |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 1986 | private void saveCurrentInputMethodAndSubtypeToHistory() { |
| 1987 | String subtypeId = NOT_A_SUBTYPE_ID_STR; |
| 1988 | if (mCurrentSubtype != null) { |
| 1989 | subtypeId = String.valueOf(mCurrentSubtype.hashCode()); |
| 1990 | } |
satok | 57ffc00 | 2011-01-25 00:11:47 +0900 | [diff] [blame] | 1991 | if (canAddToLastInputMethod(mCurrentSubtype)) { |
| 1992 | mSettings.addSubtypeToHistory(mCurMethodId, subtypeId); |
| 1993 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1994 | } |
| 1995 | |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 1996 | private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId, |
| 1997 | boolean setSubtypeOnly) { |
| 1998 | // Update the history of InputMethod and Subtype |
| 1999 | saveCurrentInputMethodAndSubtypeToHistory(); |
| 2000 | |
| 2001 | // Set Subtype here |
| 2002 | if (imi == null || subtypeId < 0) { |
| 2003 | mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID); |
Tadashi G. Takaoka | 0ba75bb | 2010-11-09 12:19:32 -0800 | [diff] [blame] | 2004 | mCurrentSubtype = null; |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2005 | } else { |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2006 | if (subtypeId < imi.getSubtypeCount()) { |
| 2007 | InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId); |
| 2008 | mSettings.putSelectedSubtype(subtype.hashCode()); |
| 2009 | mCurrentSubtype = subtype; |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2010 | } else { |
| 2011 | mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID); |
| 2012 | mCurrentSubtype = null; |
| 2013 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2014 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2015 | |
| 2016 | if (!setSubtypeOnly) { |
| 2017 | // Set InputMethod here |
| 2018 | mSettings.putSelectedInputMethod(imi != null ? imi.getId() : ""); |
| 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) { |
| 2023 | InputMethodInfo imi = mMethodMap.get(newDefaultIme); |
| 2024 | int lastSubtypeId = NOT_A_SUBTYPE_ID; |
| 2025 | // newDefaultIme is empty when there is no candidate for the selected IME. |
| 2026 | if (imi != null && !TextUtils.isEmpty(newDefaultIme)) { |
| 2027 | String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme); |
| 2028 | if (subtypeHashCode != null) { |
| 2029 | try { |
| 2030 | lastSubtypeId = getSubtypeIdFromHashCode( |
| 2031 | imi, Integer.valueOf(subtypeHashCode)); |
| 2032 | } catch (NumberFormatException e) { |
| 2033 | Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e); |
| 2034 | } |
| 2035 | } |
| 2036 | } |
| 2037 | setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | private int getSelectedInputMethodSubtypeId(String id) { |
| 2041 | InputMethodInfo imi = mMethodMap.get(id); |
| 2042 | if (imi == null) { |
| 2043 | return NOT_A_SUBTYPE_ID; |
| 2044 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2045 | int subtypeId; |
| 2046 | try { |
| 2047 | subtypeId = Settings.Secure.getInt(mContext.getContentResolver(), |
| 2048 | Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE); |
| 2049 | } catch (SettingNotFoundException e) { |
| 2050 | return NOT_A_SUBTYPE_ID; |
| 2051 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2052 | return getSubtypeIdFromHashCode(imi, subtypeId); |
| 2053 | } |
| 2054 | |
| 2055 | private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) { |
satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 2056 | if (imi != null) { |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2057 | final int subtypeCount = imi.getSubtypeCount(); |
| 2058 | for (int i = 0; i < subtypeCount; ++i) { |
| 2059 | InputMethodSubtype ims = imi.getSubtypeAt(i); |
satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 2060 | if (subtypeHashCode == ims.hashCode()) { |
| 2061 | return i; |
| 2062 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2063 | } |
| 2064 | } |
| 2065 | return NOT_A_SUBTYPE_ID; |
| 2066 | } |
| 2067 | |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2068 | private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked( |
| 2069 | Resources res, List<InputMethodSubtype> subtypes) { |
| 2070 | final String systemLocale = res.getConfiguration().locale.toString(); |
satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2071 | if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>(); |
| 2072 | HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap = |
| 2073 | new HashMap<String, InputMethodSubtype>(); |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2074 | final int N = subtypes.size(); |
| 2075 | boolean containsKeyboardSubtype = false; |
| 2076 | for (int i = 0; i < N; ++i) { |
| 2077 | InputMethodSubtype subtype = subtypes.get(i); |
satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2078 | final String locale = subtype.getLocale(); |
| 2079 | final String mode = subtype.getMode(); |
| 2080 | // When system locale starts with subtype's locale, that subtype will be applicable |
| 2081 | // for system locale |
| 2082 | // For instance, it's clearly applicable for cases like system locale = en_US and |
| 2083 | // subtype = en, but it is not necessarily considered applicable for cases like system |
| 2084 | // locale = en and subtype = en_US. |
| 2085 | // We just call systemLocale.startsWith(locale) in this function because there is no |
| 2086 | // need to find applicable subtypes aggressively unlike |
| 2087 | // findLastResortApplicableSubtypeLocked. |
| 2088 | if (systemLocale.startsWith(locale)) { |
| 2089 | InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode); |
| 2090 | // If more applicable subtypes are contained, skip. |
| 2091 | if (applicableSubtype != null |
| 2092 | && systemLocale.equals(applicableSubtype.getLocale())) continue; |
| 2093 | applicableModeAndSubtypesMap.put(mode, subtype); |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2094 | if (!containsKeyboardSubtype |
| 2095 | && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) { |
| 2096 | containsKeyboardSubtype = true; |
| 2097 | } |
| 2098 | } |
| 2099 | } |
satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2100 | ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>( |
| 2101 | applicableModeAndSubtypesMap.values()); |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2102 | if (!containsKeyboardSubtype) { |
| 2103 | InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked( |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2104 | res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true); |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2105 | if (lastResortKeyboardSubtype != null) { |
| 2106 | applicableSubtypes.add(lastResortKeyboardSubtype); |
| 2107 | } |
| 2108 | } |
| 2109 | return applicableSubtypes; |
| 2110 | } |
| 2111 | |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2112 | /** |
| 2113 | * If there are no selected subtypes, tries finding the most applicable one according to the |
| 2114 | * given locale. |
| 2115 | * @param subtypes this function will search the most applicable subtype in subtypes |
| 2116 | * @param mode subtypes will be filtered by mode |
| 2117 | * @param locale subtypes will be filtered by locale |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2118 | * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype, |
| 2119 | * it will return the first subtype matched with mode |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2120 | * @return the most applicable subtypeId |
| 2121 | */ |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2122 | private static InputMethodSubtype findLastResortApplicableSubtypeLocked( |
| 2123 | Resources res, List<InputMethodSubtype> subtypes, String mode, String locale, |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2124 | boolean canIgnoreLocaleAsLastResort) { |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2125 | if (subtypes == null || subtypes.size() == 0) { |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2126 | return null; |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2127 | } |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2128 | if (TextUtils.isEmpty(locale)) { |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2129 | locale = res.getConfiguration().locale.toString(); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2130 | } |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2131 | final String language = locale.substring(0, 2); |
| 2132 | boolean partialMatchFound = false; |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2133 | InputMethodSubtype applicableSubtype = null; |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2134 | InputMethodSubtype firstMatchedModeSubtype = null; |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2135 | final int N = subtypes.size(); |
| 2136 | for (int i = 0; i < N; ++i) { |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2137 | InputMethodSubtype subtype = subtypes.get(i); |
| 2138 | final String subtypeLocale = subtype.getLocale(); |
satok | d871343 | 2011-01-18 00:55:13 +0900 | [diff] [blame] | 2139 | // An applicable subtype should match "mode". If mode is null, mode will be ignored, |
| 2140 | // and all subtypes with all modes can be candidates. |
| 2141 | if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) { |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2142 | if (firstMatchedModeSubtype == null) { |
| 2143 | firstMatchedModeSubtype = subtype; |
| 2144 | } |
satok | 9ef0283 | 2010-11-04 21:17:48 +0900 | [diff] [blame] | 2145 | if (locale.equals(subtypeLocale)) { |
| 2146 | // 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] | 2147 | applicableSubtype = subtype; |
satok | 9ef0283 | 2010-11-04 21:17:48 +0900 | [diff] [blame] | 2148 | break; |
| 2149 | } else if (!partialMatchFound && subtypeLocale.startsWith(language)) { |
| 2150 | // 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] | 2151 | applicableSubtype = subtype; |
satok | 9ef0283 | 2010-11-04 21:17:48 +0900 | [diff] [blame] | 2152 | partialMatchFound = true; |
| 2153 | } |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2154 | } |
| 2155 | } |
| 2156 | |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2157 | if (applicableSubtype == null && canIgnoreLocaleAsLastResort) { |
| 2158 | return firstMatchedModeSubtype; |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2159 | } |
| 2160 | |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2161 | // The first subtype applicable to the system locale will be defined as the most applicable |
| 2162 | // subtype. |
| 2163 | if (DEBUG) { |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2164 | if (applicableSubtype != null) { |
| 2165 | Slog.d(TAG, "Applicable InputMethodSubtype was found: " |
| 2166 | + applicableSubtype.getMode() + "," + applicableSubtype.getLocale()); |
| 2167 | } |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2168 | } |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2169 | return applicableSubtype; |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2170 | } |
| 2171 | |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2172 | // If there are no selected shortcuts, tries finding the most applicable ones. |
| 2173 | private Pair<InputMethodInfo, InputMethodSubtype> |
| 2174 | findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) { |
| 2175 | List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked(); |
| 2176 | InputMethodInfo mostApplicableIMI = null; |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2177 | InputMethodSubtype mostApplicableSubtype = null; |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2178 | boolean foundInSystemIME = false; |
| 2179 | |
| 2180 | // Search applicable subtype for each InputMethodInfo |
| 2181 | for (InputMethodInfo imi: imis) { |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2182 | final String imiId = imi.getId(); |
| 2183 | if (foundInSystemIME && !imiId.equals(mCurMethodId)) { |
| 2184 | continue; |
| 2185 | } |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2186 | InputMethodSubtype subtype = null; |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2187 | final List<InputMethodSubtype> enabledSubtypes = |
| 2188 | getEnabledInputMethodSubtypeList(imi, true); |
| 2189 | // 1. Search by the current subtype's locale from enabledSubtypes. |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2190 | if (mCurrentSubtype != null) { |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2191 | subtype = findLastResortApplicableSubtypeLocked( |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2192 | mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2193 | } |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2194 | // 2. Search by the system locale from enabledSubtypes. |
| 2195 | // 3. Search the first enabled subtype matched with mode from enabledSubtypes. |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2196 | if (subtype == null) { |
| 2197 | subtype = findLastResortApplicableSubtypeLocked( |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2198 | mRes, enabledSubtypes, mode, null, true); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2199 | } |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2200 | // 4. Search by the current subtype's locale from all subtypes. |
| 2201 | if (subtype == null && mCurrentSubtype != null) { |
| 2202 | subtype = findLastResortApplicableSubtypeLocked( |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2203 | mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false); |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2204 | } |
| 2205 | // 5. Search by the system locale from all subtypes. |
| 2206 | // 6. Search the first enabled subtype matched with mode from all subtypes. |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2207 | if (subtype == null) { |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2208 | subtype = findLastResortApplicableSubtypeLocked( |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2209 | mRes, getSubtypes(imi), mode, null, true); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2210 | } |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2211 | if (subtype != null) { |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2212 | if (imiId.equals(mCurMethodId)) { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2213 | // The current input method is the most applicable IME. |
| 2214 | mostApplicableIMI = imi; |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2215 | mostApplicableSubtype = subtype; |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2216 | break; |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2217 | } else if (!foundInSystemIME) { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2218 | // The system input method is 2nd applicable IME. |
| 2219 | mostApplicableIMI = imi; |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2220 | mostApplicableSubtype = subtype; |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2221 | if ((imi.getServiceInfo().applicationInfo.flags |
| 2222 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 2223 | foundInSystemIME = true; |
| 2224 | } |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2225 | } |
| 2226 | } |
| 2227 | } |
| 2228 | if (DEBUG) { |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2229 | if (mostApplicableIMI != null) { |
| 2230 | Slog.w(TAG, "Most applicable shortcut input method was:" |
| 2231 | + mostApplicableIMI.getId()); |
| 2232 | if (mostApplicableSubtype != null) { |
| 2233 | Slog.w(TAG, "Most applicable shortcut input method subtype was:" |
| 2234 | + "," + mostApplicableSubtype.getMode() + "," |
| 2235 | + mostApplicableSubtype.getLocale()); |
| 2236 | } |
| 2237 | } |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2238 | } |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2239 | if (mostApplicableIMI != null) { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2240 | return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI, |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2241 | mostApplicableSubtype); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2242 | } else { |
| 2243 | return null; |
| 2244 | } |
| 2245 | } |
| 2246 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2247 | /** |
| 2248 | * @return Return the current subtype of this input method. |
| 2249 | */ |
| 2250 | public InputMethodSubtype getCurrentInputMethodSubtype() { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2251 | boolean subtypeIsSelected = false; |
| 2252 | try { |
| 2253 | subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(), |
| 2254 | Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID; |
| 2255 | } catch (SettingNotFoundException e) { |
| 2256 | } |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 2257 | synchronized (mMethodMap) { |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 2258 | if (!subtypeIsSelected || mCurrentSubtype == null) { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2259 | String lastInputMethodId = Settings.Secure.getString( |
| 2260 | mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 2261 | int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId); |
| 2262 | if (subtypeId == NOT_A_SUBTYPE_ID) { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2263 | InputMethodInfo imi = mMethodMap.get(lastInputMethodId); |
| 2264 | if (imi != null) { |
| 2265 | // If there are no selected subtypes, the framework will try to find |
satok | d871343 | 2011-01-18 00:55:13 +0900 | [diff] [blame] | 2266 | // the most applicable subtype from explicitly or implicitly enabled |
| 2267 | // subtypes. |
| 2268 | List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes = |
| 2269 | getEnabledInputMethodSubtypeList(imi, true); |
| 2270 | // If there is only one explicitly or implicitly enabled subtype, |
| 2271 | // just returns it. |
| 2272 | if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) { |
| 2273 | mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0); |
| 2274 | } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) { |
| 2275 | mCurrentSubtype = findLastResortApplicableSubtypeLocked( |
| 2276 | mRes, explicitlyOrImplicitlyEnabledSubtypes, |
| 2277 | SUBTYPE_MODE_KEYBOARD, null, true); |
| 2278 | if (mCurrentSubtype == null) { |
| 2279 | mCurrentSubtype = findLastResortApplicableSubtypeLocked( |
| 2280 | mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null, |
| 2281 | true); |
| 2282 | } |
| 2283 | } |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2284 | } |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2285 | } else { |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 2286 | mCurrentSubtype = |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2287 | getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId); |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 2288 | } |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2289 | } |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 2290 | return mCurrentSubtype; |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2291 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2292 | } |
| 2293 | |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 2294 | private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi, |
| 2295 | InputMethodSubtype subtype) { |
| 2296 | if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) { |
| 2297 | mShortcutInputMethodsAndSubtypes.get(imi).add(subtype); |
| 2298 | } else { |
| 2299 | ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>(); |
| 2300 | subtypes.add(subtype); |
| 2301 | mShortcutInputMethodsAndSubtypes.put(imi, subtypes); |
| 2302 | } |
| 2303 | } |
| 2304 | |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2305 | // TODO: We should change the return type from List to List<Parcelable> |
| 2306 | public List getShortcutInputMethodsAndSubtypes() { |
| 2307 | synchronized (mMethodMap) { |
satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2308 | ArrayList<Object> ret = new ArrayList<Object>(); |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 2309 | if (mShortcutInputMethodsAndSubtypes.size() == 0) { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2310 | // If there are no selected shortcut subtypes, the framework will try to find |
| 2311 | // the most applicable subtype from all subtypes whose mode is |
| 2312 | // 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] | 2313 | Pair<InputMethodInfo, InputMethodSubtype> info = |
| 2314 | findLastResortApplicableShortcutInputMethodAndSubtypeLocked( |
| 2315 | SUBTYPE_MODE_VOICE); |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2316 | if (info != null) { |
satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2317 | ret.add(info.first); |
| 2318 | ret.add(info.second); |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2319 | } |
satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2320 | return ret; |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 2321 | } |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 2322 | for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) { |
| 2323 | ret.add(imi); |
| 2324 | for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) { |
| 2325 | ret.add(subtype); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2326 | } |
| 2327 | } |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 2328 | return ret; |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2329 | } |
| 2330 | } |
| 2331 | |
satok | b66d287 | 2010-11-10 01:04:04 +0900 | [diff] [blame] | 2332 | public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) { |
| 2333 | synchronized (mMethodMap) { |
| 2334 | if (subtype != null && mCurMethodId != null) { |
| 2335 | InputMethodInfo imi = mMethodMap.get(mCurMethodId); |
| 2336 | int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode()); |
| 2337 | if (subtypeId != NOT_A_SUBTYPE_ID) { |
| 2338 | setInputMethodLocked(mCurMethodId, subtypeId); |
| 2339 | return true; |
| 2340 | } |
| 2341 | } |
| 2342 | return false; |
| 2343 | } |
| 2344 | } |
| 2345 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2346 | /** |
| 2347 | * Utility class for putting and getting settings for InputMethod |
| 2348 | * TODO: Move all putters and getters of settings to this class. |
| 2349 | */ |
| 2350 | private static class InputMethodSettings { |
| 2351 | // The string for enabled input method is saved as follows: |
| 2352 | // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0") |
| 2353 | private static final char INPUT_METHOD_SEPARATER = ':'; |
| 2354 | private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';'; |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2355 | private final TextUtils.SimpleStringSplitter mInputMethodSplitter = |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2356 | new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER); |
| 2357 | |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2358 | private final TextUtils.SimpleStringSplitter mSubtypeSplitter = |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2359 | new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER); |
| 2360 | |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2361 | private final Resources mRes; |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2362 | private final ContentResolver mResolver; |
| 2363 | private final HashMap<String, InputMethodInfo> mMethodMap; |
| 2364 | private final ArrayList<InputMethodInfo> mMethodList; |
| 2365 | |
| 2366 | private String mEnabledInputMethodsStrCache; |
| 2367 | |
| 2368 | private static void buildEnabledInputMethodsSettingString( |
| 2369 | StringBuilder builder, Pair<String, ArrayList<String>> pair) { |
| 2370 | String id = pair.first; |
| 2371 | ArrayList<String> subtypes = pair.second; |
| 2372 | builder.append(id); |
satok | 57c767c | 2010-11-01 22:34:08 +0900 | [diff] [blame] | 2373 | // Inputmethod and subtypes are saved in the settings as follows: |
| 2374 | // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1 |
| 2375 | for (String subtypeId: subtypes) { |
| 2376 | builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2377 | } |
| 2378 | } |
| 2379 | |
| 2380 | public InputMethodSettings( |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2381 | Resources res, ContentResolver resolver, |
| 2382 | HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) { |
| 2383 | mRes = res; |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2384 | mResolver = resolver; |
| 2385 | mMethodMap = methodMap; |
| 2386 | mMethodList = methodList; |
| 2387 | } |
| 2388 | |
| 2389 | public List<InputMethodInfo> getEnabledInputMethodListLocked() { |
| 2390 | return createEnabledInputMethodListLocked( |
| 2391 | getEnabledInputMethodsAndSubtypeListLocked()); |
| 2392 | } |
| 2393 | |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2394 | public List<Pair<InputMethodInfo, ArrayList<String>>> |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2395 | getEnabledInputMethodAndSubtypeHashCodeListLocked() { |
| 2396 | return createEnabledInputMethodAndSubtypeHashCodeListLocked( |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2397 | getEnabledInputMethodsAndSubtypeListLocked()); |
| 2398 | } |
| 2399 | |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2400 | public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked( |
| 2401 | InputMethodInfo imi) { |
| 2402 | List<Pair<String, ArrayList<String>>> imsList = |
| 2403 | getEnabledInputMethodsAndSubtypeListLocked(); |
| 2404 | ArrayList<InputMethodSubtype> enabledSubtypes = |
| 2405 | new ArrayList<InputMethodSubtype>(); |
satok | 884ef9a | 2010-11-18 10:39:46 +0900 | [diff] [blame] | 2406 | if (imi != null) { |
| 2407 | for (Pair<String, ArrayList<String>> imsPair : imsList) { |
| 2408 | InputMethodInfo info = mMethodMap.get(imsPair.first); |
| 2409 | if (info != null && info.getId().equals(imi.getId())) { |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2410 | final int subtypeCount = info.getSubtypeCount(); |
| 2411 | for (int i = 0; i < subtypeCount; ++i) { |
| 2412 | InputMethodSubtype ims = info.getSubtypeAt(i); |
satok | 884ef9a | 2010-11-18 10:39:46 +0900 | [diff] [blame] | 2413 | for (String s: imsPair.second) { |
| 2414 | if (String.valueOf(ims.hashCode()).equals(s)) { |
| 2415 | enabledSubtypes.add(ims); |
| 2416 | } |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2417 | } |
| 2418 | } |
satok | 884ef9a | 2010-11-18 10:39:46 +0900 | [diff] [blame] | 2419 | break; |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2420 | } |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2421 | } |
| 2422 | } |
| 2423 | return enabledSubtypes; |
| 2424 | } |
| 2425 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2426 | // At the initial boot, the settings for input methods are not set, |
| 2427 | // so we need to enable IME in that case. |
| 2428 | public void enableAllIMEsIfThereIsNoEnabledIME() { |
| 2429 | if (TextUtils.isEmpty(getEnabledInputMethodsStr())) { |
| 2430 | StringBuilder sb = new StringBuilder(); |
| 2431 | final int N = mMethodList.size(); |
| 2432 | for (int i = 0; i < N; i++) { |
| 2433 | InputMethodInfo imi = mMethodList.get(i); |
| 2434 | Slog.i(TAG, "Adding: " + imi.getId()); |
| 2435 | if (i > 0) sb.append(':'); |
| 2436 | sb.append(imi.getId()); |
| 2437 | } |
| 2438 | putEnabledInputMethodsStr(sb.toString()); |
| 2439 | } |
| 2440 | } |
| 2441 | |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2442 | private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2443 | ArrayList<Pair<String, ArrayList<String>>> imsList |
| 2444 | = new ArrayList<Pair<String, ArrayList<String>>>(); |
| 2445 | final String enabledInputMethodsStr = getEnabledInputMethodsStr(); |
| 2446 | if (TextUtils.isEmpty(enabledInputMethodsStr)) { |
| 2447 | return imsList; |
| 2448 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2449 | mInputMethodSplitter.setString(enabledInputMethodsStr); |
| 2450 | while (mInputMethodSplitter.hasNext()) { |
| 2451 | String nextImsStr = mInputMethodSplitter.next(); |
| 2452 | mSubtypeSplitter.setString(nextImsStr); |
| 2453 | if (mSubtypeSplitter.hasNext()) { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2454 | ArrayList<String> subtypeHashes = new ArrayList<String>(); |
| 2455 | // The first element is ime id. |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2456 | String imeId = mSubtypeSplitter.next(); |
| 2457 | while (mSubtypeSplitter.hasNext()) { |
| 2458 | subtypeHashes.add(mSubtypeSplitter.next()); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2459 | } |
| 2460 | imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes)); |
| 2461 | } |
| 2462 | } |
| 2463 | return imsList; |
| 2464 | } |
| 2465 | |
| 2466 | public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) { |
| 2467 | if (reloadInputMethodStr) { |
| 2468 | getEnabledInputMethodsStr(); |
| 2469 | } |
| 2470 | if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) { |
| 2471 | // Add in the newly enabled input method. |
| 2472 | putEnabledInputMethodsStr(id); |
| 2473 | } else { |
| 2474 | putEnabledInputMethodsStr( |
| 2475 | mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id); |
| 2476 | } |
| 2477 | } |
| 2478 | |
| 2479 | /** |
| 2480 | * Build and put a string of EnabledInputMethods with removing specified Id. |
| 2481 | * @return the specified id was removed or not. |
| 2482 | */ |
| 2483 | public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked( |
| 2484 | StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) { |
| 2485 | boolean isRemoved = false; |
| 2486 | boolean needsAppendSeparator = false; |
| 2487 | for (Pair<String, ArrayList<String>> ims: imsList) { |
| 2488 | String curId = ims.first; |
| 2489 | if (curId.equals(id)) { |
| 2490 | // We are disabling this input method, and it is |
| 2491 | // currently enabled. Skip it to remove from the |
| 2492 | // new list. |
| 2493 | isRemoved = true; |
| 2494 | } else { |
| 2495 | if (needsAppendSeparator) { |
| 2496 | builder.append(INPUT_METHOD_SEPARATER); |
| 2497 | } else { |
| 2498 | needsAppendSeparator = true; |
| 2499 | } |
| 2500 | buildEnabledInputMethodsSettingString(builder, ims); |
| 2501 | } |
| 2502 | } |
| 2503 | if (isRemoved) { |
| 2504 | // Update the setting with the new list of input methods. |
| 2505 | putEnabledInputMethodsStr(builder.toString()); |
| 2506 | } |
| 2507 | return isRemoved; |
| 2508 | } |
| 2509 | |
| 2510 | private List<InputMethodInfo> createEnabledInputMethodListLocked( |
| 2511 | List<Pair<String, ArrayList<String>>> imsList) { |
| 2512 | final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>(); |
| 2513 | for (Pair<String, ArrayList<String>> ims: imsList) { |
| 2514 | InputMethodInfo info = mMethodMap.get(ims.first); |
| 2515 | if (info != null) { |
| 2516 | res.add(info); |
| 2517 | } |
| 2518 | } |
| 2519 | return res; |
| 2520 | } |
| 2521 | |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2522 | private List<Pair<InputMethodInfo, ArrayList<String>>> |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2523 | createEnabledInputMethodAndSubtypeHashCodeListLocked( |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2524 | List<Pair<String, ArrayList<String>>> imsList) { |
| 2525 | final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res |
| 2526 | = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>(); |
| 2527 | for (Pair<String, ArrayList<String>> ims : imsList) { |
| 2528 | InputMethodInfo info = mMethodMap.get(ims.first); |
| 2529 | if (info != null) { |
| 2530 | res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second)); |
| 2531 | } |
| 2532 | } |
| 2533 | return res; |
| 2534 | } |
| 2535 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2536 | private void putEnabledInputMethodsStr(String str) { |
| 2537 | Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str); |
| 2538 | mEnabledInputMethodsStrCache = str; |
| 2539 | } |
| 2540 | |
| 2541 | private String getEnabledInputMethodsStr() { |
| 2542 | mEnabledInputMethodsStrCache = Settings.Secure.getString( |
| 2543 | mResolver, Settings.Secure.ENABLED_INPUT_METHODS); |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2544 | if (DEBUG) { |
| 2545 | Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache); |
| 2546 | } |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2547 | return mEnabledInputMethodsStrCache; |
| 2548 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2549 | |
| 2550 | private void saveSubtypeHistory( |
| 2551 | List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) { |
| 2552 | StringBuilder builder = new StringBuilder(); |
| 2553 | boolean isImeAdded = false; |
| 2554 | if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) { |
| 2555 | builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append( |
| 2556 | newSubtypeId); |
| 2557 | isImeAdded = true; |
| 2558 | } |
| 2559 | for (Pair<String, String> ime: savedImes) { |
| 2560 | String imeId = ime.first; |
| 2561 | String subtypeId = ime.second; |
| 2562 | if (TextUtils.isEmpty(subtypeId)) { |
| 2563 | subtypeId = NOT_A_SUBTYPE_ID_STR; |
| 2564 | } |
| 2565 | if (isImeAdded) { |
| 2566 | builder.append(INPUT_METHOD_SEPARATER); |
| 2567 | } else { |
| 2568 | isImeAdded = true; |
| 2569 | } |
| 2570 | builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append( |
| 2571 | subtypeId); |
| 2572 | } |
| 2573 | // Remove the last INPUT_METHOD_SEPARATER |
| 2574 | putSubtypeHistoryStr(builder.toString()); |
| 2575 | } |
| 2576 | |
| 2577 | public void addSubtypeToHistory(String imeId, String subtypeId) { |
| 2578 | List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked(); |
| 2579 | for (Pair<String, String> ime: subtypeHistory) { |
| 2580 | if (ime.first.equals(imeId)) { |
| 2581 | if (DEBUG) { |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2582 | Slog.v(TAG, "Subtype found in the history: " + imeId + ", " |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2583 | + ime.second); |
| 2584 | } |
| 2585 | // We should break here |
| 2586 | subtypeHistory.remove(ime); |
| 2587 | break; |
| 2588 | } |
| 2589 | } |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2590 | if (DEBUG) { |
| 2591 | Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId); |
| 2592 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2593 | saveSubtypeHistory(subtypeHistory, imeId, subtypeId); |
| 2594 | } |
| 2595 | |
| 2596 | private void putSubtypeHistoryStr(String str) { |
| 2597 | if (DEBUG) { |
| 2598 | Slog.d(TAG, "putSubtypeHistoryStr: " + str); |
| 2599 | } |
| 2600 | Settings.Secure.putString( |
| 2601 | mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str); |
| 2602 | } |
| 2603 | |
| 2604 | public Pair<String, String> getLastInputMethodAndSubtypeLocked() { |
| 2605 | // Gets the first one from the history |
| 2606 | return getLastSubtypeForInputMethodLockedInternal(null); |
| 2607 | } |
| 2608 | |
| 2609 | public String getLastSubtypeForInputMethodLocked(String imeId) { |
| 2610 | Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId); |
| 2611 | if (ime != null) { |
| 2612 | return ime.second; |
| 2613 | } else { |
| 2614 | return null; |
| 2615 | } |
| 2616 | } |
| 2617 | |
| 2618 | private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) { |
| 2619 | List<Pair<String, ArrayList<String>>> enabledImes = |
| 2620 | getEnabledInputMethodsAndSubtypeListLocked(); |
| 2621 | List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked(); |
| 2622 | for (Pair<String, String> imeAndSubtype: subtypeHistory) { |
| 2623 | final String imeInTheHistory = imeAndSubtype.first; |
| 2624 | // If imeId is empty, returns the first IME and subtype in the history |
| 2625 | if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) { |
| 2626 | final String subtypeInTheHistory = imeAndSubtype.second; |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2627 | final String subtypeHashCode = |
| 2628 | getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked( |
| 2629 | enabledImes, imeInTheHistory, subtypeInTheHistory); |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2630 | if (!TextUtils.isEmpty(subtypeHashCode)) { |
| 2631 | if (DEBUG) { |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2632 | Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode); |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2633 | } |
| 2634 | return new Pair<String, String>(imeInTheHistory, subtypeHashCode); |
| 2635 | } |
| 2636 | } |
| 2637 | } |
| 2638 | if (DEBUG) { |
| 2639 | Slog.d(TAG, "No enabled IME found in the history"); |
| 2640 | } |
| 2641 | return null; |
| 2642 | } |
| 2643 | |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2644 | private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String, |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2645 | ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) { |
| 2646 | for (Pair<String, ArrayList<String>> enabledIme: enabledImes) { |
| 2647 | if (enabledIme.first.equals(imeId)) { |
satok | f6cafb6 | 2011-01-17 16:29:02 +0900 | [diff] [blame] | 2648 | final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second; |
| 2649 | if (explicitlyEnabledSubtypes.size() == 0) { |
| 2650 | // If there are no explicitly enabled subtypes, applicable subtypes are |
| 2651 | // enabled implicitly. |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2652 | InputMethodInfo ime = mMethodMap.get(imeId); |
| 2653 | // If IME is enabled and no subtypes are enabled, applicable subtypes |
| 2654 | // are enabled implicitly, so needs to treat them to be enabled. |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2655 | if (ime != null && ime.getSubtypeCount() > 0) { |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2656 | List<InputMethodSubtype> implicitlySelectedSubtypes = |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2657 | getApplicableSubtypesLocked(mRes, getSubtypes(ime)); |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2658 | if (implicitlySelectedSubtypes != null) { |
| 2659 | final int N = implicitlySelectedSubtypes.size(); |
| 2660 | for (int i = 0; i < N; ++i) { |
| 2661 | final InputMethodSubtype st = implicitlySelectedSubtypes.get(i); |
| 2662 | if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) { |
| 2663 | return subtypeHashCode; |
| 2664 | } |
| 2665 | } |
| 2666 | } |
| 2667 | } |
| 2668 | } else { |
satok | f6cafb6 | 2011-01-17 16:29:02 +0900 | [diff] [blame] | 2669 | for (String s: explicitlyEnabledSubtypes) { |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2670 | if (s.equals(subtypeHashCode)) { |
| 2671 | // If both imeId and subtypeId are enabled, return subtypeId. |
| 2672 | return s; |
| 2673 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2674 | } |
| 2675 | } |
| 2676 | // If imeId was enabled but subtypeId was disabled. |
| 2677 | return NOT_A_SUBTYPE_ID_STR; |
| 2678 | } |
| 2679 | } |
| 2680 | // If both imeId and subtypeId are disabled, return null |
| 2681 | return null; |
| 2682 | } |
| 2683 | |
| 2684 | private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() { |
| 2685 | ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>(); |
| 2686 | final String subtypeHistoryStr = getSubtypeHistoryStr(); |
| 2687 | if (TextUtils.isEmpty(subtypeHistoryStr)) { |
| 2688 | return imsList; |
| 2689 | } |
| 2690 | mInputMethodSplitter.setString(subtypeHistoryStr); |
| 2691 | while (mInputMethodSplitter.hasNext()) { |
| 2692 | String nextImsStr = mInputMethodSplitter.next(); |
| 2693 | mSubtypeSplitter.setString(nextImsStr); |
| 2694 | if (mSubtypeSplitter.hasNext()) { |
| 2695 | String subtypeId = NOT_A_SUBTYPE_ID_STR; |
| 2696 | // The first element is ime id. |
| 2697 | String imeId = mSubtypeSplitter.next(); |
| 2698 | while (mSubtypeSplitter.hasNext()) { |
| 2699 | subtypeId = mSubtypeSplitter.next(); |
| 2700 | break; |
| 2701 | } |
| 2702 | imsList.add(new Pair<String, String>(imeId, subtypeId)); |
| 2703 | } |
| 2704 | } |
| 2705 | return imsList; |
| 2706 | } |
| 2707 | |
| 2708 | private String getSubtypeHistoryStr() { |
| 2709 | if (DEBUG) { |
| 2710 | Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString( |
| 2711 | mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY)); |
| 2712 | } |
| 2713 | return Settings.Secure.getString( |
| 2714 | mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY); |
| 2715 | } |
| 2716 | |
| 2717 | public void putSelectedInputMethod(String imeId) { |
| 2718 | Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId); |
| 2719 | } |
| 2720 | |
| 2721 | public void putSelectedSubtype(int subtypeId) { |
| 2722 | Settings.Secure.putInt( |
| 2723 | mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId); |
| 2724 | } |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2725 | } |
| 2726 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2727 | // ---------------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2728 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2729 | @Override |
| 2730 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 2731 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 2732 | != PackageManager.PERMISSION_GRANTED) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2733 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2734 | pw.println("Permission Denial: can't dump InputMethodManager from from pid=" |
| 2735 | + Binder.getCallingPid() |
| 2736 | + ", uid=" + Binder.getCallingUid()); |
| 2737 | return; |
| 2738 | } |
| 2739 | |
| 2740 | IInputMethod method; |
| 2741 | ClientState client; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2742 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2743 | final Printer p = new PrintWriterPrinter(pw); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2744 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2745 | synchronized (mMethodMap) { |
| 2746 | p.println("Current Input Method Manager state:"); |
| 2747 | int N = mMethodList.size(); |
| 2748 | p.println(" Input Methods:"); |
| 2749 | for (int i=0; i<N; i++) { |
| 2750 | InputMethodInfo info = mMethodList.get(i); |
| 2751 | p.println(" InputMethod #" + i + ":"); |
| 2752 | info.dump(p, " "); |
| 2753 | } |
| 2754 | p.println(" Clients:"); |
| 2755 | for (ClientState ci : mClients.values()) { |
| 2756 | p.println(" Client " + ci + ":"); |
| 2757 | p.println(" client=" + ci.client); |
| 2758 | p.println(" inputContext=" + ci.inputContext); |
| 2759 | p.println(" sessionRequested=" + ci.sessionRequested); |
| 2760 | p.println(" curSession=" + ci.curSession); |
| 2761 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2762 | p.println(" mCurMethodId=" + mCurMethodId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2763 | client = mCurClient; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2764 | p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq); |
| 2765 | p.println(" mCurFocusedWindow=" + mCurFocusedWindow); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2766 | p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection |
| 2767 | + " mBoundToMethod=" + mBoundToMethod); |
| 2768 | p.println(" mCurToken=" + mCurToken); |
| 2769 | p.println(" mCurIntent=" + mCurIntent); |
| 2770 | method = mCurMethod; |
| 2771 | p.println(" mCurMethod=" + mCurMethod); |
| 2772 | p.println(" mEnabledSession=" + mEnabledSession); |
| 2773 | p.println(" mShowRequested=" + mShowRequested |
| 2774 | + " mShowExplicitlyRequested=" + mShowExplicitlyRequested |
| 2775 | + " mShowForced=" + mShowForced |
| 2776 | + " mInputShown=" + mInputShown); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 2777 | p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2778 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2779 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2780 | p.println(" "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2781 | if (client != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2782 | pw.flush(); |
| 2783 | try { |
| 2784 | client.client.asBinder().dump(fd, args); |
| 2785 | } catch (RemoteException e) { |
| 2786 | p.println("Input method client dead: " + e); |
| 2787 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2788 | } else { |
| 2789 | p.println("No input method client."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2790 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2791 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2792 | p.println(" "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2793 | if (method != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2794 | pw.flush(); |
| 2795 | try { |
| 2796 | method.asBinder().dump(fd, args); |
| 2797 | } catch (RemoteException e) { |
| 2798 | p.println("Input method service dead: " + e); |
| 2799 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2800 | } else { |
| 2801 | p.println("No input method service."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2802 | } |
| 2803 | } |
| 2804 | } |