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