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