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