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