InputMethodManagerService.java revision 3d46bab02978df14354379c4b10d7f68c3b80771
1/* 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 * use this file except in compliance with the License. You may obtain a copy of 5 * the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 * License for the specific language governing permissions and limitations under 13 * the License. 14 */ 15 16package com.android.server; 17 18import com.android.internal.content.PackageMonitor; 19import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController; 20import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem; 21import com.android.internal.inputmethod.InputMethodUtils; 22import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings; 23import com.android.internal.os.HandlerCaller; 24import com.android.internal.os.SomeArgs; 25import com.android.internal.util.FastXmlSerializer; 26import com.android.internal.view.IInputContext; 27import com.android.internal.view.IInputMethod; 28import com.android.internal.view.IInputSessionCallback; 29import com.android.internal.view.IInputMethodClient; 30import com.android.internal.view.IInputMethodManager; 31import com.android.internal.view.IInputMethodSession; 32import com.android.internal.view.InputBindResult; 33import com.android.server.statusbar.StatusBarManagerService; 34import com.android.server.wm.WindowManagerService; 35 36import org.xmlpull.v1.XmlPullParser; 37import org.xmlpull.v1.XmlPullParserException; 38import org.xmlpull.v1.XmlSerializer; 39 40import android.app.ActivityManagerNative; 41import android.app.AppGlobals; 42import android.app.AlertDialog; 43import android.app.IUserSwitchObserver; 44import android.app.KeyguardManager; 45import android.app.Notification; 46import android.app.NotificationManager; 47import android.app.PendingIntent; 48import android.content.BroadcastReceiver; 49import android.content.ComponentName; 50import android.content.ContentResolver; 51import android.content.Context; 52import android.content.DialogInterface; 53import android.content.DialogInterface.OnCancelListener; 54import android.content.Intent; 55import android.content.IntentFilter; 56import android.content.ServiceConnection; 57import android.content.pm.ApplicationInfo; 58import android.content.pm.IPackageManager; 59import android.content.pm.PackageManager; 60import android.content.pm.ResolveInfo; 61import android.content.pm.ServiceInfo; 62import android.content.pm.UserInfo; 63import android.content.res.Configuration; 64import android.content.res.Resources; 65import android.content.res.TypedArray; 66import android.database.ContentObserver; 67import android.inputmethodservice.InputMethodService; 68import android.os.Binder; 69import android.os.Environment; 70import android.os.Handler; 71import android.os.IBinder; 72import android.os.IInterface; 73import android.os.IRemoteCallback; 74import android.os.Message; 75import android.os.Process; 76import android.os.Parcel; 77import android.os.RemoteException; 78import android.os.ResultReceiver; 79import android.os.ServiceManager; 80import android.os.SystemClock; 81import android.os.UserHandle; 82import android.os.UserManager; 83import android.provider.Settings; 84import android.text.TextUtils; 85import android.text.style.SuggestionSpan; 86import android.util.AtomicFile; 87import android.util.EventLog; 88import android.util.LruCache; 89import android.util.Pair; 90import android.util.PrintWriterPrinter; 91import android.util.Printer; 92import android.util.Slog; 93import android.util.Xml; 94import android.view.IWindowManager; 95import android.view.InputChannel; 96import android.view.LayoutInflater; 97import android.view.View; 98import android.view.ViewGroup; 99import android.view.WindowManager; 100import android.view.inputmethod.EditorInfo; 101import android.view.inputmethod.InputBinding; 102import android.view.inputmethod.InputMethod; 103import android.view.inputmethod.InputMethodInfo; 104import android.view.inputmethod.InputMethodManager; 105import android.view.inputmethod.InputMethodSubtype; 106import android.widget.ArrayAdapter; 107import android.widget.CompoundButton; 108import android.widget.CompoundButton.OnCheckedChangeListener; 109import android.widget.RadioButton; 110import android.widget.Switch; 111import android.widget.TextView; 112 113import java.io.File; 114import java.io.FileDescriptor; 115import java.io.FileInputStream; 116import java.io.FileOutputStream; 117import java.io.IOException; 118import java.io.PrintWriter; 119import java.util.ArrayList; 120import java.util.Collections; 121import java.util.HashMap; 122import java.util.List; 123import java.util.Locale; 124 125/** 126 * This class provides a system service that manages input methods. 127 */ 128public class InputMethodManagerService extends IInputMethodManager.Stub 129 implements ServiceConnection, Handler.Callback { 130 static final boolean DEBUG = false; 131 static final String TAG = "InputMethodManagerService"; 132 133 static final int MSG_SHOW_IM_PICKER = 1; 134 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2; 135 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3; 136 static final int MSG_SHOW_IM_CONFIG = 4; 137 138 static final int MSG_UNBIND_INPUT = 1000; 139 static final int MSG_BIND_INPUT = 1010; 140 static final int MSG_SHOW_SOFT_INPUT = 1020; 141 static final int MSG_HIDE_SOFT_INPUT = 1030; 142 static final int MSG_ATTACH_TOKEN = 1040; 143 static final int MSG_CREATE_SESSION = 1050; 144 145 static final int MSG_START_INPUT = 2000; 146 static final int MSG_RESTART_INPUT = 2010; 147 148 static final int MSG_UNBIND_METHOD = 3000; 149 static final int MSG_BIND_METHOD = 3010; 150 static final int MSG_SET_ACTIVE = 3020; 151 static final int MSG_SET_CURSOR_ANCHOR_MONITOR_MODE = 3030; 152 153 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000; 154 155 static final long TIME_TO_RECONNECT = 3 * 1000; 156 157 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20; 158 159 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID; 160 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher"; 161 162 163 final Context mContext; 164 final Resources mRes; 165 final Handler mHandler; 166 final InputMethodSettings mSettings; 167 final SettingsObserver mSettingsObserver; 168 final IWindowManager mIWindowManager; 169 final HandlerCaller mCaller; 170 final boolean mHasFeature; 171 private InputMethodFileManager mFileManager; 172 private final HardKeyboardListener mHardKeyboardListener; 173 private final WindowManagerService mWindowManagerService; 174 175 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1); 176 177 // All known input methods. mMethodMap also serves as the global 178 // lock for this class. 179 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>(); 180 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>(); 181 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans = 182 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE); 183 private final InputMethodSubtypeSwitchingController mSwitchingController; 184 185 // Used to bring IME service up to visible adjustment while it is being shown. 186 final ServiceConnection mVisibleConnection = new ServiceConnection() { 187 @Override public void onServiceConnected(ComponentName name, IBinder service) { 188 } 189 190 @Override public void onServiceDisconnected(ComponentName name) { 191 } 192 }; 193 boolean mVisibleBound = false; 194 195 // Ongoing notification 196 private NotificationManager mNotificationManager; 197 private KeyguardManager mKeyguardManager; 198 private StatusBarManagerService mStatusBar; 199 private Notification mImeSwitcherNotification; 200 private PendingIntent mImeSwitchPendingIntent; 201 private boolean mShowOngoingImeSwitcherForPhones; 202 private boolean mNotificationShown; 203 private final boolean mImeSelectedOnBoot; 204 205 class SessionState { 206 final ClientState client; 207 final IInputMethod method; 208 209 IInputMethodSession session; 210 InputChannel channel; 211 212 @Override 213 public String toString() { 214 return "SessionState{uid " + client.uid + " pid " + client.pid 215 + " method " + Integer.toHexString( 216 System.identityHashCode(method)) 217 + " session " + Integer.toHexString( 218 System.identityHashCode(session)) 219 + " channel " + channel 220 + "}"; 221 } 222 223 SessionState(ClientState _client, IInputMethod _method, 224 IInputMethodSession _session, InputChannel _channel) { 225 client = _client; 226 method = _method; 227 session = _session; 228 channel = _channel; 229 } 230 } 231 232 static final class ClientState { 233 final IInputMethodClient client; 234 final IInputContext inputContext; 235 final int uid; 236 final int pid; 237 final InputBinding binding; 238 239 boolean sessionRequested; 240 SessionState curSession; 241 242 @Override 243 public String toString() { 244 return "ClientState{" + Integer.toHexString( 245 System.identityHashCode(this)) + " uid " + uid 246 + " pid " + pid + "}"; 247 } 248 249 ClientState(IInputMethodClient _client, IInputContext _inputContext, 250 int _uid, int _pid) { 251 client = _client; 252 inputContext = _inputContext; 253 uid = _uid; 254 pid = _pid; 255 binding = new InputBinding(null, inputContext.asBinder(), uid, pid); 256 } 257 } 258 259 final HashMap<IBinder, ClientState> mClients 260 = new HashMap<IBinder, ClientState>(); 261 262 /** 263 * Set once the system is ready to run third party code. 264 */ 265 boolean mSystemReady; 266 267 /** 268 * Id of the currently selected input method. 269 */ 270 String mCurMethodId; 271 272 /** 273 * The current binding sequence number, incremented every time there is 274 * a new bind performed. 275 */ 276 int mCurSeq; 277 278 /** 279 * The client that is currently bound to an input method. 280 */ 281 ClientState mCurClient; 282 283 /** 284 * The last window token that gained focus. 285 */ 286 IBinder mCurFocusedWindow; 287 288 /** 289 * The input context last provided by the current client. 290 */ 291 IInputContext mCurInputContext; 292 293 /** 294 * The attributes last provided by the current client. 295 */ 296 EditorInfo mCurAttribute; 297 298 /** 299 * The input method ID of the input method service that we are currently 300 * connected to or in the process of connecting to. 301 */ 302 String mCurId; 303 304 /** 305 * The current subtype of the current input method. 306 */ 307 private InputMethodSubtype mCurrentSubtype; 308 309 // This list contains the pairs of InputMethodInfo and InputMethodSubtype. 310 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>> 311 mShortcutInputMethodsAndSubtypes = 312 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>(); 313 314 // Was the keyguard locked when this client became current? 315 private boolean mCurClientInKeyguard; 316 317 /** 318 * Set to true if our ServiceConnection is currently actively bound to 319 * a service (whether or not we have gotten its IBinder back yet). 320 */ 321 boolean mHaveConnection; 322 323 /** 324 * Set if the client has asked for the input method to be shown. 325 */ 326 boolean mShowRequested; 327 328 /** 329 * Set if we were explicitly told to show the input method. 330 */ 331 boolean mShowExplicitlyRequested; 332 333 /** 334 * Set if we were forced to be shown. 335 */ 336 boolean mShowForced; 337 338 /** 339 * Set if we last told the input method to show itself. 340 */ 341 boolean mInputShown; 342 343 /** 344 * The Intent used to connect to the current input method. 345 */ 346 Intent mCurIntent; 347 348 /** 349 * The token we have made for the currently active input method, to 350 * identify it in the future. 351 */ 352 IBinder mCurToken; 353 354 /** 355 * If non-null, this is the input method service we are currently connected 356 * to. 357 */ 358 IInputMethod mCurMethod; 359 360 /** 361 * Time that we last initiated a bind to the input method, to determine 362 * if we should try to disconnect and reconnect to it. 363 */ 364 long mLastBindTime; 365 366 /** 367 * Have we called mCurMethod.bindInput()? 368 */ 369 boolean mBoundToMethod; 370 371 /** 372 * Currently enabled session. Only touched by service thread, not 373 * protected by a lock. 374 */ 375 SessionState mEnabledSession; 376 377 /** 378 * True if the screen is on. The value is true initially. 379 */ 380 boolean mScreenOn = true; 381 382 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT; 383 int mImeWindowVis; 384 385 private AlertDialog.Builder mDialogBuilder; 386 private AlertDialog mSwitchingDialog; 387 private View mSwitchingDialogTitleView; 388 private InputMethodInfo[] mIms; 389 private int[] mSubtypeIds; 390 private Locale mLastSystemLocale; 391 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor(); 392 private final IPackageManager mIPackageManager; 393 394 class SettingsObserver extends ContentObserver { 395 String mLastEnabled = ""; 396 397 SettingsObserver(Handler handler) { 398 super(handler); 399 ContentResolver resolver = mContext.getContentResolver(); 400 resolver.registerContentObserver(Settings.Secure.getUriFor( 401 Settings.Secure.DEFAULT_INPUT_METHOD), false, this); 402 resolver.registerContentObserver(Settings.Secure.getUriFor( 403 Settings.Secure.ENABLED_INPUT_METHODS), false, this); 404 resolver.registerContentObserver(Settings.Secure.getUriFor( 405 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this); 406 } 407 408 @Override public void onChange(boolean selfChange) { 409 synchronized (mMethodMap) { 410 boolean enabledChanged = false; 411 String newEnabled = mSettings.getEnabledInputMethodsStr(); 412 if (!mLastEnabled.equals(newEnabled)) { 413 mLastEnabled = newEnabled; 414 enabledChanged = true; 415 } 416 updateFromSettingsLocked(enabledChanged); 417 } 418 } 419 } 420 421 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver { 422 private void updateActive() { 423 // Inform the current client of the change in active status 424 if (mCurClient != null && mCurClient.client != null) { 425 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( 426 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient)); 427 } 428 } 429 430 @Override 431 public void onReceive(Context context, Intent intent) { 432 final String action = intent.getAction(); 433 if (Intent.ACTION_SCREEN_ON.equals(action)) { 434 mScreenOn = true; 435 refreshImeWindowVisibilityLocked(); 436 updateActive(); 437 return; 438 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { 439 mScreenOn = false; 440 setImeWindowVisibilityStatusHiddenLocked(); 441 updateActive(); 442 return; 443 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) { 444 hideInputMethodMenu(); 445 // No need to updateActive 446 return; 447 } else if (Intent.ACTION_USER_ADDED.equals(action) 448 || Intent.ACTION_USER_REMOVED.equals(action)) { 449 updateCurrentProfileIds(); 450 return; 451 } else { 452 Slog.w(TAG, "Unexpected intent " + intent); 453 } 454 } 455 } 456 457 class MyPackageMonitor extends PackageMonitor { 458 private boolean isChangingPackagesOfCurrentUser() { 459 final int userId = getChangingUserId(); 460 final boolean retval = userId == mSettings.getCurrentUserId(); 461 if (DEBUG) { 462 if (!retval) { 463 Slog.d(TAG, "--- ignore this call back from a background user: " + userId); 464 } 465 } 466 return retval; 467 } 468 469 @Override 470 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) { 471 if (!isChangingPackagesOfCurrentUser()) { 472 return false; 473 } 474 synchronized (mMethodMap) { 475 String curInputMethodId = mSettings.getSelectedInputMethod(); 476 final int N = mMethodList.size(); 477 if (curInputMethodId != null) { 478 for (int i=0; i<N; i++) { 479 InputMethodInfo imi = mMethodList.get(i); 480 if (imi.getId().equals(curInputMethodId)) { 481 for (String pkg : packages) { 482 if (imi.getPackageName().equals(pkg)) { 483 if (!doit) { 484 return true; 485 } 486 resetSelectedInputMethodAndSubtypeLocked(""); 487 chooseNewDefaultIMELocked(); 488 return true; 489 } 490 } 491 } 492 } 493 } 494 } 495 return false; 496 } 497 498 @Override 499 public void onSomePackagesChanged() { 500 if (!isChangingPackagesOfCurrentUser()) { 501 return; 502 } 503 synchronized (mMethodMap) { 504 InputMethodInfo curIm = null; 505 String curInputMethodId = mSettings.getSelectedInputMethod(); 506 final int N = mMethodList.size(); 507 if (curInputMethodId != null) { 508 for (int i=0; i<N; i++) { 509 InputMethodInfo imi = mMethodList.get(i); 510 final String imiId = imi.getId(); 511 if (imiId.equals(curInputMethodId)) { 512 curIm = imi; 513 } 514 515 int change = isPackageDisappearing(imi.getPackageName()); 516 if (isPackageModified(imi.getPackageName())) { 517 mFileManager.deleteAllInputMethodSubtypes(imiId); 518 } 519 if (change == PACKAGE_TEMPORARY_CHANGE 520 || change == PACKAGE_PERMANENT_CHANGE) { 521 Slog.i(TAG, "Input method uninstalled, disabling: " 522 + imi.getComponent()); 523 setInputMethodEnabledLocked(imi.getId(), false); 524 } 525 } 526 } 527 528 buildInputMethodListLocked( 529 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */); 530 531 boolean changed = false; 532 533 if (curIm != null) { 534 int change = isPackageDisappearing(curIm.getPackageName()); 535 if (change == PACKAGE_TEMPORARY_CHANGE 536 || change == PACKAGE_PERMANENT_CHANGE) { 537 ServiceInfo si = null; 538 try { 539 si = mIPackageManager.getServiceInfo( 540 curIm.getComponent(), 0, mSettings.getCurrentUserId()); 541 } catch (RemoteException ex) { 542 } 543 if (si == null) { 544 // Uh oh, current input method is no longer around! 545 // Pick another one... 546 Slog.i(TAG, "Current input method removed: " + curInputMethodId); 547 setImeWindowVisibilityStatusHiddenLocked(); 548 if (!chooseNewDefaultIMELocked()) { 549 changed = true; 550 curIm = null; 551 Slog.i(TAG, "Unsetting current input method"); 552 resetSelectedInputMethodAndSubtypeLocked(""); 553 } 554 } 555 } 556 } 557 558 if (curIm == null) { 559 // We currently don't have a default input method... is 560 // one now available? 561 changed = chooseNewDefaultIMELocked(); 562 } 563 564 if (changed) { 565 updateFromSettingsLocked(false); 566 } 567 } 568 } 569 } 570 571 private static final class MethodCallback extends IInputSessionCallback.Stub { 572 private final InputMethodManagerService mParentIMMS; 573 private final IInputMethod mMethod; 574 private final InputChannel mChannel; 575 576 MethodCallback(InputMethodManagerService imms, IInputMethod method, 577 InputChannel channel) { 578 mParentIMMS = imms; 579 mMethod = method; 580 mChannel = channel; 581 } 582 583 @Override 584 public void sessionCreated(IInputMethodSession session) { 585 long ident = Binder.clearCallingIdentity(); 586 try { 587 mParentIMMS.onSessionCreated(mMethod, session, mChannel); 588 } finally { 589 Binder.restoreCallingIdentity(ident); 590 } 591 } 592 } 593 594 private class HardKeyboardListener 595 implements WindowManagerService.OnHardKeyboardStatusChangeListener { 596 @Override 597 public void onHardKeyboardStatusChange(boolean available, boolean enabled) { 598 mHandler.sendMessage(mHandler.obtainMessage( 599 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0)); 600 } 601 602 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) { 603 if (DEBUG) { 604 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = " 605 + enabled); 606 } 607 synchronized(mMethodMap) { 608 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null 609 && mSwitchingDialog.isShowing()) { 610 mSwitchingDialogTitleView.findViewById( 611 com.android.internal.R.id.hard_keyboard_section).setVisibility( 612 available ? View.VISIBLE : View.GONE); 613 } 614 } 615 } 616 } 617 618 public InputMethodManagerService(Context context, WindowManagerService windowManager) { 619 mIPackageManager = AppGlobals.getPackageManager(); 620 mContext = context; 621 mRes = context.getResources(); 622 mHandler = new Handler(this); 623 mIWindowManager = IWindowManager.Stub.asInterface( 624 ServiceManager.getService(Context.WINDOW_SERVICE)); 625 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() { 626 @Override 627 public void executeMessage(Message msg) { 628 handleMessage(msg); 629 } 630 }, true /*asyncHandler*/); 631 mWindowManagerService = windowManager; 632 mHardKeyboardListener = new HardKeyboardListener(); 633 mHasFeature = context.getPackageManager().hasSystemFeature( 634 PackageManager.FEATURE_INPUT_METHODS); 635 636 mImeSwitcherNotification = new Notification(); 637 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default; 638 mImeSwitcherNotification.when = 0; 639 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT; 640 mImeSwitcherNotification.tickerText = null; 641 mImeSwitcherNotification.defaults = 0; // please be quiet 642 mImeSwitcherNotification.sound = null; 643 mImeSwitcherNotification.vibrate = null; 644 645 // Tag this notification specially so SystemUI knows it's important 646 mImeSwitcherNotification.extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true); 647 mImeSwitcherNotification.category = Notification.CATEGORY_SYSTEM; 648 649 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER); 650 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0); 651 652 mShowOngoingImeSwitcherForPhones = false; 653 654 final IntentFilter broadcastFilter = new IntentFilter(); 655 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON); 656 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF); 657 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); 658 broadcastFilter.addAction(Intent.ACTION_USER_ADDED); 659 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED); 660 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter); 661 662 mNotificationShown = false; 663 int userId = 0; 664 try { 665 ActivityManagerNative.getDefault().registerUserSwitchObserver( 666 new IUserSwitchObserver.Stub() { 667 @Override 668 public void onUserSwitching(int newUserId, IRemoteCallback reply) { 669 synchronized(mMethodMap) { 670 switchUserLocked(newUserId); 671 } 672 if (reply != null) { 673 try { 674 reply.sendResult(null); 675 } catch (RemoteException e) { 676 } 677 } 678 } 679 680 @Override 681 public void onUserSwitchComplete(int newUserId) throws RemoteException { 682 } 683 }); 684 userId = ActivityManagerNative.getDefault().getCurrentUser().id; 685 } catch (RemoteException e) { 686 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e); 687 } 688 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true); 689 690 // mSettings should be created before buildInputMethodListLocked 691 mSettings = new InputMethodSettings( 692 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId); 693 updateCurrentProfileIds(); 694 mFileManager = new InputMethodFileManager(mMethodMap, userId); 695 synchronized (mMethodMap) { 696 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked( 697 mSettings, context); 698 } 699 700 // Just checking if defaultImiId is empty or not 701 final String defaultImiId = mSettings.getSelectedInputMethod(); 702 if (DEBUG) { 703 Slog.d(TAG, "Initial default ime = " + defaultImiId); 704 } 705 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId); 706 707 synchronized (mMethodMap) { 708 buildInputMethodListLocked(mMethodList, mMethodMap, 709 !mImeSelectedOnBoot /* resetDefaultEnabledIme */); 710 } 711 mSettings.enableAllIMEsIfThereIsNoEnabledIME(); 712 713 if (!mImeSelectedOnBoot) { 714 Slog.w(TAG, "No IME selected. Choose the most applicable IME."); 715 synchronized (mMethodMap) { 716 resetDefaultImeLocked(context); 717 } 718 } 719 720 mSettingsObserver = new SettingsObserver(mHandler); 721 synchronized (mMethodMap) { 722 updateFromSettingsLocked(true); 723 } 724 725 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME 726 // according to the new system locale. 727 final IntentFilter filter = new IntentFilter(); 728 filter.addAction(Intent.ACTION_LOCALE_CHANGED); 729 mContext.registerReceiver( 730 new BroadcastReceiver() { 731 @Override 732 public void onReceive(Context context, Intent intent) { 733 synchronized(mMethodMap) { 734 resetStateIfCurrentLocaleChangedLocked(); 735 } 736 } 737 }, filter); 738 } 739 740 private void resetDefaultImeLocked(Context context) { 741 // Do not reset the default (current) IME when it is a 3rd-party IME 742 if (mCurMethodId != null 743 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) { 744 return; 745 } 746 747 InputMethodInfo defIm = null; 748 for (InputMethodInfo imi : mMethodList) { 749 if (defIm == null) { 750 if (InputMethodUtils.isValidSystemDefaultIme( 751 mSystemReady, imi, context)) { 752 defIm = imi; 753 Slog.i(TAG, "Selected default: " + imi.getId()); 754 } 755 } 756 } 757 if (defIm == null && mMethodList.size() > 0) { 758 defIm = InputMethodUtils.getMostApplicableDefaultIME( 759 mSettings.getEnabledInputMethodListLocked()); 760 Slog.i(TAG, "No default found, using " + defIm.getId()); 761 } 762 if (defIm != null) { 763 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false); 764 } 765 } 766 767 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged, 768 final boolean resetDefaultEnabledIme) { 769 if (!mSystemReady) { 770 // not system ready 771 return; 772 } 773 final Locale newLocale = mRes.getConfiguration().locale; 774 if (!updateOnlyWhenLocaleChanged 775 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) { 776 if (!updateOnlyWhenLocaleChanged) { 777 hideCurrentInputLocked(0, null); 778 mCurMethodId = null; 779 unbindCurrentMethodLocked(true, false); 780 } 781 if (DEBUG) { 782 Slog.i(TAG, "Locale has been changed to " + newLocale); 783 } 784 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme); 785 if (!updateOnlyWhenLocaleChanged) { 786 final String selectedImiId = mSettings.getSelectedInputMethod(); 787 if (TextUtils.isEmpty(selectedImiId)) { 788 // This is the first time of the user switch and 789 // set the current ime to the proper one. 790 resetDefaultImeLocked(mContext); 791 } 792 } else { 793 // If the locale is changed, needs to reset the default ime 794 resetDefaultImeLocked(mContext); 795 } 796 updateFromSettingsLocked(true); 797 mLastSystemLocale = newLocale; 798 if (!updateOnlyWhenLocaleChanged) { 799 try { 800 startInputInnerLocked(); 801 } catch (RuntimeException e) { 802 Slog.w(TAG, "Unexpected exception", e); 803 } 804 } 805 } 806 } 807 808 private void resetStateIfCurrentLocaleChangedLocked() { 809 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */, 810 true /* resetDefaultImeLocked */); 811 } 812 813 private void switchUserLocked(int newUserId) { 814 mSettings.setCurrentUserId(newUserId); 815 updateCurrentProfileIds(); 816 // InputMethodFileManager should be reset when the user is changed 817 mFileManager = new InputMethodFileManager(mMethodMap, newUserId); 818 final String defaultImiId = mSettings.getSelectedInputMethod(); 819 // For secondary users, the list of enabled IMEs may not have been updated since the 820 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may 821 // not be empty even if the IME has been uninstalled by the primary user. 822 // Even in such cases, IMMS works fine because it will find the most applicable 823 // IME for that user. 824 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId); 825 if (DEBUG) { 826 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId); 827 } 828 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */, 829 initialUserSwitch /* needsToResetDefaultIme */); 830 if (initialUserSwitch) { 831 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(), 832 mSettings.getEnabledInputMethodListLocked()); 833 } 834 } 835 836 void updateCurrentProfileIds() { 837 List<UserInfo> profiles = 838 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId()); 839 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null 840 for (int i = 0; i < currentProfileIds.length; i++) { 841 currentProfileIds[i] = profiles.get(i).id; 842 } 843 mSettings.setCurrentProfileIds(currentProfileIds); 844 } 845 846 @Override 847 public boolean onTransact(int code, Parcel data, Parcel reply, int flags) 848 throws RemoteException { 849 try { 850 return super.onTransact(code, data, reply, flags); 851 } catch (RuntimeException e) { 852 // The input method manager only throws security exceptions, so let's 853 // log all others. 854 if (!(e instanceof SecurityException)) { 855 Slog.wtf(TAG, "Input Method Manager Crash", e); 856 } 857 throw e; 858 } 859 } 860 861 public void systemRunning(StatusBarManagerService statusBar) { 862 synchronized (mMethodMap) { 863 if (DEBUG) { 864 Slog.d(TAG, "--- systemReady"); 865 } 866 if (!mSystemReady) { 867 mSystemReady = true; 868 mKeyguardManager = 869 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); 870 mNotificationManager = (NotificationManager) 871 mContext.getSystemService(Context.NOTIFICATION_SERVICE); 872 mStatusBar = statusBar; 873 statusBar.setIconVisibility("ime", false); 874 updateImeWindowStatusLocked(); 875 mShowOngoingImeSwitcherForPhones = mRes.getBoolean( 876 com.android.internal.R.bool.show_ongoing_ime_switcher); 877 if (mShowOngoingImeSwitcherForPhones) { 878 mWindowManagerService.setOnHardKeyboardStatusChangeListener( 879 mHardKeyboardListener); 880 } 881 buildInputMethodListLocked(mMethodList, mMethodMap, 882 !mImeSelectedOnBoot /* resetDefaultEnabledIme */); 883 if (!mImeSelectedOnBoot) { 884 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here."); 885 resetStateIfCurrentLocaleChangedLocked(); 886 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed( 887 mContext.getPackageManager(), 888 mSettings.getEnabledInputMethodListLocked()); 889 } 890 mLastSystemLocale = mRes.getConfiguration().locale; 891 try { 892 startInputInnerLocked(); 893 } catch (RuntimeException e) { 894 Slog.w(TAG, "Unexpected exception", e); 895 } 896 } 897 } 898 } 899 900 private void setImeWindowVisibilityStatusHiddenLocked() { 901 mImeWindowVis = 0; 902 updateImeWindowStatusLocked(); 903 } 904 905 private void refreshImeWindowVisibilityLocked() { 906 final Configuration conf = mRes.getConfiguration(); 907 final boolean haveHardKeyboard = conf.keyboard 908 != Configuration.KEYBOARD_NOKEYS; 909 final boolean hardKeyShown = haveHardKeyboard 910 && conf.hardKeyboardHidden 911 != Configuration.HARDKEYBOARDHIDDEN_YES; 912 913 final boolean isScreenLocked = isKeyguardLocked(); 914 final boolean inputActive = !isScreenLocked && (mInputShown || hardKeyShown); 915 // We assume the softkeyboard is shown when the input is active as long as the 916 // hard keyboard is not shown. 917 final boolean inputVisible = inputActive && !hardKeyShown; 918 mImeWindowVis = (inputActive ? InputMethodService.IME_ACTIVE : 0) 919 | (inputVisible ? InputMethodService.IME_VISIBLE : 0); 920 updateImeWindowStatusLocked(); 921 } 922 923 private void updateImeWindowStatusLocked() { 924 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition); 925 } 926 927 // --------------------------------------------------------------------------------------- 928 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either 929 // 1) it comes from the system process 930 // 2) the calling process' user id is identical to the current user id IMMS thinks. 931 private boolean calledFromValidUser() { 932 final int uid = Binder.getCallingUid(); 933 final int userId = UserHandle.getUserId(uid); 934 if (DEBUG) { 935 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? " 936 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID 937 + " calling userId = " + userId + ", foreground user id = " 938 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid() 939 + InputMethodUtils.getApiCallStack()); 940 } 941 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) { 942 return true; 943 } 944 945 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the 946 // foreground user, not for the user of that process. Accordingly InputMethodManagerService 947 // must not manage background users' states in any functions. 948 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded 949 // by a token. 950 if (mContext.checkCallingOrSelfPermission( 951 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) 952 == PackageManager.PERMISSION_GRANTED) { 953 if (DEBUG) { 954 Slog.d(TAG, "--- Access granted because the calling process has " 955 + "the INTERACT_ACROSS_USERS_FULL permission"); 956 } 957 return true; 958 } 959 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" 960 + InputMethodUtils.getStackTrace()); 961 return false; 962 } 963 964 private boolean bindCurrentInputMethodService( 965 Intent service, ServiceConnection conn, int flags) { 966 if (service == null || conn == null) { 967 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn); 968 return false; 969 } 970 return mContext.bindServiceAsUser(service, conn, flags, 971 new UserHandle(mSettings.getCurrentUserId())); 972 } 973 974 @Override 975 public List<InputMethodInfo> getInputMethodList() { 976 // TODO: Make this work even for non-current users? 977 if (!calledFromValidUser()) { 978 return Collections.emptyList(); 979 } 980 synchronized (mMethodMap) { 981 return new ArrayList<InputMethodInfo>(mMethodList); 982 } 983 } 984 985 @Override 986 public List<InputMethodInfo> getEnabledInputMethodList() { 987 // TODO: Make this work even for non-current users? 988 if (!calledFromValidUser()) { 989 return Collections.emptyList(); 990 } 991 synchronized (mMethodMap) { 992 return mSettings.getEnabledInputMethodListLocked(); 993 } 994 } 995 996 /** 997 * @param imiId if null, returns enabled subtypes for the current imi 998 * @return enabled subtypes of the specified imi 999 */ 1000 @Override 1001 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId, 1002 boolean allowsImplicitlySelectedSubtypes) { 1003 // TODO: Make this work even for non-current users? 1004 if (!calledFromValidUser()) { 1005 return Collections.<InputMethodSubtype>emptyList(); 1006 } 1007 synchronized (mMethodMap) { 1008 final InputMethodInfo imi; 1009 if (imiId == null && mCurMethodId != null) { 1010 imi = mMethodMap.get(mCurMethodId); 1011 } else { 1012 imi = mMethodMap.get(imiId); 1013 } 1014 if (imi == null) { 1015 return Collections.<InputMethodSubtype>emptyList(); 1016 } 1017 return mSettings.getEnabledInputMethodSubtypeListLocked( 1018 mContext, imi, allowsImplicitlySelectedSubtypes); 1019 } 1020 } 1021 1022 @Override 1023 public void addClient(IInputMethodClient client, 1024 IInputContext inputContext, int uid, int pid) { 1025 if (!calledFromValidUser()) { 1026 return; 1027 } 1028 synchronized (mMethodMap) { 1029 mClients.put(client.asBinder(), new ClientState(client, 1030 inputContext, uid, pid)); 1031 } 1032 } 1033 1034 @Override 1035 public void removeClient(IInputMethodClient client) { 1036 if (!calledFromValidUser()) { 1037 return; 1038 } 1039 synchronized (mMethodMap) { 1040 ClientState cs = mClients.remove(client.asBinder()); 1041 if (cs != null) { 1042 clearClientSessionLocked(cs); 1043 } 1044 } 1045 } 1046 1047 void executeOrSendMessage(IInterface target, Message msg) { 1048 if (target.asBinder() instanceof Binder) { 1049 mCaller.sendMessage(msg); 1050 } else { 1051 handleMessage(msg); 1052 msg.recycle(); 1053 } 1054 } 1055 1056 void unbindCurrentClientLocked() { 1057 if (mCurClient != null) { 1058 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = " 1059 + mCurClient.client.asBinder()); 1060 if (mBoundToMethod) { 1061 mBoundToMethod = false; 1062 if (mCurMethod != null) { 1063 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO( 1064 MSG_UNBIND_INPUT, mCurMethod)); 1065 } 1066 } 1067 1068 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( 1069 MSG_SET_ACTIVE, 0, mCurClient)); 1070 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( 1071 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); 1072 mCurClient.sessionRequested = false; 1073 mCurClient = null; 1074 1075 hideInputMethodMenuLocked(); 1076 } 1077 } 1078 1079 private int getImeShowFlags() { 1080 int flags = 0; 1081 if (mShowForced) { 1082 flags |= InputMethod.SHOW_FORCED 1083 | InputMethod.SHOW_EXPLICIT; 1084 } else if (mShowExplicitlyRequested) { 1085 flags |= InputMethod.SHOW_EXPLICIT; 1086 } 1087 return flags; 1088 } 1089 1090 private int getAppShowFlags() { 1091 int flags = 0; 1092 if (mShowForced) { 1093 flags |= InputMethodManager.SHOW_FORCED; 1094 } else if (!mShowExplicitlyRequested) { 1095 flags |= InputMethodManager.SHOW_IMPLICIT; 1096 } 1097 return flags; 1098 } 1099 1100 InputBindResult attachNewInputLocked(boolean initial) { 1101 if (!mBoundToMethod) { 1102 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( 1103 MSG_BIND_INPUT, mCurMethod, mCurClient.binding)); 1104 mBoundToMethod = true; 1105 } 1106 final SessionState session = mCurClient.curSession; 1107 if (initial) { 1108 executeOrSendMessage(session.method, mCaller.obtainMessageOOO( 1109 MSG_START_INPUT, session, mCurInputContext, mCurAttribute)); 1110 } else { 1111 executeOrSendMessage(session.method, mCaller.obtainMessageOOO( 1112 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute)); 1113 } 1114 if (mShowRequested) { 1115 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input"); 1116 showCurrentInputLocked(getAppShowFlags(), null); 1117 } 1118 return new InputBindResult(session.session, 1119 session.channel != null ? session.channel.dup() : null, mCurId, mCurSeq); 1120 } 1121 1122 InputBindResult startInputLocked(IInputMethodClient client, 1123 IInputContext inputContext, EditorInfo attribute, int controlFlags) { 1124 // If no method is currently selected, do nothing. 1125 if (mCurMethodId == null) { 1126 return mNoBinding; 1127 } 1128 1129 ClientState cs = mClients.get(client.asBinder()); 1130 if (cs == null) { 1131 throw new IllegalArgumentException("unknown client " 1132 + client.asBinder()); 1133 } 1134 1135 try { 1136 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) { 1137 // Check with the window manager to make sure this client actually 1138 // has a window with focus. If not, reject. This is thread safe 1139 // because if the focus changes some time before or after, the 1140 // next client receiving focus that has any interest in input will 1141 // be calling through here after that change happens. 1142 Slog.w(TAG, "Starting input on non-focused client " + cs.client 1143 + " (uid=" + cs.uid + " pid=" + cs.pid + ")"); 1144 return null; 1145 } 1146 } catch (RemoteException e) { 1147 } 1148 1149 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags); 1150 } 1151 1152 InputBindResult startInputUncheckedLocked(ClientState cs, 1153 IInputContext inputContext, EditorInfo attribute, int controlFlags) { 1154 // If no method is currently selected, do nothing. 1155 if (mCurMethodId == null) { 1156 return mNoBinding; 1157 } 1158 1159 if (mCurClient != cs) { 1160 // Was the keyguard locked when switching over to the new client? 1161 mCurClientInKeyguard = isKeyguardLocked(); 1162 // If the client is changing, we need to switch over to the new 1163 // one. 1164 unbindCurrentClientLocked(); 1165 if (DEBUG) Slog.v(TAG, "switching to client: client = " 1166 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard); 1167 1168 // If the screen is on, inform the new client it is active 1169 if (mScreenOn) { 1170 executeOrSendMessage(cs.client, mCaller.obtainMessageIO( 1171 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs)); 1172 } 1173 } 1174 1175 // Bump up the sequence for this client and attach it. 1176 mCurSeq++; 1177 if (mCurSeq <= 0) mCurSeq = 1; 1178 mCurClient = cs; 1179 mCurInputContext = inputContext; 1180 mCurAttribute = attribute; 1181 1182 // Check if the input method is changing. 1183 if (mCurId != null && mCurId.equals(mCurMethodId)) { 1184 if (cs.curSession != null) { 1185 // Fast case: if we are already connected to the input method, 1186 // then just return it. 1187 return attachNewInputLocked( 1188 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0); 1189 } 1190 if (mHaveConnection) { 1191 if (mCurMethod != null) { 1192 // Return to client, and we will get back with it when 1193 // we have had a session made for it. 1194 requestClientSessionLocked(cs); 1195 return new InputBindResult(null, null, mCurId, mCurSeq); 1196 } else if (SystemClock.uptimeMillis() 1197 < (mLastBindTime+TIME_TO_RECONNECT)) { 1198 // In this case we have connected to the service, but 1199 // don't yet have its interface. If it hasn't been too 1200 // long since we did the connection, we'll return to 1201 // the client and wait to get the service interface so 1202 // we can report back. If it has been too long, we want 1203 // to fall through so we can try a disconnect/reconnect 1204 // to see if we can get back in touch with the service. 1205 return new InputBindResult(null, null, mCurId, mCurSeq); 1206 } else { 1207 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, 1208 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0); 1209 } 1210 } 1211 } 1212 1213 return startInputInnerLocked(); 1214 } 1215 1216 InputBindResult startInputInnerLocked() { 1217 if (mCurMethodId == null) { 1218 return mNoBinding; 1219 } 1220 1221 if (!mSystemReady) { 1222 // If the system is not yet ready, we shouldn't be running third 1223 // party code. 1224 return new InputBindResult(null, null, mCurMethodId, mCurSeq); 1225 } 1226 1227 InputMethodInfo info = mMethodMap.get(mCurMethodId); 1228 if (info == null) { 1229 throw new IllegalArgumentException("Unknown id: " + mCurMethodId); 1230 } 1231 1232 unbindCurrentMethodLocked(false, true); 1233 1234 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE); 1235 mCurIntent.setComponent(info.getComponent()); 1236 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL, 1237 com.android.internal.R.string.input_method_binding_label); 1238 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity( 1239 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0)); 1240 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE 1241 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND 1242 | Context.BIND_SHOWING_UI)) { 1243 mLastBindTime = SystemClock.uptimeMillis(); 1244 mHaveConnection = true; 1245 mCurId = info.getId(); 1246 mCurToken = new Binder(); 1247 try { 1248 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken); 1249 mIWindowManager.addWindowToken(mCurToken, 1250 WindowManager.LayoutParams.TYPE_INPUT_METHOD); 1251 } catch (RemoteException e) { 1252 } 1253 return new InputBindResult(null, null, mCurId, mCurSeq); 1254 } else { 1255 mCurIntent = null; 1256 Slog.w(TAG, "Failure connecting to input method service: " 1257 + mCurIntent); 1258 } 1259 return null; 1260 } 1261 1262 @Override 1263 public InputBindResult startInput(IInputMethodClient client, 1264 IInputContext inputContext, EditorInfo attribute, int controlFlags) { 1265 if (!calledFromValidUser()) { 1266 return null; 1267 } 1268 synchronized (mMethodMap) { 1269 final long ident = Binder.clearCallingIdentity(); 1270 try { 1271 return startInputLocked(client, inputContext, attribute, controlFlags); 1272 } finally { 1273 Binder.restoreCallingIdentity(ident); 1274 } 1275 } 1276 } 1277 1278 @Override 1279 public void finishInput(IInputMethodClient client) { 1280 } 1281 1282 @Override 1283 public void onServiceConnected(ComponentName name, IBinder service) { 1284 synchronized (mMethodMap) { 1285 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) { 1286 mCurMethod = IInputMethod.Stub.asInterface(service); 1287 if (mCurToken == null) { 1288 Slog.w(TAG, "Service connected without a token!"); 1289 unbindCurrentMethodLocked(false, false); 1290 return; 1291 } 1292 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken); 1293 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( 1294 MSG_ATTACH_TOKEN, mCurMethod, mCurToken)); 1295 if (mCurClient != null) { 1296 clearClientSessionLocked(mCurClient); 1297 requestClientSessionLocked(mCurClient); 1298 } 1299 } 1300 } 1301 } 1302 1303 void onSessionCreated(IInputMethod method, IInputMethodSession session, 1304 InputChannel channel) { 1305 synchronized (mMethodMap) { 1306 if (mCurMethod != null && method != null 1307 && mCurMethod.asBinder() == method.asBinder()) { 1308 if (mCurClient != null) { 1309 clearClientSessionLocked(mCurClient); 1310 mCurClient.curSession = new SessionState(mCurClient, 1311 method, session, channel); 1312 InputBindResult res = attachNewInputLocked(true); 1313 if (res.method != null) { 1314 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO( 1315 MSG_BIND_METHOD, mCurClient.client, res)); 1316 } 1317 return; 1318 } 1319 } 1320 } 1321 1322 // Session abandoned. Close its associated input channel. 1323 channel.dispose(); 1324 } 1325 1326 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) { 1327 if (mVisibleBound) { 1328 mContext.unbindService(mVisibleConnection); 1329 mVisibleBound = false; 1330 } 1331 1332 if (mHaveConnection) { 1333 mContext.unbindService(this); 1334 mHaveConnection = false; 1335 } 1336 1337 if (mCurToken != null) { 1338 try { 1339 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken); 1340 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) { 1341 // The current IME is shown. Hence an IME switch (transition) is happening. 1342 mWindowManagerService.saveLastInputMethodWindowForTransition(); 1343 } 1344 mIWindowManager.removeWindowToken(mCurToken); 1345 } catch (RemoteException e) { 1346 } 1347 mCurToken = null; 1348 } 1349 1350 mCurId = null; 1351 clearCurMethodLocked(); 1352 1353 if (reportToClient && mCurClient != null) { 1354 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( 1355 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); 1356 } 1357 } 1358 1359 void requestClientSessionLocked(ClientState cs) { 1360 if (!cs.sessionRequested) { 1361 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs); 1362 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString()); 1363 cs.sessionRequested = true; 1364 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO( 1365 MSG_CREATE_SESSION, mCurMethod, channels[1], 1366 new MethodCallback(this, mCurMethod, channels[0]))); 1367 } 1368 } 1369 1370 void clearClientSessionLocked(ClientState cs) { 1371 finishSessionLocked(cs.curSession); 1372 cs.curSession = null; 1373 cs.sessionRequested = false; 1374 } 1375 1376 private void finishSessionLocked(SessionState sessionState) { 1377 if (sessionState != null) { 1378 if (sessionState.session != null) { 1379 try { 1380 sessionState.session.finishSession(); 1381 } catch (RemoteException e) { 1382 Slog.w(TAG, "Session failed to close due to remote exception", e); 1383 setImeWindowVisibilityStatusHiddenLocked(); 1384 } 1385 sessionState.session = null; 1386 } 1387 if (sessionState.channel != null) { 1388 sessionState.channel.dispose(); 1389 sessionState.channel = null; 1390 } 1391 } 1392 } 1393 1394 void clearCurMethodLocked() { 1395 if (mCurMethod != null) { 1396 for (ClientState cs : mClients.values()) { 1397 clearClientSessionLocked(cs); 1398 } 1399 1400 finishSessionLocked(mEnabledSession); 1401 mEnabledSession = null; 1402 mCurMethod = null; 1403 } 1404 if (mStatusBar != null) { 1405 mStatusBar.setIconVisibility("ime", false); 1406 } 1407 } 1408 1409 @Override 1410 public void onServiceDisconnected(ComponentName name) { 1411 synchronized (mMethodMap) { 1412 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name 1413 + " mCurIntent=" + mCurIntent); 1414 if (mCurMethod != null && mCurIntent != null 1415 && name.equals(mCurIntent.getComponent())) { 1416 clearCurMethodLocked(); 1417 // We consider this to be a new bind attempt, since the system 1418 // should now try to restart the service for us. 1419 mLastBindTime = SystemClock.uptimeMillis(); 1420 mShowRequested = mInputShown; 1421 mInputShown = false; 1422 if (mCurClient != null) { 1423 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( 1424 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); 1425 } 1426 } 1427 } 1428 } 1429 1430 @Override 1431 public void updateStatusIcon(IBinder token, String packageName, int iconId) { 1432 int uid = Binder.getCallingUid(); 1433 long ident = Binder.clearCallingIdentity(); 1434 try { 1435 if (token == null || mCurToken != token) { 1436 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token); 1437 return; 1438 } 1439 1440 synchronized (mMethodMap) { 1441 if (iconId == 0) { 1442 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method"); 1443 if (mStatusBar != null) { 1444 mStatusBar.setIconVisibility("ime", false); 1445 } 1446 } else if (packageName != null) { 1447 if (DEBUG) Slog.d(TAG, "show a small icon for the input method"); 1448 CharSequence contentDescription = null; 1449 try { 1450 // Use PackageManager to load label 1451 final PackageManager packageManager = mContext.getPackageManager(); 1452 contentDescription = packageManager.getApplicationLabel( 1453 mIPackageManager.getApplicationInfo(packageName, 0, 1454 mSettings.getCurrentUserId())); 1455 } catch (RemoteException e) { 1456 /* ignore */ 1457 } 1458 if (mStatusBar != null) { 1459 mStatusBar.setIcon("ime", packageName, iconId, 0, 1460 contentDescription != null 1461 ? contentDescription.toString() : null); 1462 mStatusBar.setIconVisibility("ime", true); 1463 } 1464 } 1465 } 1466 } finally { 1467 Binder.restoreCallingIdentity(ident); 1468 } 1469 } 1470 1471 private boolean needsToShowImeSwitchOngoingNotification() { 1472 if (!mShowOngoingImeSwitcherForPhones) return false; 1473 if (mSwitchingDialog != null) return false; 1474 if (isScreenLocked()) return false; 1475 synchronized (mMethodMap) { 1476 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked(); 1477 final int N = imis.size(); 1478 if (N > 2) return true; 1479 if (N < 1) return false; 1480 int nonAuxCount = 0; 1481 int auxCount = 0; 1482 InputMethodSubtype nonAuxSubtype = null; 1483 InputMethodSubtype auxSubtype = null; 1484 for(int i = 0; i < N; ++i) { 1485 final InputMethodInfo imi = imis.get(i); 1486 final List<InputMethodSubtype> subtypes = 1487 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true); 1488 final int subtypeCount = subtypes.size(); 1489 if (subtypeCount == 0) { 1490 ++nonAuxCount; 1491 } else { 1492 for (int j = 0; j < subtypeCount; ++j) { 1493 final InputMethodSubtype subtype = subtypes.get(j); 1494 if (!subtype.isAuxiliary()) { 1495 ++nonAuxCount; 1496 nonAuxSubtype = subtype; 1497 } else { 1498 ++auxCount; 1499 auxSubtype = subtype; 1500 } 1501 } 1502 } 1503 } 1504 if (nonAuxCount > 1 || auxCount > 1) { 1505 return true; 1506 } else if (nonAuxCount == 1 && auxCount == 1) { 1507 if (nonAuxSubtype != null && auxSubtype != null 1508 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale()) 1509 || auxSubtype.overridesImplicitlyEnabledSubtype() 1510 || nonAuxSubtype.overridesImplicitlyEnabledSubtype()) 1511 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) { 1512 return false; 1513 } 1514 return true; 1515 } 1516 return false; 1517 } 1518 } 1519 1520 private boolean isKeyguardLocked() { 1521 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked(); 1522 } 1523 1524 // Caution! This method is called in this class. Handle multi-user carefully 1525 @SuppressWarnings("deprecation") 1526 @Override 1527 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) { 1528 final long ident = Binder.clearCallingIdentity(); 1529 try { 1530 if (token == null || mCurToken != token) { 1531 int uid = Binder.getCallingUid(); 1532 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token); 1533 return; 1534 } 1535 synchronized (mMethodMap) { 1536 // apply policy for binder calls 1537 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) { 1538 vis = 0; 1539 } 1540 mImeWindowVis = vis; 1541 mBackDisposition = backDisposition; 1542 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0) 1543 && (mWindowManagerService.isHardKeyboardAvailable() 1544 || (vis & (InputMethodService.IME_VISIBLE)) != 0); 1545 final boolean needsToShowImeSwitcher = iconVisibility 1546 && needsToShowImeSwitchOngoingNotification(); 1547 if (mStatusBar != null) { 1548 mStatusBar.setImeWindowStatus(token, vis, backDisposition, 1549 needsToShowImeSwitcher); 1550 } 1551 final InputMethodInfo imi = mMethodMap.get(mCurMethodId); 1552 if (imi != null && needsToShowImeSwitcher) { 1553 // Used to load label 1554 final CharSequence title = mRes.getText( 1555 com.android.internal.R.string.select_input_method); 1556 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName( 1557 mContext, imi, mCurrentSubtype); 1558 1559 mImeSwitcherNotification.setLatestEventInfo( 1560 mContext, title, summary, mImeSwitchPendingIntent); 1561 if ((mNotificationManager != null) 1562 && !mWindowManagerService.hasNavigationBar()) { 1563 if (DEBUG) { 1564 Slog.d(TAG, "--- show notification: label = " + summary); 1565 } 1566 mNotificationManager.notifyAsUser(null, 1567 com.android.internal.R.string.select_input_method, 1568 mImeSwitcherNotification, UserHandle.ALL); 1569 mNotificationShown = true; 1570 } 1571 } else { 1572 if (mNotificationShown && mNotificationManager != null) { 1573 if (DEBUG) { 1574 Slog.d(TAG, "--- hide notification"); 1575 } 1576 mNotificationManager.cancelAsUser(null, 1577 com.android.internal.R.string.select_input_method, UserHandle.ALL); 1578 mNotificationShown = false; 1579 } 1580 } 1581 } 1582 } finally { 1583 Binder.restoreCallingIdentity(ident); 1584 } 1585 } 1586 1587 @Override 1588 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) { 1589 if (!calledFromValidUser()) { 1590 return; 1591 } 1592 synchronized (mMethodMap) { 1593 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId); 1594 for (int i = 0; i < spans.length; ++i) { 1595 SuggestionSpan ss = spans[i]; 1596 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) { 1597 mSecureSuggestionSpans.put(ss, currentImi); 1598 } 1599 } 1600 } 1601 } 1602 1603 @Override 1604 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) { 1605 if (!calledFromValidUser()) { 1606 return false; 1607 } 1608 synchronized (mMethodMap) { 1609 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span); 1610 // TODO: Do not send the intent if the process of the targetImi is already dead. 1611 if (targetImi != null) { 1612 final String[] suggestions = span.getSuggestions(); 1613 if (index < 0 || index >= suggestions.length) return false; 1614 final String className = span.getNotificationTargetClassName(); 1615 final Intent intent = new Intent(); 1616 // Ensures that only a class in the original IME package will receive the 1617 // notification. 1618 intent.setClassName(targetImi.getPackageName(), className); 1619 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED); 1620 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString); 1621 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]); 1622 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode()); 1623 final long ident = Binder.clearCallingIdentity(); 1624 try { 1625 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT); 1626 } finally { 1627 Binder.restoreCallingIdentity(ident); 1628 } 1629 return true; 1630 } 1631 } 1632 return false; 1633 } 1634 1635 void updateFromSettingsLocked(boolean enabledMayChange) { 1636 if (enabledMayChange) { 1637 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked(); 1638 for (int i=0; i<enabled.size(); i++) { 1639 // We allow the user to select "disabled until used" apps, so if they 1640 // are enabling one of those here we now need to make it enabled. 1641 InputMethodInfo imm = enabled.get(i); 1642 try { 1643 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(), 1644 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS, 1645 mSettings.getCurrentUserId()); 1646 if (ai != null && ai.enabledSetting 1647 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) { 1648 if (DEBUG) { 1649 Slog.d(TAG, "Update state(" + imm.getId() 1650 + "): DISABLED_UNTIL_USED -> DEFAULT"); 1651 } 1652 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(), 1653 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 1654 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(), 1655 mContext.getBasePackageName()); 1656 } 1657 } catch (RemoteException e) { 1658 } 1659 } 1660 } 1661 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and 1662 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in 1663 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not 1664 // enabled. 1665 String id = mSettings.getSelectedInputMethod(); 1666 // There is no input method selected, try to choose new applicable input method. 1667 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) { 1668 id = mSettings.getSelectedInputMethod(); 1669 } 1670 if (!TextUtils.isEmpty(id)) { 1671 try { 1672 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id)); 1673 } catch (IllegalArgumentException e) { 1674 Slog.w(TAG, "Unknown input method from prefs: " + id, e); 1675 mCurMethodId = null; 1676 unbindCurrentMethodLocked(true, false); 1677 } 1678 mShortcutInputMethodsAndSubtypes.clear(); 1679 } else { 1680 // There is no longer an input method set, so stop any current one. 1681 mCurMethodId = null; 1682 unbindCurrentMethodLocked(true, false); 1683 } 1684 // Here is not the perfect place to reset the switching controller. Ideally 1685 // mSwitchingController and mSettings should be able to share the same state. 1686 // TODO: Make sure that mSwitchingController and mSettings are sharing the 1687 // the same enabled IMEs list. 1688 mSwitchingController.resetCircularListLocked(mContext); 1689 } 1690 1691 /* package */ void setInputMethodLocked(String id, int subtypeId) { 1692 InputMethodInfo info = mMethodMap.get(id); 1693 if (info == null) { 1694 throw new IllegalArgumentException("Unknown id: " + id); 1695 } 1696 1697 // See if we need to notify a subtype change within the same IME. 1698 if (id.equals(mCurMethodId)) { 1699 final int subtypeCount = info.getSubtypeCount(); 1700 if (subtypeCount <= 0) { 1701 return; 1702 } 1703 final InputMethodSubtype oldSubtype = mCurrentSubtype; 1704 final InputMethodSubtype newSubtype; 1705 if (subtypeId >= 0 && subtypeId < subtypeCount) { 1706 newSubtype = info.getSubtypeAt(subtypeId); 1707 } else { 1708 // If subtype is null, try to find the most applicable one from 1709 // getCurrentInputMethodSubtype. 1710 newSubtype = getCurrentInputMethodSubtypeLocked(); 1711 } 1712 if (newSubtype == null || oldSubtype == null) { 1713 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype 1714 + ", new subtype = " + newSubtype); 1715 return; 1716 } 1717 if (newSubtype != oldSubtype) { 1718 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true); 1719 if (mCurMethod != null) { 1720 try { 1721 refreshImeWindowVisibilityLocked(); 1722 mCurMethod.changeInputMethodSubtype(newSubtype); 1723 } catch (RemoteException e) { 1724 Slog.w(TAG, "Failed to call changeInputMethodSubtype"); 1725 } 1726 } 1727 } 1728 return; 1729 } 1730 1731 // Changing to a different IME. 1732 final long ident = Binder.clearCallingIdentity(); 1733 try { 1734 // Set a subtype to this input method. 1735 // subtypeId the name of a subtype which will be set. 1736 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false); 1737 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked() 1738 // because mCurMethodId is stored as a history in 1739 // setSelectedInputMethodAndSubtypeLocked(). 1740 mCurMethodId = id; 1741 1742 if (ActivityManagerNative.isSystemReady()) { 1743 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED); 1744 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); 1745 intent.putExtra("input_method_id", id); 1746 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT); 1747 } 1748 unbindCurrentClientLocked(); 1749 } finally { 1750 Binder.restoreCallingIdentity(ident); 1751 } 1752 } 1753 1754 @Override 1755 public boolean showSoftInput(IInputMethodClient client, int flags, 1756 ResultReceiver resultReceiver) { 1757 if (!calledFromValidUser()) { 1758 return false; 1759 } 1760 int uid = Binder.getCallingUid(); 1761 long ident = Binder.clearCallingIdentity(); 1762 try { 1763 synchronized (mMethodMap) { 1764 if (mCurClient == null || client == null 1765 || mCurClient.client.asBinder() != client.asBinder()) { 1766 try { 1767 // We need to check if this is the current client with 1768 // focus in the window manager, to allow this call to 1769 // be made before input is started in it. 1770 if (!mIWindowManager.inputMethodClientHasFocus(client)) { 1771 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client); 1772 return false; 1773 } 1774 } catch (RemoteException e) { 1775 return false; 1776 } 1777 } 1778 1779 if (DEBUG) Slog.v(TAG, "Client requesting input be shown"); 1780 return showCurrentInputLocked(flags, resultReceiver); 1781 } 1782 } finally { 1783 Binder.restoreCallingIdentity(ident); 1784 } 1785 } 1786 1787 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) { 1788 mShowRequested = true; 1789 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) { 1790 mShowExplicitlyRequested = true; 1791 } 1792 if ((flags&InputMethodManager.SHOW_FORCED) != 0) { 1793 mShowExplicitlyRequested = true; 1794 mShowForced = true; 1795 } 1796 1797 if (!mSystemReady) { 1798 return false; 1799 } 1800 1801 boolean res = false; 1802 if (mCurMethod != null) { 1803 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken); 1804 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO( 1805 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod, 1806 resultReceiver)); 1807 mInputShown = true; 1808 if (mHaveConnection && !mVisibleBound) { 1809 bindCurrentInputMethodService( 1810 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE 1811 | Context.BIND_TREAT_LIKE_ACTIVITY); 1812 mVisibleBound = true; 1813 } 1814 res = true; 1815 } else if (mHaveConnection && SystemClock.uptimeMillis() 1816 >= (mLastBindTime+TIME_TO_RECONNECT)) { 1817 // The client has asked to have the input method shown, but 1818 // we have been sitting here too long with a connection to the 1819 // service and no interface received, so let's disconnect/connect 1820 // to try to prod things along. 1821 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId, 1822 SystemClock.uptimeMillis()-mLastBindTime,1); 1823 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()"); 1824 mContext.unbindService(this); 1825 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE 1826 | Context.BIND_NOT_VISIBLE); 1827 } else { 1828 if (DEBUG) { 1829 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = " 1830 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis())); 1831 } 1832 } 1833 1834 return res; 1835 } 1836 1837 @Override 1838 public boolean hideSoftInput(IInputMethodClient client, int flags, 1839 ResultReceiver resultReceiver) { 1840 if (!calledFromValidUser()) { 1841 return false; 1842 } 1843 int uid = Binder.getCallingUid(); 1844 long ident = Binder.clearCallingIdentity(); 1845 try { 1846 synchronized (mMethodMap) { 1847 if (mCurClient == null || client == null 1848 || mCurClient.client.asBinder() != client.asBinder()) { 1849 try { 1850 // We need to check if this is the current client with 1851 // focus in the window manager, to allow this call to 1852 // be made before input is started in it. 1853 if (!mIWindowManager.inputMethodClientHasFocus(client)) { 1854 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid " 1855 + uid + ": " + client); 1856 setImeWindowVisibilityStatusHiddenLocked(); 1857 return false; 1858 } 1859 } catch (RemoteException e) { 1860 setImeWindowVisibilityStatusHiddenLocked(); 1861 return false; 1862 } 1863 } 1864 1865 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden"); 1866 return hideCurrentInputLocked(flags, resultReceiver); 1867 } 1868 } finally { 1869 Binder.restoreCallingIdentity(ident); 1870 } 1871 } 1872 1873 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) { 1874 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0 1875 && (mShowExplicitlyRequested || mShowForced)) { 1876 if (DEBUG) Slog.v(TAG, "Not hiding: explicit show not cancelled by non-explicit hide"); 1877 return false; 1878 } 1879 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) { 1880 if (DEBUG) Slog.v(TAG, "Not hiding: forced show not cancelled by not-always hide"); 1881 return false; 1882 } 1883 boolean res; 1884 if (mInputShown && mCurMethod != null) { 1885 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( 1886 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver)); 1887 res = true; 1888 } else { 1889 res = false; 1890 } 1891 if (mHaveConnection && mVisibleBound) { 1892 mContext.unbindService(mVisibleConnection); 1893 mVisibleBound = false; 1894 } 1895 mInputShown = false; 1896 mShowRequested = false; 1897 mShowExplicitlyRequested = false; 1898 mShowForced = false; 1899 return res; 1900 } 1901 1902 @Override 1903 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken, 1904 int controlFlags, int softInputMode, int windowFlags, 1905 EditorInfo attribute, IInputContext inputContext) { 1906 // Needs to check the validity before clearing calling identity 1907 final boolean calledFromValidUser = calledFromValidUser(); 1908 1909 InputBindResult res = null; 1910 long ident = Binder.clearCallingIdentity(); 1911 try { 1912 synchronized (mMethodMap) { 1913 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder() 1914 + " controlFlags=#" + Integer.toHexString(controlFlags) 1915 + " softInputMode=#" + Integer.toHexString(softInputMode) 1916 + " windowFlags=#" + Integer.toHexString(windowFlags)); 1917 1918 ClientState cs = mClients.get(client.asBinder()); 1919 if (cs == null) { 1920 throw new IllegalArgumentException("unknown client " 1921 + client.asBinder()); 1922 } 1923 1924 try { 1925 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) { 1926 // Check with the window manager to make sure this client actually 1927 // has a window with focus. If not, reject. This is thread safe 1928 // because if the focus changes some time before or after, the 1929 // next client receiving focus that has any interest in input will 1930 // be calling through here after that change happens. 1931 Slog.w(TAG, "Focus gain on non-focused client " + cs.client 1932 + " (uid=" + cs.uid + " pid=" + cs.pid + ")"); 1933 return null; 1934 } 1935 } catch (RemoteException e) { 1936 } 1937 1938 if (!calledFromValidUser) { 1939 Slog.w(TAG, "A background user is requesting window. Hiding IME."); 1940 Slog.w(TAG, "If you want to interect with IME, you need " 1941 + "android.permission.INTERACT_ACROSS_USERS_FULL"); 1942 hideCurrentInputLocked(0, null); 1943 return null; 1944 } 1945 1946 if (mCurFocusedWindow == windowToken) { 1947 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client 1948 + " attribute=" + attribute + ", token = " + windowToken); 1949 if (attribute != null) { 1950 return startInputUncheckedLocked(cs, inputContext, attribute, 1951 controlFlags); 1952 } 1953 return null; 1954 } 1955 mCurFocusedWindow = windowToken; 1956 1957 // Should we auto-show the IME even if the caller has not 1958 // specified what should be done with it? 1959 // We only do this automatically if the window can resize 1960 // to accommodate the IME (so what the user sees will give 1961 // them good context without input information being obscured 1962 // by the IME) or if running on a large screen where there 1963 // is more room for the target window + IME. 1964 final boolean doAutoShow = 1965 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) 1966 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE 1967 || mRes.getConfiguration().isLayoutSizeAtLeast( 1968 Configuration.SCREENLAYOUT_SIZE_LARGE); 1969 final boolean isTextEditor = 1970 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0; 1971 1972 // We want to start input before showing the IME, but after closing 1973 // it. We want to do this after closing it to help the IME disappear 1974 // more quickly (not get stuck behind it initializing itself for the 1975 // new focused input, even if its window wants to hide the IME). 1976 boolean didStart = false; 1977 1978 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) { 1979 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED: 1980 if (!isTextEditor || !doAutoShow) { 1981 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) { 1982 // There is no focus view, and this window will 1983 // be behind any soft input window, so hide the 1984 // soft input window if it is shown. 1985 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input"); 1986 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null); 1987 } 1988 } else if (isTextEditor && doAutoShow && (softInputMode & 1989 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { 1990 // There is a focus view, and we are navigating forward 1991 // into the window, so show the input window for the user. 1992 // We only do this automatically if the window can resize 1993 // to accommodate the IME (so what the user sees will give 1994 // them good context without input information being obscured 1995 // by the IME) or if running on a large screen where there 1996 // is more room for the target window + IME. 1997 if (DEBUG) Slog.v(TAG, "Unspecified window will show input"); 1998 if (attribute != null) { 1999 res = startInputUncheckedLocked(cs, inputContext, attribute, 2000 controlFlags); 2001 didStart = true; 2002 } 2003 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); 2004 } 2005 break; 2006 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED: 2007 // Do nothing. 2008 break; 2009 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN: 2010 if ((softInputMode & 2011 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { 2012 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward"); 2013 hideCurrentInputLocked(0, null); 2014 } 2015 break; 2016 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN: 2017 if (DEBUG) Slog.v(TAG, "Window asks to hide input"); 2018 hideCurrentInputLocked(0, null); 2019 break; 2020 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE: 2021 if ((softInputMode & 2022 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { 2023 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward"); 2024 if (attribute != null) { 2025 res = startInputUncheckedLocked(cs, inputContext, attribute, 2026 controlFlags); 2027 didStart = true; 2028 } 2029 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); 2030 } 2031 break; 2032 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE: 2033 if (DEBUG) Slog.v(TAG, "Window asks to always show input"); 2034 if (attribute != null) { 2035 res = startInputUncheckedLocked(cs, inputContext, attribute, 2036 controlFlags); 2037 didStart = true; 2038 } 2039 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); 2040 break; 2041 } 2042 2043 if (!didStart && attribute != null) { 2044 res = startInputUncheckedLocked(cs, inputContext, attribute, 2045 controlFlags); 2046 } 2047 } 2048 } finally { 2049 Binder.restoreCallingIdentity(ident); 2050 } 2051 2052 return res; 2053 } 2054 2055 @Override 2056 public void showInputMethodPickerFromClient(IInputMethodClient client) { 2057 if (!calledFromValidUser()) { 2058 return; 2059 } 2060 synchronized (mMethodMap) { 2061 if (mCurClient == null || client == null 2062 || mCurClient.client.asBinder() != client.asBinder()) { 2063 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid " 2064 + Binder.getCallingUid() + ": " + client); 2065 } 2066 2067 // Always call subtype picker, because subtype picker is a superset of input method 2068 // picker. 2069 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER); 2070 } 2071 } 2072 2073 @Override 2074 public void setInputMethod(IBinder token, String id) { 2075 if (!calledFromValidUser()) { 2076 return; 2077 } 2078 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID); 2079 } 2080 2081 @Override 2082 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) { 2083 if (!calledFromValidUser()) { 2084 return; 2085 } 2086 synchronized (mMethodMap) { 2087 if (subtype != null) { 2088 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode( 2089 mMethodMap.get(id), subtype.hashCode())); 2090 } else { 2091 setInputMethod(token, id); 2092 } 2093 } 2094 } 2095 2096 @Override 2097 public void showInputMethodAndSubtypeEnablerFromClient( 2098 IInputMethodClient client, String inputMethodId) { 2099 if (!calledFromValidUser()) { 2100 return; 2101 } 2102 synchronized (mMethodMap) { 2103 if (mCurClient == null || client == null 2104 || mCurClient.client.asBinder() != client.asBinder()) { 2105 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client); 2106 } 2107 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO( 2108 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId)); 2109 } 2110 } 2111 2112 @Override 2113 public boolean switchToLastInputMethod(IBinder token) { 2114 if (!calledFromValidUser()) { 2115 return false; 2116 } 2117 synchronized (mMethodMap) { 2118 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked(); 2119 final InputMethodInfo lastImi; 2120 if (lastIme != null) { 2121 lastImi = mMethodMap.get(lastIme.first); 2122 } else { 2123 lastImi = null; 2124 } 2125 String targetLastImiId = null; 2126 int subtypeId = NOT_A_SUBTYPE_ID; 2127 if (lastIme != null && lastImi != null) { 2128 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId); 2129 final int lastSubtypeHash = Integer.valueOf(lastIme.second); 2130 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID 2131 : mCurrentSubtype.hashCode(); 2132 // If the last IME is the same as the current IME and the last subtype is not 2133 // defined, there is no need to switch to the last IME. 2134 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) { 2135 targetLastImiId = lastIme.first; 2136 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash); 2137 } 2138 } 2139 2140 if (TextUtils.isEmpty(targetLastImiId) 2141 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) { 2142 // This is a safety net. If the currentSubtype can't be added to the history 2143 // and the framework couldn't find the last ime, we will make the last ime be 2144 // the most applicable enabled keyboard subtype of the system imes. 2145 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked(); 2146 if (enabled != null) { 2147 final int N = enabled.size(); 2148 final String locale = mCurrentSubtype == null 2149 ? mRes.getConfiguration().locale.toString() 2150 : mCurrentSubtype.getLocale(); 2151 for (int i = 0; i < N; ++i) { 2152 final InputMethodInfo imi = enabled.get(i); 2153 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) { 2154 InputMethodSubtype keyboardSubtype = 2155 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes, 2156 InputMethodUtils.getSubtypes(imi), 2157 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true); 2158 if (keyboardSubtype != null) { 2159 targetLastImiId = imi.getId(); 2160 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode( 2161 imi, keyboardSubtype.hashCode()); 2162 if(keyboardSubtype.getLocale().equals(locale)) { 2163 break; 2164 } 2165 } 2166 } 2167 } 2168 } 2169 } 2170 2171 if (!TextUtils.isEmpty(targetLastImiId)) { 2172 if (DEBUG) { 2173 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second 2174 + ", from: " + mCurMethodId + ", " + subtypeId); 2175 } 2176 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId); 2177 return true; 2178 } else { 2179 return false; 2180 } 2181 } 2182 } 2183 2184 @Override 2185 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) { 2186 if (!calledFromValidUser()) { 2187 return false; 2188 } 2189 synchronized (mMethodMap) { 2190 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked( 2191 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype); 2192 if (nextSubtype == null) { 2193 return false; 2194 } 2195 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId); 2196 return true; 2197 } 2198 } 2199 2200 @Override 2201 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) { 2202 if (!calledFromValidUser()) { 2203 return false; 2204 } 2205 synchronized (mMethodMap) { 2206 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked( 2207 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype); 2208 if (nextSubtype == null) { 2209 return false; 2210 } 2211 return true; 2212 } 2213 } 2214 2215 @Override 2216 public InputMethodSubtype getLastInputMethodSubtype() { 2217 if (!calledFromValidUser()) { 2218 return null; 2219 } 2220 synchronized (mMethodMap) { 2221 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked(); 2222 // TODO: Handle the case of the last IME with no subtypes 2223 if (lastIme == null || TextUtils.isEmpty(lastIme.first) 2224 || TextUtils.isEmpty(lastIme.second)) return null; 2225 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first); 2226 if (lastImi == null) return null; 2227 try { 2228 final int lastSubtypeHash = Integer.valueOf(lastIme.second); 2229 final int lastSubtypeId = 2230 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash); 2231 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) { 2232 return null; 2233 } 2234 return lastImi.getSubtypeAt(lastSubtypeId); 2235 } catch (NumberFormatException e) { 2236 return null; 2237 } 2238 } 2239 } 2240 2241 @Override 2242 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) { 2243 if (!calledFromValidUser()) { 2244 return; 2245 } 2246 // By this IPC call, only a process which shares the same uid with the IME can add 2247 // additional input method subtypes to the IME. 2248 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return; 2249 synchronized (mMethodMap) { 2250 final InputMethodInfo imi = mMethodMap.get(imiId); 2251 if (imi == null) return; 2252 final String[] packageInfos; 2253 try { 2254 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid()); 2255 } catch (RemoteException e) { 2256 Slog.e(TAG, "Failed to get package infos"); 2257 return; 2258 } 2259 if (packageInfos != null) { 2260 final int packageNum = packageInfos.length; 2261 for (int i = 0; i < packageNum; ++i) { 2262 if (packageInfos[i].equals(imi.getPackageName())) { 2263 mFileManager.addInputMethodSubtypes(imi, subtypes); 2264 final long ident = Binder.clearCallingIdentity(); 2265 try { 2266 buildInputMethodListLocked(mMethodList, mMethodMap, 2267 false /* resetDefaultEnabledIme */); 2268 } finally { 2269 Binder.restoreCallingIdentity(ident); 2270 } 2271 return; 2272 } 2273 } 2274 } 2275 } 2276 return; 2277 } 2278 2279 @Override 2280 public int getInputMethodWindowVisibleHeight() { 2281 return mWindowManagerService.getInputMethodWindowVisibleHeight(); 2282 } 2283 2284 @Override 2285 public void notifyTextCommitted() { 2286 if (DEBUG) { 2287 Slog.d(TAG, "Got the notification of commitText"); 2288 } 2289 synchronized (mMethodMap) { 2290 final InputMethodInfo imi = mMethodMap.get(mCurMethodId); 2291 if (imi != null) { 2292 mSwitchingController.onCommitTextLocked(imi, mCurrentSubtype); 2293 } 2294 } 2295 } 2296 2297 @Override 2298 public void setCursorAnchorMonitorMode(IBinder token, int monitorMode) { 2299 if (DEBUG) { 2300 Slog.d(TAG, "setCursorAnchorMonitorMode: monitorMode=" + monitorMode); 2301 } 2302 if (!calledFromValidUser()) { 2303 return; 2304 } 2305 synchronized (mMethodMap) { 2306 if (token == null || mCurToken != token) { 2307 if (DEBUG) { 2308 Slog.w(TAG, "Ignoring setCursorAnchorMonitorMode from uid " 2309 + Binder.getCallingUid() + " token: " + token); 2310 } 2311 return; 2312 } 2313 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIO( 2314 MSG_SET_CURSOR_ANCHOR_MONITOR_MODE, monitorMode, mCurClient)); 2315 } 2316 } 2317 2318 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) { 2319 synchronized (mMethodMap) { 2320 if (token == null) { 2321 if (mContext.checkCallingOrSelfPermission( 2322 android.Manifest.permission.WRITE_SECURE_SETTINGS) 2323 != PackageManager.PERMISSION_GRANTED) { 2324 throw new SecurityException( 2325 "Using null token requires permission " 2326 + android.Manifest.permission.WRITE_SECURE_SETTINGS); 2327 } 2328 } else if (mCurToken != token) { 2329 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid() 2330 + " token: " + token); 2331 return; 2332 } 2333 2334 final long ident = Binder.clearCallingIdentity(); 2335 try { 2336 setInputMethodLocked(id, subtypeId); 2337 } finally { 2338 Binder.restoreCallingIdentity(ident); 2339 } 2340 } 2341 } 2342 2343 @Override 2344 public void hideMySoftInput(IBinder token, int flags) { 2345 if (!calledFromValidUser()) { 2346 return; 2347 } 2348 synchronized (mMethodMap) { 2349 if (token == null || mCurToken != token) { 2350 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid " 2351 + Binder.getCallingUid() + " token: " + token); 2352 return; 2353 } 2354 long ident = Binder.clearCallingIdentity(); 2355 try { 2356 hideCurrentInputLocked(flags, null); 2357 } finally { 2358 Binder.restoreCallingIdentity(ident); 2359 } 2360 } 2361 } 2362 2363 @Override 2364 public void showMySoftInput(IBinder token, int flags) { 2365 if (!calledFromValidUser()) { 2366 return; 2367 } 2368 synchronized (mMethodMap) { 2369 if (token == null || mCurToken != token) { 2370 Slog.w(TAG, "Ignoring showMySoftInput of uid " 2371 + Binder.getCallingUid() + " token: " + token); 2372 return; 2373 } 2374 long ident = Binder.clearCallingIdentity(); 2375 try { 2376 showCurrentInputLocked(flags, null); 2377 } finally { 2378 Binder.restoreCallingIdentity(ident); 2379 } 2380 } 2381 } 2382 2383 void setEnabledSessionInMainThread(SessionState session) { 2384 if (mEnabledSession != session) { 2385 if (mEnabledSession != null && mEnabledSession.session != null) { 2386 try { 2387 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession); 2388 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false); 2389 } catch (RemoteException e) { 2390 } 2391 } 2392 mEnabledSession = session; 2393 if (mEnabledSession != null && mEnabledSession.session != null) { 2394 try { 2395 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession); 2396 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true); 2397 } catch (RemoteException e) { 2398 } 2399 } 2400 } 2401 } 2402 2403 @Override 2404 public boolean handleMessage(Message msg) { 2405 SomeArgs args; 2406 switch (msg.what) { 2407 case MSG_SHOW_IM_PICKER: 2408 showInputMethodMenu(); 2409 return true; 2410 2411 case MSG_SHOW_IM_SUBTYPE_PICKER: 2412 showInputMethodSubtypeMenu(); 2413 return true; 2414 2415 case MSG_SHOW_IM_SUBTYPE_ENABLER: 2416 args = (SomeArgs)msg.obj; 2417 showInputMethodAndSubtypeEnabler((String)args.arg1); 2418 args.recycle(); 2419 return true; 2420 2421 case MSG_SHOW_IM_CONFIG: 2422 showConfigureInputMethods(); 2423 return true; 2424 2425 // --------------------------------------------------------- 2426 2427 case MSG_UNBIND_INPUT: 2428 try { 2429 ((IInputMethod)msg.obj).unbindInput(); 2430 } catch (RemoteException e) { 2431 // There is nothing interesting about the method dying. 2432 } 2433 return true; 2434 case MSG_BIND_INPUT: 2435 args = (SomeArgs)msg.obj; 2436 try { 2437 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2); 2438 } catch (RemoteException e) { 2439 } 2440 args.recycle(); 2441 return true; 2442 case MSG_SHOW_SOFT_INPUT: 2443 args = (SomeArgs)msg.obj; 2444 try { 2445 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput(" 2446 + msg.arg1 + ", " + args.arg2 + ")"); 2447 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2); 2448 } catch (RemoteException e) { 2449 } 2450 args.recycle(); 2451 return true; 2452 case MSG_HIDE_SOFT_INPUT: 2453 args = (SomeArgs)msg.obj; 2454 try { 2455 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, " 2456 + args.arg2 + ")"); 2457 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2); 2458 } catch (RemoteException e) { 2459 } 2460 args.recycle(); 2461 return true; 2462 case MSG_ATTACH_TOKEN: 2463 args = (SomeArgs)msg.obj; 2464 try { 2465 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2); 2466 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2); 2467 } catch (RemoteException e) { 2468 } 2469 args.recycle(); 2470 return true; 2471 case MSG_CREATE_SESSION: { 2472 args = (SomeArgs)msg.obj; 2473 IInputMethod method = (IInputMethod)args.arg1; 2474 InputChannel channel = (InputChannel)args.arg2; 2475 try { 2476 method.createSession(channel, (IInputSessionCallback)args.arg3); 2477 } catch (RemoteException e) { 2478 } finally { 2479 // Dispose the channel if the input method is not local to this process 2480 // because the remote proxy will get its own copy when unparceled. 2481 if (channel != null && Binder.isProxy(method)) { 2482 channel.dispose(); 2483 } 2484 } 2485 args.recycle(); 2486 return true; 2487 } 2488 // --------------------------------------------------------- 2489 2490 case MSG_START_INPUT: 2491 args = (SomeArgs)msg.obj; 2492 try { 2493 SessionState session = (SessionState)args.arg1; 2494 setEnabledSessionInMainThread(session); 2495 session.method.startInput((IInputContext)args.arg2, 2496 (EditorInfo)args.arg3); 2497 } catch (RemoteException e) { 2498 } 2499 args.recycle(); 2500 return true; 2501 case MSG_RESTART_INPUT: 2502 args = (SomeArgs)msg.obj; 2503 try { 2504 SessionState session = (SessionState)args.arg1; 2505 setEnabledSessionInMainThread(session); 2506 session.method.restartInput((IInputContext)args.arg2, 2507 (EditorInfo)args.arg3); 2508 } catch (RemoteException e) { 2509 } 2510 args.recycle(); 2511 return true; 2512 2513 // --------------------------------------------------------- 2514 2515 case MSG_UNBIND_METHOD: 2516 try { 2517 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1); 2518 } catch (RemoteException e) { 2519 // There is nothing interesting about the last client dying. 2520 } 2521 return true; 2522 case MSG_BIND_METHOD: { 2523 args = (SomeArgs)msg.obj; 2524 IInputMethodClient client = (IInputMethodClient)args.arg1; 2525 InputBindResult res = (InputBindResult)args.arg2; 2526 try { 2527 client.onBindMethod(res); 2528 } catch (RemoteException e) { 2529 Slog.w(TAG, "Client died receiving input method " + args.arg2); 2530 } finally { 2531 // Dispose the channel if the input method is not local to this process 2532 // because the remote proxy will get its own copy when unparceled. 2533 if (res.channel != null && Binder.isProxy(client)) { 2534 res.channel.dispose(); 2535 } 2536 } 2537 args.recycle(); 2538 return true; 2539 } 2540 case MSG_SET_ACTIVE: 2541 try { 2542 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0); 2543 } catch (RemoteException e) { 2544 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid " 2545 + ((ClientState)msg.obj).pid + " uid " 2546 + ((ClientState)msg.obj).uid); 2547 } 2548 return true; 2549 case MSG_SET_CURSOR_ANCHOR_MONITOR_MODE: 2550 try { 2551 ((ClientState)msg.obj).client.setCursorAnchorMonitorMode(msg.arg1); 2552 } catch (RemoteException e) { 2553 Slog.w(TAG, "Got RemoteException sending setCursorAnchorMonitorMode " 2554 + "notification to pid " + ((ClientState)msg.obj).pid 2555 + " uid " + ((ClientState)msg.obj).uid); 2556 } 2557 return true; 2558 2559 // -------------------------------------------------------------- 2560 case MSG_HARD_KEYBOARD_SWITCH_CHANGED: 2561 mHardKeyboardListener.handleHardKeyboardStatusChange( 2562 msg.arg1 == 1, msg.arg2 == 1); 2563 return true; 2564 } 2565 return false; 2566 } 2567 2568 private boolean chooseNewDefaultIMELocked() { 2569 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME( 2570 mSettings.getEnabledInputMethodListLocked()); 2571 if (imi != null) { 2572 if (DEBUG) { 2573 Slog.d(TAG, "New default IME was selected: " + imi.getId()); 2574 } 2575 resetSelectedInputMethodAndSubtypeLocked(imi.getId()); 2576 return true; 2577 } 2578 2579 return false; 2580 } 2581 2582 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list, 2583 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) { 2584 if (DEBUG) { 2585 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme 2586 + " \n ------ \n" + InputMethodUtils.getStackTrace()); 2587 } 2588 list.clear(); 2589 map.clear(); 2590 2591 // Use for queryIntentServicesAsUser 2592 final PackageManager pm = mContext.getPackageManager(); 2593 String disabledSysImes = mSettings.getDisabledSystemInputMethods(); 2594 if (disabledSysImes == null) disabledSysImes = ""; 2595 2596 final List<ResolveInfo> services = pm.queryIntentServicesAsUser( 2597 new Intent(InputMethod.SERVICE_INTERFACE), 2598 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS, 2599 mSettings.getCurrentUserId()); 2600 2601 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes = 2602 mFileManager.getAllAdditionalInputMethodSubtypes(); 2603 for (int i = 0; i < services.size(); ++i) { 2604 ResolveInfo ri = services.get(i); 2605 ServiceInfo si = ri.serviceInfo; 2606 ComponentName compName = new ComponentName(si.packageName, si.name); 2607 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals( 2608 si.permission)) { 2609 Slog.w(TAG, "Skipping input method " + compName 2610 + ": it does not require the permission " 2611 + android.Manifest.permission.BIND_INPUT_METHOD); 2612 continue; 2613 } 2614 2615 if (DEBUG) Slog.d(TAG, "Checking " + compName); 2616 2617 try { 2618 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes); 2619 list.add(p); 2620 final String id = p.getId(); 2621 map.put(id, p); 2622 2623 if (DEBUG) { 2624 Slog.d(TAG, "Found an input method " + p); 2625 } 2626 2627 } catch (XmlPullParserException e) { 2628 Slog.w(TAG, "Unable to load input method " + compName, e); 2629 } catch (IOException e) { 2630 Slog.w(TAG, "Unable to load input method " + compName, e); 2631 } 2632 } 2633 2634 if (resetDefaultEnabledIme) { 2635 final ArrayList<InputMethodInfo> defaultEnabledIme = 2636 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list); 2637 for (int i = 0; i < defaultEnabledIme.size(); ++i) { 2638 final InputMethodInfo imi = defaultEnabledIme.get(i); 2639 if (DEBUG) { 2640 Slog.d(TAG, "--- enable ime = " + imi); 2641 } 2642 setInputMethodEnabledLocked(imi.getId(), true); 2643 } 2644 } 2645 2646 final String defaultImiId = mSettings.getSelectedInputMethod(); 2647 if (!TextUtils.isEmpty(defaultImiId)) { 2648 if (!map.containsKey(defaultImiId)) { 2649 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME."); 2650 if (chooseNewDefaultIMELocked()) { 2651 updateFromSettingsLocked(true); 2652 } 2653 } else { 2654 // Double check that the default IME is certainly enabled. 2655 setInputMethodEnabledLocked(defaultImiId, true); 2656 } 2657 } 2658 // Here is not the perfect place to reset the switching controller. Ideally 2659 // mSwitchingController and mSettings should be able to share the same state. 2660 // TODO: Make sure that mSwitchingController and mSettings are sharing the 2661 // the same enabled IMEs list. 2662 mSwitchingController.resetCircularListLocked(mContext); 2663 } 2664 2665 // ---------------------------------------------------------------------- 2666 2667 private void showInputMethodMenu() { 2668 showInputMethodMenuInternal(false); 2669 } 2670 2671 private void showInputMethodSubtypeMenu() { 2672 showInputMethodMenuInternal(true); 2673 } 2674 2675 private void showInputMethodAndSubtypeEnabler(String inputMethodId) { 2676 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS); 2677 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 2678 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED 2679 | Intent.FLAG_ACTIVITY_CLEAR_TOP); 2680 if (!TextUtils.isEmpty(inputMethodId)) { 2681 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId); 2682 } 2683 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT); 2684 } 2685 2686 private void showConfigureInputMethods() { 2687 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS); 2688 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 2689 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED 2690 | Intent.FLAG_ACTIVITY_CLEAR_TOP); 2691 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT); 2692 } 2693 2694 private boolean isScreenLocked() { 2695 return mKeyguardManager != null 2696 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure(); 2697 } 2698 private void showInputMethodMenuInternal(boolean showSubtypes) { 2699 if (DEBUG) Slog.v(TAG, "Show switching menu"); 2700 2701 final Context context = mContext; 2702 final boolean isScreenLocked = isScreenLocked(); 2703 2704 final String lastInputMethodId = mSettings.getSelectedInputMethod(); 2705 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId); 2706 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId); 2707 2708 synchronized (mMethodMap) { 2709 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis = 2710 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked( 2711 mContext); 2712 if (immis == null || immis.size() == 0) { 2713 return; 2714 } 2715 2716 hideInputMethodMenuLocked(); 2717 2718 final List<ImeSubtypeListItem> imList = 2719 mSwitchingController.getSortedInputMethodAndSubtypeListLocked( 2720 showSubtypes, mInputShown, isScreenLocked); 2721 2722 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) { 2723 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked(); 2724 if (currentSubtype != null) { 2725 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId); 2726 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode( 2727 currentImi, currentSubtype.hashCode()); 2728 } 2729 } 2730 2731 final int N = imList.size(); 2732 mIms = new InputMethodInfo[N]; 2733 mSubtypeIds = new int[N]; 2734 int checkedItem = 0; 2735 for (int i = 0; i < N; ++i) { 2736 final ImeSubtypeListItem item = imList.get(i); 2737 mIms[i] = item.mImi; 2738 mSubtypeIds[i] = item.mSubtypeId; 2739 if (mIms[i].getId().equals(lastInputMethodId)) { 2740 int subtypeId = mSubtypeIds[i]; 2741 if ((subtypeId == NOT_A_SUBTYPE_ID) 2742 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0) 2743 || (subtypeId == lastInputMethodSubtypeId)) { 2744 checkedItem = i; 2745 } 2746 } 2747 } 2748 final TypedArray a = context.obtainStyledAttributes(null, 2749 com.android.internal.R.styleable.DialogPreference, 2750 com.android.internal.R.attr.alertDialogStyle, 0); 2751 mDialogBuilder = new AlertDialog.Builder(context) 2752 .setOnCancelListener(new OnCancelListener() { 2753 @Override 2754 public void onCancel(DialogInterface dialog) { 2755 hideInputMethodMenu(); 2756 } 2757 }) 2758 .setIcon(a.getDrawable( 2759 com.android.internal.R.styleable.DialogPreference_dialogTitle)); 2760 a.recycle(); 2761 final LayoutInflater inflater = 2762 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 2763 final View tv = inflater.inflate( 2764 com.android.internal.R.layout.input_method_switch_dialog_title, null); 2765 mDialogBuilder.setCustomTitle(tv); 2766 2767 // Setup layout for a toggle switch of the hardware keyboard 2768 mSwitchingDialogTitleView = tv; 2769 mSwitchingDialogTitleView.findViewById( 2770 com.android.internal.R.id.hard_keyboard_section).setVisibility( 2771 mWindowManagerService.isHardKeyboardAvailable() ? 2772 View.VISIBLE : View.GONE); 2773 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById( 2774 com.android.internal.R.id.hard_keyboard_switch)); 2775 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled()); 2776 hardKeySwitch.setOnCheckedChangeListener( 2777 new OnCheckedChangeListener() { 2778 @Override 2779 public void onCheckedChanged( 2780 CompoundButton buttonView, boolean isChecked) { 2781 mWindowManagerService.setHardKeyboardEnabled(isChecked); 2782 // Ensure that the input method dialog is dismissed when changing 2783 // the hardware keyboard state. 2784 hideInputMethodMenu(); 2785 } 2786 }); 2787 2788 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context, 2789 com.android.internal.R.layout.simple_list_item_2_single_choice, imList, 2790 checkedItem); 2791 2792 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, 2793 new AlertDialog.OnClickListener() { 2794 @Override 2795 public void onClick(DialogInterface dialog, int which) { 2796 synchronized (mMethodMap) { 2797 if (mIms == null || mIms.length <= which 2798 || mSubtypeIds == null || mSubtypeIds.length <= which) { 2799 return; 2800 } 2801 InputMethodInfo im = mIms[which]; 2802 int subtypeId = mSubtypeIds[which]; 2803 adapter.mCheckedItem = which; 2804 adapter.notifyDataSetChanged(); 2805 hideInputMethodMenu(); 2806 if (im != null) { 2807 if ((subtypeId < 0) 2808 || (subtypeId >= im.getSubtypeCount())) { 2809 subtypeId = NOT_A_SUBTYPE_ID; 2810 } 2811 setInputMethodLocked(im.getId(), subtypeId); 2812 } 2813 } 2814 } 2815 }); 2816 2817 if (showSubtypes && !isScreenLocked) { 2818 mDialogBuilder.setPositiveButton( 2819 com.android.internal.R.string.configure_input_methods, 2820 new DialogInterface.OnClickListener() { 2821 @Override 2822 public void onClick(DialogInterface dialog, int whichButton) { 2823 showConfigureInputMethods(); 2824 } 2825 }); 2826 } 2827 mSwitchingDialog = mDialogBuilder.create(); 2828 mSwitchingDialog.setCanceledOnTouchOutside(true); 2829 mSwitchingDialog.getWindow().setType( 2830 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG); 2831 mSwitchingDialog.getWindow().getAttributes().privateFlags |= 2832 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS; 2833 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method"); 2834 updateImeWindowStatusLocked(); 2835 mSwitchingDialog.show(); 2836 } 2837 } 2838 2839 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> { 2840 private final LayoutInflater mInflater; 2841 private final int mTextViewResourceId; 2842 private final List<ImeSubtypeListItem> mItemsList; 2843 public int mCheckedItem; 2844 public ImeSubtypeListAdapter(Context context, int textViewResourceId, 2845 List<ImeSubtypeListItem> itemsList, int checkedItem) { 2846 super(context, textViewResourceId, itemsList); 2847 mTextViewResourceId = textViewResourceId; 2848 mItemsList = itemsList; 2849 mCheckedItem = checkedItem; 2850 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 2851 } 2852 2853 @Override 2854 public View getView(int position, View convertView, ViewGroup parent) { 2855 final View view = convertView != null ? convertView 2856 : mInflater.inflate(mTextViewResourceId, null); 2857 if (position < 0 || position >= mItemsList.size()) return view; 2858 final ImeSubtypeListItem item = mItemsList.get(position); 2859 final CharSequence imeName = item.mImeName; 2860 final CharSequence subtypeName = item.mSubtypeName; 2861 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1); 2862 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2); 2863 if (TextUtils.isEmpty(subtypeName)) { 2864 firstTextView.setText(imeName); 2865 secondTextView.setVisibility(View.GONE); 2866 } else { 2867 firstTextView.setText(subtypeName); 2868 secondTextView.setText(imeName); 2869 secondTextView.setVisibility(View.VISIBLE); 2870 } 2871 final RadioButton radioButton = 2872 (RadioButton)view.findViewById(com.android.internal.R.id.radio); 2873 radioButton.setChecked(position == mCheckedItem); 2874 return view; 2875 } 2876 } 2877 2878 void hideInputMethodMenu() { 2879 synchronized (mMethodMap) { 2880 hideInputMethodMenuLocked(); 2881 } 2882 } 2883 2884 void hideInputMethodMenuLocked() { 2885 if (DEBUG) Slog.v(TAG, "Hide switching menu"); 2886 2887 if (mSwitchingDialog != null) { 2888 mSwitchingDialog.dismiss(); 2889 mSwitchingDialog = null; 2890 } 2891 2892 updateImeWindowStatusLocked(); 2893 mDialogBuilder = null; 2894 mIms = null; 2895 } 2896 2897 // ---------------------------------------------------------------------- 2898 2899 @Override 2900 public boolean setInputMethodEnabled(String id, boolean enabled) { 2901 // TODO: Make this work even for non-current users? 2902 if (!calledFromValidUser()) { 2903 return false; 2904 } 2905 synchronized (mMethodMap) { 2906 if (mContext.checkCallingOrSelfPermission( 2907 android.Manifest.permission.WRITE_SECURE_SETTINGS) 2908 != PackageManager.PERMISSION_GRANTED) { 2909 throw new SecurityException( 2910 "Requires permission " 2911 + android.Manifest.permission.WRITE_SECURE_SETTINGS); 2912 } 2913 2914 long ident = Binder.clearCallingIdentity(); 2915 try { 2916 return setInputMethodEnabledLocked(id, enabled); 2917 } finally { 2918 Binder.restoreCallingIdentity(ident); 2919 } 2920 } 2921 } 2922 2923 boolean setInputMethodEnabledLocked(String id, boolean enabled) { 2924 // Make sure this is a valid input method. 2925 InputMethodInfo imm = mMethodMap.get(id); 2926 if (imm == null) { 2927 throw new IllegalArgumentException("Unknown id: " + mCurMethodId); 2928 } 2929 2930 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings 2931 .getEnabledInputMethodsAndSubtypeListLocked(); 2932 2933 if (enabled) { 2934 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) { 2935 if (pair.first.equals(id)) { 2936 // We are enabling this input method, but it is already enabled. 2937 // Nothing to do. The previous state was enabled. 2938 return true; 2939 } 2940 } 2941 mSettings.appendAndPutEnabledInputMethodLocked(id, false); 2942 // Previous state was disabled. 2943 return false; 2944 } else { 2945 StringBuilder builder = new StringBuilder(); 2946 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked( 2947 builder, enabledInputMethodsList, id)) { 2948 // Disabled input method is currently selected, switch to another one. 2949 final String selId = mSettings.getSelectedInputMethod(); 2950 if (id.equals(selId) && !chooseNewDefaultIMELocked()) { 2951 Slog.i(TAG, "Can't find new IME, unsetting the current input method."); 2952 resetSelectedInputMethodAndSubtypeLocked(""); 2953 } 2954 // Previous state was enabled. 2955 return true; 2956 } else { 2957 // We are disabling the input method but it is already disabled. 2958 // Nothing to do. The previous state was disabled. 2959 return false; 2960 } 2961 } 2962 } 2963 2964 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId, 2965 boolean setSubtypeOnly) { 2966 // Update the history of InputMethod and Subtype 2967 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype); 2968 2969 // Set Subtype here 2970 if (imi == null || subtypeId < 0) { 2971 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID); 2972 mCurrentSubtype = null; 2973 } else { 2974 if (subtypeId < imi.getSubtypeCount()) { 2975 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId); 2976 mSettings.putSelectedSubtype(subtype.hashCode()); 2977 mCurrentSubtype = subtype; 2978 } else { 2979 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID); 2980 // If the subtype is not specified, choose the most applicable one 2981 mCurrentSubtype = getCurrentInputMethodSubtypeLocked(); 2982 } 2983 } 2984 2985 // Workaround. 2986 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked 2987 // IMEs are not recognized and considered uninstalled. 2988 // Actually, we can't move everything after SystemReady because 2989 // IMMS needs to run in the encryption lock screen. So, we just skip changing 2990 // the default IME here and try cheking the default IME again in systemReady(). 2991 // TODO: Do nothing before system ready and implement a separated logic for 2992 // the encryption lock screen. 2993 // TODO: ASEC should be ready before IMMS is instantiated. 2994 if (mSystemReady && !setSubtypeOnly) { 2995 // Set InputMethod here 2996 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : ""); 2997 } 2998 } 2999 3000 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) { 3001 InputMethodInfo imi = mMethodMap.get(newDefaultIme); 3002 int lastSubtypeId = NOT_A_SUBTYPE_ID; 3003 // newDefaultIme is empty when there is no candidate for the selected IME. 3004 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) { 3005 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme); 3006 if (subtypeHashCode != null) { 3007 try { 3008 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode( 3009 imi, Integer.valueOf(subtypeHashCode)); 3010 } catch (NumberFormatException e) { 3011 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e); 3012 } 3013 } 3014 } 3015 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false); 3016 } 3017 3018 // If there are no selected shortcuts, tries finding the most applicable ones. 3019 private Pair<InputMethodInfo, InputMethodSubtype> 3020 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) { 3021 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked(); 3022 InputMethodInfo mostApplicableIMI = null; 3023 InputMethodSubtype mostApplicableSubtype = null; 3024 boolean foundInSystemIME = false; 3025 3026 // Search applicable subtype for each InputMethodInfo 3027 for (InputMethodInfo imi: imis) { 3028 final String imiId = imi.getId(); 3029 if (foundInSystemIME && !imiId.equals(mCurMethodId)) { 3030 continue; 3031 } 3032 InputMethodSubtype subtype = null; 3033 final List<InputMethodSubtype> enabledSubtypes = 3034 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true); 3035 // 1. Search by the current subtype's locale from enabledSubtypes. 3036 if (mCurrentSubtype != null) { 3037 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked( 3038 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false); 3039 } 3040 // 2. Search by the system locale from enabledSubtypes. 3041 // 3. Search the first enabled subtype matched with mode from enabledSubtypes. 3042 if (subtype == null) { 3043 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked( 3044 mRes, enabledSubtypes, mode, null, true); 3045 } 3046 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes = 3047 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode); 3048 final ArrayList<InputMethodSubtype> subtypesForSearch = 3049 overridingImplicitlyEnabledSubtypes.isEmpty() 3050 ? InputMethodUtils.getSubtypes(imi) 3051 : overridingImplicitlyEnabledSubtypes; 3052 // 4. Search by the current subtype's locale from all subtypes. 3053 if (subtype == null && mCurrentSubtype != null) { 3054 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked( 3055 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false); 3056 } 3057 // 5. Search by the system locale from all subtypes. 3058 // 6. Search the first enabled subtype matched with mode from all subtypes. 3059 if (subtype == null) { 3060 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked( 3061 mRes, subtypesForSearch, mode, null, true); 3062 } 3063 if (subtype != null) { 3064 if (imiId.equals(mCurMethodId)) { 3065 // The current input method is the most applicable IME. 3066 mostApplicableIMI = imi; 3067 mostApplicableSubtype = subtype; 3068 break; 3069 } else if (!foundInSystemIME) { 3070 // The system input method is 2nd applicable IME. 3071 mostApplicableIMI = imi; 3072 mostApplicableSubtype = subtype; 3073 if ((imi.getServiceInfo().applicationInfo.flags 3074 & ApplicationInfo.FLAG_SYSTEM) != 0) { 3075 foundInSystemIME = true; 3076 } 3077 } 3078 } 3079 } 3080 if (DEBUG) { 3081 if (mostApplicableIMI != null) { 3082 Slog.w(TAG, "Most applicable shortcut input method was:" 3083 + mostApplicableIMI.getId()); 3084 if (mostApplicableSubtype != null) { 3085 Slog.w(TAG, "Most applicable shortcut input method subtype was:" 3086 + "," + mostApplicableSubtype.getMode() + "," 3087 + mostApplicableSubtype.getLocale()); 3088 } 3089 } 3090 } 3091 if (mostApplicableIMI != null) { 3092 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI, 3093 mostApplicableSubtype); 3094 } else { 3095 return null; 3096 } 3097 } 3098 3099 /** 3100 * @return Return the current subtype of this input method. 3101 */ 3102 @Override 3103 public InputMethodSubtype getCurrentInputMethodSubtype() { 3104 // TODO: Make this work even for non-current users? 3105 if (!calledFromValidUser()) { 3106 return null; 3107 } 3108 synchronized (mMethodMap) { 3109 return getCurrentInputMethodSubtypeLocked(); 3110 } 3111 } 3112 3113 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() { 3114 if (mCurMethodId == null) { 3115 return null; 3116 } 3117 final boolean subtypeIsSelected = mSettings.isSubtypeSelected(); 3118 final InputMethodInfo imi = mMethodMap.get(mCurMethodId); 3119 if (imi == null || imi.getSubtypeCount() == 0) { 3120 return null; 3121 } 3122 if (!subtypeIsSelected || mCurrentSubtype == null 3123 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) { 3124 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId); 3125 if (subtypeId == NOT_A_SUBTYPE_ID) { 3126 // If there are no selected subtypes, the framework will try to find 3127 // the most applicable subtype from explicitly or implicitly enabled 3128 // subtypes. 3129 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes = 3130 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true); 3131 // If there is only one explicitly or implicitly enabled subtype, 3132 // just returns it. 3133 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) { 3134 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0); 3135 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) { 3136 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked( 3137 mRes, explicitlyOrImplicitlyEnabledSubtypes, 3138 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true); 3139 if (mCurrentSubtype == null) { 3140 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked( 3141 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null, 3142 true); 3143 } 3144 } 3145 } else { 3146 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId); 3147 } 3148 } 3149 return mCurrentSubtype; 3150 } 3151 3152 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi, 3153 InputMethodSubtype subtype) { 3154 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) { 3155 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype); 3156 } else { 3157 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>(); 3158 subtypes.add(subtype); 3159 mShortcutInputMethodsAndSubtypes.put(imi, subtypes); 3160 } 3161 } 3162 3163 // TODO: We should change the return type from List to List<Parcelable> 3164 @SuppressWarnings("rawtypes") 3165 @Override 3166 public List getShortcutInputMethodsAndSubtypes() { 3167 synchronized (mMethodMap) { 3168 ArrayList<Object> ret = new ArrayList<Object>(); 3169 if (mShortcutInputMethodsAndSubtypes.size() == 0) { 3170 // If there are no selected shortcut subtypes, the framework will try to find 3171 // the most applicable subtype from all subtypes whose mode is 3172 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode. 3173 Pair<InputMethodInfo, InputMethodSubtype> info = 3174 findLastResortApplicableShortcutInputMethodAndSubtypeLocked( 3175 InputMethodUtils.SUBTYPE_MODE_VOICE); 3176 if (info != null) { 3177 ret.add(info.first); 3178 ret.add(info.second); 3179 } 3180 return ret; 3181 } 3182 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) { 3183 ret.add(imi); 3184 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) { 3185 ret.add(subtype); 3186 } 3187 } 3188 return ret; 3189 } 3190 } 3191 3192 @Override 3193 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) { 3194 // TODO: Make this work even for non-current users? 3195 if (!calledFromValidUser()) { 3196 return false; 3197 } 3198 synchronized (mMethodMap) { 3199 if (subtype != null && mCurMethodId != null) { 3200 InputMethodInfo imi = mMethodMap.get(mCurMethodId); 3201 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode()); 3202 if (subtypeId != NOT_A_SUBTYPE_ID) { 3203 setInputMethodLocked(mCurMethodId, subtypeId); 3204 return true; 3205 } 3206 } 3207 return false; 3208 } 3209 } 3210 3211 // TODO: Cache the state for each user and reset when the cached user is removed. 3212 private static class InputMethodFileManager { 3213 private static final String SYSTEM_PATH = "system"; 3214 private static final String INPUT_METHOD_PATH = "inputmethod"; 3215 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml"; 3216 private static final String NODE_SUBTYPES = "subtypes"; 3217 private static final String NODE_SUBTYPE = "subtype"; 3218 private static final String NODE_IMI = "imi"; 3219 private static final String ATTR_ID = "id"; 3220 private static final String ATTR_LABEL = "label"; 3221 private static final String ATTR_ICON = "icon"; 3222 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale"; 3223 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode"; 3224 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue"; 3225 private static final String ATTR_IS_AUXILIARY = "isAuxiliary"; 3226 private final AtomicFile mAdditionalInputMethodSubtypeFile; 3227 private final HashMap<String, InputMethodInfo> mMethodMap; 3228 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap = 3229 new HashMap<String, List<InputMethodSubtype>>(); 3230 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) { 3231 if (methodMap == null) { 3232 throw new NullPointerException("methodMap is null"); 3233 } 3234 mMethodMap = methodMap; 3235 final File systemDir = userId == UserHandle.USER_OWNER 3236 ? new File(Environment.getDataDirectory(), SYSTEM_PATH) 3237 : Environment.getUserSystemDirectory(userId); 3238 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH); 3239 if (!inputMethodDir.mkdirs()) { 3240 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath()); 3241 } 3242 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME); 3243 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile); 3244 if (!subtypeFile.exists()) { 3245 // If "subtypes.xml" doesn't exist, create a blank file. 3246 writeAdditionalInputMethodSubtypes( 3247 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap); 3248 } else { 3249 readAdditionalInputMethodSubtypes( 3250 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile); 3251 } 3252 } 3253 3254 private void deleteAllInputMethodSubtypes(String imiId) { 3255 synchronized (mMethodMap) { 3256 mAdditionalSubtypesMap.remove(imiId); 3257 writeAdditionalInputMethodSubtypes( 3258 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap); 3259 } 3260 } 3261 3262 public void addInputMethodSubtypes( 3263 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) { 3264 synchronized (mMethodMap) { 3265 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>(); 3266 final int N = additionalSubtypes.length; 3267 for (int i = 0; i < N; ++i) { 3268 final InputMethodSubtype subtype = additionalSubtypes[i]; 3269 if (!subtypes.contains(subtype)) { 3270 subtypes.add(subtype); 3271 } else { 3272 Slog.w(TAG, "Duplicated subtype definition found: " 3273 + subtype.getLocale() + ", " + subtype.getMode()); 3274 } 3275 } 3276 mAdditionalSubtypesMap.put(imi.getId(), subtypes); 3277 writeAdditionalInputMethodSubtypes( 3278 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap); 3279 } 3280 } 3281 3282 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() { 3283 synchronized (mMethodMap) { 3284 return mAdditionalSubtypesMap; 3285 } 3286 } 3287 3288 private static void writeAdditionalInputMethodSubtypes( 3289 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile, 3290 HashMap<String, InputMethodInfo> methodMap) { 3291 // Safety net for the case that this function is called before methodMap is set. 3292 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0; 3293 FileOutputStream fos = null; 3294 try { 3295 fos = subtypesFile.startWrite(); 3296 final XmlSerializer out = new FastXmlSerializer(); 3297 out.setOutput(fos, "utf-8"); 3298 out.startDocument(null, true); 3299 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); 3300 out.startTag(null, NODE_SUBTYPES); 3301 for (String imiId : allSubtypes.keySet()) { 3302 if (isSetMethodMap && !methodMap.containsKey(imiId)) { 3303 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId); 3304 continue; 3305 } 3306 out.startTag(null, NODE_IMI); 3307 out.attribute(null, ATTR_ID, imiId); 3308 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId); 3309 final int N = subtypesList.size(); 3310 for (int i = 0; i < N; ++i) { 3311 final InputMethodSubtype subtype = subtypesList.get(i); 3312 out.startTag(null, NODE_SUBTYPE); 3313 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId())); 3314 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId())); 3315 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale()); 3316 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode()); 3317 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue()); 3318 out.attribute(null, ATTR_IS_AUXILIARY, 3319 String.valueOf(subtype.isAuxiliary() ? 1 : 0)); 3320 out.endTag(null, NODE_SUBTYPE); 3321 } 3322 out.endTag(null, NODE_IMI); 3323 } 3324 out.endTag(null, NODE_SUBTYPES); 3325 out.endDocument(); 3326 subtypesFile.finishWrite(fos); 3327 } catch (java.io.IOException e) { 3328 Slog.w(TAG, "Error writing subtypes", e); 3329 if (fos != null) { 3330 subtypesFile.failWrite(fos); 3331 } 3332 } 3333 } 3334 3335 private static void readAdditionalInputMethodSubtypes( 3336 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) { 3337 if (allSubtypes == null || subtypesFile == null) return; 3338 allSubtypes.clear(); 3339 FileInputStream fis = null; 3340 try { 3341 fis = subtypesFile.openRead(); 3342 final XmlPullParser parser = Xml.newPullParser(); 3343 parser.setInput(fis, null); 3344 int type = parser.getEventType(); 3345 // Skip parsing until START_TAG 3346 while ((type = parser.next()) != XmlPullParser.START_TAG 3347 && type != XmlPullParser.END_DOCUMENT) {} 3348 String firstNodeName = parser.getName(); 3349 if (!NODE_SUBTYPES.equals(firstNodeName)) { 3350 throw new XmlPullParserException("Xml doesn't start with subtypes"); 3351 } 3352 final int depth =parser.getDepth(); 3353 String currentImiId = null; 3354 ArrayList<InputMethodSubtype> tempSubtypesArray = null; 3355 while (((type = parser.next()) != XmlPullParser.END_TAG 3356 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { 3357 if (type != XmlPullParser.START_TAG) 3358 continue; 3359 final String nodeName = parser.getName(); 3360 if (NODE_IMI.equals(nodeName)) { 3361 currentImiId = parser.getAttributeValue(null, ATTR_ID); 3362 if (TextUtils.isEmpty(currentImiId)) { 3363 Slog.w(TAG, "Invalid imi id found in subtypes.xml"); 3364 continue; 3365 } 3366 tempSubtypesArray = new ArrayList<InputMethodSubtype>(); 3367 allSubtypes.put(currentImiId, tempSubtypesArray); 3368 } else if (NODE_SUBTYPE.equals(nodeName)) { 3369 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) { 3370 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId); 3371 continue; 3372 } 3373 final int icon = Integer.valueOf( 3374 parser.getAttributeValue(null, ATTR_ICON)); 3375 final int label = Integer.valueOf( 3376 parser.getAttributeValue(null, ATTR_LABEL)); 3377 final String imeSubtypeLocale = 3378 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE); 3379 final String imeSubtypeMode = 3380 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE); 3381 final String imeSubtypeExtraValue = 3382 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE); 3383 final boolean isAuxiliary = "1".equals(String.valueOf( 3384 parser.getAttributeValue(null, ATTR_IS_AUXILIARY))); 3385 final InputMethodSubtype subtype = 3386 new InputMethodSubtype(label, icon, imeSubtypeLocale, 3387 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary); 3388 tempSubtypesArray.add(subtype); 3389 } 3390 } 3391 } catch (XmlPullParserException e) { 3392 Slog.w(TAG, "Error reading subtypes: " + e); 3393 return; 3394 } catch (java.io.IOException e) { 3395 Slog.w(TAG, "Error reading subtypes: " + e); 3396 return; 3397 } catch (NumberFormatException e) { 3398 Slog.w(TAG, "Error reading subtypes: " + e); 3399 return; 3400 } finally { 3401 if (fis != null) { 3402 try { 3403 fis.close(); 3404 } catch (java.io.IOException e1) { 3405 Slog.w(TAG, "Failed to close."); 3406 } 3407 } 3408 } 3409 } 3410 } 3411 3412 @Override 3413 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 3414 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) 3415 != PackageManager.PERMISSION_GRANTED) { 3416 3417 pw.println("Permission Denial: can't dump InputMethodManager from from pid=" 3418 + Binder.getCallingPid() 3419 + ", uid=" + Binder.getCallingUid()); 3420 return; 3421 } 3422 3423 IInputMethod method; 3424 ClientState client; 3425 3426 final Printer p = new PrintWriterPrinter(pw); 3427 3428 synchronized (mMethodMap) { 3429 p.println("Current Input Method Manager state:"); 3430 int N = mMethodList.size(); 3431 p.println(" Input Methods:"); 3432 for (int i=0; i<N; i++) { 3433 InputMethodInfo info = mMethodList.get(i); 3434 p.println(" InputMethod #" + i + ":"); 3435 info.dump(p, " "); 3436 } 3437 p.println(" Clients:"); 3438 for (ClientState ci : mClients.values()) { 3439 p.println(" Client " + ci + ":"); 3440 p.println(" client=" + ci.client); 3441 p.println(" inputContext=" + ci.inputContext); 3442 p.println(" sessionRequested=" + ci.sessionRequested); 3443 p.println(" curSession=" + ci.curSession); 3444 } 3445 p.println(" mCurMethodId=" + mCurMethodId); 3446 client = mCurClient; 3447 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq); 3448 p.println(" mCurFocusedWindow=" + mCurFocusedWindow); 3449 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection 3450 + " mBoundToMethod=" + mBoundToMethod); 3451 p.println(" mCurToken=" + mCurToken); 3452 p.println(" mCurIntent=" + mCurIntent); 3453 method = mCurMethod; 3454 p.println(" mCurMethod=" + mCurMethod); 3455 p.println(" mEnabledSession=" + mEnabledSession); 3456 p.println(" mShowRequested=" + mShowRequested 3457 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested 3458 + " mShowForced=" + mShowForced 3459 + " mInputShown=" + mInputShown); 3460 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mScreenOn); 3461 } 3462 3463 p.println(" "); 3464 if (client != null) { 3465 pw.flush(); 3466 try { 3467 client.client.asBinder().dump(fd, args); 3468 } catch (RemoteException e) { 3469 p.println("Input method client dead: " + e); 3470 } 3471 } else { 3472 p.println("No input method client."); 3473 } 3474 3475 p.println(" "); 3476 if (method != null) { 3477 pw.flush(); 3478 try { 3479 method.asBinder().dump(fd, args); 3480 } catch (RemoteException e) { 3481 p.println("Input method service dead: " + e); 3482 } 3483 } else { 3484 p.println("No input method service."); 3485 } 3486 } 3487} 3488