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