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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | public void addClient(IInputMethodClient client, |
| 559 | IInputContext inputContext, int uid, int pid) { |
| 560 | synchronized (mMethodMap) { |
| 561 | mClients.put(client.asBinder(), new ClientState(client, |
| 562 | inputContext, uid, pid)); |
| 563 | } |
| 564 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 565 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | public void removeClient(IInputMethodClient client) { |
| 567 | synchronized (mMethodMap) { |
| 568 | mClients.remove(client.asBinder()); |
| 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 | void executeOrSendMessage(IInterface target, Message msg) { |
| 573 | if (target.asBinder() instanceof Binder) { |
| 574 | mCaller.sendMessage(msg); |
| 575 | } else { |
| 576 | handleMessage(msg); |
| 577 | msg.recycle(); |
| 578 | } |
| 579 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 580 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 581 | void unbindCurrentClientLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | if (mCurClient != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 583 | if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | + mCurClient.client.asBinder()); |
| 585 | if (mBoundToMethod) { |
| 586 | mBoundToMethod = false; |
| 587 | if (mCurMethod != null) { |
| 588 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageO( |
| 589 | MSG_UNBIND_INPUT, mCurMethod)); |
| 590 | } |
| 591 | } |
| 592 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 593 | MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); |
| 594 | mCurClient.sessionRequested = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 595 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | // Call setActive(false) on the old client |
| 597 | try { |
| 598 | mCurClient.client.setActive(false); |
| 599 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 600 | 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] | 601 | + mCurClient.pid + " uid " + mCurClient.uid); |
| 602 | } |
| 603 | mCurClient = null; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 604 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 605 | hideInputMethodMenuLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | } |
| 607 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 608 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | private int getImeShowFlags() { |
| 610 | int flags = 0; |
| 611 | if (mShowForced) { |
| 612 | flags |= InputMethod.SHOW_FORCED |
| 613 | | InputMethod.SHOW_EXPLICIT; |
| 614 | } else if (mShowExplicitlyRequested) { |
| 615 | flags |= InputMethod.SHOW_EXPLICIT; |
| 616 | } |
| 617 | return flags; |
| 618 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 619 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 620 | private int getAppShowFlags() { |
| 621 | int flags = 0; |
| 622 | if (mShowForced) { |
| 623 | flags |= InputMethodManager.SHOW_FORCED; |
| 624 | } else if (!mShowExplicitlyRequested) { |
| 625 | flags |= InputMethodManager.SHOW_IMPLICIT; |
| 626 | } |
| 627 | return flags; |
| 628 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 629 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 630 | InputBindResult attachNewInputLocked(boolean initial, boolean needResult) { |
| 631 | if (!mBoundToMethod) { |
| 632 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 633 | MSG_BIND_INPUT, mCurMethod, mCurClient.binding)); |
| 634 | mBoundToMethod = true; |
| 635 | } |
| 636 | final SessionState session = mCurClient.curSession; |
| 637 | if (initial) { |
| 638 | executeOrSendMessage(session.method, mCaller.obtainMessageOOO( |
| 639 | MSG_START_INPUT, session, mCurInputContext, mCurAttribute)); |
| 640 | } else { |
| 641 | executeOrSendMessage(session.method, mCaller.obtainMessageOOO( |
| 642 | MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute)); |
| 643 | } |
| 644 | if (mShowRequested) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 645 | 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] | 646 | showCurrentInputLocked(getAppShowFlags(), null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 647 | } |
| 648 | return needResult |
| 649 | ? new InputBindResult(session.session, mCurId, mCurSeq) |
| 650 | : null; |
| 651 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 652 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 653 | InputBindResult startInputLocked(IInputMethodClient client, |
| 654 | IInputContext inputContext, EditorInfo attribute, |
| 655 | boolean initial, boolean needResult) { |
| 656 | // If no method is currently selected, do nothing. |
| 657 | if (mCurMethodId == null) { |
| 658 | return mNoBinding; |
| 659 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 660 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 661 | ClientState cs = mClients.get(client.asBinder()); |
| 662 | if (cs == null) { |
| 663 | throw new IllegalArgumentException("unknown client " |
| 664 | + client.asBinder()); |
| 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 | try { |
| 668 | if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) { |
| 669 | // Check with the window manager to make sure this client actually |
| 670 | // has a window with focus. If not, reject. This is thread safe |
| 671 | // because if the focus changes some time before or after, the |
| 672 | // next client receiving focus that has any interest in input will |
| 673 | // be calling through here after that change happens. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 674 | 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] | 675 | + " (uid=" + cs.uid + " pid=" + cs.pid + ")"); |
| 676 | return null; |
| 677 | } |
| 678 | } catch (RemoteException e) { |
| 679 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 680 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | if (mCurClient != cs) { |
| 682 | // If the client is changing, we need to switch over to the new |
| 683 | // one. |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 684 | unbindCurrentClientLocked(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 685 | if (DEBUG) Slog.v(TAG, "switching to client: client = " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | + cs.client.asBinder()); |
| 687 | |
| 688 | // If the screen is on, inform the new client it is active |
| 689 | if (mScreenOn) { |
| 690 | try { |
| 691 | cs.client.setActive(mScreenOn); |
| 692 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 693 | 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] | 694 | + cs.pid + " uid " + cs.uid); |
| 695 | } |
| 696 | } |
| 697 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 698 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 699 | // Bump up the sequence for this client and attach it. |
| 700 | mCurSeq++; |
| 701 | if (mCurSeq <= 0) mCurSeq = 1; |
| 702 | mCurClient = cs; |
| 703 | mCurInputContext = inputContext; |
| 704 | mCurAttribute = attribute; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 705 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | // Check if the input method is changing. |
| 707 | if (mCurId != null && mCurId.equals(mCurMethodId)) { |
| 708 | if (cs.curSession != null) { |
| 709 | // Fast case: if we are already connected to the input method, |
| 710 | // then just return it. |
| 711 | return attachNewInputLocked(initial, needResult); |
| 712 | } |
| 713 | if (mHaveConnection) { |
| 714 | if (mCurMethod != null) { |
| 715 | if (!cs.sessionRequested) { |
| 716 | cs.sessionRequested = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 717 | 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] | 718 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 719 | MSG_CREATE_SESSION, mCurMethod, |
| 720 | new MethodCallback(mCurMethod))); |
| 721 | } |
| 722 | // Return to client, and we will get back with it when |
| 723 | // we have had a session made for it. |
| 724 | return new InputBindResult(null, mCurId, mCurSeq); |
| 725 | } else if (SystemClock.uptimeMillis() |
| 726 | < (mLastBindTime+TIME_TO_RECONNECT)) { |
| 727 | // In this case we have connected to the service, but |
| 728 | // don't yet have its interface. If it hasn't been too |
| 729 | // long since we did the connection, we'll return to |
| 730 | // the client and wait to get the service interface so |
| 731 | // we can report back. If it has been too long, we want |
| 732 | // to fall through so we can try a disconnect/reconnect |
| 733 | // to see if we can get back in touch with the service. |
| 734 | return new InputBindResult(null, mCurId, mCurSeq); |
| 735 | } else { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 736 | EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, |
| 737 | mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 741 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 742 | return startInputInnerLocked(); |
| 743 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 744 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 745 | InputBindResult startInputInnerLocked() { |
| 746 | if (mCurMethodId == null) { |
| 747 | return mNoBinding; |
| 748 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 749 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 750 | if (!mSystemReady) { |
| 751 | // If the system is not yet ready, we shouldn't be running third |
| 752 | // party code. |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 753 | return new InputBindResult(null, mCurMethodId, mCurSeq); |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 754 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 755 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 756 | InputMethodInfo info = mMethodMap.get(mCurMethodId); |
| 757 | if (info == null) { |
| 758 | throw new IllegalArgumentException("Unknown id: " + mCurMethodId); |
| 759 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 760 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 761 | unbindCurrentMethodLocked(false); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 762 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 763 | mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE); |
| 764 | mCurIntent.setComponent(info.getComponent()); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 765 | mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL, |
| 766 | com.android.internal.R.string.input_method_binding_label); |
| 767 | mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity( |
| 768 | 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] | 769 | if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) { |
| 770 | mLastBindTime = SystemClock.uptimeMillis(); |
| 771 | mHaveConnection = true; |
| 772 | mCurId = info.getId(); |
| 773 | mCurToken = new Binder(); |
| 774 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 775 | if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | mIWindowManager.addWindowToken(mCurToken, |
| 777 | WindowManager.LayoutParams.TYPE_INPUT_METHOD); |
| 778 | } catch (RemoteException e) { |
| 779 | } |
| 780 | return new InputBindResult(null, mCurId, mCurSeq); |
| 781 | } else { |
| 782 | mCurIntent = null; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 783 | Slog.w(TAG, "Failure connecting to input method service: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 784 | + mCurIntent); |
| 785 | } |
| 786 | return null; |
| 787 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 788 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | public InputBindResult startInput(IInputMethodClient client, |
| 790 | IInputContext inputContext, EditorInfo attribute, |
| 791 | boolean initial, boolean needResult) { |
| 792 | synchronized (mMethodMap) { |
| 793 | final long ident = Binder.clearCallingIdentity(); |
| 794 | try { |
| 795 | return startInputLocked(client, inputContext, attribute, |
| 796 | initial, needResult); |
| 797 | } finally { |
| 798 | Binder.restoreCallingIdentity(ident); |
| 799 | } |
| 800 | } |
| 801 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 802 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 803 | public void finishInput(IInputMethodClient client) { |
| 804 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 805 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 806 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 807 | synchronized (mMethodMap) { |
| 808 | if (mCurIntent != null && name.equals(mCurIntent.getComponent())) { |
| 809 | mCurMethod = IInputMethod.Stub.asInterface(service); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 810 | if (mCurToken == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 811 | Slog.w(TAG, "Service connected without a token!"); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 812 | unbindCurrentMethodLocked(false); |
| 813 | return; |
| 814 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 815 | if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 816 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 817 | MSG_ATTACH_TOKEN, mCurMethod, mCurToken)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | if (mCurClient != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 819 | if (DEBUG) Slog.v(TAG, "Creating first session while with client " |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 820 | + mCurClient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 822 | MSG_CREATE_SESSION, mCurMethod, |
| 823 | new MethodCallback(mCurMethod))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | void onSessionCreated(IInputMethod method, IInputMethodSession session) { |
| 830 | synchronized (mMethodMap) { |
| 831 | if (mCurMethod != null && method != null |
| 832 | && mCurMethod.asBinder() == method.asBinder()) { |
| 833 | if (mCurClient != null) { |
| 834 | mCurClient.curSession = new SessionState(mCurClient, |
| 835 | method, session); |
| 836 | mCurClient.sessionRequested = false; |
| 837 | InputBindResult res = attachNewInputLocked(true, true); |
| 838 | if (res.method != null) { |
| 839 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO( |
| 840 | MSG_BIND_METHOD, mCurClient.client, res)); |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 846 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 847 | void unbindCurrentMethodLocked(boolean reportToClient) { |
| 848 | if (mHaveConnection) { |
| 849 | mContext.unbindService(this); |
| 850 | mHaveConnection = false; |
| 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 | if (mCurToken != null) { |
| 854 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 855 | if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 856 | mIWindowManager.removeWindowToken(mCurToken); |
| 857 | } catch (RemoteException e) { |
| 858 | } |
| 859 | mCurToken = null; |
| 860 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 861 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 862 | mCurId = null; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 863 | clearCurMethodLocked(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 864 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 865 | if (reportToClient && mCurClient != null) { |
| 866 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 867 | MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); |
| 868 | } |
| 869 | } |
| 870 | |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 871 | private void finishSession(SessionState sessionState) { |
| 872 | if (sessionState != null && sessionState.session != null) { |
| 873 | try { |
| 874 | sessionState.session.finishSession(); |
| 875 | } catch (RemoteException e) { |
Jean-Baptiste Queru | 9d0f6df | 2010-03-29 12:55:09 -0700 | [diff] [blame] | 876 | Slog.w(TAG, "Session failed to close due to remote exception", e); |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 880 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 881 | void clearCurMethodLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 882 | if (mCurMethod != null) { |
| 883 | for (ClientState cs : mClients.values()) { |
| 884 | cs.sessionRequested = false; |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 885 | finishSession(cs.curSession); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | cs.curSession = null; |
| 887 | } |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 888 | |
| 889 | finishSession(mEnabledSession); |
| 890 | mEnabledSession = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 891 | mCurMethod = null; |
| 892 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 893 | mStatusBar.setIconVisibility("ime", false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 894 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 895 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 896 | public void onServiceDisconnected(ComponentName name) { |
| 897 | synchronized (mMethodMap) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 898 | if (DEBUG) Slog.v(TAG, "Service disconnected: " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 899 | + " mCurIntent=" + mCurIntent); |
| 900 | if (mCurMethod != null && mCurIntent != null |
| 901 | && name.equals(mCurIntent.getComponent())) { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 902 | clearCurMethodLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 903 | // We consider this to be a new bind attempt, since the system |
| 904 | // should now try to restart the service for us. |
| 905 | mLastBindTime = SystemClock.uptimeMillis(); |
| 906 | mShowRequested = mInputShown; |
| 907 | mInputShown = false; |
| 908 | if (mCurClient != null) { |
| 909 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 910 | MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | public void updateStatusIcon(IBinder token, String packageName, int iconId) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 917 | int uid = Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | long ident = Binder.clearCallingIdentity(); |
| 919 | try { |
| 920 | if (token == null || mCurToken != token) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 921 | 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] | 922 | return; |
| 923 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 924 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | synchronized (mMethodMap) { |
| 926 | if (iconId == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 927 | 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] | 928 | mStatusBar.setIconVisibility("ime", false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | } else if (packageName != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 930 | 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] | 931 | mStatusBar.setIcon("ime", packageName, iconId, 0); |
| 932 | mStatusBar.setIconVisibility("ime", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | } finally { |
| 936 | Binder.restoreCallingIdentity(ident); |
| 937 | } |
| 938 | } |
| 939 | |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 940 | public void setIMEButtonVisible(IBinder token, boolean visible) { |
| 941 | int uid = Binder.getCallingUid(); |
| 942 | long ident = Binder.clearCallingIdentity(); |
| 943 | try { |
| 944 | if (token == null || mCurToken != token) { |
| 945 | Slog.w(TAG, "Ignoring setIMEButtonVisible of uid " + uid + " token: " + token); |
| 946 | return; |
| 947 | } |
| 948 | |
| 949 | synchronized (mMethodMap) { |
| 950 | mStatusBar.setIMEButtonVisible(visible); |
| 951 | } |
| 952 | } finally { |
| 953 | Binder.restoreCallingIdentity(ident); |
| 954 | } |
| 955 | } |
| 956 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 957 | void updateFromSettingsLocked() { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 958 | // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and |
| 959 | // ENABLED_INPUT_METHODS is taking care of keeping them correctly in |
| 960 | // sync, so we will never have a DEFAULT_INPUT_METHOD that is not |
| 961 | // enabled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 962 | String id = Settings.Secure.getString(mContext.getContentResolver(), |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 963 | Settings.Secure.DEFAULT_INPUT_METHOD); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 964 | if (id != null && id.length() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 965 | try { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 966 | setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 967 | } catch (IllegalArgumentException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 968 | 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] | 969 | mCurMethodId = null; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 970 | unbindCurrentMethodLocked(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 971 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 972 | } else { |
| 973 | // 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] | 974 | mCurMethodId = null; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 975 | unbindCurrentMethodLocked(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 976 | } |
| 977 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 978 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 979 | /* package */ void setInputMethodLocked(String id, int subtypeId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 980 | InputMethodInfo info = mMethodMap.get(id); |
| 981 | if (info == null) { |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 982 | throw new IllegalArgumentException("Unknown id: " + id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 983 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 984 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 985 | if (id.equals(mCurMethodId)) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 986 | if (subtypeId != NOT_A_SUBTYPE_ID) { |
| 987 | InputMethodSubtype subtype = info.getSubtypes().get(subtypeId); |
| 988 | if (subtype != mCurrentSubtype) { |
| 989 | synchronized (mMethodMap) { |
| 990 | if (mCurMethod != null) { |
| 991 | try { |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame^] | 992 | setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true); |
satok | 06e0744 | 2010-11-02 19:46:55 +0900 | [diff] [blame] | 993 | if (mInputShown) { |
| 994 | // If mInputShown is false, there is no IME button on the |
| 995 | // system bar. |
| 996 | // Thus there is no need to make it invisible explicitly. |
| 997 | mStatusBar.setIMEButtonVisible(true); |
| 998 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 999 | mCurMethod.changeInputMethodSubtype(subtype); |
| 1000 | } catch (RemoteException e) { |
| 1001 | return; |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1007 | return; |
| 1008 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1009 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1010 | final long ident = Binder.clearCallingIdentity(); |
| 1011 | try { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1012 | // Set a subtype to this input method. |
| 1013 | // subtypeId the name of a subtype which will be set. |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame^] | 1014 | setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false); |
| 1015 | // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked() |
| 1016 | // because mCurMethodId is stored as a history in |
| 1017 | // setSelectedInputMethodAndSubtypeLocked(). |
| 1018 | mCurMethodId = id; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1019 | |
| 1020 | if (ActivityManagerNative.isSystemReady()) { |
| 1021 | Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1022 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | intent.putExtra("input_method_id", id); |
| 1024 | mContext.sendBroadcast(intent); |
| 1025 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1026 | unbindCurrentClientLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1027 | } finally { |
| 1028 | Binder.restoreCallingIdentity(ident); |
| 1029 | } |
| 1030 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1031 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1032 | public boolean showSoftInput(IInputMethodClient client, int flags, |
| 1033 | ResultReceiver resultReceiver) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1034 | int uid = Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1035 | long ident = Binder.clearCallingIdentity(); |
| 1036 | try { |
| 1037 | synchronized (mMethodMap) { |
| 1038 | if (mCurClient == null || client == null |
| 1039 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1040 | try { |
| 1041 | // We need to check if this is the current client with |
| 1042 | // focus in the window manager, to allow this call to |
| 1043 | // be made before input is started in it. |
| 1044 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1045 | Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1046 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1047 | } |
| 1048 | } catch (RemoteException e) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1049 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1050 | } |
| 1051 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1052 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1053 | 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] | 1054 | return showCurrentInputLocked(flags, resultReceiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1055 | } |
| 1056 | } finally { |
| 1057 | Binder.restoreCallingIdentity(ident); |
| 1058 | } |
| 1059 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1060 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1061 | boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1062 | mShowRequested = true; |
| 1063 | if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) { |
| 1064 | mShowExplicitlyRequested = true; |
| 1065 | } |
| 1066 | if ((flags&InputMethodManager.SHOW_FORCED) != 0) { |
| 1067 | mShowExplicitlyRequested = true; |
| 1068 | mShowForced = true; |
| 1069 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1070 | |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 1071 | if (!mSystemReady) { |
| 1072 | return false; |
| 1073 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1074 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1075 | boolean res = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1076 | if (mCurMethod != null) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1077 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO( |
| 1078 | MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod, |
| 1079 | resultReceiver)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | mInputShown = true; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1081 | res = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1082 | } else if (mHaveConnection && SystemClock.uptimeMillis() |
| 1083 | < (mLastBindTime+TIME_TO_RECONNECT)) { |
| 1084 | // The client has asked to have the input method shown, but |
| 1085 | // we have been sitting here too long with a connection to the |
| 1086 | // service and no interface received, so let's disconnect/connect |
| 1087 | // to try to prod things along. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1088 | EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1089 | SystemClock.uptimeMillis()-mLastBindTime,1); |
| 1090 | mContext.unbindService(this); |
| 1091 | mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE); |
| 1092 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1093 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1094 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1095 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1096 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1097 | public boolean hideSoftInput(IInputMethodClient client, int flags, |
| 1098 | ResultReceiver resultReceiver) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1099 | int uid = Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1100 | long ident = Binder.clearCallingIdentity(); |
| 1101 | try { |
| 1102 | synchronized (mMethodMap) { |
| 1103 | if (mCurClient == null || client == null |
| 1104 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1105 | try { |
| 1106 | // We need to check if this is the current client with |
| 1107 | // focus in the window manager, to allow this call to |
| 1108 | // be made before input is started in it. |
| 1109 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1110 | if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid " |
| 1111 | + uid + ": " + client); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1112 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1113 | } |
| 1114 | } catch (RemoteException e) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1115 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1116 | } |
| 1117 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1118 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1119 | 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] | 1120 | return hideCurrentInputLocked(flags, resultReceiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1121 | } |
| 1122 | } finally { |
| 1123 | Binder.restoreCallingIdentity(ident); |
| 1124 | } |
| 1125 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1126 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1127 | boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1128 | if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0 |
| 1129 | && (mShowExplicitlyRequested || mShowForced)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1130 | if (DEBUG) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1131 | "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] | 1132 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1133 | } |
| 1134 | if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1135 | if (DEBUG) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1136 | "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] | 1137 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1138 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1139 | boolean res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1140 | if (mInputShown && mCurMethod != null) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1141 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 1142 | MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver)); |
| 1143 | res = true; |
| 1144 | } else { |
| 1145 | res = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1146 | } |
| 1147 | mInputShown = false; |
| 1148 | mShowRequested = false; |
| 1149 | mShowExplicitlyRequested = false; |
| 1150 | mShowForced = false; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1151 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1152 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1153 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1154 | public void windowGainedFocus(IInputMethodClient client, IBinder windowToken, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1155 | boolean viewHasFocus, boolean isTextEditor, int softInputMode, |
| 1156 | boolean first, int windowFlags) { |
| 1157 | long ident = Binder.clearCallingIdentity(); |
| 1158 | try { |
| 1159 | synchronized (mMethodMap) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1160 | if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1161 | + " viewHasFocus=" + viewHasFocus |
| 1162 | + " isTextEditor=" + isTextEditor |
| 1163 | + " softInputMode=#" + Integer.toHexString(softInputMode) |
| 1164 | + " first=" + first + " flags=#" |
| 1165 | + Integer.toHexString(windowFlags)); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1166 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1167 | if (mCurClient == null || client == null |
| 1168 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1169 | try { |
| 1170 | // We need to check if this is the current client with |
| 1171 | // focus in the window manager, to allow this call to |
| 1172 | // be made before input is started in it. |
| 1173 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1174 | 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] | 1175 | return; |
| 1176 | } |
| 1177 | } catch (RemoteException e) { |
| 1178 | } |
| 1179 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1180 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1181 | if (mCurFocusedWindow == windowToken) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1182 | 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] | 1183 | return; |
| 1184 | } |
| 1185 | mCurFocusedWindow = windowToken; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1186 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1187 | switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) { |
| 1188 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED: |
| 1189 | if (!isTextEditor || (softInputMode & |
| 1190 | WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
| 1191 | != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) { |
| 1192 | if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) { |
| 1193 | // There is no focus view, and this window will |
| 1194 | // be behind any soft input window, so hide the |
| 1195 | // soft input window if it is shown. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1196 | 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] | 1197 | hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1198 | } |
| 1199 | } else if (isTextEditor && (softInputMode & |
| 1200 | WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
| 1201 | == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
| 1202 | && (softInputMode & |
| 1203 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
| 1204 | // There is a focus view, and we are navigating forward |
| 1205 | // into the window, so show the input window for the user. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1206 | 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] | 1207 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1208 | } |
| 1209 | break; |
| 1210 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED: |
| 1211 | // Do nothing. |
| 1212 | break; |
| 1213 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN: |
| 1214 | if ((softInputMode & |
| 1215 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1216 | 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] | 1217 | hideCurrentInputLocked(0, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1218 | } |
| 1219 | break; |
| 1220 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN: |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1221 | 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] | 1222 | hideCurrentInputLocked(0, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1223 | break; |
| 1224 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE: |
| 1225 | if ((softInputMode & |
| 1226 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1227 | 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] | 1228 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1229 | } |
| 1230 | break; |
| 1231 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE: |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1232 | 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] | 1233 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1234 | break; |
| 1235 | } |
| 1236 | } |
| 1237 | } finally { |
| 1238 | Binder.restoreCallingIdentity(ident); |
| 1239 | } |
| 1240 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1241 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1242 | public void showInputMethodPickerFromClient(IInputMethodClient client) { |
| 1243 | synchronized (mMethodMap) { |
| 1244 | if (mCurClient == null || client == null |
| 1245 | || mCurClient.client.asBinder() != client.asBinder()) { |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1246 | Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid " |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1247 | + Binder.getCallingUid() + ": " + client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | mHandler.sendEmptyMessage(MSG_SHOW_IM_PICKER); |
| 1251 | } |
| 1252 | } |
| 1253 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1254 | public void showInputMethodSubtypePickerFromClient(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 showInputMethodSubtypePickerFromClient of: " + client); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER); |
| 1262 | } |
| 1263 | } |
| 1264 | |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1265 | public void showInputMethodAndSubtypeEnablerFromClient( |
| 1266 | IInputMethodClient client, String topId) { |
| 1267 | // TODO: Handle topId for setting the top position of the list activity |
| 1268 | synchronized (mMethodMap) { |
| 1269 | if (mCurClient == null || client == null |
| 1270 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1271 | Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client); |
| 1272 | } |
| 1273 | |
| 1274 | mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_ENABLER); |
| 1275 | } |
| 1276 | } |
| 1277 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1278 | public void setInputMethod(IBinder token, String id) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1279 | setInputMethodWithSubtype(token, id, NOT_A_SUBTYPE_ID); |
| 1280 | } |
| 1281 | |
| 1282 | private void setInputMethodWithSubtype(IBinder token, String id, int subtypeId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1283 | synchronized (mMethodMap) { |
| 1284 | if (token == null) { |
| 1285 | if (mContext.checkCallingOrSelfPermission( |
| 1286 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1287 | != PackageManager.PERMISSION_GRANTED) { |
| 1288 | throw new SecurityException( |
| 1289 | "Using null token requires permission " |
| 1290 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 1291 | } |
| 1292 | } else if (mCurToken != token) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1293 | Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid() |
| 1294 | + " token: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1295 | return; |
| 1296 | } |
| 1297 | |
| 1298 | long ident = Binder.clearCallingIdentity(); |
| 1299 | try { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1300 | setInputMethodLocked(id, subtypeId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1301 | } finally { |
| 1302 | Binder.restoreCallingIdentity(ident); |
| 1303 | } |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | public void hideMySoftInput(IBinder token, int flags) { |
| 1308 | synchronized (mMethodMap) { |
| 1309 | if (token == null || mCurToken != token) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1310 | if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid " |
| 1311 | + Binder.getCallingUid() + " token: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1312 | return; |
| 1313 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1314 | long ident = Binder.clearCallingIdentity(); |
| 1315 | try { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1316 | hideCurrentInputLocked(flags, null); |
| 1317 | } finally { |
| 1318 | Binder.restoreCallingIdentity(ident); |
| 1319 | } |
| 1320 | } |
| 1321 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1322 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1323 | public void showMySoftInput(IBinder token, int flags) { |
| 1324 | synchronized (mMethodMap) { |
| 1325 | if (token == null || mCurToken != token) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1326 | Slog.w(TAG, "Ignoring showMySoftInput of uid " |
| 1327 | + Binder.getCallingUid() + " token: " + token); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1328 | return; |
| 1329 | } |
| 1330 | long ident = Binder.clearCallingIdentity(); |
| 1331 | try { |
| 1332 | showCurrentInputLocked(flags, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1333 | } finally { |
| 1334 | Binder.restoreCallingIdentity(ident); |
| 1335 | } |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | void setEnabledSessionInMainThread(SessionState session) { |
| 1340 | if (mEnabledSession != session) { |
| 1341 | if (mEnabledSession != null) { |
| 1342 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1343 | if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1344 | mEnabledSession.method.setSessionEnabled( |
| 1345 | mEnabledSession.session, false); |
| 1346 | } catch (RemoteException e) { |
| 1347 | } |
| 1348 | } |
| 1349 | mEnabledSession = session; |
| 1350 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1351 | if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1352 | session.method.setSessionEnabled( |
| 1353 | session.session, true); |
| 1354 | } catch (RemoteException e) { |
| 1355 | } |
| 1356 | } |
| 1357 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1358 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1359 | public boolean handleMessage(Message msg) { |
| 1360 | HandlerCaller.SomeArgs args; |
| 1361 | switch (msg.what) { |
| 1362 | case MSG_SHOW_IM_PICKER: |
| 1363 | showInputMethodMenu(); |
| 1364 | return true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1365 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1366 | case MSG_SHOW_IM_SUBTYPE_PICKER: |
| 1367 | showInputMethodSubtypeMenu(); |
| 1368 | return true; |
| 1369 | |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1370 | case MSG_SHOW_IM_SUBTYPE_ENABLER: |
| 1371 | showInputMethodAndSubtypeEnabler(); |
| 1372 | return true; |
| 1373 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1374 | // --------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1375 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1376 | case MSG_UNBIND_INPUT: |
| 1377 | try { |
| 1378 | ((IInputMethod)msg.obj).unbindInput(); |
| 1379 | } catch (RemoteException e) { |
| 1380 | // There is nothing interesting about the method dying. |
| 1381 | } |
| 1382 | return true; |
| 1383 | case MSG_BIND_INPUT: |
| 1384 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1385 | try { |
| 1386 | ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2); |
| 1387 | } catch (RemoteException e) { |
| 1388 | } |
| 1389 | return true; |
| 1390 | case MSG_SHOW_SOFT_INPUT: |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1391 | args = (HandlerCaller.SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1392 | try { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1393 | ((IInputMethod)args.arg1).showSoftInput(msg.arg1, |
| 1394 | (ResultReceiver)args.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1395 | } catch (RemoteException e) { |
| 1396 | } |
| 1397 | return true; |
| 1398 | case MSG_HIDE_SOFT_INPUT: |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1399 | args = (HandlerCaller.SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1400 | try { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1401 | ((IInputMethod)args.arg1).hideSoftInput(0, |
| 1402 | (ResultReceiver)args.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1403 | } catch (RemoteException e) { |
| 1404 | } |
| 1405 | return true; |
| 1406 | case MSG_ATTACH_TOKEN: |
| 1407 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1408 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1409 | 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] | 1410 | ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2); |
| 1411 | } catch (RemoteException e) { |
| 1412 | } |
| 1413 | return true; |
| 1414 | case MSG_CREATE_SESSION: |
| 1415 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1416 | try { |
| 1417 | ((IInputMethod)args.arg1).createSession( |
| 1418 | (IInputMethodCallback)args.arg2); |
| 1419 | } catch (RemoteException e) { |
| 1420 | } |
| 1421 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1422 | // --------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1423 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1424 | case MSG_START_INPUT: |
| 1425 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1426 | try { |
| 1427 | SessionState session = (SessionState)args.arg1; |
| 1428 | setEnabledSessionInMainThread(session); |
| 1429 | session.method.startInput((IInputContext)args.arg2, |
| 1430 | (EditorInfo)args.arg3); |
| 1431 | } catch (RemoteException e) { |
| 1432 | } |
| 1433 | return true; |
| 1434 | case MSG_RESTART_INPUT: |
| 1435 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1436 | try { |
| 1437 | SessionState session = (SessionState)args.arg1; |
| 1438 | setEnabledSessionInMainThread(session); |
| 1439 | session.method.restartInput((IInputContext)args.arg2, |
| 1440 | (EditorInfo)args.arg3); |
| 1441 | } catch (RemoteException e) { |
| 1442 | } |
| 1443 | return true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1444 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1445 | // --------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1446 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1447 | case MSG_UNBIND_METHOD: |
| 1448 | try { |
| 1449 | ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1); |
| 1450 | } catch (RemoteException e) { |
| 1451 | // There is nothing interesting about the last client dying. |
| 1452 | } |
| 1453 | return true; |
| 1454 | case MSG_BIND_METHOD: |
| 1455 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1456 | try { |
| 1457 | ((IInputMethodClient)args.arg1).onBindMethod( |
| 1458 | (InputBindResult)args.arg2); |
| 1459 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1460 | 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] | 1461 | } |
| 1462 | return true; |
| 1463 | } |
| 1464 | return false; |
| 1465 | } |
| 1466 | |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1467 | private boolean isSystemIme(InputMethodInfo inputMethod) { |
| 1468 | return (inputMethod.getServiceInfo().applicationInfo.flags |
| 1469 | & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 1470 | } |
| 1471 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1472 | private boolean chooseNewDefaultIMELocked() { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1473 | List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked(); |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1474 | if (enabled != null && enabled.size() > 0) { |
Dianne Hackborn | 83e48f5 | 2010-03-23 23:03:25 -0700 | [diff] [blame] | 1475 | // We'd prefer to fall back on a system IME, since that is safer. |
| 1476 | int i=enabled.size(); |
| 1477 | while (i > 0) { |
| 1478 | i--; |
| 1479 | if ((enabled.get(i).getServiceInfo().applicationInfo.flags |
| 1480 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 1481 | break; |
| 1482 | } |
| 1483 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1484 | InputMethodInfo imi = enabled.get(i); |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame^] | 1485 | resetSelectedInputMethodAndSubtypeLocked(imi.getId()); |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1486 | return true; |
| 1487 | } |
| 1488 | |
| 1489 | return false; |
| 1490 | } |
| 1491 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1492 | void buildInputMethodListLocked(ArrayList<InputMethodInfo> list, |
| 1493 | HashMap<String, InputMethodInfo> map) { |
| 1494 | list.clear(); |
| 1495 | map.clear(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1496 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1497 | PackageManager pm = mContext.getPackageManager(); |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 1498 | final Configuration config = mContext.getResources().getConfiguration(); |
| 1499 | final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY; |
| 1500 | String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(), |
| 1501 | Secure.DISABLED_SYSTEM_INPUT_METHODS); |
| 1502 | if (disabledSysImes == null) disabledSysImes = ""; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1503 | |
| 1504 | List<ResolveInfo> services = pm.queryIntentServices( |
| 1505 | new Intent(InputMethod.SERVICE_INTERFACE), |
| 1506 | PackageManager.GET_META_DATA); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1507 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1508 | for (int i = 0; i < services.size(); ++i) { |
| 1509 | ResolveInfo ri = services.get(i); |
| 1510 | ServiceInfo si = ri.serviceInfo; |
| 1511 | ComponentName compName = new ComponentName(si.packageName, si.name); |
| 1512 | if (!android.Manifest.permission.BIND_INPUT_METHOD.equals( |
| 1513 | si.permission)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1514 | Slog.w(TAG, "Skipping input method " + compName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1515 | + ": it does not require the permission " |
| 1516 | + android.Manifest.permission.BIND_INPUT_METHOD); |
| 1517 | continue; |
| 1518 | } |
| 1519 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1520 | if (DEBUG) Slog.d(TAG, "Checking " + compName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1521 | |
| 1522 | try { |
| 1523 | InputMethodInfo p = new InputMethodInfo(mContext, ri); |
| 1524 | list.add(p); |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 1525 | final String id = p.getId(); |
| 1526 | map.put(id, p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1527 | |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 1528 | // System IMEs are enabled by default, unless there's a hard keyboard |
| 1529 | // and the system IME was explicitly disabled |
| 1530 | if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) { |
| 1531 | setInputMethodEnabledLocked(id, true); |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1532 | } |
| 1533 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1534 | if (DEBUG) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1535 | 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] | 1536 | } |
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 | } catch (XmlPullParserException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1539 | 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] | 1540 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1541 | 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] | 1542 | } |
| 1543 | } |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1544 | |
| 1545 | String defaultIme = Settings.Secure.getString(mContext |
| 1546 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 1547 | if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1548 | if (chooseNewDefaultIMELocked()) { |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1549 | updateFromSettingsLocked(); |
| 1550 | } |
| 1551 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1552 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1553 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1554 | // ---------------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1555 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1556 | private void showInputMethodMenu() { |
| 1557 | showInputMethodMenuInternal(false); |
| 1558 | } |
| 1559 | |
| 1560 | private void showInputMethodSubtypeMenu() { |
| 1561 | showInputMethodMenuInternal(true); |
| 1562 | } |
| 1563 | |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1564 | private void showInputMethodAndSubtypeEnabler() { |
satok | 86417ea | 2010-10-27 14:11:03 +0900 | [diff] [blame] | 1565 | Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_AND_SUBTYPE_ENABLER); |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1566 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
satok | 86417ea | 2010-10-27 14:11:03 +0900 | [diff] [blame] | 1567 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
| 1568 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1569 | mContext.startActivity(intent); |
| 1570 | } |
| 1571 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1572 | private void showInputMethodMenuInternal(boolean showSubtypes) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1573 | if (DEBUG) Slog.v(TAG, "Show switching menu"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1574 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1575 | final Context context = mContext; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1576 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1577 | final PackageManager pm = context.getPackageManager(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1578 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1579 | String lastInputMethodId = Settings.Secure.getString(context |
| 1580 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1581 | int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1582 | if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1583 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1584 | synchronized (mMethodMap) { |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1585 | final List<Pair<InputMethodInfo, ArrayList<String>>> immis = |
| 1586 | mSettings.getEnabledInputMethodAndSubtypeListLocked(); |
| 1587 | ArrayList<Integer> subtypeIds = new ArrayList<Integer>(); |
| 1588 | |
| 1589 | if (immis == null || immis.size() == 0) { |
| 1590 | return; |
| 1591 | } |
| 1592 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1593 | hideInputMethodMenuLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1594 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1595 | int N = immis.size(); |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 1596 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1597 | final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap = |
| 1598 | new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance()); |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 1599 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1600 | for (int i = 0; i < N; ++i) { |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1601 | InputMethodInfo property = immis.get(i).first; |
| 1602 | final ArrayList<String> enabledSubtypeIds = immis.get(i).second; |
| 1603 | HashSet<String> enabledSubtypeSet = new HashSet<String>(); |
| 1604 | for (String s : enabledSubtypeIds) { |
| 1605 | enabledSubtypeSet.add(s); |
| 1606 | } |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1607 | if (property == null) { |
| 1608 | continue; |
| 1609 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1610 | ArrayList<InputMethodSubtype> subtypes = property.getSubtypes(); |
| 1611 | CharSequence label = property.loadLabel(pm); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1612 | if (showSubtypes && enabledSubtypeSet.size() > 0) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1613 | for (int j = 0; j < subtypes.size(); ++j) { |
| 1614 | InputMethodSubtype subtype = subtypes.get(j); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1615 | if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) { |
| 1616 | CharSequence title; |
| 1617 | int nameResId = subtype.getNameResId(); |
satok | 9ef0283 | 2010-11-04 21:17:48 +0900 | [diff] [blame] | 1618 | String mode = subtype.getMode(); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1619 | if (nameResId != 0) { |
| 1620 | title = pm.getText(property.getPackageName(), nameResId, |
| 1621 | property.getServiceInfo().applicationInfo); |
| 1622 | } else { |
| 1623 | CharSequence language = subtype.getLocale(); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1624 | // TODO: Use more friendly Title and UI |
| 1625 | title = label + "," + (mode == null ? "" : mode) + "," |
| 1626 | + (language == null ? "" : language); |
| 1627 | } |
| 1628 | imMap.put(title, new Pair<InputMethodInfo, Integer>(property, j)); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1629 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1630 | } |
| 1631 | } else { |
| 1632 | imMap.put(label, |
| 1633 | new Pair<InputMethodInfo, Integer>(property, NOT_A_SUBTYPE_ID)); |
| 1634 | subtypeIds.add(0); |
| 1635 | } |
Dianne Hackborn | 97106ab | 2010-03-03 00:08:31 -0800 | [diff] [blame] | 1636 | } |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 1637 | |
| 1638 | N = imMap.size(); |
| 1639 | mItems = imMap.keySet().toArray(new CharSequence[N]); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1640 | mIms = new InputMethodInfo[N]; |
| 1641 | mSubtypeIds = new int[N]; |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1642 | int checkedItem = 0; |
| 1643 | for (int i = 0; i < N; ++i) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1644 | Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]); |
| 1645 | mIms[i] = value.first; |
| 1646 | mSubtypeIds[i] = value.second; |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1647 | if (mIms[i].getId().equals(lastInputMethodId)) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1648 | int subtypeId = mSubtypeIds[i]; |
| 1649 | if ((subtypeId == NOT_A_SUBTYPE_ID) |
| 1650 | || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0) |
| 1651 | || (subtypeId == lastInputMethodSubtypeId)) { |
| 1652 | checkedItem = i; |
| 1653 | } |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1654 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1655 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1656 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1657 | AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() { |
| 1658 | public void onClick(DialogInterface dialog, int which) { |
| 1659 | hideInputMethodMenu(); |
| 1660 | } |
| 1661 | }; |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1662 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1663 | TypedArray a = context.obtainStyledAttributes(null, |
| 1664 | com.android.internal.R.styleable.DialogPreference, |
| 1665 | com.android.internal.R.attr.alertDialogStyle, 0); |
| 1666 | mDialogBuilder = new AlertDialog.Builder(context) |
| 1667 | .setTitle(com.android.internal.R.string.select_input_method) |
| 1668 | .setOnCancelListener(new OnCancelListener() { |
| 1669 | public void onCancel(DialogInterface dialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1670 | hideInputMethodMenu(); |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1671 | } |
| 1672 | }) |
| 1673 | .setIcon(a.getDrawable( |
| 1674 | com.android.internal.R.styleable.DialogPreference_dialogTitle)); |
| 1675 | a.recycle(); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1676 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1677 | mDialogBuilder.setSingleChoiceItems(mItems, checkedItem, |
| 1678 | new AlertDialog.OnClickListener() { |
| 1679 | public void onClick(DialogInterface dialog, int which) { |
| 1680 | synchronized (mMethodMap) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1681 | if (mIms == null || mIms.length <= which |
| 1682 | || mSubtypeIds == null || mSubtypeIds.length <= which) { |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1683 | return; |
| 1684 | } |
| 1685 | InputMethodInfo im = mIms[which]; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1686 | int subtypeId = mSubtypeIds[which]; |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1687 | hideInputMethodMenu(); |
| 1688 | if (im != null) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1689 | if ((subtypeId < 0) |
| 1690 | || (subtypeId >= im.getSubtypes().size())) { |
| 1691 | subtypeId = NOT_A_SUBTYPE_ID; |
| 1692 | } |
| 1693 | setInputMethodLocked(im.getId(), subtypeId); |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1694 | } |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1695 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1696 | } |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1697 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1698 | |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1699 | if (showSubtypes) { |
| 1700 | mDialogBuilder.setPositiveButton(com.android.internal.R.string.more_item_label, |
| 1701 | new DialogInterface.OnClickListener() { |
| 1702 | public void onClick(DialogInterface dialog, int whichButton) { |
| 1703 | showInputMethodAndSubtypeEnabler(); |
| 1704 | } |
| 1705 | }); |
| 1706 | } |
satok | 0ff647b | 2010-10-08 13:49:28 +0900 | [diff] [blame] | 1707 | mDialogBuilder.setNegativeButton(com.android.internal.R.string.cancel, |
| 1708 | new DialogInterface.OnClickListener() { |
| 1709 | public void onClick(DialogInterface dialog, int whichButton) { |
| 1710 | hideInputMethodMenu(); |
| 1711 | } |
| 1712 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1713 | mSwitchingDialog = mDialogBuilder.create(); |
| 1714 | mSwitchingDialog.getWindow().setType( |
| 1715 | WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG); |
| 1716 | mSwitchingDialog.show(); |
| 1717 | } |
| 1718 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1719 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1720 | void hideInputMethodMenu() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1721 | synchronized (mMethodMap) { |
| 1722 | hideInputMethodMenuLocked(); |
| 1723 | } |
| 1724 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1725 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1726 | void hideInputMethodMenuLocked() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1727 | if (DEBUG) Slog.v(TAG, "Hide switching menu"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1728 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1729 | if (mSwitchingDialog != null) { |
| 1730 | mSwitchingDialog.dismiss(); |
| 1731 | mSwitchingDialog = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1732 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1733 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1734 | mDialogBuilder = null; |
| 1735 | mItems = null; |
| 1736 | mIms = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1737 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1738 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1739 | // ---------------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1740 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1741 | public boolean setInputMethodEnabled(String id, boolean enabled) { |
| 1742 | synchronized (mMethodMap) { |
| 1743 | if (mContext.checkCallingOrSelfPermission( |
| 1744 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1745 | != PackageManager.PERMISSION_GRANTED) { |
| 1746 | throw new SecurityException( |
| 1747 | "Requires permission " |
| 1748 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 1749 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1750 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1751 | long ident = Binder.clearCallingIdentity(); |
| 1752 | try { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1753 | return setInputMethodEnabledLocked(id, enabled); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1754 | } finally { |
| 1755 | Binder.restoreCallingIdentity(ident); |
| 1756 | } |
| 1757 | } |
| 1758 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1759 | |
| 1760 | boolean setInputMethodEnabledLocked(String id, boolean enabled) { |
| 1761 | // Make sure this is a valid input method. |
| 1762 | InputMethodInfo imm = mMethodMap.get(id); |
| 1763 | if (imm == null) { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1764 | throw new IllegalArgumentException("Unknown id: " + mCurMethodId); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1765 | } |
| 1766 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1767 | List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings |
| 1768 | .getEnabledInputMethodsAndSubtypeListLocked(); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1769 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1770 | if (enabled) { |
| 1771 | for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) { |
| 1772 | if (pair.first.equals(id)) { |
| 1773 | // We are enabling this input method, but it is already enabled. |
| 1774 | // Nothing to do. The previous state was enabled. |
| 1775 | return true; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1776 | } |
| 1777 | } |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1778 | mSettings.appendAndPutEnabledInputMethodLocked(id, false); |
| 1779 | // Previous state was disabled. |
| 1780 | return false; |
| 1781 | } else { |
| 1782 | StringBuilder builder = new StringBuilder(); |
| 1783 | if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked( |
| 1784 | builder, enabledInputMethodsList, id)) { |
| 1785 | // Disabled input method is currently selected, switch to another one. |
| 1786 | String selId = Settings.Secure.getString(mContext.getContentResolver(), |
| 1787 | Settings.Secure.DEFAULT_INPUT_METHOD); |
| 1788 | if (id.equals(selId)) { |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame^] | 1789 | resetSelectedInputMethodAndSubtypeLocked(enabledInputMethodsList.size() > 0 |
| 1790 | ? enabledInputMethodsList.get(0).first : ""); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1791 | } |
| 1792 | // Previous state was enabled. |
| 1793 | return true; |
| 1794 | } else { |
| 1795 | // We are disabling the input method but it is already disabled. |
| 1796 | // Nothing to do. The previous state was disabled. |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1797 | return false; |
| 1798 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1799 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1800 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1801 | |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame^] | 1802 | private void saveCurrentInputMethodAndSubtypeToHistory() { |
| 1803 | String subtypeId = NOT_A_SUBTYPE_ID_STR; |
| 1804 | if (mCurrentSubtype != null) { |
| 1805 | subtypeId = String.valueOf(mCurrentSubtype.hashCode()); |
| 1806 | } |
| 1807 | mSettings.addSubtypeToHistory(mCurMethodId, subtypeId); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1808 | } |
| 1809 | |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame^] | 1810 | private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId, |
| 1811 | boolean setSubtypeOnly) { |
| 1812 | // Update the history of InputMethod and Subtype |
| 1813 | saveCurrentInputMethodAndSubtypeToHistory(); |
| 1814 | |
| 1815 | // Set Subtype here |
| 1816 | if (imi == null || subtypeId < 0) { |
| 1817 | mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID); |
Tadashi G. Takaoka | 0ba75bb | 2010-11-09 12:19:32 -0800 | [diff] [blame] | 1818 | mCurrentSubtype = null; |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame^] | 1819 | } else { |
| 1820 | final ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes(); |
| 1821 | if (subtypeId < subtypes.size()) { |
| 1822 | mSettings.putSelectedSubtype(subtypes.get(subtypeId).hashCode()); |
| 1823 | mCurrentSubtype = subtypes.get(subtypeId); |
| 1824 | } else { |
| 1825 | mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID); |
| 1826 | mCurrentSubtype = null; |
| 1827 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1828 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame^] | 1829 | |
| 1830 | if (!setSubtypeOnly) { |
| 1831 | // Set InputMethod here |
| 1832 | mSettings.putSelectedInputMethod(imi != null ? imi.getId() : ""); |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) { |
| 1837 | InputMethodInfo imi = mMethodMap.get(newDefaultIme); |
| 1838 | int lastSubtypeId = NOT_A_SUBTYPE_ID; |
| 1839 | // newDefaultIme is empty when there is no candidate for the selected IME. |
| 1840 | if (imi != null && !TextUtils.isEmpty(newDefaultIme)) { |
| 1841 | String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme); |
| 1842 | if (subtypeHashCode != null) { |
| 1843 | try { |
| 1844 | lastSubtypeId = getSubtypeIdFromHashCode( |
| 1845 | imi, Integer.valueOf(subtypeHashCode)); |
| 1846 | } catch (NumberFormatException e) { |
| 1847 | Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e); |
| 1848 | } |
| 1849 | } |
| 1850 | } |
| 1851 | setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | private int getSelectedInputMethodSubtypeId(String id) { |
| 1855 | InputMethodInfo imi = mMethodMap.get(id); |
| 1856 | if (imi == null) { |
| 1857 | return NOT_A_SUBTYPE_ID; |
| 1858 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1859 | int subtypeId; |
| 1860 | try { |
| 1861 | subtypeId = Settings.Secure.getInt(mContext.getContentResolver(), |
| 1862 | Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE); |
| 1863 | } catch (SettingNotFoundException e) { |
| 1864 | return NOT_A_SUBTYPE_ID; |
| 1865 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame^] | 1866 | return getSubtypeIdFromHashCode(imi, subtypeId); |
| 1867 | } |
| 1868 | |
| 1869 | private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) { |
| 1870 | ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes(); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1871 | for (int i = 0; i < subtypes.size(); ++i) { |
| 1872 | InputMethodSubtype ims = subtypes.get(i); |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame^] | 1873 | if (subtypeHashCode == ims.hashCode()) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1874 | return i; |
| 1875 | } |
| 1876 | } |
| 1877 | return NOT_A_SUBTYPE_ID; |
| 1878 | } |
| 1879 | |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 1880 | // If there are no selected subtypes, tries finding the most applicable one according to the |
| 1881 | // current system locale |
| 1882 | private int findApplicableSubtype(String id) { |
| 1883 | InputMethodInfo imi = mMethodMap.get(id); |
| 1884 | if (imi == null) { |
| 1885 | return NOT_A_SUBTYPE_ID; |
| 1886 | } |
| 1887 | ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes(); |
| 1888 | if (subtypes == null || subtypes.size() == 0) { |
| 1889 | return NOT_A_SUBTYPE_ID; |
| 1890 | } |
| 1891 | final String locale = mContext.getResources().getConfiguration().locale.toString(); |
| 1892 | final String language = locale.substring(0, 2); |
| 1893 | boolean partialMatchFound = false; |
| 1894 | int applicableSubtypeId = DEFAULT_SUBTYPE_ID; |
| 1895 | for (int i = 0; i < subtypes.size(); ++i) { |
| 1896 | final String subtypeLocale = subtypes.get(i).getLocale(); |
satok | 9ef0283 | 2010-11-04 21:17:48 +0900 | [diff] [blame] | 1897 | // An applicable subtype should be a keyboard subtype |
| 1898 | if (subtypes.get(i).getMode().equalsIgnoreCase("keyboard")) { |
| 1899 | if (locale.equals(subtypeLocale)) { |
| 1900 | // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US") |
| 1901 | applicableSubtypeId = i; |
| 1902 | break; |
| 1903 | } else if (!partialMatchFound && subtypeLocale.startsWith(language)) { |
| 1904 | // Partial match (e.g. system locale is "en_US" and subtype locale is "en") |
| 1905 | applicableSubtypeId = i; |
| 1906 | partialMatchFound = true; |
| 1907 | } |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | // The first subtype applicable to the system locale will be defined as the most applicable |
| 1912 | // subtype. |
| 1913 | if (DEBUG) { |
| 1914 | Slog.d(TAG, "Applicable InputMethodSubtype was found: " + applicableSubtypeId |
| 1915 | + subtypes.get(applicableSubtypeId).getLocale()); |
| 1916 | } |
| 1917 | return applicableSubtypeId; |
| 1918 | } |
| 1919 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1920 | /** |
| 1921 | * @return Return the current subtype of this input method. |
| 1922 | */ |
| 1923 | public InputMethodSubtype getCurrentInputMethodSubtype() { |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 1924 | boolean subtypeIsSelected = false; |
| 1925 | try { |
| 1926 | subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(), |
| 1927 | Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID; |
| 1928 | } catch (SettingNotFoundException e) { |
| 1929 | } |
| 1930 | if (!subtypeIsSelected || mCurrentSubtype == null) { |
| 1931 | String lastInputMethodId = Settings.Secure.getString(mContext |
| 1932 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
| 1933 | int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId); |
| 1934 | if (subtypeId == NOT_A_SUBTYPE_ID) { |
| 1935 | subtypeId = findApplicableSubtype(lastInputMethodId); |
| 1936 | } |
| 1937 | if (subtypeId != NOT_A_SUBTYPE_ID) { |
| 1938 | mCurrentSubtype = mMethodMap.get(lastInputMethodId).getSubtypes().get(subtypeId); |
| 1939 | } |
| 1940 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1941 | return mCurrentSubtype; |
| 1942 | } |
| 1943 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1944 | /** |
| 1945 | * Utility class for putting and getting settings for InputMethod |
| 1946 | * TODO: Move all putters and getters of settings to this class. |
| 1947 | */ |
| 1948 | private static class InputMethodSettings { |
| 1949 | // The string for enabled input method is saved as follows: |
| 1950 | // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0") |
| 1951 | private static final char INPUT_METHOD_SEPARATER = ':'; |
| 1952 | private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';'; |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame^] | 1953 | private final TextUtils.SimpleStringSplitter mInputMethodSplitter = |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1954 | new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER); |
| 1955 | |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame^] | 1956 | private final TextUtils.SimpleStringSplitter mSubtypeSplitter = |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1957 | new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER); |
| 1958 | |
| 1959 | private final ContentResolver mResolver; |
| 1960 | private final HashMap<String, InputMethodInfo> mMethodMap; |
| 1961 | private final ArrayList<InputMethodInfo> mMethodList; |
| 1962 | |
| 1963 | private String mEnabledInputMethodsStrCache; |
| 1964 | |
| 1965 | private static void buildEnabledInputMethodsSettingString( |
| 1966 | StringBuilder builder, Pair<String, ArrayList<String>> pair) { |
| 1967 | String id = pair.first; |
| 1968 | ArrayList<String> subtypes = pair.second; |
| 1969 | builder.append(id); |
satok | 57c767c | 2010-11-01 22:34:08 +0900 | [diff] [blame] | 1970 | // Inputmethod and subtypes are saved in the settings as follows: |
| 1971 | // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1 |
| 1972 | for (String subtypeId: subtypes) { |
| 1973 | builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | public InputMethodSettings( |
| 1978 | ContentResolver resolver, HashMap<String, InputMethodInfo> methodMap, |
| 1979 | ArrayList<InputMethodInfo> methodList) { |
| 1980 | mResolver = resolver; |
| 1981 | mMethodMap = methodMap; |
| 1982 | mMethodList = methodList; |
| 1983 | } |
| 1984 | |
| 1985 | public List<InputMethodInfo> getEnabledInputMethodListLocked() { |
| 1986 | return createEnabledInputMethodListLocked( |
| 1987 | getEnabledInputMethodsAndSubtypeListLocked()); |
| 1988 | } |
| 1989 | |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1990 | public List<Pair<InputMethodInfo, ArrayList<String>>> |
| 1991 | getEnabledInputMethodAndSubtypeListLocked() { |
| 1992 | return createEnabledInputMethodAndSubtypeListLocked( |
| 1993 | getEnabledInputMethodsAndSubtypeListLocked()); |
| 1994 | } |
| 1995 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1996 | // At the initial boot, the settings for input methods are not set, |
| 1997 | // so we need to enable IME in that case. |
| 1998 | public void enableAllIMEsIfThereIsNoEnabledIME() { |
| 1999 | if (TextUtils.isEmpty(getEnabledInputMethodsStr())) { |
| 2000 | StringBuilder sb = new StringBuilder(); |
| 2001 | final int N = mMethodList.size(); |
| 2002 | for (int i = 0; i < N; i++) { |
| 2003 | InputMethodInfo imi = mMethodList.get(i); |
| 2004 | Slog.i(TAG, "Adding: " + imi.getId()); |
| 2005 | if (i > 0) sb.append(':'); |
| 2006 | sb.append(imi.getId()); |
| 2007 | } |
| 2008 | putEnabledInputMethodsStr(sb.toString()); |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | public List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() { |
| 2013 | ArrayList<Pair<String, ArrayList<String>>> imsList |
| 2014 | = new ArrayList<Pair<String, ArrayList<String>>>(); |
| 2015 | final String enabledInputMethodsStr = getEnabledInputMethodsStr(); |
| 2016 | if (TextUtils.isEmpty(enabledInputMethodsStr)) { |
| 2017 | return imsList; |
| 2018 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame^] | 2019 | mInputMethodSplitter.setString(enabledInputMethodsStr); |
| 2020 | while (mInputMethodSplitter.hasNext()) { |
| 2021 | String nextImsStr = mInputMethodSplitter.next(); |
| 2022 | mSubtypeSplitter.setString(nextImsStr); |
| 2023 | if (mSubtypeSplitter.hasNext()) { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2024 | ArrayList<String> subtypeHashes = new ArrayList<String>(); |
| 2025 | // The first element is ime id. |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame^] | 2026 | String imeId = mSubtypeSplitter.next(); |
| 2027 | while (mSubtypeSplitter.hasNext()) { |
| 2028 | subtypeHashes.add(mSubtypeSplitter.next()); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2029 | } |
| 2030 | imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes)); |
| 2031 | } |
| 2032 | } |
| 2033 | return imsList; |
| 2034 | } |
| 2035 | |
| 2036 | public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) { |
| 2037 | if (reloadInputMethodStr) { |
| 2038 | getEnabledInputMethodsStr(); |
| 2039 | } |
| 2040 | if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) { |
| 2041 | // Add in the newly enabled input method. |
| 2042 | putEnabledInputMethodsStr(id); |
| 2043 | } else { |
| 2044 | putEnabledInputMethodsStr( |
| 2045 | mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id); |
| 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | /** |
| 2050 | * Build and put a string of EnabledInputMethods with removing specified Id. |
| 2051 | * @return the specified id was removed or not. |
| 2052 | */ |
| 2053 | public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked( |
| 2054 | StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) { |
| 2055 | boolean isRemoved = false; |
| 2056 | boolean needsAppendSeparator = false; |
| 2057 | for (Pair<String, ArrayList<String>> ims: imsList) { |
| 2058 | String curId = ims.first; |
| 2059 | if (curId.equals(id)) { |
| 2060 | // We are disabling this input method, and it is |
| 2061 | // currently enabled. Skip it to remove from the |
| 2062 | // new list. |
| 2063 | isRemoved = true; |
| 2064 | } else { |
| 2065 | if (needsAppendSeparator) { |
| 2066 | builder.append(INPUT_METHOD_SEPARATER); |
| 2067 | } else { |
| 2068 | needsAppendSeparator = true; |
| 2069 | } |
| 2070 | buildEnabledInputMethodsSettingString(builder, ims); |
| 2071 | } |
| 2072 | } |
| 2073 | if (isRemoved) { |
| 2074 | // Update the setting with the new list of input methods. |
| 2075 | putEnabledInputMethodsStr(builder.toString()); |
| 2076 | } |
| 2077 | return isRemoved; |
| 2078 | } |
| 2079 | |
| 2080 | private List<InputMethodInfo> createEnabledInputMethodListLocked( |
| 2081 | List<Pair<String, ArrayList<String>>> imsList) { |
| 2082 | final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>(); |
| 2083 | for (Pair<String, ArrayList<String>> ims: imsList) { |
| 2084 | InputMethodInfo info = mMethodMap.get(ims.first); |
| 2085 | if (info != null) { |
| 2086 | res.add(info); |
| 2087 | } |
| 2088 | } |
| 2089 | return res; |
| 2090 | } |
| 2091 | |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2092 | private List<Pair<InputMethodInfo, ArrayList<String>>> |
| 2093 | createEnabledInputMethodAndSubtypeListLocked( |
| 2094 | List<Pair<String, ArrayList<String>>> imsList) { |
| 2095 | final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res |
| 2096 | = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>(); |
| 2097 | for (Pair<String, ArrayList<String>> ims : imsList) { |
| 2098 | InputMethodInfo info = mMethodMap.get(ims.first); |
| 2099 | if (info != null) { |
| 2100 | res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second)); |
| 2101 | } |
| 2102 | } |
| 2103 | return res; |
| 2104 | } |
| 2105 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2106 | private void putEnabledInputMethodsStr(String str) { |
| 2107 | Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str); |
| 2108 | mEnabledInputMethodsStrCache = str; |
| 2109 | } |
| 2110 | |
| 2111 | private String getEnabledInputMethodsStr() { |
| 2112 | mEnabledInputMethodsStrCache = Settings.Secure.getString( |
| 2113 | mResolver, Settings.Secure.ENABLED_INPUT_METHODS); |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame^] | 2114 | if (DEBUG) { |
| 2115 | Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache); |
| 2116 | } |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2117 | return mEnabledInputMethodsStrCache; |
| 2118 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame^] | 2119 | |
| 2120 | private void saveSubtypeHistory( |
| 2121 | List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) { |
| 2122 | StringBuilder builder = new StringBuilder(); |
| 2123 | boolean isImeAdded = false; |
| 2124 | if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) { |
| 2125 | builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append( |
| 2126 | newSubtypeId); |
| 2127 | isImeAdded = true; |
| 2128 | } |
| 2129 | for (Pair<String, String> ime: savedImes) { |
| 2130 | String imeId = ime.first; |
| 2131 | String subtypeId = ime.second; |
| 2132 | if (TextUtils.isEmpty(subtypeId)) { |
| 2133 | subtypeId = NOT_A_SUBTYPE_ID_STR; |
| 2134 | } |
| 2135 | if (isImeAdded) { |
| 2136 | builder.append(INPUT_METHOD_SEPARATER); |
| 2137 | } else { |
| 2138 | isImeAdded = true; |
| 2139 | } |
| 2140 | builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append( |
| 2141 | subtypeId); |
| 2142 | } |
| 2143 | // Remove the last INPUT_METHOD_SEPARATER |
| 2144 | putSubtypeHistoryStr(builder.toString()); |
| 2145 | } |
| 2146 | |
| 2147 | public void addSubtypeToHistory(String imeId, String subtypeId) { |
| 2148 | List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked(); |
| 2149 | for (Pair<String, String> ime: subtypeHistory) { |
| 2150 | if (ime.first.equals(imeId)) { |
| 2151 | if (DEBUG) { |
| 2152 | Slog.v(TAG, "Subtype found in the history: " + imeId |
| 2153 | + ime.second); |
| 2154 | } |
| 2155 | // We should break here |
| 2156 | subtypeHistory.remove(ime); |
| 2157 | break; |
| 2158 | } |
| 2159 | } |
| 2160 | saveSubtypeHistory(subtypeHistory, imeId, subtypeId); |
| 2161 | } |
| 2162 | |
| 2163 | private void putSubtypeHistoryStr(String str) { |
| 2164 | if (DEBUG) { |
| 2165 | Slog.d(TAG, "putSubtypeHistoryStr: " + str); |
| 2166 | } |
| 2167 | Settings.Secure.putString( |
| 2168 | mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str); |
| 2169 | } |
| 2170 | |
| 2171 | public Pair<String, String> getLastInputMethodAndSubtypeLocked() { |
| 2172 | // Gets the first one from the history |
| 2173 | return getLastSubtypeForInputMethodLockedInternal(null); |
| 2174 | } |
| 2175 | |
| 2176 | public String getLastSubtypeForInputMethodLocked(String imeId) { |
| 2177 | Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId); |
| 2178 | if (ime != null) { |
| 2179 | return ime.second; |
| 2180 | } else { |
| 2181 | return null; |
| 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) { |
| 2186 | List<Pair<String, ArrayList<String>>> enabledImes = |
| 2187 | getEnabledInputMethodsAndSubtypeListLocked(); |
| 2188 | List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked(); |
| 2189 | for (Pair<String, String> imeAndSubtype: subtypeHistory) { |
| 2190 | final String imeInTheHistory = imeAndSubtype.first; |
| 2191 | // If imeId is empty, returns the first IME and subtype in the history |
| 2192 | if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) { |
| 2193 | final String subtypeInTheHistory = imeAndSubtype.second; |
| 2194 | final String subtypeHashCode = getEnabledSubtypeForInputMethodAndSubtypeLocked( |
| 2195 | enabledImes, imeInTheHistory, subtypeInTheHistory); |
| 2196 | if (!TextUtils.isEmpty(subtypeHashCode)) { |
| 2197 | if (DEBUG) { |
| 2198 | Slog.d(TAG, "Enabled subtype found in the history:" + subtypeHashCode); |
| 2199 | } |
| 2200 | return new Pair<String, String>(imeInTheHistory, subtypeHashCode); |
| 2201 | } |
| 2202 | } |
| 2203 | } |
| 2204 | if (DEBUG) { |
| 2205 | Slog.d(TAG, "No enabled IME found in the history"); |
| 2206 | } |
| 2207 | return null; |
| 2208 | } |
| 2209 | |
| 2210 | private String getEnabledSubtypeForInputMethodAndSubtypeLocked(List<Pair<String, |
| 2211 | ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) { |
| 2212 | for (Pair<String, ArrayList<String>> enabledIme: enabledImes) { |
| 2213 | if (enabledIme.first.equals(imeId)) { |
| 2214 | for (String s: enabledIme.second) { |
| 2215 | if (s.equals(subtypeHashCode)) { |
| 2216 | // If both imeId and subtypeId are enabled, return subtypeId. |
| 2217 | return s; |
| 2218 | } |
| 2219 | } |
| 2220 | // If imeId was enabled but subtypeId was disabled. |
| 2221 | return NOT_A_SUBTYPE_ID_STR; |
| 2222 | } |
| 2223 | } |
| 2224 | // If both imeId and subtypeId are disabled, return null |
| 2225 | return null; |
| 2226 | } |
| 2227 | |
| 2228 | private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() { |
| 2229 | ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>(); |
| 2230 | final String subtypeHistoryStr = getSubtypeHistoryStr(); |
| 2231 | if (TextUtils.isEmpty(subtypeHistoryStr)) { |
| 2232 | return imsList; |
| 2233 | } |
| 2234 | mInputMethodSplitter.setString(subtypeHistoryStr); |
| 2235 | while (mInputMethodSplitter.hasNext()) { |
| 2236 | String nextImsStr = mInputMethodSplitter.next(); |
| 2237 | mSubtypeSplitter.setString(nextImsStr); |
| 2238 | if (mSubtypeSplitter.hasNext()) { |
| 2239 | String subtypeId = NOT_A_SUBTYPE_ID_STR; |
| 2240 | // The first element is ime id. |
| 2241 | String imeId = mSubtypeSplitter.next(); |
| 2242 | while (mSubtypeSplitter.hasNext()) { |
| 2243 | subtypeId = mSubtypeSplitter.next(); |
| 2244 | break; |
| 2245 | } |
| 2246 | imsList.add(new Pair<String, String>(imeId, subtypeId)); |
| 2247 | } |
| 2248 | } |
| 2249 | return imsList; |
| 2250 | } |
| 2251 | |
| 2252 | private String getSubtypeHistoryStr() { |
| 2253 | if (DEBUG) { |
| 2254 | Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString( |
| 2255 | mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY)); |
| 2256 | } |
| 2257 | return Settings.Secure.getString( |
| 2258 | mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY); |
| 2259 | } |
| 2260 | |
| 2261 | public void putSelectedInputMethod(String imeId) { |
| 2262 | Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId); |
| 2263 | } |
| 2264 | |
| 2265 | public void putSelectedSubtype(int subtypeId) { |
| 2266 | Settings.Secure.putInt( |
| 2267 | mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId); |
| 2268 | } |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2269 | } |
| 2270 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2271 | // ---------------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2272 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2273 | @Override |
| 2274 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 2275 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 2276 | != PackageManager.PERMISSION_GRANTED) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2277 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2278 | pw.println("Permission Denial: can't dump InputMethodManager from from pid=" |
| 2279 | + Binder.getCallingPid() |
| 2280 | + ", uid=" + Binder.getCallingUid()); |
| 2281 | return; |
| 2282 | } |
| 2283 | |
| 2284 | IInputMethod method; |
| 2285 | ClientState client; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2286 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2287 | final Printer p = new PrintWriterPrinter(pw); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2288 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2289 | synchronized (mMethodMap) { |
| 2290 | p.println("Current Input Method Manager state:"); |
| 2291 | int N = mMethodList.size(); |
| 2292 | p.println(" Input Methods:"); |
| 2293 | for (int i=0; i<N; i++) { |
| 2294 | InputMethodInfo info = mMethodList.get(i); |
| 2295 | p.println(" InputMethod #" + i + ":"); |
| 2296 | info.dump(p, " "); |
| 2297 | } |
| 2298 | p.println(" Clients:"); |
| 2299 | for (ClientState ci : mClients.values()) { |
| 2300 | p.println(" Client " + ci + ":"); |
| 2301 | p.println(" client=" + ci.client); |
| 2302 | p.println(" inputContext=" + ci.inputContext); |
| 2303 | p.println(" sessionRequested=" + ci.sessionRequested); |
| 2304 | p.println(" curSession=" + ci.curSession); |
| 2305 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2306 | p.println(" mCurMethodId=" + mCurMethodId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2307 | client = mCurClient; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2308 | p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq); |
| 2309 | p.println(" mCurFocusedWindow=" + mCurFocusedWindow); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2310 | p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection |
| 2311 | + " mBoundToMethod=" + mBoundToMethod); |
| 2312 | p.println(" mCurToken=" + mCurToken); |
| 2313 | p.println(" mCurIntent=" + mCurIntent); |
| 2314 | method = mCurMethod; |
| 2315 | p.println(" mCurMethod=" + mCurMethod); |
| 2316 | p.println(" mEnabledSession=" + mEnabledSession); |
| 2317 | p.println(" mShowRequested=" + mShowRequested |
| 2318 | + " mShowExplicitlyRequested=" + mShowExplicitlyRequested |
| 2319 | + " mShowForced=" + mShowForced |
| 2320 | + " mInputShown=" + mInputShown); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 2321 | p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2322 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2323 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2324 | p.println(" "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2325 | if (client != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2326 | pw.flush(); |
| 2327 | try { |
| 2328 | client.client.asBinder().dump(fd, args); |
| 2329 | } catch (RemoteException e) { |
| 2330 | p.println("Input method client dead: " + e); |
| 2331 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2332 | } else { |
| 2333 | p.println("No input method client."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2334 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2335 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2336 | p.println(" "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2337 | if (method != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2338 | pw.flush(); |
| 2339 | try { |
| 2340 | method.asBinder().dump(fd, args); |
| 2341 | } catch (RemoteException e) { |
| 2342 | p.println("Input method service dead: " + e); |
| 2343 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2344 | } else { |
| 2345 | p.println("No input method service."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2346 | } |
| 2347 | } |
| 2348 | } |