The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006-2008 The Android Open Source Project |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * the License at |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 9 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 19 | import com.android.internal.content.PackageMonitor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import com.android.internal.os.HandlerCaller; |
| 21 | import com.android.internal.view.IInputContext; |
| 22 | import com.android.internal.view.IInputMethod; |
| 23 | import com.android.internal.view.IInputMethodCallback; |
| 24 | import com.android.internal.view.IInputMethodClient; |
| 25 | import com.android.internal.view.IInputMethodManager; |
| 26 | import com.android.internal.view.IInputMethodSession; |
| 27 | import com.android.internal.view.InputBindResult; |
| 28 | |
Joe Onorato | 7a0f36b | 2010-06-07 10:24:36 -0700 | [diff] [blame] | 29 | import com.android.server.StatusBarManagerService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
| 31 | import org.xmlpull.v1.XmlPullParserException; |
| 32 | |
| 33 | import android.app.ActivityManagerNative; |
| 34 | import android.app.AlertDialog; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 35 | import android.app.PendingIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.content.ComponentName; |
| 37 | import android.content.ContentResolver; |
| 38 | import android.content.Context; |
| 39 | import android.content.DialogInterface; |
| 40 | import android.content.IntentFilter; |
| 41 | import android.content.DialogInterface.OnCancelListener; |
| 42 | import android.content.Intent; |
| 43 | import android.content.ServiceConnection; |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 44 | import android.content.pm.ApplicationInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.content.pm.PackageManager; |
| 46 | import android.content.pm.ResolveInfo; |
| 47 | import android.content.pm.ServiceInfo; |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 48 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.content.res.Resources; |
| 50 | import android.content.res.TypedArray; |
| 51 | import android.database.ContentObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.os.Binder; |
| 53 | import android.os.Handler; |
| 54 | import android.os.IBinder; |
| 55 | import android.os.IInterface; |
| 56 | import android.os.Message; |
| 57 | import android.os.Parcel; |
| 58 | import android.os.RemoteException; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 59 | import android.os.ResultReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.os.ServiceManager; |
| 61 | import android.os.SystemClock; |
| 62 | import android.provider.Settings; |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 63 | import android.provider.Settings.Secure; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import android.text.TextUtils; |
| 65 | import android.util.EventLog; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 66 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import android.util.PrintWriterPrinter; |
| 68 | import android.util.Printer; |
| 69 | import android.view.IWindowManager; |
| 70 | import android.view.WindowManager; |
| 71 | import android.view.inputmethod.InputBinding; |
| 72 | import android.view.inputmethod.InputMethod; |
| 73 | import android.view.inputmethod.InputMethodInfo; |
| 74 | import android.view.inputmethod.InputMethodManager; |
| 75 | import android.view.inputmethod.EditorInfo; |
| 76 | |
| 77 | import java.io.FileDescriptor; |
| 78 | import java.io.IOException; |
| 79 | import java.io.PrintWriter; |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 80 | import java.text.Collator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | import java.util.ArrayList; |
| 82 | import java.util.HashMap; |
| 83 | import java.util.List; |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 84 | import java.util.Map; |
| 85 | import java.util.TreeMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * This class provides a system service that manages input methods. |
| 89 | */ |
| 90 | public class InputMethodManagerService extends IInputMethodManager.Stub |
| 91 | implements ServiceConnection, Handler.Callback { |
| 92 | static final boolean DEBUG = false; |
| 93 | static final String TAG = "InputManagerService"; |
| 94 | |
| 95 | static final int MSG_SHOW_IM_PICKER = 1; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 96 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | static final int MSG_UNBIND_INPUT = 1000; |
| 98 | static final int MSG_BIND_INPUT = 1010; |
| 99 | static final int MSG_SHOW_SOFT_INPUT = 1020; |
| 100 | static final int MSG_HIDE_SOFT_INPUT = 1030; |
| 101 | static final int MSG_ATTACH_TOKEN = 1040; |
| 102 | static final int MSG_CREATE_SESSION = 1050; |
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_START_INPUT = 2000; |
| 105 | static final int MSG_RESTART_INPUT = 2010; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 106 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | static final int MSG_UNBIND_METHOD = 3000; |
| 108 | static final int MSG_BIND_METHOD = 3010; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 109 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | static final long TIME_TO_RECONNECT = 10*1000; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 111 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | final Context mContext; |
| 113 | final Handler mHandler; |
| 114 | final SettingsObserver mSettingsObserver; |
Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 115 | final StatusBarManagerService mStatusBar; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | final IWindowManager mIWindowManager; |
| 117 | final HandlerCaller mCaller; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 118 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | final InputBindResult mNoBinding = new InputBindResult(null, null, -1); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 120 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | // All known input methods. mMethodMap also serves as the global |
| 122 | // lock for this class. |
| 123 | final ArrayList<InputMethodInfo> mMethodList |
| 124 | = new ArrayList<InputMethodInfo>(); |
| 125 | final HashMap<String, InputMethodInfo> mMethodMap |
| 126 | = new HashMap<String, InputMethodInfo>(); |
| 127 | |
| 128 | final TextUtils.SimpleStringSplitter mStringColonSplitter |
| 129 | = new TextUtils.SimpleStringSplitter(':'); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 130 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | class SessionState { |
| 132 | final ClientState client; |
| 133 | final IInputMethod method; |
| 134 | final IInputMethodSession session; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 135 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | @Override |
| 137 | public String toString() { |
| 138 | return "SessionState{uid " + client.uid + " pid " + client.pid |
| 139 | + " method " + Integer.toHexString( |
| 140 | System.identityHashCode(method)) |
| 141 | + " session " + Integer.toHexString( |
| 142 | System.identityHashCode(session)) |
| 143 | + "}"; |
| 144 | } |
| 145 | |
| 146 | SessionState(ClientState _client, IInputMethod _method, |
| 147 | IInputMethodSession _session) { |
| 148 | client = _client; |
| 149 | method = _method; |
| 150 | session = _session; |
| 151 | } |
| 152 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 153 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | class ClientState { |
| 155 | final IInputMethodClient client; |
| 156 | final IInputContext inputContext; |
| 157 | final int uid; |
| 158 | final int pid; |
| 159 | final InputBinding binding; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 160 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | boolean sessionRequested; |
| 162 | SessionState curSession; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 163 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | @Override |
| 165 | public String toString() { |
| 166 | return "ClientState{" + Integer.toHexString( |
| 167 | System.identityHashCode(this)) + " uid " + uid |
| 168 | + " pid " + pid + "}"; |
| 169 | } |
| 170 | |
| 171 | ClientState(IInputMethodClient _client, IInputContext _inputContext, |
| 172 | int _uid, int _pid) { |
| 173 | client = _client; |
| 174 | inputContext = _inputContext; |
| 175 | uid = _uid; |
| 176 | pid = _pid; |
| 177 | binding = new InputBinding(null, inputContext.asBinder(), uid, pid); |
| 178 | } |
| 179 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 180 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | final HashMap<IBinder, ClientState> mClients |
| 182 | = new HashMap<IBinder, ClientState>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 183 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | /** |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 185 | * Set once the system is ready to run third party code. |
| 186 | */ |
| 187 | boolean mSystemReady; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 188 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 189 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | * Id of the currently selected input method. |
| 191 | */ |
| 192 | String mCurMethodId; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 193 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | /** |
| 195 | * The current binding sequence number, incremented every time there is |
| 196 | * a new bind performed. |
| 197 | */ |
| 198 | int mCurSeq; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 199 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | /** |
| 201 | * The client that is currently bound to an input method. |
| 202 | */ |
| 203 | ClientState mCurClient; |
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 | /** |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 206 | * The last window token that gained focus. |
| 207 | */ |
| 208 | IBinder mCurFocusedWindow; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 209 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 210 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | * The input context last provided by the current client. |
| 212 | */ |
| 213 | IInputContext mCurInputContext; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 214 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | /** |
| 216 | * The attributes last provided by the current client. |
| 217 | */ |
| 218 | EditorInfo mCurAttribute; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 219 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | /** |
| 221 | * The input method ID of the input method service that we are currently |
| 222 | * connected to or in the process of connecting to. |
| 223 | */ |
| 224 | String mCurId; |
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 | * Set to true if our ServiceConnection is currently actively bound to |
| 228 | * a service (whether or not we have gotten its IBinder back yet). |
| 229 | */ |
| 230 | boolean mHaveConnection; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 231 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | /** |
| 233 | * Set if the client has asked for the input method to be shown. |
| 234 | */ |
| 235 | boolean mShowRequested; |
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 | /** |
| 238 | * Set if we were explicitly told to show the input method. |
| 239 | */ |
| 240 | boolean mShowExplicitlyRequested; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 241 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | /** |
| 243 | * Set if we were forced to be shown. |
| 244 | */ |
| 245 | boolean mShowForced; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 246 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | /** |
| 248 | * Set if we last told the input method to show itself. |
| 249 | */ |
| 250 | boolean mInputShown; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 251 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | /** |
| 253 | * The Intent used to connect to the current input method. |
| 254 | */ |
| 255 | Intent mCurIntent; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 256 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | /** |
| 258 | * The token we have made for the currently active input method, to |
| 259 | * identify it in the future. |
| 260 | */ |
| 261 | IBinder mCurToken; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | /** |
| 264 | * If non-null, this is the input method service we are currently connected |
| 265 | * to. |
| 266 | */ |
| 267 | IInputMethod mCurMethod; |
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 | * Time that we last initiated a bind to the input method, to determine |
| 271 | * if we should try to disconnect and reconnect to it. |
| 272 | */ |
| 273 | long mLastBindTime; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 274 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | /** |
| 276 | * Have we called mCurMethod.bindInput()? |
| 277 | */ |
| 278 | boolean mBoundToMethod; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 279 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | /** |
| 281 | * Currently enabled session. Only touched by service thread, not |
| 282 | * protected by a lock. |
| 283 | */ |
| 284 | SessionState mEnabledSession; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 285 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | /** |
| 287 | * True if the screen is on. The value is true initially. |
| 288 | */ |
| 289 | boolean mScreenOn = true; |
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 | AlertDialog.Builder mDialogBuilder; |
| 292 | AlertDialog mSwitchingDialog; |
| 293 | InputMethodInfo[] mIms; |
| 294 | CharSequence[] mItems; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 295 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | class SettingsObserver extends ContentObserver { |
| 297 | SettingsObserver(Handler handler) { |
| 298 | super(handler); |
| 299 | ContentResolver resolver = mContext.getContentResolver(); |
| 300 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 301 | Settings.Secure.DEFAULT_INPUT_METHOD), false, this); |
| 302 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 303 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | @Override public void onChange(boolean selfChange) { |
| 305 | synchronized (mMethodMap) { |
| 306 | updateFromSettingsLocked(); |
| 307 | } |
| 308 | } |
| 309 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 310 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | class ScreenOnOffReceiver extends android.content.BroadcastReceiver { |
| 312 | @Override |
| 313 | public void onReceive(Context context, Intent intent) { |
| 314 | if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { |
| 315 | mScreenOn = true; |
| 316 | } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { |
| 317 | mScreenOn = false; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 318 | } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { |
| 319 | hideInputMethodMenu(); |
| 320 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 322 | Slog.w(TAG, "Unexpected intent " + intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | // Inform the current client of the change in active status |
| 326 | try { |
| 327 | if (mCurClient != null && mCurClient.client != null) { |
| 328 | mCurClient.client.setActive(mScreenOn); |
| 329 | } |
| 330 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 331 | 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] | 332 | + mCurClient.pid + " uid " + mCurClient.uid); |
| 333 | } |
| 334 | } |
| 335 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 336 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 337 | class MyPackageMonitor extends PackageMonitor { |
| 338 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | @Override |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 340 | 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] | 341 | synchronized (mMethodMap) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 342 | String curInputMethodId = Settings.Secure.getString(mContext |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
| 344 | final int N = mMethodList.size(); |
| 345 | if (curInputMethodId != null) { |
| 346 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 347 | InputMethodInfo imi = mMethodList.get(i); |
| 348 | if (imi.getId().equals(curInputMethodId)) { |
| 349 | for (String pkg : packages) { |
| 350 | if (imi.getPackageName().equals(pkg)) { |
| 351 | if (!doit) { |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | Settings.Secure.putString(mContext.getContentResolver(), |
| 356 | Settings.Secure.DEFAULT_INPUT_METHOD, ""); |
| 357 | chooseNewDefaultIMELocked(); |
| 358 | return true; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public void onSomePackagesChanged() { |
| 370 | synchronized (mMethodMap) { |
| 371 | InputMethodInfo curIm = null; |
| 372 | String curInputMethodId = Settings.Secure.getString(mContext |
| 373 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
| 374 | final int N = mMethodList.size(); |
| 375 | if (curInputMethodId != null) { |
| 376 | for (int i=0; i<N; i++) { |
| 377 | InputMethodInfo imi = mMethodList.get(i); |
| 378 | if (imi.getId().equals(curInputMethodId)) { |
| 379 | curIm = imi; |
| 380 | } |
| 381 | int change = isPackageDisappearing(imi.getPackageName()); |
| 382 | if (change == PACKAGE_TEMPORARY_CHANGE |
| 383 | || change == PACKAGE_PERMANENT_CHANGE) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 384 | Slog.i(TAG, "Input method uninstalled, disabling: " |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 385 | + imi.getComponent()); |
| 386 | setInputMethodEnabledLocked(imi.getId(), false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 390 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 391 | buildInputMethodListLocked(mMethodList, mMethodMap); |
| 392 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | boolean changed = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 394 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 395 | if (curIm != null) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 396 | int change = isPackageDisappearing(curIm.getPackageName()); |
| 397 | if (change == PACKAGE_TEMPORARY_CHANGE |
| 398 | || change == PACKAGE_PERMANENT_CHANGE) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 399 | ServiceInfo si = null; |
| 400 | try { |
| 401 | si = mContext.getPackageManager().getServiceInfo( |
| 402 | curIm.getComponent(), 0); |
| 403 | } catch (PackageManager.NameNotFoundException ex) { |
| 404 | } |
| 405 | if (si == null) { |
| 406 | // Uh oh, current input method is no longer around! |
| 407 | // Pick another one... |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 408 | Slog.i(TAG, "Current input method removed: " + curInputMethodId); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 409 | if (!chooseNewDefaultIMELocked()) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 410 | changed = true; |
| 411 | curIm = null; |
| 412 | curInputMethodId = ""; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 413 | Slog.i(TAG, "Unsetting current input method"); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 414 | Settings.Secure.putString(mContext.getContentResolver(), |
| 415 | Settings.Secure.DEFAULT_INPUT_METHOD, |
| 416 | curInputMethodId); |
| 417 | } |
| 418 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 419 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | if (curIm == null) { |
| 423 | // We currently don't have a default input method... is |
| 424 | // one now available? |
| 425 | changed = chooseNewDefaultIMELocked(); |
| 426 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 427 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 428 | if (changed) { |
| 429 | updateFromSettingsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | } |
| 433 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 434 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | class MethodCallback extends IInputMethodCallback.Stub { |
| 436 | final IInputMethod mMethod; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 437 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | MethodCallback(IInputMethod method) { |
| 439 | mMethod = method; |
| 440 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 441 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | public void finishedEvent(int seq, boolean handled) throws RemoteException { |
| 443 | } |
| 444 | |
| 445 | public void sessionCreated(IInputMethodSession session) throws RemoteException { |
| 446 | onSessionCreated(mMethod, session); |
| 447 | } |
| 448 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 449 | |
Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 450 | public InputMethodManagerService(Context context, StatusBarManagerService statusBar) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | mContext = context; |
| 452 | mHandler = new Handler(this); |
| 453 | mIWindowManager = IWindowManager.Stub.asInterface( |
| 454 | ServiceManager.getService(Context.WINDOW_SERVICE)); |
| 455 | mCaller = new HandlerCaller(context, new HandlerCaller.Callback() { |
| 456 | public void executeMessage(Message msg) { |
| 457 | handleMessage(msg); |
| 458 | } |
| 459 | }); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 460 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 461 | (new MyPackageMonitor()).register(mContext, true); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 462 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | IntentFilter screenOnOffFilt = new IntentFilter(); |
| 464 | screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON); |
| 465 | screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 466 | screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 468 | |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 469 | mStatusBar = statusBar; |
| 470 | statusBar.setIconVisibility("ime", false); |
| 471 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | buildInputMethodListLocked(mMethodList, mMethodMap); |
| 473 | |
| 474 | final String enabledStr = Settings.Secure.getString( |
| 475 | mContext.getContentResolver(), |
| 476 | Settings.Secure.ENABLED_INPUT_METHODS); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 477 | Slog.i(TAG, "Enabled input methods: " + enabledStr); |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 478 | final String defaultIme = Settings.Secure.getString(mContext |
| 479 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
| 480 | if (enabledStr == null || TextUtils.isEmpty(defaultIme)) { |
| 481 | Slog.i(TAG, "Enabled input methods or default IME has not been set, enabling all"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | InputMethodInfo defIm = null; |
| 483 | StringBuilder sb = new StringBuilder(256); |
| 484 | final int N = mMethodList.size(); |
| 485 | for (int i=0; i<N; i++) { |
| 486 | InputMethodInfo imi = mMethodList.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 487 | Slog.i(TAG, "Adding: " + imi.getId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | if (i > 0) sb.append(':'); |
| 489 | sb.append(imi.getId()); |
| 490 | if (defIm == null && imi.getIsDefaultResourceId() != 0) { |
| 491 | try { |
| 492 | Resources res = mContext.createPackageContext( |
| 493 | imi.getPackageName(), 0).getResources(); |
| 494 | if (res.getBoolean(imi.getIsDefaultResourceId())) { |
| 495 | defIm = imi; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 496 | Slog.i(TAG, "Selected default: " + imi.getId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | } |
| 498 | } catch (PackageManager.NameNotFoundException ex) { |
| 499 | } catch (Resources.NotFoundException ex) { |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | if (defIm == null && N > 0) { |
| 504 | defIm = mMethodList.get(0); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 505 | Slog.i(TAG, "No default found, using " + defIm.getId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | } |
| 507 | Settings.Secure.putString(mContext.getContentResolver(), |
| 508 | Settings.Secure.ENABLED_INPUT_METHODS, sb.toString()); |
| 509 | if (defIm != null) { |
| 510 | Settings.Secure.putString(mContext.getContentResolver(), |
| 511 | Settings.Secure.DEFAULT_INPUT_METHOD, defIm.getId()); |
| 512 | } |
| 513 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 514 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | mSettingsObserver = new SettingsObserver(mHandler); |
| 516 | updateFromSettingsLocked(); |
| 517 | } |
| 518 | |
| 519 | @Override |
| 520 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 521 | throws RemoteException { |
| 522 | try { |
| 523 | return super.onTransact(code, data, reply, flags); |
| 524 | } catch (RuntimeException e) { |
| 525 | // The input method manager only throws security exceptions, so let's |
| 526 | // log all others. |
| 527 | if (!(e instanceof SecurityException)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 528 | Slog.e(TAG, "Input Method Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | } |
| 530 | throw e; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | public void systemReady() { |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 535 | synchronized (mMethodMap) { |
| 536 | if (!mSystemReady) { |
| 537 | mSystemReady = true; |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 538 | try { |
| 539 | startInputInnerLocked(); |
| 540 | } catch (RuntimeException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 541 | Slog.w(TAG, "Unexpected exception", e); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 542 | } |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 543 | } |
| 544 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 545 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 546 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | public List<InputMethodInfo> getInputMethodList() { |
| 548 | synchronized (mMethodMap) { |
| 549 | return new ArrayList<InputMethodInfo>(mMethodList); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | public List<InputMethodInfo> getEnabledInputMethodList() { |
| 554 | synchronized (mMethodMap) { |
| 555 | return getEnabledInputMethodListLocked(); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | List<InputMethodInfo> getEnabledInputMethodListLocked() { |
| 560 | final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 561 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | final String enabledStr = Settings.Secure.getString( |
| 563 | mContext.getContentResolver(), |
| 564 | Settings.Secure.ENABLED_INPUT_METHODS); |
| 565 | if (enabledStr != null) { |
| 566 | final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter; |
| 567 | splitter.setString(enabledStr); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 568 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | while (splitter.hasNext()) { |
| 570 | InputMethodInfo info = mMethodMap.get(splitter.next()); |
| 571 | if (info != null) { |
| 572 | res.add(info); |
| 573 | } |
| 574 | } |
| 575 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 576 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | return res; |
| 578 | } |
| 579 | |
| 580 | public void addClient(IInputMethodClient client, |
| 581 | IInputContext inputContext, int uid, int pid) { |
| 582 | synchronized (mMethodMap) { |
| 583 | mClients.put(client.asBinder(), new ClientState(client, |
| 584 | inputContext, uid, pid)); |
| 585 | } |
| 586 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 587 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 588 | public void removeClient(IInputMethodClient client) { |
| 589 | synchronized (mMethodMap) { |
| 590 | mClients.remove(client.asBinder()); |
| 591 | } |
| 592 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 593 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | void executeOrSendMessage(IInterface target, Message msg) { |
| 595 | if (target.asBinder() instanceof Binder) { |
| 596 | mCaller.sendMessage(msg); |
| 597 | } else { |
| 598 | handleMessage(msg); |
| 599 | msg.recycle(); |
| 600 | } |
| 601 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 602 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 603 | void unbindCurrentClientLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 604 | if (mCurClient != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 605 | if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | + mCurClient.client.asBinder()); |
| 607 | if (mBoundToMethod) { |
| 608 | mBoundToMethod = false; |
| 609 | if (mCurMethod != null) { |
| 610 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageO( |
| 611 | MSG_UNBIND_INPUT, mCurMethod)); |
| 612 | } |
| 613 | } |
| 614 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 615 | MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); |
| 616 | mCurClient.sessionRequested = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 617 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | // Call setActive(false) on the old client |
| 619 | try { |
| 620 | mCurClient.client.setActive(false); |
| 621 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 622 | 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] | 623 | + mCurClient.pid + " uid " + mCurClient.uid); |
| 624 | } |
| 625 | mCurClient = null; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 626 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 627 | hideInputMethodMenuLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | } |
| 629 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 630 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | private int getImeShowFlags() { |
| 632 | int flags = 0; |
| 633 | if (mShowForced) { |
| 634 | flags |= InputMethod.SHOW_FORCED |
| 635 | | InputMethod.SHOW_EXPLICIT; |
| 636 | } else if (mShowExplicitlyRequested) { |
| 637 | flags |= InputMethod.SHOW_EXPLICIT; |
| 638 | } |
| 639 | return flags; |
| 640 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 641 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 642 | private int getAppShowFlags() { |
| 643 | int flags = 0; |
| 644 | if (mShowForced) { |
| 645 | flags |= InputMethodManager.SHOW_FORCED; |
| 646 | } else if (!mShowExplicitlyRequested) { |
| 647 | flags |= InputMethodManager.SHOW_IMPLICIT; |
| 648 | } |
| 649 | return flags; |
| 650 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 651 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 652 | InputBindResult attachNewInputLocked(boolean initial, boolean needResult) { |
| 653 | if (!mBoundToMethod) { |
| 654 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 655 | MSG_BIND_INPUT, mCurMethod, mCurClient.binding)); |
| 656 | mBoundToMethod = true; |
| 657 | } |
| 658 | final SessionState session = mCurClient.curSession; |
| 659 | if (initial) { |
| 660 | executeOrSendMessage(session.method, mCaller.obtainMessageOOO( |
| 661 | MSG_START_INPUT, session, mCurInputContext, mCurAttribute)); |
| 662 | } else { |
| 663 | executeOrSendMessage(session.method, mCaller.obtainMessageOOO( |
| 664 | MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute)); |
| 665 | } |
| 666 | if (mShowRequested) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 667 | 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] | 668 | showCurrentInputLocked(getAppShowFlags(), null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 669 | } |
| 670 | return needResult |
| 671 | ? new InputBindResult(session.session, mCurId, mCurSeq) |
| 672 | : null; |
| 673 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 674 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | InputBindResult startInputLocked(IInputMethodClient client, |
| 676 | IInputContext inputContext, EditorInfo attribute, |
| 677 | boolean initial, boolean needResult) { |
| 678 | // If no method is currently selected, do nothing. |
| 679 | if (mCurMethodId == null) { |
| 680 | return mNoBinding; |
| 681 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 682 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 683 | ClientState cs = mClients.get(client.asBinder()); |
| 684 | if (cs == null) { |
| 685 | throw new IllegalArgumentException("unknown client " |
| 686 | + client.asBinder()); |
| 687 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 688 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | try { |
| 690 | if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) { |
| 691 | // Check with the window manager to make sure this client actually |
| 692 | // has a window with focus. If not, reject. This is thread safe |
| 693 | // because if the focus changes some time before or after, the |
| 694 | // next client receiving focus that has any interest in input will |
| 695 | // be calling through here after that change happens. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 696 | 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] | 697 | + " (uid=" + cs.uid + " pid=" + cs.pid + ")"); |
| 698 | return null; |
| 699 | } |
| 700 | } catch (RemoteException e) { |
| 701 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 702 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | if (mCurClient != cs) { |
| 704 | // If the client is changing, we need to switch over to the new |
| 705 | // one. |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 706 | unbindCurrentClientLocked(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 707 | if (DEBUG) Slog.v(TAG, "switching to client: client = " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 708 | + cs.client.asBinder()); |
| 709 | |
| 710 | // If the screen is on, inform the new client it is active |
| 711 | if (mScreenOn) { |
| 712 | try { |
| 713 | cs.client.setActive(mScreenOn); |
| 714 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 715 | 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] | 716 | + cs.pid + " uid " + cs.uid); |
| 717 | } |
| 718 | } |
| 719 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 720 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | // Bump up the sequence for this client and attach it. |
| 722 | mCurSeq++; |
| 723 | if (mCurSeq <= 0) mCurSeq = 1; |
| 724 | mCurClient = cs; |
| 725 | mCurInputContext = inputContext; |
| 726 | mCurAttribute = attribute; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 727 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | // Check if the input method is changing. |
| 729 | if (mCurId != null && mCurId.equals(mCurMethodId)) { |
| 730 | if (cs.curSession != null) { |
| 731 | // Fast case: if we are already connected to the input method, |
| 732 | // then just return it. |
| 733 | return attachNewInputLocked(initial, needResult); |
| 734 | } |
| 735 | if (mHaveConnection) { |
| 736 | if (mCurMethod != null) { |
| 737 | if (!cs.sessionRequested) { |
| 738 | cs.sessionRequested = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 739 | 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] | 740 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 741 | MSG_CREATE_SESSION, mCurMethod, |
| 742 | new MethodCallback(mCurMethod))); |
| 743 | } |
| 744 | // Return to client, and we will get back with it when |
| 745 | // we have had a session made for it. |
| 746 | return new InputBindResult(null, mCurId, mCurSeq); |
| 747 | } else if (SystemClock.uptimeMillis() |
| 748 | < (mLastBindTime+TIME_TO_RECONNECT)) { |
| 749 | // In this case we have connected to the service, but |
| 750 | // don't yet have its interface. If it hasn't been too |
| 751 | // long since we did the connection, we'll return to |
| 752 | // the client and wait to get the service interface so |
| 753 | // we can report back. If it has been too long, we want |
| 754 | // to fall through so we can try a disconnect/reconnect |
| 755 | // to see if we can get back in touch with the service. |
| 756 | return new InputBindResult(null, mCurId, mCurSeq); |
| 757 | } else { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 758 | EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, |
| 759 | mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 763 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 764 | return startInputInnerLocked(); |
| 765 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 766 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 767 | InputBindResult startInputInnerLocked() { |
| 768 | if (mCurMethodId == null) { |
| 769 | return mNoBinding; |
| 770 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 771 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 772 | if (!mSystemReady) { |
| 773 | // If the system is not yet ready, we shouldn't be running third |
| 774 | // party code. |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 775 | return new InputBindResult(null, mCurMethodId, mCurSeq); |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 776 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 777 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 778 | InputMethodInfo info = mMethodMap.get(mCurMethodId); |
| 779 | if (info == null) { |
| 780 | throw new IllegalArgumentException("Unknown id: " + mCurMethodId); |
| 781 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 782 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 783 | unbindCurrentMethodLocked(false); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 784 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE); |
| 786 | mCurIntent.setComponent(info.getComponent()); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 787 | mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL, |
| 788 | com.android.internal.R.string.input_method_binding_label); |
| 789 | mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity( |
| 790 | 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] | 791 | if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) { |
| 792 | mLastBindTime = SystemClock.uptimeMillis(); |
| 793 | mHaveConnection = true; |
| 794 | mCurId = info.getId(); |
| 795 | mCurToken = new Binder(); |
| 796 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 797 | if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | mIWindowManager.addWindowToken(mCurToken, |
| 799 | WindowManager.LayoutParams.TYPE_INPUT_METHOD); |
| 800 | } catch (RemoteException e) { |
| 801 | } |
| 802 | return new InputBindResult(null, mCurId, mCurSeq); |
| 803 | } else { |
| 804 | mCurIntent = null; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 805 | Slog.w(TAG, "Failure connecting to input method service: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 806 | + mCurIntent); |
| 807 | } |
| 808 | return null; |
| 809 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 810 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | public InputBindResult startInput(IInputMethodClient client, |
| 812 | IInputContext inputContext, EditorInfo attribute, |
| 813 | boolean initial, boolean needResult) { |
| 814 | synchronized (mMethodMap) { |
| 815 | final long ident = Binder.clearCallingIdentity(); |
| 816 | try { |
| 817 | return startInputLocked(client, inputContext, attribute, |
| 818 | initial, needResult); |
| 819 | } finally { |
| 820 | Binder.restoreCallingIdentity(ident); |
| 821 | } |
| 822 | } |
| 823 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 824 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 825 | public void finishInput(IInputMethodClient client) { |
| 826 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 827 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 828 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 829 | synchronized (mMethodMap) { |
| 830 | if (mCurIntent != null && name.equals(mCurIntent.getComponent())) { |
| 831 | mCurMethod = IInputMethod.Stub.asInterface(service); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 832 | if (mCurToken == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 833 | Slog.w(TAG, "Service connected without a token!"); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 834 | unbindCurrentMethodLocked(false); |
| 835 | return; |
| 836 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 837 | if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 838 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 839 | MSG_ATTACH_TOKEN, mCurMethod, mCurToken)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 840 | if (mCurClient != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 841 | if (DEBUG) Slog.v(TAG, "Creating first session while with client " |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 842 | + mCurClient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 843 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 844 | MSG_CREATE_SESSION, mCurMethod, |
| 845 | new MethodCallback(mCurMethod))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | void onSessionCreated(IInputMethod method, IInputMethodSession session) { |
| 852 | synchronized (mMethodMap) { |
| 853 | if (mCurMethod != null && method != null |
| 854 | && mCurMethod.asBinder() == method.asBinder()) { |
| 855 | if (mCurClient != null) { |
| 856 | mCurClient.curSession = new SessionState(mCurClient, |
| 857 | method, session); |
| 858 | mCurClient.sessionRequested = false; |
| 859 | InputBindResult res = attachNewInputLocked(true, true); |
| 860 | if (res.method != null) { |
| 861 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO( |
| 862 | MSG_BIND_METHOD, mCurClient.client, res)); |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | } |
| 867 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 868 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 869 | void unbindCurrentMethodLocked(boolean reportToClient) { |
| 870 | if (mHaveConnection) { |
| 871 | mContext.unbindService(this); |
| 872 | mHaveConnection = false; |
| 873 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 874 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 875 | if (mCurToken != null) { |
| 876 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 877 | if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 878 | mIWindowManager.removeWindowToken(mCurToken); |
| 879 | } catch (RemoteException e) { |
| 880 | } |
| 881 | mCurToken = null; |
| 882 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 883 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 884 | mCurId = null; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 885 | clearCurMethodLocked(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 886 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 887 | if (reportToClient && mCurClient != null) { |
| 888 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 889 | MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); |
| 890 | } |
| 891 | } |
| 892 | |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 893 | private void finishSession(SessionState sessionState) { |
| 894 | if (sessionState != null && sessionState.session != null) { |
| 895 | try { |
| 896 | sessionState.session.finishSession(); |
| 897 | } catch (RemoteException e) { |
Jean-Baptiste Queru | 9d0f6df | 2010-03-29 12:55:09 -0700 | [diff] [blame] | 898 | Slog.w(TAG, "Session failed to close due to remote exception", e); |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 902 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 903 | void clearCurMethodLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 904 | if (mCurMethod != null) { |
| 905 | for (ClientState cs : mClients.values()) { |
| 906 | cs.sessionRequested = false; |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 907 | finishSession(cs.curSession); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 908 | cs.curSession = null; |
| 909 | } |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 910 | |
| 911 | finishSession(mEnabledSession); |
| 912 | mEnabledSession = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 | mCurMethod = null; |
| 914 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 915 | mStatusBar.setIconVisibility("ime", false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 916 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 917 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | public void onServiceDisconnected(ComponentName name) { |
| 919 | synchronized (mMethodMap) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 920 | if (DEBUG) Slog.v(TAG, "Service disconnected: " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | + " mCurIntent=" + mCurIntent); |
| 922 | if (mCurMethod != null && mCurIntent != null |
| 923 | && name.equals(mCurIntent.getComponent())) { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 924 | clearCurMethodLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | // We consider this to be a new bind attempt, since the system |
| 926 | // should now try to restart the service for us. |
| 927 | mLastBindTime = SystemClock.uptimeMillis(); |
| 928 | mShowRequested = mInputShown; |
| 929 | mInputShown = false; |
| 930 | if (mCurClient != null) { |
| 931 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 932 | MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | public void updateStatusIcon(IBinder token, String packageName, int iconId) { |
| 939 | long ident = Binder.clearCallingIdentity(); |
| 940 | try { |
| 941 | if (token == null || mCurToken != token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 942 | Slog.w(TAG, "Ignoring setInputMethod of token: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 943 | return; |
| 944 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 945 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 946 | synchronized (mMethodMap) { |
| 947 | if (iconId == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 948 | 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] | 949 | mStatusBar.setIconVisibility("ime", false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 950 | } else if (packageName != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 951 | 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] | 952 | mStatusBar.setIcon("ime", packageName, iconId, 0); |
| 953 | mStatusBar.setIconVisibility("ime", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | } finally { |
| 957 | Binder.restoreCallingIdentity(ident); |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | void updateFromSettingsLocked() { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 962 | // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and |
| 963 | // ENABLED_INPUT_METHODS is taking care of keeping them correctly in |
| 964 | // sync, so we will never have a DEFAULT_INPUT_METHOD that is not |
| 965 | // enabled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 966 | String id = Settings.Secure.getString(mContext.getContentResolver(), |
| 967 | Settings.Secure.DEFAULT_INPUT_METHOD); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 968 | if (id != null && id.length() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 969 | try { |
| 970 | setInputMethodLocked(id); |
| 971 | } catch (IllegalArgumentException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 972 | 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] | 973 | mCurMethodId = null; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 974 | unbindCurrentMethodLocked(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 975 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 976 | } else { |
| 977 | // 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] | 978 | mCurMethodId = null; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 979 | unbindCurrentMethodLocked(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 980 | } |
| 981 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 982 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 983 | void setInputMethodLocked(String id) { |
| 984 | InputMethodInfo info = mMethodMap.get(id); |
| 985 | if (info == null) { |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 986 | throw new IllegalArgumentException("Unknown id: " + id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 987 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 988 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 989 | if (id.equals(mCurMethodId)) { |
| 990 | return; |
| 991 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 992 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 993 | final long ident = Binder.clearCallingIdentity(); |
| 994 | try { |
| 995 | mCurMethodId = id; |
| 996 | Settings.Secure.putString(mContext.getContentResolver(), |
| 997 | Settings.Secure.DEFAULT_INPUT_METHOD, id); |
| 998 | |
| 999 | if (ActivityManagerNative.isSystemReady()) { |
| 1000 | Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1001 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1002 | intent.putExtra("input_method_id", id); |
| 1003 | mContext.sendBroadcast(intent); |
| 1004 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1005 | unbindCurrentClientLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1006 | } finally { |
| 1007 | Binder.restoreCallingIdentity(ident); |
| 1008 | } |
| 1009 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1010 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1011 | public boolean showSoftInput(IInputMethodClient client, int flags, |
| 1012 | ResultReceiver resultReceiver) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | long ident = Binder.clearCallingIdentity(); |
| 1014 | try { |
| 1015 | synchronized (mMethodMap) { |
| 1016 | if (mCurClient == null || client == null |
| 1017 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1018 | try { |
| 1019 | // We need to check if this is the current client with |
| 1020 | // focus in the window manager, to allow this call to |
| 1021 | // be made before input is started in it. |
| 1022 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1023 | Slog.w(TAG, "Ignoring showSoftInput of: " + client); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1024 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1025 | } |
| 1026 | } catch (RemoteException e) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1027 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1028 | } |
| 1029 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1030 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1031 | 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] | 1032 | return showCurrentInputLocked(flags, resultReceiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1033 | } |
| 1034 | } finally { |
| 1035 | Binder.restoreCallingIdentity(ident); |
| 1036 | } |
| 1037 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1038 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1039 | boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | mShowRequested = true; |
| 1041 | if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) { |
| 1042 | mShowExplicitlyRequested = true; |
| 1043 | } |
| 1044 | if ((flags&InputMethodManager.SHOW_FORCED) != 0) { |
| 1045 | mShowExplicitlyRequested = true; |
| 1046 | mShowForced = true; |
| 1047 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1048 | |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 1049 | if (!mSystemReady) { |
| 1050 | return false; |
| 1051 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1052 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1053 | boolean res = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1054 | if (mCurMethod != null) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1055 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO( |
| 1056 | MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod, |
| 1057 | resultReceiver)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1058 | mInputShown = true; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1059 | res = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1060 | } else if (mHaveConnection && SystemClock.uptimeMillis() |
| 1061 | < (mLastBindTime+TIME_TO_RECONNECT)) { |
| 1062 | // The client has asked to have the input method shown, but |
| 1063 | // we have been sitting here too long with a connection to the |
| 1064 | // service and no interface received, so let's disconnect/connect |
| 1065 | // to try to prod things along. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1066 | EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | SystemClock.uptimeMillis()-mLastBindTime,1); |
| 1068 | mContext.unbindService(this); |
| 1069 | mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE); |
| 1070 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1071 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1072 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1074 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1075 | public boolean hideSoftInput(IInputMethodClient client, int flags, |
| 1076 | ResultReceiver resultReceiver) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | long ident = Binder.clearCallingIdentity(); |
| 1078 | try { |
| 1079 | synchronized (mMethodMap) { |
| 1080 | if (mCurClient == null || client == null |
| 1081 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1082 | try { |
| 1083 | // We need to check if this is the current client with |
| 1084 | // focus in the window manager, to allow this call to |
| 1085 | // be made before input is started in it. |
| 1086 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1087 | Slog.w(TAG, "Ignoring hideSoftInput of: " + client); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1088 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1089 | } |
| 1090 | } catch (RemoteException e) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1091 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1092 | } |
| 1093 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1094 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1095 | 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] | 1096 | return hideCurrentInputLocked(flags, resultReceiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1097 | } |
| 1098 | } finally { |
| 1099 | Binder.restoreCallingIdentity(ident); |
| 1100 | } |
| 1101 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1102 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1103 | boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1104 | if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0 |
| 1105 | && (mShowExplicitlyRequested || mShowForced)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1106 | if (DEBUG) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1107 | "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] | 1108 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1109 | } |
| 1110 | if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1111 | if (DEBUG) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1112 | "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] | 1113 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1114 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1115 | boolean res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1116 | if (mInputShown && mCurMethod != null) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1117 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 1118 | MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver)); |
| 1119 | res = true; |
| 1120 | } else { |
| 1121 | res = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1122 | } |
| 1123 | mInputShown = false; |
| 1124 | mShowRequested = false; |
| 1125 | mShowExplicitlyRequested = false; |
| 1126 | mShowForced = false; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1127 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1128 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1129 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1130 | public void windowGainedFocus(IInputMethodClient client, IBinder windowToken, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1131 | boolean viewHasFocus, boolean isTextEditor, int softInputMode, |
| 1132 | boolean first, int windowFlags) { |
| 1133 | long ident = Binder.clearCallingIdentity(); |
| 1134 | try { |
| 1135 | synchronized (mMethodMap) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1136 | if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1137 | + " viewHasFocus=" + viewHasFocus |
| 1138 | + " isTextEditor=" + isTextEditor |
| 1139 | + " softInputMode=#" + Integer.toHexString(softInputMode) |
| 1140 | + " first=" + first + " flags=#" |
| 1141 | + Integer.toHexString(windowFlags)); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1142 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1143 | if (mCurClient == null || client == null |
| 1144 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1145 | try { |
| 1146 | // We need to check if this is the current client with |
| 1147 | // focus in the window manager, to allow this call to |
| 1148 | // be made before input is started in it. |
| 1149 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1150 | 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] | 1151 | return; |
| 1152 | } |
| 1153 | } catch (RemoteException e) { |
| 1154 | } |
| 1155 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1156 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1157 | if (mCurFocusedWindow == windowToken) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1158 | 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] | 1159 | return; |
| 1160 | } |
| 1161 | mCurFocusedWindow = windowToken; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1162 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1163 | switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) { |
| 1164 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED: |
| 1165 | if (!isTextEditor || (softInputMode & |
| 1166 | WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
| 1167 | != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) { |
| 1168 | if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) { |
| 1169 | // There is no focus view, and this window will |
| 1170 | // be behind any soft input window, so hide the |
| 1171 | // soft input window if it is shown. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1172 | 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] | 1173 | hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1174 | } |
| 1175 | } else if (isTextEditor && (softInputMode & |
| 1176 | WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
| 1177 | == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
| 1178 | && (softInputMode & |
| 1179 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
| 1180 | // There is a focus view, and we are navigating forward |
| 1181 | // into the window, so show the input window for the user. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1182 | 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] | 1183 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1184 | } |
| 1185 | break; |
| 1186 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED: |
| 1187 | // Do nothing. |
| 1188 | break; |
| 1189 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN: |
| 1190 | if ((softInputMode & |
| 1191 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1192 | 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] | 1193 | hideCurrentInputLocked(0, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1194 | } |
| 1195 | break; |
| 1196 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN: |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1197 | 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] | 1198 | hideCurrentInputLocked(0, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1199 | break; |
| 1200 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE: |
| 1201 | if ((softInputMode & |
| 1202 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1203 | 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] | 1204 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1205 | } |
| 1206 | break; |
| 1207 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE: |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1208 | 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] | 1209 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1210 | break; |
| 1211 | } |
| 1212 | } |
| 1213 | } finally { |
| 1214 | Binder.restoreCallingIdentity(ident); |
| 1215 | } |
| 1216 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1217 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1218 | public void showInputMethodPickerFromClient(IInputMethodClient client) { |
| 1219 | synchronized (mMethodMap) { |
| 1220 | if (mCurClient == null || client == null |
| 1221 | || mCurClient.client.asBinder() != client.asBinder()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1222 | Slog.w(TAG, "Ignoring showInputMethodDialogFromClient of: " + client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | mHandler.sendEmptyMessage(MSG_SHOW_IM_PICKER); |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | public void setInputMethod(IBinder token, String id) { |
| 1230 | synchronized (mMethodMap) { |
| 1231 | if (token == null) { |
| 1232 | if (mContext.checkCallingOrSelfPermission( |
| 1233 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1234 | != PackageManager.PERMISSION_GRANTED) { |
| 1235 | throw new SecurityException( |
| 1236 | "Using null token requires permission " |
| 1237 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 1238 | } |
| 1239 | } else if (mCurToken != token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1240 | Slog.w(TAG, "Ignoring setInputMethod of token: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1241 | return; |
| 1242 | } |
| 1243 | |
| 1244 | long ident = Binder.clearCallingIdentity(); |
| 1245 | try { |
| 1246 | setInputMethodLocked(id); |
| 1247 | } finally { |
| 1248 | Binder.restoreCallingIdentity(ident); |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | public void hideMySoftInput(IBinder token, int flags) { |
| 1254 | synchronized (mMethodMap) { |
| 1255 | if (token == null || mCurToken != token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1256 | Slog.w(TAG, "Ignoring hideInputMethod of token: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1257 | return; |
| 1258 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1259 | long ident = Binder.clearCallingIdentity(); |
| 1260 | try { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1261 | hideCurrentInputLocked(flags, null); |
| 1262 | } finally { |
| 1263 | Binder.restoreCallingIdentity(ident); |
| 1264 | } |
| 1265 | } |
| 1266 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1267 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1268 | public void showMySoftInput(IBinder token, int flags) { |
| 1269 | synchronized (mMethodMap) { |
| 1270 | if (token == null || mCurToken != token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1271 | Slog.w(TAG, "Ignoring hideInputMethod of token: " + token); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1272 | return; |
| 1273 | } |
| 1274 | long ident = Binder.clearCallingIdentity(); |
| 1275 | try { |
| 1276 | showCurrentInputLocked(flags, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1277 | } finally { |
| 1278 | Binder.restoreCallingIdentity(ident); |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | void setEnabledSessionInMainThread(SessionState session) { |
| 1284 | if (mEnabledSession != session) { |
| 1285 | if (mEnabledSession != null) { |
| 1286 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1287 | if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1288 | mEnabledSession.method.setSessionEnabled( |
| 1289 | mEnabledSession.session, false); |
| 1290 | } catch (RemoteException e) { |
| 1291 | } |
| 1292 | } |
| 1293 | mEnabledSession = session; |
| 1294 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1295 | if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1296 | session.method.setSessionEnabled( |
| 1297 | session.session, true); |
| 1298 | } catch (RemoteException e) { |
| 1299 | } |
| 1300 | } |
| 1301 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1302 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1303 | public boolean handleMessage(Message msg) { |
| 1304 | HandlerCaller.SomeArgs args; |
| 1305 | switch (msg.what) { |
| 1306 | case MSG_SHOW_IM_PICKER: |
| 1307 | showInputMethodMenu(); |
| 1308 | return true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1309 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1310 | // --------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1311 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1312 | case MSG_UNBIND_INPUT: |
| 1313 | try { |
| 1314 | ((IInputMethod)msg.obj).unbindInput(); |
| 1315 | } catch (RemoteException e) { |
| 1316 | // There is nothing interesting about the method dying. |
| 1317 | } |
| 1318 | return true; |
| 1319 | case MSG_BIND_INPUT: |
| 1320 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1321 | try { |
| 1322 | ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2); |
| 1323 | } catch (RemoteException e) { |
| 1324 | } |
| 1325 | return true; |
| 1326 | case MSG_SHOW_SOFT_INPUT: |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1327 | args = (HandlerCaller.SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1328 | try { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1329 | ((IInputMethod)args.arg1).showSoftInput(msg.arg1, |
| 1330 | (ResultReceiver)args.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1331 | } catch (RemoteException e) { |
| 1332 | } |
| 1333 | return true; |
| 1334 | case MSG_HIDE_SOFT_INPUT: |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1335 | args = (HandlerCaller.SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1336 | try { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1337 | ((IInputMethod)args.arg1).hideSoftInput(0, |
| 1338 | (ResultReceiver)args.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1339 | } catch (RemoteException e) { |
| 1340 | } |
| 1341 | return true; |
| 1342 | case MSG_ATTACH_TOKEN: |
| 1343 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1344 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1345 | 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] | 1346 | ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2); |
| 1347 | } catch (RemoteException e) { |
| 1348 | } |
| 1349 | return true; |
| 1350 | case MSG_CREATE_SESSION: |
| 1351 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1352 | try { |
| 1353 | ((IInputMethod)args.arg1).createSession( |
| 1354 | (IInputMethodCallback)args.arg2); |
| 1355 | } catch (RemoteException e) { |
| 1356 | } |
| 1357 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1358 | // --------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1359 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1360 | case MSG_START_INPUT: |
| 1361 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1362 | try { |
| 1363 | SessionState session = (SessionState)args.arg1; |
| 1364 | setEnabledSessionInMainThread(session); |
| 1365 | session.method.startInput((IInputContext)args.arg2, |
| 1366 | (EditorInfo)args.arg3); |
| 1367 | } catch (RemoteException e) { |
| 1368 | } |
| 1369 | return true; |
| 1370 | case MSG_RESTART_INPUT: |
| 1371 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1372 | try { |
| 1373 | SessionState session = (SessionState)args.arg1; |
| 1374 | setEnabledSessionInMainThread(session); |
| 1375 | session.method.restartInput((IInputContext)args.arg2, |
| 1376 | (EditorInfo)args.arg3); |
| 1377 | } catch (RemoteException e) { |
| 1378 | } |
| 1379 | return true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1380 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1381 | // --------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1382 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1383 | case MSG_UNBIND_METHOD: |
| 1384 | try { |
| 1385 | ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1); |
| 1386 | } catch (RemoteException e) { |
| 1387 | // There is nothing interesting about the last client dying. |
| 1388 | } |
| 1389 | return true; |
| 1390 | case MSG_BIND_METHOD: |
| 1391 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1392 | try { |
| 1393 | ((IInputMethodClient)args.arg1).onBindMethod( |
| 1394 | (InputBindResult)args.arg2); |
| 1395 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1396 | 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] | 1397 | } |
| 1398 | return true; |
| 1399 | } |
| 1400 | return false; |
| 1401 | } |
| 1402 | |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1403 | private boolean isSystemIme(InputMethodInfo inputMethod) { |
| 1404 | return (inputMethod.getServiceInfo().applicationInfo.flags |
| 1405 | & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 1406 | } |
| 1407 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1408 | private boolean chooseNewDefaultIMELocked() { |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1409 | List<InputMethodInfo> enabled = getEnabledInputMethodListLocked(); |
| 1410 | if (enabled != null && enabled.size() > 0) { |
Dianne Hackborn | 83e48f5 | 2010-03-23 23:03:25 -0700 | [diff] [blame] | 1411 | // We'd prefer to fall back on a system IME, since that is safer. |
| 1412 | int i=enabled.size(); |
| 1413 | while (i > 0) { |
| 1414 | i--; |
| 1415 | if ((enabled.get(i).getServiceInfo().applicationInfo.flags |
| 1416 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 1417 | break; |
| 1418 | } |
| 1419 | } |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1420 | Settings.Secure.putString(mContext.getContentResolver(), |
| 1421 | Settings.Secure.DEFAULT_INPUT_METHOD, |
Dianne Hackborn | 83e48f5 | 2010-03-23 23:03:25 -0700 | [diff] [blame] | 1422 | enabled.get(i).getId()); |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1423 | return true; |
| 1424 | } |
| 1425 | |
| 1426 | return false; |
| 1427 | } |
| 1428 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1429 | void buildInputMethodListLocked(ArrayList<InputMethodInfo> list, |
| 1430 | HashMap<String, InputMethodInfo> map) { |
| 1431 | list.clear(); |
| 1432 | map.clear(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1433 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1434 | PackageManager pm = mContext.getPackageManager(); |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 1435 | final Configuration config = mContext.getResources().getConfiguration(); |
| 1436 | final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY; |
| 1437 | String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(), |
| 1438 | Secure.DISABLED_SYSTEM_INPUT_METHODS); |
| 1439 | if (disabledSysImes == null) disabledSysImes = ""; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1440 | |
| 1441 | List<ResolveInfo> services = pm.queryIntentServices( |
| 1442 | new Intent(InputMethod.SERVICE_INTERFACE), |
| 1443 | PackageManager.GET_META_DATA); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1444 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1445 | for (int i = 0; i < services.size(); ++i) { |
| 1446 | ResolveInfo ri = services.get(i); |
| 1447 | ServiceInfo si = ri.serviceInfo; |
| 1448 | ComponentName compName = new ComponentName(si.packageName, si.name); |
| 1449 | if (!android.Manifest.permission.BIND_INPUT_METHOD.equals( |
| 1450 | si.permission)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1451 | Slog.w(TAG, "Skipping input method " + compName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1452 | + ": it does not require the permission " |
| 1453 | + android.Manifest.permission.BIND_INPUT_METHOD); |
| 1454 | continue; |
| 1455 | } |
| 1456 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1457 | if (DEBUG) Slog.d(TAG, "Checking " + compName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1458 | |
| 1459 | try { |
| 1460 | InputMethodInfo p = new InputMethodInfo(mContext, ri); |
| 1461 | list.add(p); |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 1462 | final String id = p.getId(); |
| 1463 | map.put(id, p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1464 | |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 1465 | // System IMEs are enabled by default, unless there's a hard keyboard |
| 1466 | // and the system IME was explicitly disabled |
| 1467 | if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) { |
| 1468 | setInputMethodEnabledLocked(id, true); |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1469 | } |
| 1470 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1471 | if (DEBUG) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1472 | 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] | 1473 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1474 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1475 | } catch (XmlPullParserException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1476 | 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] | 1477 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1478 | 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] | 1479 | } |
| 1480 | } |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1481 | |
| 1482 | String defaultIme = Settings.Secure.getString(mContext |
| 1483 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 1484 | if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1485 | if (chooseNewDefaultIMELocked()) { |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1486 | updateFromSettingsLocked(); |
| 1487 | } |
| 1488 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1489 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1490 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1491 | // ---------------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1492 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1493 | void showInputMethodMenu() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1494 | if (DEBUG) Slog.v(TAG, "Show switching menu"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1495 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1496 | final Context context = mContext; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1497 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1498 | final PackageManager pm = context.getPackageManager(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1499 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1500 | String lastInputMethodId = Settings.Secure.getString(context |
| 1501 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1502 | if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1503 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1504 | final List<InputMethodInfo> immis = getEnabledInputMethodList(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1505 | |
Dianne Hackborn | 97106ab | 2010-03-03 00:08:31 -0800 | [diff] [blame] | 1506 | if (immis == null) { |
| 1507 | return; |
| 1508 | } |
| 1509 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1510 | synchronized (mMethodMap) { |
| 1511 | hideInputMethodMenuLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1512 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1513 | int N = immis.size(); |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 1514 | |
| 1515 | final Map<CharSequence, InputMethodInfo> imMap = |
| 1516 | new TreeMap<CharSequence, InputMethodInfo>(Collator.getInstance()); |
| 1517 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1518 | for (int i = 0; i < N; ++i) { |
| 1519 | InputMethodInfo property = immis.get(i); |
| 1520 | if (property == null) { |
| 1521 | continue; |
| 1522 | } |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 1523 | imMap.put(property.loadLabel(pm), property); |
Dianne Hackborn | 97106ab | 2010-03-03 00:08:31 -0800 | [diff] [blame] | 1524 | } |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 1525 | |
| 1526 | N = imMap.size(); |
| 1527 | mItems = imMap.keySet().toArray(new CharSequence[N]); |
| 1528 | mIms = imMap.values().toArray(new InputMethodInfo[N]); |
| 1529 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1530 | int checkedItem = 0; |
| 1531 | for (int i = 0; i < N; ++i) { |
| 1532 | if (mIms[i].getId().equals(lastInputMethodId)) { |
| 1533 | checkedItem = i; |
| 1534 | break; |
| 1535 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1536 | } |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1537 | |
| 1538 | AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() { |
| 1539 | public void onClick(DialogInterface dialog, int which) { |
| 1540 | hideInputMethodMenu(); |
| 1541 | } |
| 1542 | }; |
| 1543 | |
| 1544 | TypedArray a = context.obtainStyledAttributes(null, |
| 1545 | com.android.internal.R.styleable.DialogPreference, |
| 1546 | com.android.internal.R.attr.alertDialogStyle, 0); |
| 1547 | mDialogBuilder = new AlertDialog.Builder(context) |
| 1548 | .setTitle(com.android.internal.R.string.select_input_method) |
| 1549 | .setOnCancelListener(new OnCancelListener() { |
| 1550 | public void onCancel(DialogInterface dialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1551 | hideInputMethodMenu(); |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1552 | } |
| 1553 | }) |
| 1554 | .setIcon(a.getDrawable( |
| 1555 | com.android.internal.R.styleable.DialogPreference_dialogTitle)); |
| 1556 | a.recycle(); |
| 1557 | |
| 1558 | mDialogBuilder.setSingleChoiceItems(mItems, checkedItem, |
| 1559 | new AlertDialog.OnClickListener() { |
| 1560 | public void onClick(DialogInterface dialog, int which) { |
| 1561 | synchronized (mMethodMap) { |
| 1562 | if (mIms == null || mIms.length <= which) { |
| 1563 | return; |
| 1564 | } |
| 1565 | InputMethodInfo im = mIms[which]; |
| 1566 | hideInputMethodMenu(); |
| 1567 | if (im != null) { |
| 1568 | setInputMethodLocked(im.getId()); |
| 1569 | } |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1570 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1571 | } |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1572 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1573 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1574 | mSwitchingDialog = mDialogBuilder.create(); |
| 1575 | mSwitchingDialog.getWindow().setType( |
| 1576 | WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG); |
| 1577 | mSwitchingDialog.show(); |
| 1578 | } |
| 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 | void hideInputMethodMenu() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1582 | synchronized (mMethodMap) { |
| 1583 | hideInputMethodMenuLocked(); |
| 1584 | } |
| 1585 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1586 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1587 | void hideInputMethodMenuLocked() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1588 | if (DEBUG) Slog.v(TAG, "Hide switching menu"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1589 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1590 | if (mSwitchingDialog != null) { |
| 1591 | mSwitchingDialog.dismiss(); |
| 1592 | mSwitchingDialog = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1593 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1594 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1595 | mDialogBuilder = null; |
| 1596 | mItems = null; |
| 1597 | mIms = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1598 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1599 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1600 | // ---------------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1601 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1602 | public boolean setInputMethodEnabled(String id, boolean enabled) { |
| 1603 | synchronized (mMethodMap) { |
| 1604 | if (mContext.checkCallingOrSelfPermission( |
| 1605 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1606 | != PackageManager.PERMISSION_GRANTED) { |
| 1607 | throw new SecurityException( |
| 1608 | "Requires permission " |
| 1609 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 1610 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1611 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1612 | long ident = Binder.clearCallingIdentity(); |
| 1613 | try { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1614 | return setInputMethodEnabledLocked(id, enabled); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1615 | } finally { |
| 1616 | Binder.restoreCallingIdentity(ident); |
| 1617 | } |
| 1618 | } |
| 1619 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1620 | |
| 1621 | boolean setInputMethodEnabledLocked(String id, boolean enabled) { |
| 1622 | // Make sure this is a valid input method. |
| 1623 | InputMethodInfo imm = mMethodMap.get(id); |
| 1624 | if (imm == null) { |
| 1625 | if (imm == null) { |
| 1626 | throw new IllegalArgumentException("Unknown id: " + mCurMethodId); |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | StringBuilder builder = new StringBuilder(256); |
| 1631 | |
| 1632 | boolean removed = false; |
| 1633 | String firstId = null; |
| 1634 | |
| 1635 | // Look through the currently enabled input methods. |
| 1636 | String enabledStr = Settings.Secure.getString(mContext.getContentResolver(), |
| 1637 | Settings.Secure.ENABLED_INPUT_METHODS); |
| 1638 | if (enabledStr != null) { |
| 1639 | final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter; |
| 1640 | splitter.setString(enabledStr); |
| 1641 | while (splitter.hasNext()) { |
| 1642 | String curId = splitter.next(); |
| 1643 | if (curId.equals(id)) { |
| 1644 | if (enabled) { |
| 1645 | // We are enabling this input method, but it is |
| 1646 | // already enabled. Nothing to do. The previous |
| 1647 | // state was enabled. |
| 1648 | return true; |
| 1649 | } |
| 1650 | // We are disabling this input method, and it is |
| 1651 | // currently enabled. Skip it to remove from the |
| 1652 | // new list. |
| 1653 | removed = true; |
| 1654 | } else if (!enabled) { |
| 1655 | // We are building a new list of input methods that |
| 1656 | // doesn't contain the given one. |
| 1657 | if (firstId == null) firstId = curId; |
| 1658 | if (builder.length() > 0) builder.append(':'); |
| 1659 | builder.append(curId); |
| 1660 | } |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | if (!enabled) { |
| 1665 | if (!removed) { |
| 1666 | // We are disabling the input method but it is already |
| 1667 | // disabled. Nothing to do. The previous state was |
| 1668 | // disabled. |
| 1669 | return false; |
| 1670 | } |
| 1671 | // Update the setting with the new list of input methods. |
| 1672 | Settings.Secure.putString(mContext.getContentResolver(), |
| 1673 | Settings.Secure.ENABLED_INPUT_METHODS, builder.toString()); |
| 1674 | // We the disabled input method is currently selected, switch |
| 1675 | // to another one. |
| 1676 | String selId = Settings.Secure.getString(mContext.getContentResolver(), |
| 1677 | Settings.Secure.DEFAULT_INPUT_METHOD); |
| 1678 | if (id.equals(selId)) { |
| 1679 | Settings.Secure.putString(mContext.getContentResolver(), |
| 1680 | Settings.Secure.DEFAULT_INPUT_METHOD, |
| 1681 | firstId != null ? firstId : ""); |
| 1682 | } |
| 1683 | // Previous state was enabled. |
| 1684 | return true; |
| 1685 | } |
| 1686 | |
| 1687 | // Add in the newly enabled input method. |
| 1688 | if (enabledStr == null || enabledStr.length() == 0) { |
| 1689 | enabledStr = id; |
| 1690 | } else { |
| 1691 | enabledStr = enabledStr + ':' + id; |
| 1692 | } |
| 1693 | |
| 1694 | Settings.Secure.putString(mContext.getContentResolver(), |
| 1695 | Settings.Secure.ENABLED_INPUT_METHODS, enabledStr); |
| 1696 | |
| 1697 | // Previous state was disabled. |
| 1698 | return false; |
| 1699 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1700 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1701 | // ---------------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1702 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1703 | @Override |
| 1704 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1705 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1706 | != PackageManager.PERMISSION_GRANTED) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1707 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1708 | pw.println("Permission Denial: can't dump InputMethodManager from from pid=" |
| 1709 | + Binder.getCallingPid() |
| 1710 | + ", uid=" + Binder.getCallingUid()); |
| 1711 | return; |
| 1712 | } |
| 1713 | |
| 1714 | IInputMethod method; |
| 1715 | ClientState client; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1716 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1717 | final Printer p = new PrintWriterPrinter(pw); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1718 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1719 | synchronized (mMethodMap) { |
| 1720 | p.println("Current Input Method Manager state:"); |
| 1721 | int N = mMethodList.size(); |
| 1722 | p.println(" Input Methods:"); |
| 1723 | for (int i=0; i<N; i++) { |
| 1724 | InputMethodInfo info = mMethodList.get(i); |
| 1725 | p.println(" InputMethod #" + i + ":"); |
| 1726 | info.dump(p, " "); |
| 1727 | } |
| 1728 | p.println(" Clients:"); |
| 1729 | for (ClientState ci : mClients.values()) { |
| 1730 | p.println(" Client " + ci + ":"); |
| 1731 | p.println(" client=" + ci.client); |
| 1732 | p.println(" inputContext=" + ci.inputContext); |
| 1733 | p.println(" sessionRequested=" + ci.sessionRequested); |
| 1734 | p.println(" curSession=" + ci.curSession); |
| 1735 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1736 | p.println(" mCurMethodId=" + mCurMethodId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1737 | client = mCurClient; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1738 | p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq); |
| 1739 | p.println(" mCurFocusedWindow=" + mCurFocusedWindow); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1740 | p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection |
| 1741 | + " mBoundToMethod=" + mBoundToMethod); |
| 1742 | p.println(" mCurToken=" + mCurToken); |
| 1743 | p.println(" mCurIntent=" + mCurIntent); |
| 1744 | method = mCurMethod; |
| 1745 | p.println(" mCurMethod=" + mCurMethod); |
| 1746 | p.println(" mEnabledSession=" + mEnabledSession); |
| 1747 | p.println(" mShowRequested=" + mShowRequested |
| 1748 | + " mShowExplicitlyRequested=" + mShowExplicitlyRequested |
| 1749 | + " mShowForced=" + mShowForced |
| 1750 | + " mInputShown=" + mInputShown); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 1751 | p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1752 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1753 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame^] | 1754 | p.println(" "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1755 | if (client != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1756 | pw.flush(); |
| 1757 | try { |
| 1758 | client.client.asBinder().dump(fd, args); |
| 1759 | } catch (RemoteException e) { |
| 1760 | p.println("Input method client dead: " + e); |
| 1761 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame^] | 1762 | } else { |
| 1763 | p.println("No input method client."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1764 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1765 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame^] | 1766 | p.println(" "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1767 | if (method != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1768 | pw.flush(); |
| 1769 | try { |
| 1770 | method.asBinder().dump(fd, args); |
| 1771 | } catch (RemoteException e) { |
| 1772 | p.println("Input method service dead: " + e); |
| 1773 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame^] | 1774 | } else { |
| 1775 | p.println("No input method service."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1776 | } |
| 1777 | } |
| 1778 | } |