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