blob: b2cc6bde3c64625ed246c21c75f39008d57091a1 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2008 The Android Open Source Project
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.server;
18
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080019import com.android.internal.content.PackageMonitor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import com.android.internal.os.HandlerCaller;
21import com.android.internal.view.IInputContext;
22import com.android.internal.view.IInputMethod;
23import com.android.internal.view.IInputMethodCallback;
24import com.android.internal.view.IInputMethodClient;
25import com.android.internal.view.IInputMethodManager;
26import com.android.internal.view.IInputMethodSession;
27import com.android.internal.view.InputBindResult;
28
Joe Onorato7a0f36b2010-06-07 10:24:36 -070029import com.android.server.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31import org.xmlpull.v1.XmlPullParserException;
32
33import android.app.ActivityManagerNative;
34import android.app.AlertDialog;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070035import android.app.PendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.ComponentName;
37import android.content.ContentResolver;
38import android.content.Context;
39import android.content.DialogInterface;
40import android.content.IntentFilter;
41import android.content.DialogInterface.OnCancelListener;
42import android.content.Intent;
43import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070044import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.pm.PackageManager;
46import android.content.pm.ResolveInfo;
47import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070048import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.res.Resources;
50import android.content.res.TypedArray;
51import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Binder;
53import android.os.Handler;
54import android.os.IBinder;
55import android.os.IInterface;
56import android.os.Message;
57import android.os.Parcel;
satok4e4569d2010-11-19 18:45:53 +090058import android.os.Parcelable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080060import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.ServiceManager;
62import android.os.SystemClock;
63import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070064import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090065import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.text.TextUtils;
67import android.util.EventLog;
satokab751aa2010-09-14 19:17:36 +090068import android.util.Pair;
Joe Onorato8a9b2202010-02-26 18:56:32 -080069import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.util.PrintWriterPrinter;
71import android.util.Printer;
72import android.view.IWindowManager;
73import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputBinding;
76import android.view.inputmethod.InputMethod;
77import android.view.inputmethod.InputMethodInfo;
78import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090079import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
81import java.io.FileDescriptor;
82import java.io.IOException;
83import java.io.PrintWriter;
satok913a8922010-08-26 21:53:41 +090084import java.text.Collator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import java.util.ArrayList;
86import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +090087import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import java.util.List;
satok913a8922010-08-26 21:53:41 +090089import java.util.Map;
90import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92/**
93 * This class provides a system service that manages input methods.
94 */
95public class InputMethodManagerService extends IInputMethodManager.Stub
96 implements ServiceConnection, Handler.Callback {
97 static final boolean DEBUG = false;
98 static final String TAG = "InputManagerService";
99
100 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900101 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900102 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 static final int MSG_UNBIND_INPUT = 1000;
105 static final int MSG_BIND_INPUT = 1010;
106 static final int MSG_SHOW_SOFT_INPUT = 1020;
107 static final int MSG_HIDE_SOFT_INPUT = 1030;
108 static final int MSG_ATTACH_TOKEN = 1040;
109 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 static final int MSG_START_INPUT = 2000;
112 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 static final int MSG_UNBIND_METHOD = 3000;
115 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800118
satokab751aa2010-09-14 19:17:36 +0900119 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900120 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok8fbb1e82010-11-02 23:15:58 +0900121 // If IME doesn't support the system locale, the default subtype will be the first defined one.
122 private static final int DEFAULT_SUBTYPE_ID = 0;
satokab751aa2010-09-14 19:17:36 +0900123
satok4e4569d2010-11-19 18:45:53 +0900124 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
125 private static final String SUBTYPE_MODE_VOICE = "voice";
126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 final Context mContext;
128 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900129 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700131 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 final IWindowManager mIWindowManager;
133 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 // All known input methods. mMethodMap also serves as the global
138 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900139 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
140 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 class SessionState {
143 final ClientState client;
144 final IInputMethod method;
145 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 @Override
148 public String toString() {
149 return "SessionState{uid " + client.uid + " pid " + client.pid
150 + " method " + Integer.toHexString(
151 System.identityHashCode(method))
152 + " session " + Integer.toHexString(
153 System.identityHashCode(session))
154 + "}";
155 }
156
157 SessionState(ClientState _client, IInputMethod _method,
158 IInputMethodSession _session) {
159 client = _client;
160 method = _method;
161 session = _session;
162 }
163 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 class ClientState {
166 final IInputMethodClient client;
167 final IInputContext inputContext;
168 final int uid;
169 final int pid;
170 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 boolean sessionRequested;
173 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 @Override
176 public String toString() {
177 return "ClientState{" + Integer.toHexString(
178 System.identityHashCode(this)) + " uid " + uid
179 + " pid " + pid + "}";
180 }
181
182 ClientState(IInputMethodClient _client, IInputContext _inputContext,
183 int _uid, int _pid) {
184 client = _client;
185 inputContext = _inputContext;
186 uid = _uid;
187 pid = _pid;
188 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
189 }
190 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 final HashMap<IBinder, ClientState> mClients
193 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700196 * Set once the system is ready to run third party code.
197 */
198 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800199
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700200 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * Id of the currently selected input method.
202 */
203 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 /**
206 * The current binding sequence number, incremented every time there is
207 * a new bind performed.
208 */
209 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 /**
212 * The client that is currently bound to an input method.
213 */
214 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700217 * The last window token that gained focus.
218 */
219 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800220
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700221 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * The input context last provided by the current client.
223 */
224 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 /**
227 * The attributes last provided by the current client.
228 */
229 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 /**
232 * The input method ID of the input method service that we are currently
233 * connected to or in the process of connecting to.
234 */
235 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 /**
satokab751aa2010-09-14 19:17:36 +0900238 * The current subtype of the current input method.
239 */
240 private InputMethodSubtype mCurrentSubtype;
241
satok4e4569d2010-11-19 18:45:53 +0900242 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
243 private List<Pair<InputMethodInfo, InputMethodSubtype>> mShortcutInputMethodsAndSubtypes;
244 // This list is used for returning the pairs of InputMethodInfo and InputMethodSubtype through
245 // aidl. This list has imi1, subtype1 imi2, subtype2...
246 private List mShortcutInputMethodsAndSubtypesObjectList;
satokab751aa2010-09-14 19:17:36 +0900247
248 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 * Set to true if our ServiceConnection is currently actively bound to
250 * a service (whether or not we have gotten its IBinder back yet).
251 */
252 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 /**
255 * Set if the client has asked for the input method to be shown.
256 */
257 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 /**
260 * Set if we were explicitly told to show the input method.
261 */
262 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 /**
265 * Set if we were forced to be shown.
266 */
267 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 /**
270 * Set if we last told the input method to show itself.
271 */
272 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 /**
275 * The Intent used to connect to the current input method.
276 */
277 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 /**
280 * The token we have made for the currently active input method, to
281 * identify it in the future.
282 */
283 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 /**
286 * If non-null, this is the input method service we are currently connected
287 * to.
288 */
289 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 /**
292 * Time that we last initiated a bind to the input method, to determine
293 * if we should try to disconnect and reconnect to it.
294 */
295 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 /**
298 * Have we called mCurMethod.bindInput()?
299 */
300 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 /**
303 * Currently enabled session. Only touched by service thread, not
304 * protected by a lock.
305 */
306 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 /**
309 * True if the screen is on. The value is true initially.
310 */
311 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 AlertDialog.Builder mDialogBuilder;
314 AlertDialog mSwitchingDialog;
315 InputMethodInfo[] mIms;
316 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900317 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 class SettingsObserver extends ContentObserver {
320 SettingsObserver(Handler handler) {
321 super(handler);
322 ContentResolver resolver = mContext.getContentResolver();
323 resolver.registerContentObserver(Settings.Secure.getUriFor(
324 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900325 resolver.registerContentObserver(Settings.Secure.getUriFor(
326 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 @Override public void onChange(boolean selfChange) {
330 synchronized (mMethodMap) {
331 updateFromSettingsLocked();
332 }
333 }
334 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
337 @Override
338 public void onReceive(Context context, Intent intent) {
339 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
340 mScreenOn = true;
341 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
342 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700343 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
344 hideInputMethodMenu();
345 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800347 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 }
349
350 // Inform the current client of the change in active status
351 try {
352 if (mCurClient != null && mCurClient.client != null) {
353 mCurClient.client.setActive(mScreenOn);
354 }
355 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800356 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 + mCurClient.pid + " uid " + mCurClient.uid);
358 }
359 }
360 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800361
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800362 class MyPackageMonitor extends PackageMonitor {
363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800365 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800367 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
369 final int N = mMethodList.size();
370 if (curInputMethodId != null) {
371 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800372 InputMethodInfo imi = mMethodList.get(i);
373 if (imi.getId().equals(curInputMethodId)) {
374 for (String pkg : packages) {
375 if (imi.getPackageName().equals(pkg)) {
376 if (!doit) {
377 return true;
378 }
satok723a27e2010-11-11 14:58:11 +0900379 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800380 chooseNewDefaultIMELocked();
381 return true;
382 }
383 }
384 }
385 }
386 }
387 }
388 return false;
389 }
390
391 @Override
392 public void onSomePackagesChanged() {
393 synchronized (mMethodMap) {
394 InputMethodInfo curIm = null;
395 String curInputMethodId = Settings.Secure.getString(mContext
396 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
397 final int N = mMethodList.size();
398 if (curInputMethodId != null) {
399 for (int i=0; i<N; i++) {
400 InputMethodInfo imi = mMethodList.get(i);
401 if (imi.getId().equals(curInputMethodId)) {
402 curIm = imi;
403 }
404 int change = isPackageDisappearing(imi.getPackageName());
405 if (change == PACKAGE_TEMPORARY_CHANGE
406 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800407 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800408 + imi.getComponent());
409 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 }
411 }
412 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800413
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800414 buildInputMethodListLocked(mMethodList, mMethodMap);
415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800417
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800418 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800419 int change = isPackageDisappearing(curIm.getPackageName());
420 if (change == PACKAGE_TEMPORARY_CHANGE
421 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800422 ServiceInfo si = null;
423 try {
424 si = mContext.getPackageManager().getServiceInfo(
425 curIm.getComponent(), 0);
426 } catch (PackageManager.NameNotFoundException ex) {
427 }
428 if (si == null) {
429 // Uh oh, current input method is no longer around!
430 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800431 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800432 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800433 changed = true;
434 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800435 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900436 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800437 }
438 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800439 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800440 }
satokab751aa2010-09-14 19:17:36 +0900441
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800442 if (curIm == null) {
443 // We currently don't have a default input method... is
444 // one now available?
445 changed = chooseNewDefaultIMELocked();
446 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800447
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800448 if (changed) {
449 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 }
451 }
452 }
453 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 class MethodCallback extends IInputMethodCallback.Stub {
456 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 MethodCallback(IInputMethod method) {
459 mMethod = method;
460 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 public void finishedEvent(int seq, boolean handled) throws RemoteException {
463 }
464
465 public void sessionCreated(IInputMethodSession session) throws RemoteException {
466 onSessionCreated(mMethod, session);
467 }
468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800469
Joe Onorato089de882010-04-12 08:18:45 -0700470 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 mContext = context;
472 mHandler = new Handler(this);
473 mIWindowManager = IWindowManager.Stub.asInterface(
474 ServiceManager.getService(Context.WINDOW_SERVICE));
475 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
476 public void executeMessage(Message msg) {
477 handleMessage(msg);
478 }
479 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800480
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800481 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 IntentFilter screenOnOffFilt = new IntentFilter();
484 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
485 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700486 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800488
satok913a8922010-08-26 21:53:41 +0900489 mStatusBar = statusBar;
490 statusBar.setIconVisibility("ime", false);
491
satokd87c2592010-09-29 11:52:06 +0900492 // mSettings should be created before buildInputMethodListLocked
493 mSettings = new InputMethodSettings(context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900495 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496
satokd87c2592010-09-29 11:52:06 +0900497 if (TextUtils.isEmpty(Settings.Secure.getString(
498 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900500 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
502 try {
satokd87c2592010-09-29 11:52:06 +0900503 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 imi.getPackageName(), 0).getResources();
505 if (res.getBoolean(imi.getIsDefaultResourceId())) {
506 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800507 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 }
509 } catch (PackageManager.NameNotFoundException ex) {
510 } catch (Resources.NotFoundException ex) {
511 }
512 }
513 }
satokd87c2592010-09-29 11:52:06 +0900514 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800516 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900519 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 }
521 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 mSettingsObserver = new SettingsObserver(mHandler);
524 updateFromSettingsLocked();
525 }
526
527 @Override
528 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
529 throws RemoteException {
530 try {
531 return super.onTransact(code, data, reply, flags);
532 } catch (RuntimeException e) {
533 // The input method manager only throws security exceptions, so let's
534 // log all others.
535 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800536 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 }
538 throw e;
539 }
540 }
541
542 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700543 synchronized (mMethodMap) {
544 if (!mSystemReady) {
545 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700546 try {
547 startInputInnerLocked();
548 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800549 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700550 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700551 }
552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 public List<InputMethodInfo> getInputMethodList() {
556 synchronized (mMethodMap) {
557 return new ArrayList<InputMethodInfo>(mMethodList);
558 }
559 }
560
561 public List<InputMethodInfo> getEnabledInputMethodList() {
562 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900563 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
565 }
566
satok67ddf9c2010-11-17 09:45:54 +0900567 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi) {
568 synchronized (mMethodMap) {
satok884ef9a2010-11-18 10:39:46 +0900569 if (imi == null && mCurMethodId != null) {
570 imi = mMethodMap.get(mCurMethodId);
571 }
satok67ddf9c2010-11-17 09:45:54 +0900572 return mSettings.getEnabledInputMethodSubtypeListLocked(imi);
573 }
574 }
575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 public void addClient(IInputMethodClient client,
577 IInputContext inputContext, int uid, int pid) {
578 synchronized (mMethodMap) {
579 mClients.put(client.asBinder(), new ClientState(client,
580 inputContext, uid, pid));
581 }
582 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 public void removeClient(IInputMethodClient client) {
585 synchronized (mMethodMap) {
586 mClients.remove(client.asBinder());
587 }
588 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 void executeOrSendMessage(IInterface target, Message msg) {
591 if (target.asBinder() instanceof Binder) {
592 mCaller.sendMessage(msg);
593 } else {
594 handleMessage(msg);
595 msg.recycle();
596 }
597 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800598
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700599 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800601 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 + mCurClient.client.asBinder());
603 if (mBoundToMethod) {
604 mBoundToMethod = false;
605 if (mCurMethod != null) {
606 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
607 MSG_UNBIND_INPUT, mCurMethod));
608 }
609 }
610 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
611 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
612 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 // Call setActive(false) on the old client
615 try {
616 mCurClient.client.setActive(false);
617 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800618 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 + mCurClient.pid + " uid " + mCurClient.uid);
620 }
621 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800622
The Android Open Source Project10592532009-03-18 17:39:46 -0700623 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 }
625 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 private int getImeShowFlags() {
628 int flags = 0;
629 if (mShowForced) {
630 flags |= InputMethod.SHOW_FORCED
631 | InputMethod.SHOW_EXPLICIT;
632 } else if (mShowExplicitlyRequested) {
633 flags |= InputMethod.SHOW_EXPLICIT;
634 }
635 return flags;
636 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 private int getAppShowFlags() {
639 int flags = 0;
640 if (mShowForced) {
641 flags |= InputMethodManager.SHOW_FORCED;
642 } else if (!mShowExplicitlyRequested) {
643 flags |= InputMethodManager.SHOW_IMPLICIT;
644 }
645 return flags;
646 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
649 if (!mBoundToMethod) {
650 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
651 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
652 mBoundToMethod = true;
653 }
654 final SessionState session = mCurClient.curSession;
655 if (initial) {
656 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
657 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
658 } else {
659 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
660 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
661 }
662 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800663 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800664 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 }
666 return needResult
667 ? new InputBindResult(session.session, mCurId, mCurSeq)
668 : null;
669 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 InputBindResult startInputLocked(IInputMethodClient client,
672 IInputContext inputContext, EditorInfo attribute,
673 boolean initial, boolean needResult) {
674 // If no method is currently selected, do nothing.
675 if (mCurMethodId == null) {
676 return mNoBinding;
677 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 ClientState cs = mClients.get(client.asBinder());
680 if (cs == null) {
681 throw new IllegalArgumentException("unknown client "
682 + client.asBinder());
683 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 try {
686 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
687 // Check with the window manager to make sure this client actually
688 // has a window with focus. If not, reject. This is thread safe
689 // because if the focus changes some time before or after, the
690 // next client receiving focus that has any interest in input will
691 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800692 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
694 return null;
695 }
696 } catch (RemoteException e) {
697 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 if (mCurClient != cs) {
700 // If the client is changing, we need to switch over to the new
701 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700702 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800703 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 + cs.client.asBinder());
705
706 // If the screen is on, inform the new client it is active
707 if (mScreenOn) {
708 try {
709 cs.client.setActive(mScreenOn);
710 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800711 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 + cs.pid + " uid " + cs.uid);
713 }
714 }
715 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 // Bump up the sequence for this client and attach it.
718 mCurSeq++;
719 if (mCurSeq <= 0) mCurSeq = 1;
720 mCurClient = cs;
721 mCurInputContext = inputContext;
722 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 // Check if the input method is changing.
725 if (mCurId != null && mCurId.equals(mCurMethodId)) {
726 if (cs.curSession != null) {
727 // Fast case: if we are already connected to the input method,
728 // then just return it.
729 return attachNewInputLocked(initial, needResult);
730 }
731 if (mHaveConnection) {
732 if (mCurMethod != null) {
733 if (!cs.sessionRequested) {
734 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800735 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
737 MSG_CREATE_SESSION, mCurMethod,
738 new MethodCallback(mCurMethod)));
739 }
740 // Return to client, and we will get back with it when
741 // we have had a session made for it.
742 return new InputBindResult(null, mCurId, mCurSeq);
743 } else if (SystemClock.uptimeMillis()
744 < (mLastBindTime+TIME_TO_RECONNECT)) {
745 // In this case we have connected to the service, but
746 // don't yet have its interface. If it hasn't been too
747 // long since we did the connection, we'll return to
748 // the client and wait to get the service interface so
749 // we can report back. If it has been too long, we want
750 // to fall through so we can try a disconnect/reconnect
751 // to see if we can get back in touch with the service.
752 return new InputBindResult(null, mCurId, mCurSeq);
753 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800754 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
755 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 }
757 }
758 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800759
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700760 return startInputInnerLocked();
761 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800762
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700763 InputBindResult startInputInnerLocked() {
764 if (mCurMethodId == null) {
765 return mNoBinding;
766 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800767
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700768 if (!mSystemReady) {
769 // If the system is not yet ready, we shouldn't be running third
770 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700771 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700772 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 InputMethodInfo info = mMethodMap.get(mCurMethodId);
775 if (info == null) {
776 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
777 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800778
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700779 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
782 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700783 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
784 com.android.internal.R.string.input_method_binding_label);
785 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
786 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
788 mLastBindTime = SystemClock.uptimeMillis();
789 mHaveConnection = true;
790 mCurId = info.getId();
791 mCurToken = new Binder();
792 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800793 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 mIWindowManager.addWindowToken(mCurToken,
795 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
796 } catch (RemoteException e) {
797 }
798 return new InputBindResult(null, mCurId, mCurSeq);
799 } else {
800 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800801 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 + mCurIntent);
803 }
804 return null;
805 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 public InputBindResult startInput(IInputMethodClient client,
808 IInputContext inputContext, EditorInfo attribute,
809 boolean initial, boolean needResult) {
810 synchronized (mMethodMap) {
811 final long ident = Binder.clearCallingIdentity();
812 try {
813 return startInputLocked(client, inputContext, attribute,
814 initial, needResult);
815 } finally {
816 Binder.restoreCallingIdentity(ident);
817 }
818 }
819 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 public void finishInput(IInputMethodClient client) {
822 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 public void onServiceConnected(ComponentName name, IBinder service) {
825 synchronized (mMethodMap) {
826 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
827 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700828 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800829 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700830 unbindCurrentMethodLocked(false);
831 return;
832 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800833 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700834 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
835 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800837 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700838 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700840 MSG_CREATE_SESSION, mCurMethod,
841 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 }
843 }
844 }
845 }
846
847 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
848 synchronized (mMethodMap) {
849 if (mCurMethod != null && method != null
850 && mCurMethod.asBinder() == method.asBinder()) {
851 if (mCurClient != null) {
852 mCurClient.curSession = new SessionState(mCurClient,
853 method, session);
854 mCurClient.sessionRequested = false;
855 InputBindResult res = attachNewInputLocked(true, true);
856 if (res.method != null) {
857 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
858 MSG_BIND_METHOD, mCurClient.client, res));
859 }
860 }
861 }
862 }
863 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800864
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700865 void unbindCurrentMethodLocked(boolean reportToClient) {
866 if (mHaveConnection) {
867 mContext.unbindService(this);
868 mHaveConnection = false;
869 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800870
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700871 if (mCurToken != null) {
872 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800873 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700874 mIWindowManager.removeWindowToken(mCurToken);
875 } catch (RemoteException e) {
876 }
877 mCurToken = null;
878 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800879
The Android Open Source Project10592532009-03-18 17:39:46 -0700880 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700881 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800882
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700883 if (reportToClient && mCurClient != null) {
884 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
885 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
886 }
887 }
888
Devin Taylor0c33ed22010-02-23 13:26:46 -0600889 private void finishSession(SessionState sessionState) {
890 if (sessionState != null && sessionState.session != null) {
891 try {
892 sessionState.session.finishSession();
893 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700894 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600895 }
896 }
897 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800898
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700899 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 if (mCurMethod != null) {
901 for (ClientState cs : mClients.values()) {
902 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600903 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 cs.curSession = null;
905 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600906
907 finishSession(mEnabledSession);
908 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 mCurMethod = null;
910 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700911 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 public void onServiceDisconnected(ComponentName name) {
915 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800916 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 + " mCurIntent=" + mCurIntent);
918 if (mCurMethod != null && mCurIntent != null
919 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700920 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 // We consider this to be a new bind attempt, since the system
922 // should now try to restart the service for us.
923 mLastBindTime = SystemClock.uptimeMillis();
924 mShowRequested = mInputShown;
925 mInputShown = false;
926 if (mCurClient != null) {
927 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
928 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
929 }
930 }
931 }
932 }
933
934 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700935 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 long ident = Binder.clearCallingIdentity();
937 try {
938 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700939 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 return;
941 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 synchronized (mMethodMap) {
944 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800945 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700946 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800948 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700949 mStatusBar.setIcon("ime", packageName, iconId, 0);
950 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 }
952 }
953 } finally {
954 Binder.restoreCallingIdentity(ident);
955 }
956 }
957
satok06487a52010-10-29 11:37:18 +0900958 public void setIMEButtonVisible(IBinder token, boolean visible) {
959 int uid = Binder.getCallingUid();
960 long ident = Binder.clearCallingIdentity();
961 try {
962 if (token == null || mCurToken != token) {
963 Slog.w(TAG, "Ignoring setIMEButtonVisible of uid " + uid + " token: " + token);
964 return;
965 }
966
967 synchronized (mMethodMap) {
968 mStatusBar.setIMEButtonVisible(visible);
969 }
970 } finally {
971 Binder.restoreCallingIdentity(ident);
972 }
973 }
974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700976 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
977 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
978 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
979 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +0900981 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +0900982 // There is no input method selected, try to choose new applicable input method.
983 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
984 id = Settings.Secure.getString(mContext.getContentResolver(),
985 Settings.Secure.DEFAULT_INPUT_METHOD);
986 }
987 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 try {
satokab751aa2010-09-14 19:17:36 +0900989 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800991 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -0700992 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700993 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 }
satok4e4569d2010-11-19 18:45:53 +0900995 mShortcutInputMethodsAndSubtypes = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700996 } else {
997 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -0700998 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700999 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 }
1001 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001002
satokab751aa2010-09-14 19:17:36 +09001003 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 InputMethodInfo info = mMethodMap.get(id);
1005 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001006 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 if (id.equals(mCurMethodId)) {
satokb66d2872010-11-10 01:04:04 +09001010 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
1011 if (subtypeId >= 0 && subtypeId < subtypes.size()) {
1012 InputMethodSubtype subtype = subtypes.get(subtypeId);
satokab751aa2010-09-14 19:17:36 +09001013 if (subtype != mCurrentSubtype) {
1014 synchronized (mMethodMap) {
1015 if (mCurMethod != null) {
1016 try {
satok723a27e2010-11-11 14:58:11 +09001017 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
satok06e07442010-11-02 19:46:55 +09001018 if (mInputShown) {
1019 // If mInputShown is false, there is no IME button on the
1020 // system bar.
1021 // Thus there is no need to make it invisible explicitly.
1022 mStatusBar.setIMEButtonVisible(true);
1023 }
satokab751aa2010-09-14 19:17:36 +09001024 mCurMethod.changeInputMethodSubtype(subtype);
1025 } catch (RemoteException e) {
1026 return;
1027 }
1028 }
1029 }
1030 }
1031 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 return;
1033 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 final long ident = Binder.clearCallingIdentity();
1036 try {
satokab751aa2010-09-14 19:17:36 +09001037 // Set a subtype to this input method.
1038 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001039 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1040 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1041 // because mCurMethodId is stored as a history in
1042 // setSelectedInputMethodAndSubtypeLocked().
1043 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044
1045 if (ActivityManagerNative.isSystemReady()) {
1046 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001047 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 intent.putExtra("input_method_id", id);
1049 mContext.sendBroadcast(intent);
1050 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001051 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 } finally {
1053 Binder.restoreCallingIdentity(ident);
1054 }
1055 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001056
The Android Open Source Project4df24232009-03-05 14:34:35 -08001057 public boolean showSoftInput(IInputMethodClient client, int flags,
1058 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001059 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 long ident = Binder.clearCallingIdentity();
1061 try {
1062 synchronized (mMethodMap) {
1063 if (mCurClient == null || client == null
1064 || mCurClient.client.asBinder() != client.asBinder()) {
1065 try {
1066 // We need to check if this is the current client with
1067 // focus in the window manager, to allow this call to
1068 // be made before input is started in it.
1069 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001070 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001071 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 }
1073 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001074 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 }
1076 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001077
Joe Onorato8a9b2202010-02-26 18:56:32 -08001078 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001079 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 }
1081 } finally {
1082 Binder.restoreCallingIdentity(ident);
1083 }
1084 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001085
The Android Open Source Project4df24232009-03-05 14:34:35 -08001086 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 mShowRequested = true;
1088 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1089 mShowExplicitlyRequested = true;
1090 }
1091 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1092 mShowExplicitlyRequested = true;
1093 mShowForced = true;
1094 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001095
Dianne Hackborncc278702009-09-02 23:07:23 -07001096 if (!mSystemReady) {
1097 return false;
1098 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001099
The Android Open Source Project4df24232009-03-05 14:34:35 -08001100 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001102 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1103 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1104 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001106 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 } else if (mHaveConnection && SystemClock.uptimeMillis()
1108 < (mLastBindTime+TIME_TO_RECONNECT)) {
1109 // The client has asked to have the input method shown, but
1110 // we have been sitting here too long with a connection to the
1111 // service and no interface received, so let's disconnect/connect
1112 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001113 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 SystemClock.uptimeMillis()-mLastBindTime,1);
1115 mContext.unbindService(this);
1116 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1117 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001118
The Android Open Source Project4df24232009-03-05 14:34:35 -08001119 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001121
The Android Open Source Project4df24232009-03-05 14:34:35 -08001122 public boolean hideSoftInput(IInputMethodClient client, int flags,
1123 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001124 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 long ident = Binder.clearCallingIdentity();
1126 try {
1127 synchronized (mMethodMap) {
1128 if (mCurClient == null || client == null
1129 || mCurClient.client.asBinder() != client.asBinder()) {
1130 try {
1131 // We need to check if this is the current client with
1132 // focus in the window manager, to allow this call to
1133 // be made before input is started in it.
1134 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001135 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1136 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001137 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
1139 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001140 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 }
1142 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001143
Joe Onorato8a9b2202010-02-26 18:56:32 -08001144 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001145 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 }
1147 } finally {
1148 Binder.restoreCallingIdentity(ident);
1149 }
1150 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001151
The Android Open Source Project4df24232009-03-05 14:34:35 -08001152 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1154 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001155 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001157 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 }
1159 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001160 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001162 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001164 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001166 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1167 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1168 res = true;
1169 } else {
1170 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
1172 mInputShown = false;
1173 mShowRequested = false;
1174 mShowExplicitlyRequested = false;
1175 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001176 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001178
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001179 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1181 boolean first, int windowFlags) {
1182 long ident = Binder.clearCallingIdentity();
1183 try {
1184 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001185 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 + " viewHasFocus=" + viewHasFocus
1187 + " isTextEditor=" + isTextEditor
1188 + " softInputMode=#" + Integer.toHexString(softInputMode)
1189 + " first=" + first + " flags=#"
1190 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 if (mCurClient == null || client == null
1193 || mCurClient.client.asBinder() != client.asBinder()) {
1194 try {
1195 // We need to check if this is the current client with
1196 // focus in the window manager, to allow this call to
1197 // be made before input is started in it.
1198 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001199 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 return;
1201 }
1202 } catch (RemoteException e) {
1203 }
1204 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001205
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001206 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001207 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001208 return;
1209 }
1210 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1213 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
1214 if (!isTextEditor || (softInputMode &
1215 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1216 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) {
1217 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1218 // There is no focus view, and this window will
1219 // be behind any soft input window, so hide the
1220 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001221 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001222 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 }
1224 } else if (isTextEditor && (softInputMode &
1225 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1226 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1227 && (softInputMode &
1228 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1229 // There is a focus view, and we are navigating forward
1230 // into the window, so show the input window for the user.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001231 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001232 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 }
1234 break;
1235 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1236 // Do nothing.
1237 break;
1238 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1239 if ((softInputMode &
1240 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001241 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001242 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 }
1244 break;
1245 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001246 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001247 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 break;
1249 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1250 if ((softInputMode &
1251 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001252 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001253 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 }
1255 break;
1256 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001257 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001258 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 break;
1260 }
1261 }
1262 } finally {
1263 Binder.restoreCallingIdentity(ident);
1264 }
1265 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1268 synchronized (mMethodMap) {
1269 if (mCurClient == null || client == null
1270 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001271 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001272 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 }
1274
1275 mHandler.sendEmptyMessage(MSG_SHOW_IM_PICKER);
1276 }
1277 }
1278
satokab751aa2010-09-14 19:17:36 +09001279 public void showInputMethodSubtypePickerFromClient(IInputMethodClient client) {
1280 synchronized (mMethodMap) {
1281 if (mCurClient == null || client == null
1282 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001283 Slog.w(TAG, "Ignoring showInputMethodSubtypePickerFromClient of: " + client);
satokab751aa2010-09-14 19:17:36 +09001284 }
1285
1286 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1287 }
1288 }
1289
satok47a44912010-10-06 16:03:58 +09001290 public void showInputMethodAndSubtypeEnablerFromClient(
1291 IInputMethodClient client, String topId) {
1292 // TODO: Handle topId for setting the top position of the list activity
1293 synchronized (mMethodMap) {
1294 if (mCurClient == null || client == null
1295 || mCurClient.client.asBinder() != client.asBinder()) {
1296 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1297 }
1298
1299 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_ENABLER);
1300 }
1301 }
1302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 public void setInputMethod(IBinder token, String id) {
satokab751aa2010-09-14 19:17:36 +09001304 setInputMethodWithSubtype(token, id, NOT_A_SUBTYPE_ID);
1305 }
1306
satok735cf382010-11-11 20:40:09 +09001307 public boolean switchToLastInputMethod(IBinder token) {
1308 synchronized (mMethodMap) {
1309 Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1310 if (lastIme != null) {
1311 InputMethodInfo imi = mMethodMap.get(lastIme.first);
1312 if (imi != null) {
1313 setInputMethodWithSubtype(token, lastIme.first, getSubtypeIdFromHashCode(
1314 imi, Integer.valueOf(lastIme.second)));
1315 return true;
1316 }
1317 }
1318 return false;
1319 }
1320 }
1321
satokab751aa2010-09-14 19:17:36 +09001322 private void setInputMethodWithSubtype(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 synchronized (mMethodMap) {
1324 if (token == null) {
1325 if (mContext.checkCallingOrSelfPermission(
1326 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1327 != PackageManager.PERMISSION_GRANTED) {
1328 throw new SecurityException(
1329 "Using null token requires permission "
1330 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1331 }
1332 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001333 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1334 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 return;
1336 }
1337
1338 long ident = Binder.clearCallingIdentity();
1339 try {
satokab751aa2010-09-14 19:17:36 +09001340 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 } finally {
1342 Binder.restoreCallingIdentity(ident);
1343 }
1344 }
1345 }
1346
1347 public void hideMySoftInput(IBinder token, int flags) {
1348 synchronized (mMethodMap) {
1349 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001350 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1351 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 return;
1353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 long ident = Binder.clearCallingIdentity();
1355 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001356 hideCurrentInputLocked(flags, null);
1357 } finally {
1358 Binder.restoreCallingIdentity(ident);
1359 }
1360 }
1361 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001362
The Android Open Source Project4df24232009-03-05 14:34:35 -08001363 public void showMySoftInput(IBinder token, int flags) {
1364 synchronized (mMethodMap) {
1365 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001366 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1367 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001368 return;
1369 }
1370 long ident = Binder.clearCallingIdentity();
1371 try {
1372 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 } finally {
1374 Binder.restoreCallingIdentity(ident);
1375 }
1376 }
1377 }
1378
1379 void setEnabledSessionInMainThread(SessionState session) {
1380 if (mEnabledSession != session) {
1381 if (mEnabledSession != null) {
1382 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001383 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 mEnabledSession.method.setSessionEnabled(
1385 mEnabledSession.session, false);
1386 } catch (RemoteException e) {
1387 }
1388 }
1389 mEnabledSession = session;
1390 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001391 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 session.method.setSessionEnabled(
1393 session.session, true);
1394 } catch (RemoteException e) {
1395 }
1396 }
1397 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 public boolean handleMessage(Message msg) {
1400 HandlerCaller.SomeArgs args;
1401 switch (msg.what) {
1402 case MSG_SHOW_IM_PICKER:
1403 showInputMethodMenu();
1404 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001405
satokab751aa2010-09-14 19:17:36 +09001406 case MSG_SHOW_IM_SUBTYPE_PICKER:
1407 showInputMethodSubtypeMenu();
1408 return true;
1409
satok47a44912010-10-06 16:03:58 +09001410 case MSG_SHOW_IM_SUBTYPE_ENABLER:
1411 showInputMethodAndSubtypeEnabler();
1412 return true;
1413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 case MSG_UNBIND_INPUT:
1417 try {
1418 ((IInputMethod)msg.obj).unbindInput();
1419 } catch (RemoteException e) {
1420 // There is nothing interesting about the method dying.
1421 }
1422 return true;
1423 case MSG_BIND_INPUT:
1424 args = (HandlerCaller.SomeArgs)msg.obj;
1425 try {
1426 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1427 } catch (RemoteException e) {
1428 }
1429 return true;
1430 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001431 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001433 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1434 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 } catch (RemoteException e) {
1436 }
1437 return true;
1438 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001439 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001441 ((IInputMethod)args.arg1).hideSoftInput(0,
1442 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 } catch (RemoteException e) {
1444 }
1445 return true;
1446 case MSG_ATTACH_TOKEN:
1447 args = (HandlerCaller.SomeArgs)msg.obj;
1448 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001449 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1451 } catch (RemoteException e) {
1452 }
1453 return true;
1454 case MSG_CREATE_SESSION:
1455 args = (HandlerCaller.SomeArgs)msg.obj;
1456 try {
1457 ((IInputMethod)args.arg1).createSession(
1458 (IInputMethodCallback)args.arg2);
1459 } catch (RemoteException e) {
1460 }
1461 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 case MSG_START_INPUT:
1465 args = (HandlerCaller.SomeArgs)msg.obj;
1466 try {
1467 SessionState session = (SessionState)args.arg1;
1468 setEnabledSessionInMainThread(session);
1469 session.method.startInput((IInputContext)args.arg2,
1470 (EditorInfo)args.arg3);
1471 } catch (RemoteException e) {
1472 }
1473 return true;
1474 case MSG_RESTART_INPUT:
1475 args = (HandlerCaller.SomeArgs)msg.obj;
1476 try {
1477 SessionState session = (SessionState)args.arg1;
1478 setEnabledSessionInMainThread(session);
1479 session.method.restartInput((IInputContext)args.arg2,
1480 (EditorInfo)args.arg3);
1481 } catch (RemoteException e) {
1482 }
1483 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 case MSG_UNBIND_METHOD:
1488 try {
1489 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1490 } catch (RemoteException e) {
1491 // There is nothing interesting about the last client dying.
1492 }
1493 return true;
1494 case MSG_BIND_METHOD:
1495 args = (HandlerCaller.SomeArgs)msg.obj;
1496 try {
1497 ((IInputMethodClient)args.arg1).onBindMethod(
1498 (InputBindResult)args.arg2);
1499 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001500 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 }
1502 return true;
1503 }
1504 return false;
1505 }
1506
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001507 private boolean isSystemIme(InputMethodInfo inputMethod) {
1508 return (inputMethod.getServiceInfo().applicationInfo.flags
1509 & ApplicationInfo.FLAG_SYSTEM) != 0;
1510 }
1511
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001512 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001513 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001514 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001515 // We'd prefer to fall back on a system IME, since that is safer.
1516 int i=enabled.size();
1517 while (i > 0) {
1518 i--;
1519 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1520 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1521 break;
1522 }
1523 }
satokab751aa2010-09-14 19:17:36 +09001524 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001525 if (DEBUG) {
1526 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1527 }
satok723a27e2010-11-11 14:58:11 +09001528 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001529 return true;
1530 }
1531
1532 return false;
1533 }
1534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1536 HashMap<String, InputMethodInfo> map) {
1537 list.clear();
1538 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 PackageManager pm = mContext.getPackageManager();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001541 final Configuration config = mContext.getResources().getConfiguration();
1542 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1543 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1544 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1545 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546
1547 List<ResolveInfo> services = pm.queryIntentServices(
1548 new Intent(InputMethod.SERVICE_INTERFACE),
1549 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 for (int i = 0; i < services.size(); ++i) {
1552 ResolveInfo ri = services.get(i);
1553 ServiceInfo si = ri.serviceInfo;
1554 ComponentName compName = new ComponentName(si.packageName, si.name);
1555 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1556 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001557 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 + ": it does not require the permission "
1559 + android.Manifest.permission.BIND_INPUT_METHOD);
1560 continue;
1561 }
1562
Joe Onorato8a9b2202010-02-26 18:56:32 -08001563 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564
1565 try {
1566 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1567 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001568 final String id = p.getId();
1569 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570
Amith Yamasanie861ec12010-03-24 21:39:27 -07001571 // System IMEs are enabled by default, unless there's a hard keyboard
1572 // and the system IME was explicitly disabled
1573 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1574 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001575 }
1576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001578 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001582 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001584 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 }
1586 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001587
1588 String defaultIme = Settings.Secure.getString(mContext
1589 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001590 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001591 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001592 updateFromSettingsLocked();
1593 }
1594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001598
satokab751aa2010-09-14 19:17:36 +09001599 private void showInputMethodMenu() {
1600 showInputMethodMenuInternal(false);
1601 }
1602
1603 private void showInputMethodSubtypeMenu() {
1604 showInputMethodMenuInternal(true);
1605 }
1606
satok47a44912010-10-06 16:03:58 +09001607 private void showInputMethodAndSubtypeEnabler() {
satok86417ea2010-10-27 14:11:03 +09001608 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_AND_SUBTYPE_ENABLER);
satok47a44912010-10-06 16:03:58 +09001609 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001610 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1611 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001612 mContext.startActivity(intent);
1613 }
1614
satokab751aa2010-09-14 19:17:36 +09001615 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001616 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 String lastInputMethodId = Settings.Secure.getString(context
1623 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001624 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001625 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001626
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001627 synchronized (mMethodMap) {
satok7f35c8c2010-10-07 21:13:11 +09001628 final List<Pair<InputMethodInfo, ArrayList<String>>> immis =
satok67ddf9c2010-11-17 09:45:54 +09001629 mSettings.getEnabledInputMethodAndSubtypeHashCodeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001630 ArrayList<Integer> subtypeIds = new ArrayList<Integer>();
1631
1632 if (immis == null || immis.size() == 0) {
1633 return;
1634 }
1635
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001636 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001638 int N = immis.size();
satok913a8922010-08-26 21:53:41 +09001639
satokab751aa2010-09-14 19:17:36 +09001640 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1641 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001642
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001643 for (int i = 0; i < N; ++i) {
satok7f35c8c2010-10-07 21:13:11 +09001644 InputMethodInfo property = immis.get(i).first;
1645 final ArrayList<String> enabledSubtypeIds = immis.get(i).second;
1646 HashSet<String> enabledSubtypeSet = new HashSet<String>();
1647 for (String s : enabledSubtypeIds) {
1648 enabledSubtypeSet.add(s);
1649 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001650 if (property == null) {
1651 continue;
1652 }
satokab751aa2010-09-14 19:17:36 +09001653 ArrayList<InputMethodSubtype> subtypes = property.getSubtypes();
1654 CharSequence label = property.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001655 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokab751aa2010-09-14 19:17:36 +09001656 for (int j = 0; j < subtypes.size(); ++j) {
1657 InputMethodSubtype subtype = subtypes.get(j);
satok7f35c8c2010-10-07 21:13:11 +09001658 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1659 CharSequence title;
1660 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001661 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001662 if (nameResId != 0) {
1663 title = pm.getText(property.getPackageName(), nameResId,
1664 property.getServiceInfo().applicationInfo);
1665 } else {
1666 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001667 // TODO: Use more friendly Title and UI
1668 title = label + "," + (mode == null ? "" : mode) + ","
1669 + (language == null ? "" : language);
1670 }
1671 imMap.put(title, new Pair<InputMethodInfo, Integer>(property, j));
satokab751aa2010-09-14 19:17:36 +09001672 }
satokab751aa2010-09-14 19:17:36 +09001673 }
1674 } else {
1675 imMap.put(label,
1676 new Pair<InputMethodInfo, Integer>(property, NOT_A_SUBTYPE_ID));
1677 subtypeIds.add(0);
1678 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001679 }
satok913a8922010-08-26 21:53:41 +09001680
1681 N = imMap.size();
1682 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001683 mIms = new InputMethodInfo[N];
1684 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001685 int checkedItem = 0;
1686 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001687 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1688 mIms[i] = value.first;
1689 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001690 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001691 int subtypeId = mSubtypeIds[i];
1692 if ((subtypeId == NOT_A_SUBTYPE_ID)
1693 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1694 || (subtypeId == lastInputMethodSubtypeId)) {
1695 checkedItem = i;
1696 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 }
satokab751aa2010-09-14 19:17:36 +09001699
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001700 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1701 public void onClick(DialogInterface dialog, int which) {
1702 hideInputMethodMenu();
1703 }
1704 };
satokd87c2592010-09-29 11:52:06 +09001705
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001706 TypedArray a = context.obtainStyledAttributes(null,
1707 com.android.internal.R.styleable.DialogPreference,
1708 com.android.internal.R.attr.alertDialogStyle, 0);
1709 mDialogBuilder = new AlertDialog.Builder(context)
1710 .setTitle(com.android.internal.R.string.select_input_method)
1711 .setOnCancelListener(new OnCancelListener() {
1712 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001714 }
1715 })
1716 .setIcon(a.getDrawable(
1717 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1718 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001719
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001720 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1721 new AlertDialog.OnClickListener() {
1722 public void onClick(DialogInterface dialog, int which) {
1723 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001724 if (mIms == null || mIms.length <= which
1725 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001726 return;
1727 }
1728 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001729 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001730 hideInputMethodMenu();
1731 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001732 if ((subtypeId < 0)
1733 || (subtypeId >= im.getSubtypes().size())) {
1734 subtypeId = NOT_A_SUBTYPE_ID;
1735 }
1736 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001737 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001740 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741
satok7f35c8c2010-10-07 21:13:11 +09001742 if (showSubtypes) {
1743 mDialogBuilder.setPositiveButton(com.android.internal.R.string.more_item_label,
1744 new DialogInterface.OnClickListener() {
1745 public void onClick(DialogInterface dialog, int whichButton) {
1746 showInputMethodAndSubtypeEnabler();
1747 }
1748 });
1749 }
satok0ff647b2010-10-08 13:49:28 +09001750 mDialogBuilder.setNegativeButton(com.android.internal.R.string.cancel,
1751 new DialogInterface.OnClickListener() {
1752 public void onClick(DialogInterface dialog, int whichButton) {
1753 hideInputMethodMenu();
1754 }
1755 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 mSwitchingDialog = mDialogBuilder.create();
1757 mSwitchingDialog.getWindow().setType(
1758 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1759 mSwitchingDialog.show();
1760 }
1761 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001764 synchronized (mMethodMap) {
1765 hideInputMethodMenuLocked();
1766 }
1767 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001768
The Android Open Source Project10592532009-03-18 17:39:46 -07001769 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001770 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771
The Android Open Source Project10592532009-03-18 17:39:46 -07001772 if (mSwitchingDialog != null) {
1773 mSwitchingDialog.dismiss();
1774 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001776
The Android Open Source Project10592532009-03-18 17:39:46 -07001777 mDialogBuilder = null;
1778 mItems = null;
1779 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 public boolean setInputMethodEnabled(String id, boolean enabled) {
1785 synchronized (mMethodMap) {
1786 if (mContext.checkCallingOrSelfPermission(
1787 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1788 != PackageManager.PERMISSION_GRANTED) {
1789 throw new SecurityException(
1790 "Requires permission "
1791 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1792 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 long ident = Binder.clearCallingIdentity();
1795 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001796 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 } finally {
1798 Binder.restoreCallingIdentity(ident);
1799 }
1800 }
1801 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001802
1803 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1804 // Make sure this is a valid input method.
1805 InputMethodInfo imm = mMethodMap.get(id);
1806 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001807 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001808 }
1809
satokd87c2592010-09-29 11:52:06 +09001810 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1811 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001812
satokd87c2592010-09-29 11:52:06 +09001813 if (enabled) {
1814 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1815 if (pair.first.equals(id)) {
1816 // We are enabling this input method, but it is already enabled.
1817 // Nothing to do. The previous state was enabled.
1818 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001819 }
1820 }
satokd87c2592010-09-29 11:52:06 +09001821 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1822 // Previous state was disabled.
1823 return false;
1824 } else {
1825 StringBuilder builder = new StringBuilder();
1826 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1827 builder, enabledInputMethodsList, id)) {
1828 // Disabled input method is currently selected, switch to another one.
1829 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1830 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001831 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
1832 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
1833 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09001834 }
1835 // Previous state was enabled.
1836 return true;
1837 } else {
1838 // We are disabling the input method but it is already disabled.
1839 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001840 return false;
1841 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001842 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001843 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001844
satok723a27e2010-11-11 14:58:11 +09001845 private void saveCurrentInputMethodAndSubtypeToHistory() {
1846 String subtypeId = NOT_A_SUBTYPE_ID_STR;
1847 if (mCurrentSubtype != null) {
1848 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
1849 }
1850 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
satokab751aa2010-09-14 19:17:36 +09001851 }
1852
satok723a27e2010-11-11 14:58:11 +09001853 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
1854 boolean setSubtypeOnly) {
1855 // Update the history of InputMethod and Subtype
1856 saveCurrentInputMethodAndSubtypeToHistory();
1857
1858 // Set Subtype here
1859 if (imi == null || subtypeId < 0) {
1860 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001861 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09001862 } else {
1863 final ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1864 if (subtypeId < subtypes.size()) {
1865 mSettings.putSelectedSubtype(subtypes.get(subtypeId).hashCode());
1866 mCurrentSubtype = subtypes.get(subtypeId);
1867 } else {
1868 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
1869 mCurrentSubtype = null;
1870 }
satokab751aa2010-09-14 19:17:36 +09001871 }
satok723a27e2010-11-11 14:58:11 +09001872
1873 if (!setSubtypeOnly) {
1874 // Set InputMethod here
1875 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
1876 }
1877 }
1878
1879 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
1880 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
1881 int lastSubtypeId = NOT_A_SUBTYPE_ID;
1882 // newDefaultIme is empty when there is no candidate for the selected IME.
1883 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
1884 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
1885 if (subtypeHashCode != null) {
1886 try {
1887 lastSubtypeId = getSubtypeIdFromHashCode(
1888 imi, Integer.valueOf(subtypeHashCode));
1889 } catch (NumberFormatException e) {
1890 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
1891 }
1892 }
1893 }
1894 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09001895 }
1896
1897 private int getSelectedInputMethodSubtypeId(String id) {
1898 InputMethodInfo imi = mMethodMap.get(id);
1899 if (imi == null) {
1900 return NOT_A_SUBTYPE_ID;
1901 }
satokab751aa2010-09-14 19:17:36 +09001902 int subtypeId;
1903 try {
1904 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
1905 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
1906 } catch (SettingNotFoundException e) {
1907 return NOT_A_SUBTYPE_ID;
1908 }
satok723a27e2010-11-11 14:58:11 +09001909 return getSubtypeIdFromHashCode(imi, subtypeId);
1910 }
1911
1912 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
1913 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
satokab751aa2010-09-14 19:17:36 +09001914 for (int i = 0; i < subtypes.size(); ++i) {
1915 InputMethodSubtype ims = subtypes.get(i);
satok723a27e2010-11-11 14:58:11 +09001916 if (subtypeHashCode == ims.hashCode()) {
satokab751aa2010-09-14 19:17:36 +09001917 return i;
1918 }
1919 }
1920 return NOT_A_SUBTYPE_ID;
1921 }
1922
satok4e4569d2010-11-19 18:45:53 +09001923 /**
1924 * If there are no selected subtypes, tries finding the most applicable one according to the
1925 * given locale.
1926 * @param subtypes this function will search the most applicable subtype in subtypes
1927 * @param mode subtypes will be filtered by mode
1928 * @param locale subtypes will be filtered by locale
1929 * @param defaultSubtypeId if this function can't find the most applicable subtype, it will
1930 * return defaultSubtypeId
1931 * @return the most applicable subtypeId
1932 */
1933 private int findLastResortApplicableSubtypeLocked(
1934 List<InputMethodSubtype> subtypes, String mode, String locale, int defaultSubtypeId) {
satok8fbb1e82010-11-02 23:15:58 +09001935 if (subtypes == null || subtypes.size() == 0) {
1936 return NOT_A_SUBTYPE_ID;
1937 }
satok4e4569d2010-11-19 18:45:53 +09001938 if (TextUtils.isEmpty(locale)) {
1939 locale = mContext.getResources().getConfiguration().locale.toString();
1940 }
satok8fbb1e82010-11-02 23:15:58 +09001941 final String language = locale.substring(0, 2);
1942 boolean partialMatchFound = false;
satok4e4569d2010-11-19 18:45:53 +09001943 int applicableSubtypeId = defaultSubtypeId;
satok8fbb1e82010-11-02 23:15:58 +09001944 for (int i = 0; i < subtypes.size(); ++i) {
1945 final String subtypeLocale = subtypes.get(i).getLocale();
satok4e4569d2010-11-19 18:45:53 +09001946 // An applicable subtype should match "mode".
1947 if (subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok9ef02832010-11-04 21:17:48 +09001948 if (locale.equals(subtypeLocale)) {
1949 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
1950 applicableSubtypeId = i;
1951 break;
1952 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
1953 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
1954 applicableSubtypeId = i;
1955 partialMatchFound = true;
1956 }
satok8fbb1e82010-11-02 23:15:58 +09001957 }
1958 }
1959
1960 // The first subtype applicable to the system locale will be defined as the most applicable
1961 // subtype.
1962 if (DEBUG) {
satok03eb319a2010-11-11 18:17:42 +09001963 Slog.d(TAG, "Applicable InputMethodSubtype was found: " + applicableSubtypeId + ","
satok8fbb1e82010-11-02 23:15:58 +09001964 + subtypes.get(applicableSubtypeId).getLocale());
1965 }
1966 return applicableSubtypeId;
1967 }
1968
satok4e4569d2010-11-19 18:45:53 +09001969 // If there are no selected shortcuts, tries finding the most applicable ones.
1970 private Pair<InputMethodInfo, InputMethodSubtype>
1971 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
1972 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1973 InputMethodInfo mostApplicableIMI = null;
1974 int mostApplicableSubtypeId = NOT_A_SUBTYPE_ID;
1975 boolean foundInSystemIME = false;
1976
1977 // Search applicable subtype for each InputMethodInfo
1978 for (InputMethodInfo imi: imis) {
1979 int subtypeId = NOT_A_SUBTYPE_ID;
1980 if (mCurrentSubtype != null) {
1981 // 1. Search with the current subtype's locale and the enabled subtypes
1982 subtypeId = findLastResortApplicableSubtypeLocked(
1983 mSettings.getEnabledInputMethodSubtypeListLocked(
1984 imi), mode, mCurrentSubtype.getLocale(), NOT_A_SUBTYPE_ID);
1985 if (subtypeId == NOT_A_SUBTYPE_ID) {
1986 // 2. Search with the current subtype's locale and all subtypes
1987 subtypeId = findLastResortApplicableSubtypeLocked(imi.getSubtypes(),
1988 mode, mCurrentSubtype.getLocale(), NOT_A_SUBTYPE_ID);
1989 }
1990 }
1991 // 3. Search with the system locale and the enabled subtypes
1992 if (subtypeId == NOT_A_SUBTYPE_ID) {
1993 subtypeId = findLastResortApplicableSubtypeLocked(
1994 mSettings.getEnabledInputMethodSubtypeListLocked(
1995 imi), mode, null, NOT_A_SUBTYPE_ID);
1996 }
1997 if (subtypeId == NOT_A_SUBTYPE_ID) {
1998 // 4. Search with the system locale and all subtypes
1999 subtypeId = findLastResortApplicableSubtypeLocked(imi.getSubtypes(),
2000 mode, null, NOT_A_SUBTYPE_ID);
2001 }
2002 if (subtypeId != NOT_A_SUBTYPE_ID) {
2003 if (imi.getId().equals(mCurMethodId)) {
2004 // The current input method is the most applicable IME.
2005 mostApplicableIMI = imi;
2006 mostApplicableSubtypeId = subtypeId;
2007 break;
2008 } else if ((imi.getServiceInfo().applicationInfo.flags
2009 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2010 // The system input method is 2nd applicable IME.
2011 mostApplicableIMI = imi;
2012 mostApplicableSubtypeId = subtypeId;
2013 foundInSystemIME = true;
2014 } else if (!foundInSystemIME) {
2015 mostApplicableIMI = imi;
2016 mostApplicableSubtypeId = subtypeId;
2017 }
2018 }
2019 }
2020 if (DEBUG) {
2021 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2022 + mostApplicableIMI.getId() + "," + mostApplicableSubtypeId);
2023 }
2024 if (mostApplicableIMI != null && mostApplicableSubtypeId != NOT_A_SUBTYPE_ID) {
2025 ArrayList<Parcelable> ret = new ArrayList<Parcelable>(2);
2026 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
2027 mostApplicableIMI.getSubtypes().get(mostApplicableSubtypeId));
2028 } else {
2029 return null;
2030 }
2031 }
2032
satokab751aa2010-09-14 19:17:36 +09002033 /**
2034 * @return Return the current subtype of this input method.
2035 */
2036 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002037 boolean subtypeIsSelected = false;
2038 try {
2039 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2040 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2041 } catch (SettingNotFoundException e) {
2042 }
satok3ef8b292010-11-23 06:06:29 +09002043 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002044 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002045 String lastInputMethodId = Settings.Secure.getString(
2046 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002047 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2048 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002049 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2050 if (imi != null) {
2051 // If there are no selected subtypes, the framework will try to find
2052 // the most applicable subtype from all subtypes whose mode is
2053 // SUBTYPE_MODE_KEYBOARD. This is an exceptional case, so we will hardcode
2054 // the mode.
2055 subtypeId = findLastResortApplicableSubtypeLocked(imi.getSubtypes(),
2056 SUBTYPE_MODE_KEYBOARD, null, DEFAULT_SUBTYPE_ID);
2057 }
satok3ef8b292010-11-23 06:06:29 +09002058 }
2059 if (subtypeId != NOT_A_SUBTYPE_ID) {
2060 mCurrentSubtype =
2061 mMethodMap.get(lastInputMethodId).getSubtypes().get(subtypeId);
satok4e4569d2010-11-19 18:45:53 +09002062 } else {
2063 mCurrentSubtype = null;
satok3ef8b292010-11-23 06:06:29 +09002064 }
satok8fbb1e82010-11-02 23:15:58 +09002065 }
satok3ef8b292010-11-23 06:06:29 +09002066 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002067 }
satokab751aa2010-09-14 19:17:36 +09002068 }
2069
satok4e4569d2010-11-19 18:45:53 +09002070 // TODO: We should change the return type from List to List<Parcelable>
2071 public List getShortcutInputMethodsAndSubtypes() {
2072 synchronized (mMethodMap) {
2073 if (mShortcutInputMethodsAndSubtypesObjectList != null) {
2074 // If there are no selected shortcut subtypes, the framework will try to find
2075 // the most applicable subtype from all subtypes whose mode is
2076 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
2077 mShortcutInputMethodsAndSubtypes =
2078 new ArrayList<Pair<InputMethodInfo, InputMethodSubtype>>();
2079 mShortcutInputMethodsAndSubtypes.add(
2080 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2081 SUBTYPE_MODE_VOICE));
2082 mShortcutInputMethodsAndSubtypesObjectList = new ArrayList<Parcelable>();
2083 for (Pair ime: mShortcutInputMethodsAndSubtypes) {
2084 mShortcutInputMethodsAndSubtypesObjectList.add(ime.first);
2085 mShortcutInputMethodsAndSubtypesObjectList.add(ime.second);
2086 }
2087 }
2088 return mShortcutInputMethodsAndSubtypesObjectList;
2089 }
2090 }
2091
satokb66d2872010-11-10 01:04:04 +09002092 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2093 synchronized (mMethodMap) {
2094 if (subtype != null && mCurMethodId != null) {
2095 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2096 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2097 if (subtypeId != NOT_A_SUBTYPE_ID) {
2098 setInputMethodLocked(mCurMethodId, subtypeId);
2099 return true;
2100 }
2101 }
2102 return false;
2103 }
2104 }
2105
satokd87c2592010-09-29 11:52:06 +09002106 /**
2107 * Utility class for putting and getting settings for InputMethod
2108 * TODO: Move all putters and getters of settings to this class.
2109 */
2110 private static class InputMethodSettings {
2111 // The string for enabled input method is saved as follows:
2112 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2113 private static final char INPUT_METHOD_SEPARATER = ':';
2114 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002115 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002116 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2117
satok723a27e2010-11-11 14:58:11 +09002118 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002119 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2120
2121 private final ContentResolver mResolver;
2122 private final HashMap<String, InputMethodInfo> mMethodMap;
2123 private final ArrayList<InputMethodInfo> mMethodList;
2124
2125 private String mEnabledInputMethodsStrCache;
2126
2127 private static void buildEnabledInputMethodsSettingString(
2128 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2129 String id = pair.first;
2130 ArrayList<String> subtypes = pair.second;
2131 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002132 // Inputmethod and subtypes are saved in the settings as follows:
2133 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2134 for (String subtypeId: subtypes) {
2135 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002136 }
2137 }
2138
2139 public InputMethodSettings(
2140 ContentResolver resolver, HashMap<String, InputMethodInfo> methodMap,
2141 ArrayList<InputMethodInfo> methodList) {
2142 mResolver = resolver;
2143 mMethodMap = methodMap;
2144 mMethodList = methodList;
2145 }
2146
2147 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2148 return createEnabledInputMethodListLocked(
2149 getEnabledInputMethodsAndSubtypeListLocked());
2150 }
2151
satok7f35c8c2010-10-07 21:13:11 +09002152 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002153 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2154 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002155 getEnabledInputMethodsAndSubtypeListLocked());
2156 }
2157
satok67ddf9c2010-11-17 09:45:54 +09002158 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2159 InputMethodInfo imi) {
2160 List<Pair<String, ArrayList<String>>> imsList =
2161 getEnabledInputMethodsAndSubtypeListLocked();
2162 ArrayList<InputMethodSubtype> enabledSubtypes =
2163 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002164 if (imi != null) {
2165 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2166 InputMethodInfo info = mMethodMap.get(imsPair.first);
2167 if (info != null && info.getId().equals(imi.getId())) {
2168 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
2169 for (InputMethodSubtype ims: subtypes) {
2170 for (String s: imsPair.second) {
2171 if (String.valueOf(ims.hashCode()).equals(s)) {
2172 enabledSubtypes.add(ims);
2173 }
satok67ddf9c2010-11-17 09:45:54 +09002174 }
2175 }
satok884ef9a2010-11-18 10:39:46 +09002176 break;
satok67ddf9c2010-11-17 09:45:54 +09002177 }
satok67ddf9c2010-11-17 09:45:54 +09002178 }
2179 }
2180 return enabledSubtypes;
2181 }
2182
satokd87c2592010-09-29 11:52:06 +09002183 // At the initial boot, the settings for input methods are not set,
2184 // so we need to enable IME in that case.
2185 public void enableAllIMEsIfThereIsNoEnabledIME() {
2186 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2187 StringBuilder sb = new StringBuilder();
2188 final int N = mMethodList.size();
2189 for (int i = 0; i < N; i++) {
2190 InputMethodInfo imi = mMethodList.get(i);
2191 Slog.i(TAG, "Adding: " + imi.getId());
2192 if (i > 0) sb.append(':');
2193 sb.append(imi.getId());
2194 }
2195 putEnabledInputMethodsStr(sb.toString());
2196 }
2197 }
2198
2199 public List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
2200 ArrayList<Pair<String, ArrayList<String>>> imsList
2201 = new ArrayList<Pair<String, ArrayList<String>>>();
2202 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2203 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2204 return imsList;
2205 }
satok723a27e2010-11-11 14:58:11 +09002206 mInputMethodSplitter.setString(enabledInputMethodsStr);
2207 while (mInputMethodSplitter.hasNext()) {
2208 String nextImsStr = mInputMethodSplitter.next();
2209 mSubtypeSplitter.setString(nextImsStr);
2210 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002211 ArrayList<String> subtypeHashes = new ArrayList<String>();
2212 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002213 String imeId = mSubtypeSplitter.next();
2214 while (mSubtypeSplitter.hasNext()) {
2215 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002216 }
2217 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2218 }
2219 }
2220 return imsList;
2221 }
2222
2223 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2224 if (reloadInputMethodStr) {
2225 getEnabledInputMethodsStr();
2226 }
2227 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2228 // Add in the newly enabled input method.
2229 putEnabledInputMethodsStr(id);
2230 } else {
2231 putEnabledInputMethodsStr(
2232 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2233 }
2234 }
2235
2236 /**
2237 * Build and put a string of EnabledInputMethods with removing specified Id.
2238 * @return the specified id was removed or not.
2239 */
2240 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2241 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2242 boolean isRemoved = false;
2243 boolean needsAppendSeparator = false;
2244 for (Pair<String, ArrayList<String>> ims: imsList) {
2245 String curId = ims.first;
2246 if (curId.equals(id)) {
2247 // We are disabling this input method, and it is
2248 // currently enabled. Skip it to remove from the
2249 // new list.
2250 isRemoved = true;
2251 } else {
2252 if (needsAppendSeparator) {
2253 builder.append(INPUT_METHOD_SEPARATER);
2254 } else {
2255 needsAppendSeparator = true;
2256 }
2257 buildEnabledInputMethodsSettingString(builder, ims);
2258 }
2259 }
2260 if (isRemoved) {
2261 // Update the setting with the new list of input methods.
2262 putEnabledInputMethodsStr(builder.toString());
2263 }
2264 return isRemoved;
2265 }
2266
2267 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2268 List<Pair<String, ArrayList<String>>> imsList) {
2269 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2270 for (Pair<String, ArrayList<String>> ims: imsList) {
2271 InputMethodInfo info = mMethodMap.get(ims.first);
2272 if (info != null) {
2273 res.add(info);
2274 }
2275 }
2276 return res;
2277 }
2278
satok7f35c8c2010-10-07 21:13:11 +09002279 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002280 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002281 List<Pair<String, ArrayList<String>>> imsList) {
2282 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2283 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2284 for (Pair<String, ArrayList<String>> ims : imsList) {
2285 InputMethodInfo info = mMethodMap.get(ims.first);
2286 if (info != null) {
2287 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2288 }
2289 }
2290 return res;
2291 }
2292
satokd87c2592010-09-29 11:52:06 +09002293 private void putEnabledInputMethodsStr(String str) {
2294 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2295 mEnabledInputMethodsStrCache = str;
2296 }
2297
2298 private String getEnabledInputMethodsStr() {
2299 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2300 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002301 if (DEBUG) {
2302 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2303 }
satokd87c2592010-09-29 11:52:06 +09002304 return mEnabledInputMethodsStrCache;
2305 }
satok723a27e2010-11-11 14:58:11 +09002306
2307 private void saveSubtypeHistory(
2308 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2309 StringBuilder builder = new StringBuilder();
2310 boolean isImeAdded = false;
2311 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2312 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2313 newSubtypeId);
2314 isImeAdded = true;
2315 }
2316 for (Pair<String, String> ime: savedImes) {
2317 String imeId = ime.first;
2318 String subtypeId = ime.second;
2319 if (TextUtils.isEmpty(subtypeId)) {
2320 subtypeId = NOT_A_SUBTYPE_ID_STR;
2321 }
2322 if (isImeAdded) {
2323 builder.append(INPUT_METHOD_SEPARATER);
2324 } else {
2325 isImeAdded = true;
2326 }
2327 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2328 subtypeId);
2329 }
2330 // Remove the last INPUT_METHOD_SEPARATER
2331 putSubtypeHistoryStr(builder.toString());
2332 }
2333
2334 public void addSubtypeToHistory(String imeId, String subtypeId) {
2335 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2336 for (Pair<String, String> ime: subtypeHistory) {
2337 if (ime.first.equals(imeId)) {
2338 if (DEBUG) {
2339 Slog.v(TAG, "Subtype found in the history: " + imeId
2340 + ime.second);
2341 }
2342 // We should break here
2343 subtypeHistory.remove(ime);
2344 break;
2345 }
2346 }
2347 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2348 }
2349
2350 private void putSubtypeHistoryStr(String str) {
2351 if (DEBUG) {
2352 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2353 }
2354 Settings.Secure.putString(
2355 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2356 }
2357
2358 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2359 // Gets the first one from the history
2360 return getLastSubtypeForInputMethodLockedInternal(null);
2361 }
2362
2363 public String getLastSubtypeForInputMethodLocked(String imeId) {
2364 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2365 if (ime != null) {
2366 return ime.second;
2367 } else {
2368 return null;
2369 }
2370 }
2371
2372 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2373 List<Pair<String, ArrayList<String>>> enabledImes =
2374 getEnabledInputMethodsAndSubtypeListLocked();
2375 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2376 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2377 final String imeInTheHistory = imeAndSubtype.first;
2378 // If imeId is empty, returns the first IME and subtype in the history
2379 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2380 final String subtypeInTheHistory = imeAndSubtype.second;
2381 final String subtypeHashCode = getEnabledSubtypeForInputMethodAndSubtypeLocked(
2382 enabledImes, imeInTheHistory, subtypeInTheHistory);
2383 if (!TextUtils.isEmpty(subtypeHashCode)) {
2384 if (DEBUG) {
2385 Slog.d(TAG, "Enabled subtype found in the history:" + subtypeHashCode);
2386 }
2387 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2388 }
2389 }
2390 }
2391 if (DEBUG) {
2392 Slog.d(TAG, "No enabled IME found in the history");
2393 }
2394 return null;
2395 }
2396
2397 private String getEnabledSubtypeForInputMethodAndSubtypeLocked(List<Pair<String,
2398 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2399 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2400 if (enabledIme.first.equals(imeId)) {
2401 for (String s: enabledIme.second) {
2402 if (s.equals(subtypeHashCode)) {
2403 // If both imeId and subtypeId are enabled, return subtypeId.
2404 return s;
2405 }
2406 }
2407 // If imeId was enabled but subtypeId was disabled.
2408 return NOT_A_SUBTYPE_ID_STR;
2409 }
2410 }
2411 // If both imeId and subtypeId are disabled, return null
2412 return null;
2413 }
2414
2415 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2416 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2417 final String subtypeHistoryStr = getSubtypeHistoryStr();
2418 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2419 return imsList;
2420 }
2421 mInputMethodSplitter.setString(subtypeHistoryStr);
2422 while (mInputMethodSplitter.hasNext()) {
2423 String nextImsStr = mInputMethodSplitter.next();
2424 mSubtypeSplitter.setString(nextImsStr);
2425 if (mSubtypeSplitter.hasNext()) {
2426 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2427 // The first element is ime id.
2428 String imeId = mSubtypeSplitter.next();
2429 while (mSubtypeSplitter.hasNext()) {
2430 subtypeId = mSubtypeSplitter.next();
2431 break;
2432 }
2433 imsList.add(new Pair<String, String>(imeId, subtypeId));
2434 }
2435 }
2436 return imsList;
2437 }
2438
2439 private String getSubtypeHistoryStr() {
2440 if (DEBUG) {
2441 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2442 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2443 }
2444 return Settings.Secure.getString(
2445 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2446 }
2447
2448 public void putSelectedInputMethod(String imeId) {
2449 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2450 }
2451
2452 public void putSelectedSubtype(int subtypeId) {
2453 Settings.Secure.putInt(
2454 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2455 }
satokd87c2592010-09-29 11:52:06 +09002456 }
2457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 @Override
2461 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2462 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2463 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2466 + Binder.getCallingPid()
2467 + ", uid=" + Binder.getCallingUid());
2468 return;
2469 }
2470
2471 IInputMethod method;
2472 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 synchronized (mMethodMap) {
2477 p.println("Current Input Method Manager state:");
2478 int N = mMethodList.size();
2479 p.println(" Input Methods:");
2480 for (int i=0; i<N; i++) {
2481 InputMethodInfo info = mMethodList.get(i);
2482 p.println(" InputMethod #" + i + ":");
2483 info.dump(p, " ");
2484 }
2485 p.println(" Clients:");
2486 for (ClientState ci : mClients.values()) {
2487 p.println(" Client " + ci + ":");
2488 p.println(" client=" + ci.client);
2489 p.println(" inputContext=" + ci.inputContext);
2490 p.println(" sessionRequested=" + ci.sessionRequested);
2491 p.println(" curSession=" + ci.curSession);
2492 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002493 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002495 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2496 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2498 + " mBoundToMethod=" + mBoundToMethod);
2499 p.println(" mCurToken=" + mCurToken);
2500 p.println(" mCurIntent=" + mCurIntent);
2501 method = mCurMethod;
2502 p.println(" mCurMethod=" + mCurMethod);
2503 p.println(" mEnabledSession=" + mEnabledSession);
2504 p.println(" mShowRequested=" + mShowRequested
2505 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2506 + " mShowForced=" + mShowForced
2507 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002508 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002510
Jeff Brownb88102f2010-09-08 11:49:43 -07002511 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 pw.flush();
2514 try {
2515 client.client.asBinder().dump(fd, args);
2516 } catch (RemoteException e) {
2517 p.println("Input method client dead: " + e);
2518 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002519 } else {
2520 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002522
Jeff Brownb88102f2010-09-08 11:49:43 -07002523 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 pw.flush();
2526 try {
2527 method.asBinder().dump(fd, args);
2528 } catch (RemoteException e) {
2529 p.println("Input method service dead: " + e);
2530 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002531 } else {
2532 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 }
2534 }
2535}