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