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