blob: 6636fb732747eb168418970e2eb2ceaebf819a02 [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;
Joe Onorato857fd9b2011-01-27 15:08:35 -080052import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.Binder;
54import android.os.Handler;
55import android.os.IBinder;
56import android.os.IInterface;
57import android.os.Message;
58import android.os.Parcel;
59import 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;
satok4d733292011-01-31 22:14:32 +090063import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070065import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090066import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.text.TextUtils;
68import android.util.EventLog;
satokab751aa2010-09-14 19:17:36 +090069import android.util.Pair;
Joe Onorato8a9b2202010-02-26 18:56:32 -080070import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.util.PrintWriterPrinter;
72import android.util.Printer;
73import android.view.IWindowManager;
74import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090075import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.view.inputmethod.InputBinding;
77import android.view.inputmethod.InputMethod;
78import android.view.inputmethod.InputMethodInfo;
79import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090080import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
82import java.io.FileDescriptor;
83import java.io.IOException;
84import java.io.PrintWriter;
satok913a8922010-08-26 21:53:41 +090085import java.text.Collator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import java.util.ArrayList;
87import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +090088import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.util.List;
satok913a8922010-08-26 21:53:41 +090090import java.util.Map;
91import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
93/**
94 * This class provides a system service that manages input methods.
95 */
96public class InputMethodManagerService extends IInputMethodManager.Stub
97 implements ServiceConnection, Handler.Callback {
98 static final boolean DEBUG = false;
99 static final String TAG = "InputManagerService";
100
101 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900102 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900103 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900104 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 static final int MSG_UNBIND_INPUT = 1000;
107 static final int MSG_BIND_INPUT = 1010;
108 static final int MSG_SHOW_SOFT_INPUT = 1020;
109 static final int MSG_HIDE_SOFT_INPUT = 1030;
110 static final int MSG_ATTACH_TOKEN = 1040;
111 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 static final int MSG_START_INPUT = 2000;
114 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 static final int MSG_UNBIND_METHOD = 3000;
117 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800120
satokab751aa2010-09-14 19:17:36 +0900121 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900122 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900123 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
124 private static final String SUBTYPE_MODE_VOICE = "voice";
125
satok57ffc002011-01-25 00:11:47 +0900126 // TODO: Will formalize this value as API
127 private static final String SUBTYPE_EXTRAVALUE_EXCLUDE_FROM_LAST_IME =
128 "excludeFromLastInputMethod";
129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800131 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900133 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700135 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 final IWindowManager mIWindowManager;
137 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 // All known input methods. mMethodMap also serves as the global
142 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900143 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
144 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 class SessionState {
147 final ClientState client;
148 final IInputMethod method;
149 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 @Override
152 public String toString() {
153 return "SessionState{uid " + client.uid + " pid " + client.pid
154 + " method " + Integer.toHexString(
155 System.identityHashCode(method))
156 + " session " + Integer.toHexString(
157 System.identityHashCode(session))
158 + "}";
159 }
160
161 SessionState(ClientState _client, IInputMethod _method,
162 IInputMethodSession _session) {
163 client = _client;
164 method = _method;
165 session = _session;
166 }
167 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 class ClientState {
170 final IInputMethodClient client;
171 final IInputContext inputContext;
172 final int uid;
173 final int pid;
174 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 boolean sessionRequested;
177 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 @Override
180 public String toString() {
181 return "ClientState{" + Integer.toHexString(
182 System.identityHashCode(this)) + " uid " + uid
183 + " pid " + pid + "}";
184 }
185
186 ClientState(IInputMethodClient _client, IInputContext _inputContext,
187 int _uid, int _pid) {
188 client = _client;
189 inputContext = _inputContext;
190 uid = _uid;
191 pid = _pid;
192 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
193 }
194 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 final HashMap<IBinder, ClientState> mClients
197 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700200 * Set once the system is ready to run third party code.
201 */
202 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800203
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700204 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 * Id of the currently selected input method.
206 */
207 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 /**
210 * The current binding sequence number, incremented every time there is
211 * a new bind performed.
212 */
213 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 /**
216 * The client that is currently bound to an input method.
217 */
218 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700221 * The last window token that gained focus.
222 */
223 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800224
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700225 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 * The input context last provided by the current client.
227 */
228 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 /**
231 * The attributes last provided by the current client.
232 */
233 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 /**
236 * The input method ID of the input method service that we are currently
237 * connected to or in the process of connecting to.
238 */
239 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 /**
satokab751aa2010-09-14 19:17:36 +0900242 * The current subtype of the current input method.
243 */
244 private InputMethodSubtype mCurrentSubtype;
245
satok4e4569d2010-11-19 18:45:53 +0900246 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900247 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
248 mShortcutInputMethodsAndSubtypes =
249 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900250
251 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 * Set to true if our ServiceConnection is currently actively bound to
253 * a service (whether or not we have gotten its IBinder back yet).
254 */
255 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 /**
258 * Set if the client has asked for the input method to be shown.
259 */
260 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
263 * Set if we were explicitly told to show the input method.
264 */
265 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 /**
268 * Set if we were forced to be shown.
269 */
270 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 /**
273 * Set if we last told the input method to show itself.
274 */
275 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
278 * The Intent used to connect to the current input method.
279 */
280 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
283 * The token we have made for the currently active input method, to
284 * identify it in the future.
285 */
286 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 /**
289 * If non-null, this is the input method service we are currently connected
290 * to.
291 */
292 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
295 * Time that we last initiated a bind to the input method, to determine
296 * if we should try to disconnect and reconnect to it.
297 */
298 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 /**
301 * Have we called mCurMethod.bindInput()?
302 */
303 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 /**
306 * Currently enabled session. Only touched by service thread, not
307 * protected by a lock.
308 */
309 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 /**
312 * True if the screen is on. The value is true initially.
313 */
314 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800315
Joe Onorato857fd9b2011-01-27 15:08:35 -0800316 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
317 int mImeWindowVis;
satok4d733292011-01-31 22:14:32 +0900318 long mOldSystemSettingsVersion;
Joe Onorato857fd9b2011-01-27 15:08:35 -0800319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 AlertDialog.Builder mDialogBuilder;
321 AlertDialog mSwitchingDialog;
322 InputMethodInfo[] mIms;
323 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900324 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 class SettingsObserver extends ContentObserver {
327 SettingsObserver(Handler handler) {
328 super(handler);
329 ContentResolver resolver = mContext.getContentResolver();
330 resolver.registerContentObserver(Settings.Secure.getUriFor(
331 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900332 resolver.registerContentObserver(Settings.Secure.getUriFor(
333 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 @Override public void onChange(boolean selfChange) {
337 synchronized (mMethodMap) {
338 updateFromSettingsLocked();
339 }
340 }
341 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
344 @Override
345 public void onReceive(Context context, Intent intent) {
346 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
347 mScreenOn = true;
348 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
349 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700350 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
351 hideInputMethodMenu();
352 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800354 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 }
356
357 // Inform the current client of the change in active status
358 try {
359 if (mCurClient != null && mCurClient.client != null) {
360 mCurClient.client.setActive(mScreenOn);
361 }
362 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800363 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 + mCurClient.pid + " uid " + mCurClient.uid);
365 }
366 }
367 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800368
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800369 class MyPackageMonitor extends PackageMonitor {
370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800372 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800374 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
376 final int N = mMethodList.size();
377 if (curInputMethodId != null) {
378 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800379 InputMethodInfo imi = mMethodList.get(i);
380 if (imi.getId().equals(curInputMethodId)) {
381 for (String pkg : packages) {
382 if (imi.getPackageName().equals(pkg)) {
383 if (!doit) {
384 return true;
385 }
satok723a27e2010-11-11 14:58:11 +0900386 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800387 chooseNewDefaultIMELocked();
388 return true;
389 }
390 }
391 }
392 }
393 }
394 }
395 return false;
396 }
397
398 @Override
399 public void onSomePackagesChanged() {
400 synchronized (mMethodMap) {
401 InputMethodInfo curIm = null;
402 String curInputMethodId = Settings.Secure.getString(mContext
403 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
404 final int N = mMethodList.size();
405 if (curInputMethodId != null) {
406 for (int i=0; i<N; i++) {
407 InputMethodInfo imi = mMethodList.get(i);
408 if (imi.getId().equals(curInputMethodId)) {
409 curIm = imi;
410 }
411 int change = isPackageDisappearing(imi.getPackageName());
412 if (change == PACKAGE_TEMPORARY_CHANGE
413 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800414 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800415 + imi.getComponent());
416 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 }
418 }
419 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800420
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800421 buildInputMethodListLocked(mMethodList, mMethodMap);
422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800424
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800425 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800426 int change = isPackageDisappearing(curIm.getPackageName());
427 if (change == PACKAGE_TEMPORARY_CHANGE
428 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800429 ServiceInfo si = null;
430 try {
431 si = mContext.getPackageManager().getServiceInfo(
432 curIm.getComponent(), 0);
433 } catch (PackageManager.NameNotFoundException ex) {
434 }
435 if (si == null) {
436 // Uh oh, current input method is no longer around!
437 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800438 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800439 mImeWindowVis = 0;
440 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
441 mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800442 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800443 changed = true;
444 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800445 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900446 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800447 }
448 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800449 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800450 }
satokab751aa2010-09-14 19:17:36 +0900451
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800452 if (curIm == null) {
453 // We currently don't have a default input method... is
454 // one now available?
455 changed = chooseNewDefaultIMELocked();
456 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800457
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800458 if (changed) {
459 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 }
461 }
462 }
463 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 class MethodCallback extends IInputMethodCallback.Stub {
466 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 MethodCallback(IInputMethod method) {
469 mMethod = method;
470 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 public void finishedEvent(int seq, boolean handled) throws RemoteException {
473 }
474
475 public void sessionCreated(IInputMethodSession session) throws RemoteException {
476 onSessionCreated(mMethod, session);
477 }
478 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800479
Joe Onorato089de882010-04-12 08:18:45 -0700480 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800482 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 mHandler = new Handler(this);
484 mIWindowManager = IWindowManager.Stub.asInterface(
485 ServiceManager.getService(Context.WINDOW_SERVICE));
486 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
487 public void executeMessage(Message msg) {
488 handleMessage(msg);
489 }
490 });
satok4d733292011-01-31 22:14:32 +0900491 // Initialize the system settings version to undefined.
492 mOldSystemSettingsVersion = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800493
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800494 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 IntentFilter screenOnOffFilt = new IntentFilter();
497 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
498 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700499 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800501
satok913a8922010-08-26 21:53:41 +0900502 mStatusBar = statusBar;
503 statusBar.setIconVisibility("ime", false);
504
satokd87c2592010-09-29 11:52:06 +0900505 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900506 mSettings = new InputMethodSettings(
507 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900509 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510
satokd87c2592010-09-29 11:52:06 +0900511 if (TextUtils.isEmpty(Settings.Secure.getString(
512 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900514 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
516 try {
satokd87c2592010-09-29 11:52:06 +0900517 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 imi.getPackageName(), 0).getResources();
519 if (res.getBoolean(imi.getIsDefaultResourceId())) {
520 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800521 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 }
523 } catch (PackageManager.NameNotFoundException ex) {
524 } catch (Resources.NotFoundException ex) {
525 }
526 }
527 }
satokd87c2592010-09-29 11:52:06 +0900528 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800530 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900533 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 }
535 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 mSettingsObserver = new SettingsObserver(mHandler);
538 updateFromSettingsLocked();
539 }
540
541 @Override
542 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
543 throws RemoteException {
544 try {
545 return super.onTransact(code, data, reply, flags);
546 } catch (RuntimeException e) {
547 // The input method manager only throws security exceptions, so let's
548 // log all others.
549 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800550 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 }
552 throw e;
553 }
554 }
555
556 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700557 synchronized (mMethodMap) {
558 if (!mSystemReady) {
559 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700560 try {
561 startInputInnerLocked();
562 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800563 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700564 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700565 }
566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 public List<InputMethodInfo> getInputMethodList() {
570 synchronized (mMethodMap) {
571 return new ArrayList<InputMethodInfo>(mMethodList);
572 }
573 }
574
575 public List<InputMethodInfo> getEnabledInputMethodList() {
576 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900577 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
579 }
580
satokbb4aa062011-01-19 21:40:27 +0900581 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
582 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
583 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
584 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
585 for (InputMethodInfo imi: getEnabledInputMethodList()) {
586 enabledInputMethodAndSubtypes.put(
587 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
588 }
589 return enabledInputMethodAndSubtypes;
590 }
591
592 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
593 boolean allowsImplicitlySelectedSubtypes) {
594 if (imi == null && mCurMethodId != null) {
595 imi = mMethodMap.get(mCurMethodId);
596 }
597 final List<InputMethodSubtype> enabledSubtypes =
598 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
599 if (!allowsImplicitlySelectedSubtypes || enabledSubtypes.size() > 0) {
600 return enabledSubtypes;
601 } else {
602 return getApplicableSubtypesLocked(mRes, getSubtypes(imi));
603 }
604 }
605
satok16331c82010-12-20 23:48:46 +0900606 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
607 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900608 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900609 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900610 }
611 }
612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 public void addClient(IInputMethodClient client,
614 IInputContext inputContext, int uid, int pid) {
615 synchronized (mMethodMap) {
616 mClients.put(client.asBinder(), new ClientState(client,
617 inputContext, uid, pid));
618 }
619 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 public void removeClient(IInputMethodClient client) {
622 synchronized (mMethodMap) {
623 mClients.remove(client.asBinder());
624 }
625 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 void executeOrSendMessage(IInterface target, Message msg) {
628 if (target.asBinder() instanceof Binder) {
629 mCaller.sendMessage(msg);
630 } else {
631 handleMessage(msg);
632 msg.recycle();
633 }
634 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800635
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700636 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800638 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 + mCurClient.client.asBinder());
640 if (mBoundToMethod) {
641 mBoundToMethod = false;
642 if (mCurMethod != null) {
643 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
644 MSG_UNBIND_INPUT, mCurMethod));
645 }
646 }
647 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
648 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
649 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 // Call setActive(false) on the old client
652 try {
653 mCurClient.client.setActive(false);
654 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800655 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 + mCurClient.pid + " uid " + mCurClient.uid);
657 }
658 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800659
The Android Open Source Project10592532009-03-18 17:39:46 -0700660 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 }
662 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 private int getImeShowFlags() {
665 int flags = 0;
666 if (mShowForced) {
667 flags |= InputMethod.SHOW_FORCED
668 | InputMethod.SHOW_EXPLICIT;
669 } else if (mShowExplicitlyRequested) {
670 flags |= InputMethod.SHOW_EXPLICIT;
671 }
672 return flags;
673 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 private int getAppShowFlags() {
676 int flags = 0;
677 if (mShowForced) {
678 flags |= InputMethodManager.SHOW_FORCED;
679 } else if (!mShowExplicitlyRequested) {
680 flags |= InputMethodManager.SHOW_IMPLICIT;
681 }
682 return flags;
683 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
686 if (!mBoundToMethod) {
687 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
688 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
689 mBoundToMethod = true;
690 }
691 final SessionState session = mCurClient.curSession;
692 if (initial) {
693 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
694 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
695 } else {
696 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
697 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
698 }
699 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800700 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800701 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 }
703 return needResult
704 ? new InputBindResult(session.session, mCurId, mCurSeq)
705 : null;
706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 InputBindResult startInputLocked(IInputMethodClient client,
709 IInputContext inputContext, EditorInfo attribute,
710 boolean initial, boolean needResult) {
711 // If no method is currently selected, do nothing.
712 if (mCurMethodId == null) {
713 return mNoBinding;
714 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 ClientState cs = mClients.get(client.asBinder());
717 if (cs == null) {
718 throw new IllegalArgumentException("unknown client "
719 + client.asBinder());
720 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 try {
723 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
724 // Check with the window manager to make sure this client actually
725 // has a window with focus. If not, reject. This is thread safe
726 // because if the focus changes some time before or after, the
727 // next client receiving focus that has any interest in input will
728 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800729 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
731 return null;
732 }
733 } catch (RemoteException e) {
734 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 if (mCurClient != cs) {
737 // If the client is changing, we need to switch over to the new
738 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700739 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800740 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 + cs.client.asBinder());
742
743 // If the screen is on, inform the new client it is active
744 if (mScreenOn) {
745 try {
746 cs.client.setActive(mScreenOn);
747 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800748 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 + cs.pid + " uid " + cs.uid);
750 }
751 }
752 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 // Bump up the sequence for this client and attach it.
755 mCurSeq++;
756 if (mCurSeq <= 0) mCurSeq = 1;
757 mCurClient = cs;
758 mCurInputContext = inputContext;
759 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 // Check if the input method is changing.
762 if (mCurId != null && mCurId.equals(mCurMethodId)) {
763 if (cs.curSession != null) {
764 // Fast case: if we are already connected to the input method,
765 // then just return it.
766 return attachNewInputLocked(initial, needResult);
767 }
768 if (mHaveConnection) {
769 if (mCurMethod != null) {
770 if (!cs.sessionRequested) {
771 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800772 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
774 MSG_CREATE_SESSION, mCurMethod,
775 new MethodCallback(mCurMethod)));
776 }
777 // Return to client, and we will get back with it when
778 // we have had a session made for it.
779 return new InputBindResult(null, mCurId, mCurSeq);
780 } else if (SystemClock.uptimeMillis()
781 < (mLastBindTime+TIME_TO_RECONNECT)) {
782 // In this case we have connected to the service, but
783 // don't yet have its interface. If it hasn't been too
784 // long since we did the connection, we'll return to
785 // the client and wait to get the service interface so
786 // we can report back. If it has been too long, we want
787 // to fall through so we can try a disconnect/reconnect
788 // to see if we can get back in touch with the service.
789 return new InputBindResult(null, mCurId, mCurSeq);
790 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800791 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
792 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 }
794 }
795 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800796
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700797 return startInputInnerLocked();
798 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800799
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700800 InputBindResult startInputInnerLocked() {
801 if (mCurMethodId == null) {
802 return mNoBinding;
803 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800804
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700805 if (!mSystemReady) {
806 // If the system is not yet ready, we shouldn't be running third
807 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700808 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700809 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 InputMethodInfo info = mMethodMap.get(mCurMethodId);
812 if (info == null) {
813 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
814 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800815
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700816 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
819 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700820 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
821 com.android.internal.R.string.input_method_binding_label);
822 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
823 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
825 mLastBindTime = SystemClock.uptimeMillis();
826 mHaveConnection = true;
827 mCurId = info.getId();
828 mCurToken = new Binder();
829 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800830 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 mIWindowManager.addWindowToken(mCurToken,
832 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
833 } catch (RemoteException e) {
834 }
835 return new InputBindResult(null, mCurId, mCurSeq);
836 } else {
837 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800838 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 + mCurIntent);
840 }
841 return null;
842 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 public InputBindResult startInput(IInputMethodClient client,
845 IInputContext inputContext, EditorInfo attribute,
846 boolean initial, boolean needResult) {
847 synchronized (mMethodMap) {
848 final long ident = Binder.clearCallingIdentity();
849 try {
850 return startInputLocked(client, inputContext, attribute,
851 initial, needResult);
852 } finally {
853 Binder.restoreCallingIdentity(ident);
854 }
855 }
856 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 public void finishInput(IInputMethodClient client) {
859 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 public void onServiceConnected(ComponentName name, IBinder service) {
862 synchronized (mMethodMap) {
863 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
864 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700865 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800866 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700867 unbindCurrentMethodLocked(false);
868 return;
869 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800870 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700871 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
872 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800874 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700875 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700877 MSG_CREATE_SESSION, mCurMethod,
878 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 }
880 }
881 }
882 }
883
884 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
885 synchronized (mMethodMap) {
886 if (mCurMethod != null && method != null
887 && mCurMethod.asBinder() == method.asBinder()) {
888 if (mCurClient != null) {
889 mCurClient.curSession = new SessionState(mCurClient,
890 method, session);
891 mCurClient.sessionRequested = false;
892 InputBindResult res = attachNewInputLocked(true, true);
893 if (res.method != null) {
894 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
895 MSG_BIND_METHOD, mCurClient.client, res));
896 }
897 }
898 }
899 }
900 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800901
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700902 void unbindCurrentMethodLocked(boolean reportToClient) {
903 if (mHaveConnection) {
904 mContext.unbindService(this);
905 mHaveConnection = false;
906 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800907
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700908 if (mCurToken != null) {
909 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800910 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700911 mIWindowManager.removeWindowToken(mCurToken);
912 } catch (RemoteException e) {
913 }
914 mCurToken = null;
915 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800916
The Android Open Source Project10592532009-03-18 17:39:46 -0700917 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700918 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800919
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700920 if (reportToClient && mCurClient != null) {
921 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
922 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
923 }
924 }
925
Devin Taylor0c33ed22010-02-23 13:26:46 -0600926 private void finishSession(SessionState sessionState) {
927 if (sessionState != null && sessionState.session != null) {
928 try {
929 sessionState.session.finishSession();
930 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700931 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600932 }
933 }
934 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800935
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700936 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 if (mCurMethod != null) {
938 for (ClientState cs : mClients.values()) {
939 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600940 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 cs.curSession = null;
942 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600943
944 finishSession(mEnabledSession);
945 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 mCurMethod = null;
947 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700948 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 public void onServiceDisconnected(ComponentName name) {
952 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800953 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 + " mCurIntent=" + mCurIntent);
955 if (mCurMethod != null && mCurIntent != null
956 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700957 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 // We consider this to be a new bind attempt, since the system
959 // should now try to restart the service for us.
960 mLastBindTime = SystemClock.uptimeMillis();
961 mShowRequested = mInputShown;
962 mInputShown = false;
963 if (mCurClient != null) {
964 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
965 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
966 }
967 }
968 }
969 }
970
971 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700972 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 long ident = Binder.clearCallingIdentity();
974 try {
975 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700976 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 return;
978 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 synchronized (mMethodMap) {
981 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800982 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700983 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800985 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700986 mStatusBar.setIcon("ime", packageName, iconId, 0);
987 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 }
989 }
990 } finally {
991 Binder.restoreCallingIdentity(ident);
992 }
993 }
994
Joe Onorato857fd9b2011-01-27 15:08:35 -0800995 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +0900996 int uid = Binder.getCallingUid();
997 long ident = Binder.clearCallingIdentity();
998 try {
999 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001000 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001001 return;
1002 }
1003
1004 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001005 mImeWindowVis = vis;
1006 mBackDisposition = backDisposition;
1007 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
satok06487a52010-10-29 11:37:18 +09001008 }
1009 } finally {
1010 Binder.restoreCallingIdentity(ident);
1011 }
1012 }
1013
satok4d733292011-01-31 22:14:32 +09001014 // TODO: Investigate and fix why are settings changes getting processed before the settings seq
1015 // number is updated?
1016 // TODO: Change this stuff to not rely on modifying settings for normal user interactions.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 void updateFromSettingsLocked() {
satok4d733292011-01-31 22:14:32 +09001018 long newSystemSettingsVersion = getSystemSettingsVersion();
1019 // This is a workaround to avoid a situation that old cached value in Settings.Secure
1020 // will be handled.
1021 if (newSystemSettingsVersion == mOldSystemSettingsVersion) return;
1022
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001023 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1024 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1025 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1026 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001028 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001029 // There is no input method selected, try to choose new applicable input method.
1030 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1031 id = Settings.Secure.getString(mContext.getContentResolver(),
1032 Settings.Secure.DEFAULT_INPUT_METHOD);
1033 }
1034 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 try {
satokab751aa2010-09-14 19:17:36 +09001036 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001038 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001039 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001040 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 }
satokf3db1af2010-11-23 13:34:33 +09001042 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001043 } else {
1044 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001045 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001046 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 }
1048 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001049
satokab751aa2010-09-14 19:17:36 +09001050 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 InputMethodInfo info = mMethodMap.get(id);
1052 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001053 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001057 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001058 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1059 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001060 }
1061 if (subtype != mCurrentSubtype) {
1062 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001063 if (subtype != null) {
1064 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1065 }
satokcd7cd292010-11-20 15:46:23 +09001066 if (mCurMethod != null) {
1067 try {
satoke40dea02011-01-30 01:14:02 +09001068 final Configuration conf = mRes.getConfiguration();
1069 final boolean haveHardKeyboard = conf.keyboard
1070 != Configuration.KEYBOARD_NOKEYS;
1071 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001072 && conf.hardKeyboardHidden
1073 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001074 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1075 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1076 : 0;
Joe Onorato857fd9b2011-01-27 15:08:35 -08001077 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1078 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001079 // If subtype is null, try to find the most applicable one from
1080 // getCurrentInputMethodSubtype.
1081 if (subtype == null) {
1082 subtype = getCurrentInputMethodSubtype();
1083 }
1084 mCurMethod.changeInputMethodSubtype(subtype);
1085 } catch (RemoteException e) {
1086 return;
satokab751aa2010-09-14 19:17:36 +09001087 }
1088 }
1089 }
1090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 return;
1092 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 final long ident = Binder.clearCallingIdentity();
1095 try {
satokab751aa2010-09-14 19:17:36 +09001096 // Set a subtype to this input method.
1097 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001098 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1099 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1100 // because mCurMethodId is stored as a history in
1101 // setSelectedInputMethodAndSubtypeLocked().
1102 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103
1104 if (ActivityManagerNative.isSystemReady()) {
1105 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001106 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 intent.putExtra("input_method_id", id);
1108 mContext.sendBroadcast(intent);
1109 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001110 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 } finally {
1112 Binder.restoreCallingIdentity(ident);
1113 }
1114 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001115
The Android Open Source Project4df24232009-03-05 14:34:35 -08001116 public boolean showSoftInput(IInputMethodClient client, int flags,
1117 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001118 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 long ident = Binder.clearCallingIdentity();
1120 try {
1121 synchronized (mMethodMap) {
1122 if (mCurClient == null || client == null
1123 || mCurClient.client.asBinder() != client.asBinder()) {
1124 try {
1125 // We need to check if this is the current client with
1126 // focus in the window manager, to allow this call to
1127 // be made before input is started in it.
1128 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001129 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001130 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 }
1132 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001133 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 }
1135 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001136
Joe Onorato8a9b2202010-02-26 18:56:32 -08001137 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001138 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 }
1140 } finally {
1141 Binder.restoreCallingIdentity(ident);
1142 }
1143 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001144
The Android Open Source Project4df24232009-03-05 14:34:35 -08001145 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 mShowRequested = true;
1147 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1148 mShowExplicitlyRequested = true;
1149 }
1150 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1151 mShowExplicitlyRequested = true;
1152 mShowForced = true;
1153 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001154
Dianne Hackborncc278702009-09-02 23:07:23 -07001155 if (!mSystemReady) {
1156 return false;
1157 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001158
The Android Open Source Project4df24232009-03-05 14:34:35 -08001159 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001161 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1162 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1163 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001165 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 } else if (mHaveConnection && SystemClock.uptimeMillis()
1167 < (mLastBindTime+TIME_TO_RECONNECT)) {
1168 // The client has asked to have the input method shown, but
1169 // we have been sitting here too long with a connection to the
1170 // service and no interface received, so let's disconnect/connect
1171 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001172 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 SystemClock.uptimeMillis()-mLastBindTime,1);
1174 mContext.unbindService(this);
1175 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1176 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001177
The Android Open Source Project4df24232009-03-05 14:34:35 -08001178 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001180
The Android Open Source Project4df24232009-03-05 14:34:35 -08001181 public boolean hideSoftInput(IInputMethodClient client, int flags,
1182 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001183 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 long ident = Binder.clearCallingIdentity();
1185 try {
1186 synchronized (mMethodMap) {
1187 if (mCurClient == null || client == null
1188 || mCurClient.client.asBinder() != client.asBinder()) {
1189 try {
1190 // We need to check if this is the current client with
1191 // focus in the window manager, to allow this call to
1192 // be made before input is started in it.
1193 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001194 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1195 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001196 mImeWindowVis = 0;
1197 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1198 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001199 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 }
1201 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001202 mImeWindowVis = 0;
1203 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001204 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 }
1206 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001207
Joe Onorato8a9b2202010-02-26 18:56:32 -08001208 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001209 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 }
1211 } finally {
1212 Binder.restoreCallingIdentity(ident);
1213 }
1214 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001215
The Android Open Source Project4df24232009-03-05 14:34:35 -08001216 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1218 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001219 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001221 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 }
1223 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001224 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001226 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001228 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001230 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1231 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1232 res = true;
1233 } else {
1234 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 }
1236 mInputShown = false;
1237 mShowRequested = false;
1238 mShowExplicitlyRequested = false;
1239 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001240 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001242
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001243 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1245 boolean first, int windowFlags) {
1246 long ident = Binder.clearCallingIdentity();
1247 try {
1248 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001249 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 + " viewHasFocus=" + viewHasFocus
1251 + " isTextEditor=" + isTextEditor
1252 + " softInputMode=#" + Integer.toHexString(softInputMode)
1253 + " first=" + first + " flags=#"
1254 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 if (mCurClient == null || client == null
1257 || mCurClient.client.asBinder() != client.asBinder()) {
1258 try {
1259 // We need to check if this is the current client with
1260 // focus in the window manager, to allow this call to
1261 // be made before input is started in it.
1262 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001263 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 return;
1265 }
1266 } catch (RemoteException e) {
1267 }
1268 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001269
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001270 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001271 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001272 return;
1273 }
1274 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001275
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001276 // Should we auto-show the IME even if the caller has not
1277 // specified what should be done with it?
1278 // We only do this automatically if the window can resize
1279 // to accommodate the IME (so what the user sees will give
1280 // them good context without input information being obscured
1281 // by the IME) or if running on a large screen where there
1282 // is more room for the target window + IME.
1283 final boolean doAutoShow =
1284 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1285 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1286 || mRes.getConfiguration().isLayoutSizeAtLeast(
1287 Configuration.SCREENLAYOUT_SIZE_LARGE);
1288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1290 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001291 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1293 // There is no focus view, and this window will
1294 // be behind any soft input window, so hide the
1295 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001296 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001297 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001299 } else if (isTextEditor && doAutoShow && (softInputMode &
1300 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 // There is a focus view, and we are navigating forward
1302 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001303 // We only do this automatically if the window an resize
1304 // to accomodate the IME (so what the user sees will give
1305 // them good context without input information being obscured
1306 // by the IME) or if running on a large screen where there
1307 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001308 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001309 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 }
1311 break;
1312 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1313 // Do nothing.
1314 break;
1315 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1316 if ((softInputMode &
1317 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001318 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001319 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 }
1321 break;
1322 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001323 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001324 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 break;
1326 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1327 if ((softInputMode &
1328 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001329 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001330 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 }
1332 break;
1333 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001334 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001335 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 break;
1337 }
1338 }
1339 } finally {
1340 Binder.restoreCallingIdentity(ident);
1341 }
1342 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1345 synchronized (mMethodMap) {
1346 if (mCurClient == null || client == null
1347 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001348 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001349 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 }
1351
satok440aab52010-11-25 09:43:11 +09001352 // Always call subtype picker, because subtype picker is a superset of input method
1353 // picker.
satokab751aa2010-09-14 19:17:36 +09001354 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1355 }
1356 }
1357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001359 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1360 }
1361
1362 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1363 synchronized (mMethodMap) {
1364 if (subtype != null) {
1365 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1366 mMethodMap.get(id), subtype.hashCode()));
1367 } else {
1368 setInputMethod(token, id);
1369 }
1370 }
satokab751aa2010-09-14 19:17:36 +09001371 }
1372
satokb416a71e2010-11-25 20:42:14 +09001373 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001374 IInputMethodClient client, String inputMethodId) {
satokb416a71e2010-11-25 20:42:14 +09001375 synchronized (mMethodMap) {
1376 if (mCurClient == null || client == null
1377 || mCurClient.client.asBinder() != client.asBinder()) {
1378 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1379 }
satok7fee71f2010-12-17 18:54:26 +09001380 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1381 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09001382 }
1383 }
1384
satok735cf382010-11-11 20:40:09 +09001385 public boolean switchToLastInputMethod(IBinder token) {
1386 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001387 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1388 if (lastIme == null) return false;
1389 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1390 if (lastImi == null) return false;
1391
1392 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1393 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1394 // If the last IME is the same as the current IME and the last subtype is not defined,
1395 // there is no need to switch to the last IME.
1396 if (imiIdIsSame && lastSubtypeHash == NOT_A_SUBTYPE_ID) return false;
1397
1398 int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1399 : mCurrentSubtype.hashCode();
1400 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1401 if (DEBUG) {
1402 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second + ", from: "
1403 + mCurMethodId + ", " + currentSubtypeHash);
satok735cf382010-11-11 20:40:09 +09001404 }
satokc445bcd2011-01-25 18:57:24 +09001405 setInputMethodWithSubtypeId(token, lastIme.first, getSubtypeIdFromHashCode(
1406 lastImi, lastSubtypeHash));
1407 return true;
satok735cf382010-11-11 20:40:09 +09001408 }
1409 return false;
1410 }
1411 }
1412
satok28203512010-11-24 11:06:49 +09001413 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 synchronized (mMethodMap) {
1415 if (token == null) {
1416 if (mContext.checkCallingOrSelfPermission(
1417 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1418 != PackageManager.PERMISSION_GRANTED) {
1419 throw new SecurityException(
1420 "Using null token requires permission "
1421 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1422 }
1423 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001424 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1425 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 return;
1427 }
1428
1429 long ident = Binder.clearCallingIdentity();
1430 try {
satokab751aa2010-09-14 19:17:36 +09001431 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 } finally {
1433 Binder.restoreCallingIdentity(ident);
1434 }
1435 }
1436 }
1437
1438 public void hideMySoftInput(IBinder token, int flags) {
1439 synchronized (mMethodMap) {
1440 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001441 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1442 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 return;
1444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 long ident = Binder.clearCallingIdentity();
1446 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001447 hideCurrentInputLocked(flags, null);
1448 } finally {
1449 Binder.restoreCallingIdentity(ident);
1450 }
1451 }
1452 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001453
The Android Open Source Project4df24232009-03-05 14:34:35 -08001454 public void showMySoftInput(IBinder token, int flags) {
1455 synchronized (mMethodMap) {
1456 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001457 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1458 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001459 return;
1460 }
1461 long ident = Binder.clearCallingIdentity();
1462 try {
1463 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 } finally {
1465 Binder.restoreCallingIdentity(ident);
1466 }
1467 }
1468 }
1469
1470 void setEnabledSessionInMainThread(SessionState session) {
1471 if (mEnabledSession != session) {
1472 if (mEnabledSession != null) {
1473 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001474 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 mEnabledSession.method.setSessionEnabled(
1476 mEnabledSession.session, false);
1477 } catch (RemoteException e) {
1478 }
1479 }
1480 mEnabledSession = session;
1481 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001482 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 session.method.setSessionEnabled(
1484 session.session, true);
1485 } catch (RemoteException e) {
1486 }
1487 }
1488 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 public boolean handleMessage(Message msg) {
1491 HandlerCaller.SomeArgs args;
1492 switch (msg.what) {
1493 case MSG_SHOW_IM_PICKER:
1494 showInputMethodMenu();
1495 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001496
satokab751aa2010-09-14 19:17:36 +09001497 case MSG_SHOW_IM_SUBTYPE_PICKER:
1498 showInputMethodSubtypeMenu();
1499 return true;
1500
satok47a44912010-10-06 16:03:58 +09001501 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001502 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001503 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001504 return true;
1505
1506 case MSG_SHOW_IM_CONFIG:
1507 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001508 return true;
1509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 case MSG_UNBIND_INPUT:
1513 try {
1514 ((IInputMethod)msg.obj).unbindInput();
1515 } catch (RemoteException e) {
1516 // There is nothing interesting about the method dying.
1517 }
1518 return true;
1519 case MSG_BIND_INPUT:
1520 args = (HandlerCaller.SomeArgs)msg.obj;
1521 try {
1522 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1523 } catch (RemoteException e) {
1524 }
1525 return true;
1526 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001527 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001529 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1530 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 } catch (RemoteException e) {
1532 }
1533 return true;
1534 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001535 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001537 ((IInputMethod)args.arg1).hideSoftInput(0,
1538 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 } catch (RemoteException e) {
1540 }
1541 return true;
1542 case MSG_ATTACH_TOKEN:
1543 args = (HandlerCaller.SomeArgs)msg.obj;
1544 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001545 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1547 } catch (RemoteException e) {
1548 }
1549 return true;
1550 case MSG_CREATE_SESSION:
1551 args = (HandlerCaller.SomeArgs)msg.obj;
1552 try {
1553 ((IInputMethod)args.arg1).createSession(
1554 (IInputMethodCallback)args.arg2);
1555 } catch (RemoteException e) {
1556 }
1557 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 case MSG_START_INPUT:
1561 args = (HandlerCaller.SomeArgs)msg.obj;
1562 try {
1563 SessionState session = (SessionState)args.arg1;
1564 setEnabledSessionInMainThread(session);
1565 session.method.startInput((IInputContext)args.arg2,
1566 (EditorInfo)args.arg3);
1567 } catch (RemoteException e) {
1568 }
1569 return true;
1570 case MSG_RESTART_INPUT:
1571 args = (HandlerCaller.SomeArgs)msg.obj;
1572 try {
1573 SessionState session = (SessionState)args.arg1;
1574 setEnabledSessionInMainThread(session);
1575 session.method.restartInput((IInputContext)args.arg2,
1576 (EditorInfo)args.arg3);
1577 } catch (RemoteException e) {
1578 }
1579 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 case MSG_UNBIND_METHOD:
1584 try {
1585 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1586 } catch (RemoteException e) {
1587 // There is nothing interesting about the last client dying.
1588 }
1589 return true;
1590 case MSG_BIND_METHOD:
1591 args = (HandlerCaller.SomeArgs)msg.obj;
1592 try {
1593 ((IInputMethodClient)args.arg1).onBindMethod(
1594 (InputBindResult)args.arg2);
1595 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001596 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 }
1598 return true;
1599 }
1600 return false;
1601 }
1602
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001603 private boolean isSystemIme(InputMethodInfo inputMethod) {
1604 return (inputMethod.getServiceInfo().applicationInfo.flags
1605 & ApplicationInfo.FLAG_SYSTEM) != 0;
1606 }
1607
Ken Wakasa586f0512011-01-20 22:31:01 +09001608 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1609 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1610 final int subtypeCount = imi.getSubtypeCount();
1611 for (int i = 0; i < subtypeCount; ++i) {
1612 subtypes.add(imi.getSubtypeAt(i));
1613 }
1614 return subtypes;
1615 }
1616
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001617 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001618 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001619 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001620 // We'd prefer to fall back on a system IME, since that is safer.
1621 int i=enabled.size();
1622 while (i > 0) {
1623 i--;
1624 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1625 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1626 break;
1627 }
1628 }
satokab751aa2010-09-14 19:17:36 +09001629 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001630 if (DEBUG) {
1631 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1632 }
satok723a27e2010-11-11 14:58:11 +09001633 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001634 return true;
1635 }
1636
1637 return false;
1638 }
1639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1641 HashMap<String, InputMethodInfo> map) {
1642 list.clear();
1643 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001646 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001647 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1648 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1649 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1650 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651
1652 List<ResolveInfo> services = pm.queryIntentServices(
1653 new Intent(InputMethod.SERVICE_INTERFACE),
1654 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 for (int i = 0; i < services.size(); ++i) {
1657 ResolveInfo ri = services.get(i);
1658 ServiceInfo si = ri.serviceInfo;
1659 ComponentName compName = new ComponentName(si.packageName, si.name);
1660 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1661 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001662 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 + ": it does not require the permission "
1664 + android.Manifest.permission.BIND_INPUT_METHOD);
1665 continue;
1666 }
1667
Joe Onorato8a9b2202010-02-26 18:56:32 -08001668 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669
1670 try {
1671 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1672 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001673 final String id = p.getId();
1674 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675
Amith Yamasanie861ec12010-03-24 21:39:27 -07001676 // System IMEs are enabled by default, unless there's a hard keyboard
1677 // and the system IME was explicitly disabled
1678 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1679 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001680 }
1681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001683 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001687 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001689 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 }
1691 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001692
1693 String defaultIme = Settings.Secure.getString(mContext
1694 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001695 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001696 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001697 updateFromSettingsLocked();
1698 }
1699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001703
satokab751aa2010-09-14 19:17:36 +09001704 private void showInputMethodMenu() {
1705 showInputMethodMenuInternal(false);
1706 }
1707
1708 private void showInputMethodSubtypeMenu() {
1709 showInputMethodMenuInternal(true);
1710 }
1711
satok217f5482010-12-15 05:19:19 +09001712 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001713 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001714 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001715 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1716 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001717 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001718 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001719 }
satok217f5482010-12-15 05:19:19 +09001720 mContext.startActivity(intent);
1721 }
1722
1723 private void showConfigureInputMethods() {
1724 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1725 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1726 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1727 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001728 mContext.startActivity(intent);
1729 }
1730
satokab751aa2010-09-14 19:17:36 +09001731 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001732 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 String lastInputMethodId = Settings.Secure.getString(context
1739 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001740 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001741 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001742
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001743 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09001744 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
1745 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001746 if (immis == null || immis.size() == 0) {
1747 return;
1748 }
1749
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001750 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751
satokab751aa2010-09-14 19:17:36 +09001752 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1753 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001754
satokbb4aa062011-01-19 21:40:27 +09001755 for (InputMethodInfo imi: immis.keySet()) {
1756 if (imi == null) continue;
1757 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09001758 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09001759 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
1760 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09001761 }
satokbb4aa062011-01-19 21:40:27 +09001762 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
1763 CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001764 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09001765 final int subtypeCount = imi.getSubtypeCount();
Ken Wakasa586f0512011-01-20 22:31:01 +09001766 for (int j = 0; j < subtypeCount; ++j) {
satokbb4aa062011-01-19 21:40:27 +09001767 InputMethodSubtype subtype = imi.getSubtypeAt(j);
satok7f35c8c2010-10-07 21:13:11 +09001768 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1769 CharSequence title;
1770 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001771 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001772 if (nameResId != 0) {
satokbb4aa062011-01-19 21:40:27 +09001773 title = pm.getText(imi.getPackageName(), nameResId,
1774 imi.getServiceInfo().applicationInfo);
satok7f35c8c2010-10-07 21:13:11 +09001775 } else {
1776 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001777 // TODO: Use more friendly Title and UI
1778 title = label + "," + (mode == null ? "" : mode) + ","
1779 + (language == null ? "" : language);
1780 }
satokbb4aa062011-01-19 21:40:27 +09001781 imMap.put(title, new Pair<InputMethodInfo, Integer>(imi, j));
satokab751aa2010-09-14 19:17:36 +09001782 }
satokab751aa2010-09-14 19:17:36 +09001783 }
1784 } else {
1785 imMap.put(label,
satokbb4aa062011-01-19 21:40:27 +09001786 new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID));
satokab751aa2010-09-14 19:17:36 +09001787 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001788 }
satok913a8922010-08-26 21:53:41 +09001789
satokbb4aa062011-01-19 21:40:27 +09001790 final int N = imMap.size();
satok913a8922010-08-26 21:53:41 +09001791 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001792 mIms = new InputMethodInfo[N];
1793 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001794 int checkedItem = 0;
1795 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001796 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1797 mIms[i] = value.first;
1798 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001799 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001800 int subtypeId = mSubtypeIds[i];
1801 if ((subtypeId == NOT_A_SUBTYPE_ID)
1802 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1803 || (subtypeId == lastInputMethodSubtypeId)) {
1804 checkedItem = i;
1805 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001806 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 }
satokab751aa2010-09-14 19:17:36 +09001808
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001809 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1810 public void onClick(DialogInterface dialog, int which) {
1811 hideInputMethodMenu();
1812 }
1813 };
satokd87c2592010-09-29 11:52:06 +09001814
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001815 TypedArray a = context.obtainStyledAttributes(null,
1816 com.android.internal.R.styleable.DialogPreference,
1817 com.android.internal.R.attr.alertDialogStyle, 0);
1818 mDialogBuilder = new AlertDialog.Builder(context)
1819 .setTitle(com.android.internal.R.string.select_input_method)
1820 .setOnCancelListener(new OnCancelListener() {
1821 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001823 }
1824 })
1825 .setIcon(a.getDrawable(
1826 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1827 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001828
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001829 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1830 new AlertDialog.OnClickListener() {
1831 public void onClick(DialogInterface dialog, int which) {
1832 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001833 if (mIms == null || mIms.length <= which
1834 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001835 return;
1836 }
1837 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001838 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001839 hideInputMethodMenu();
1840 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001841 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09001842 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09001843 subtypeId = NOT_A_SUBTYPE_ID;
1844 }
1845 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001846 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001847 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001849 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850
satok7f35c8c2010-10-07 21:13:11 +09001851 if (showSubtypes) {
satok82beadf2010-12-27 19:03:06 +09001852 mDialogBuilder.setPositiveButton(
1853 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09001854 new DialogInterface.OnClickListener() {
1855 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09001856 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09001857 }
1858 });
1859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 mSwitchingDialog = mDialogBuilder.create();
1861 mSwitchingDialog.getWindow().setType(
1862 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1863 mSwitchingDialog.show();
1864 }
1865 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001868 synchronized (mMethodMap) {
1869 hideInputMethodMenuLocked();
1870 }
1871 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001872
The Android Open Source Project10592532009-03-18 17:39:46 -07001873 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001874 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875
The Android Open Source Project10592532009-03-18 17:39:46 -07001876 if (mSwitchingDialog != null) {
1877 mSwitchingDialog.dismiss();
1878 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001880
The Android Open Source Project10592532009-03-18 17:39:46 -07001881 mDialogBuilder = null;
1882 mItems = null;
1883 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 public boolean setInputMethodEnabled(String id, boolean enabled) {
1889 synchronized (mMethodMap) {
1890 if (mContext.checkCallingOrSelfPermission(
1891 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1892 != PackageManager.PERMISSION_GRANTED) {
1893 throw new SecurityException(
1894 "Requires permission "
1895 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1896 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 long ident = Binder.clearCallingIdentity();
1899 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001900 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 } finally {
1902 Binder.restoreCallingIdentity(ident);
1903 }
1904 }
1905 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001906
1907 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1908 // Make sure this is a valid input method.
1909 InputMethodInfo imm = mMethodMap.get(id);
1910 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001911 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001912 }
1913
satokd87c2592010-09-29 11:52:06 +09001914 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1915 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001916
satokd87c2592010-09-29 11:52:06 +09001917 if (enabled) {
1918 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1919 if (pair.first.equals(id)) {
1920 // We are enabling this input method, but it is already enabled.
1921 // Nothing to do. The previous state was enabled.
1922 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001923 }
1924 }
satokd87c2592010-09-29 11:52:06 +09001925 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1926 // Previous state was disabled.
1927 return false;
1928 } else {
1929 StringBuilder builder = new StringBuilder();
1930 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1931 builder, enabledInputMethodsList, id)) {
1932 // Disabled input method is currently selected, switch to another one.
1933 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1934 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001935 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
1936 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
1937 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09001938 }
1939 // Previous state was enabled.
1940 return true;
1941 } else {
1942 // We are disabling the input method but it is already disabled.
1943 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001944 return false;
1945 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001946 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001947 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001948
satok57ffc002011-01-25 00:11:47 +09001949 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
1950 if (subtype == null) return true;
1951 String[] extraValues = subtype.getExtraValue().split(",");
1952 final int N = extraValues.length;
1953 for (int i = 0; i < N; ++i) {
1954 if (SUBTYPE_EXTRAVALUE_EXCLUDE_FROM_LAST_IME.equals(extraValues[i])) {
1955 return false;
1956 }
1957 }
1958 return true;
1959 }
1960
satok723a27e2010-11-11 14:58:11 +09001961 private void saveCurrentInputMethodAndSubtypeToHistory() {
1962 String subtypeId = NOT_A_SUBTYPE_ID_STR;
1963 if (mCurrentSubtype != null) {
1964 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
1965 }
satok57ffc002011-01-25 00:11:47 +09001966 if (canAddToLastInputMethod(mCurrentSubtype)) {
1967 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
1968 }
satokab751aa2010-09-14 19:17:36 +09001969 }
1970
satok723a27e2010-11-11 14:58:11 +09001971 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
1972 boolean setSubtypeOnly) {
satok4d733292011-01-31 22:14:32 +09001973 mOldSystemSettingsVersion = getSystemSettingsVersion();
satok723a27e2010-11-11 14:58:11 +09001974 // Update the history of InputMethod and Subtype
1975 saveCurrentInputMethodAndSubtypeToHistory();
1976
1977 // Set Subtype here
1978 if (imi == null || subtypeId < 0) {
1979 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001980 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09001981 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09001982 if (subtypeId < imi.getSubtypeCount()) {
1983 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
1984 mSettings.putSelectedSubtype(subtype.hashCode());
1985 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09001986 } else {
1987 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
1988 mCurrentSubtype = null;
1989 }
satokab751aa2010-09-14 19:17:36 +09001990 }
satok723a27e2010-11-11 14:58:11 +09001991
1992 if (!setSubtypeOnly) {
1993 // Set InputMethod here
1994 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
1995 }
1996 }
1997
1998 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
1999 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2000 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2001 // newDefaultIme is empty when there is no candidate for the selected IME.
2002 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2003 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2004 if (subtypeHashCode != null) {
2005 try {
2006 lastSubtypeId = getSubtypeIdFromHashCode(
2007 imi, Integer.valueOf(subtypeHashCode));
2008 } catch (NumberFormatException e) {
2009 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2010 }
2011 }
2012 }
2013 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002014 }
2015
2016 private int getSelectedInputMethodSubtypeId(String id) {
2017 InputMethodInfo imi = mMethodMap.get(id);
2018 if (imi == null) {
2019 return NOT_A_SUBTYPE_ID;
2020 }
satokab751aa2010-09-14 19:17:36 +09002021 int subtypeId;
2022 try {
2023 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2024 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2025 } catch (SettingNotFoundException e) {
2026 return NOT_A_SUBTYPE_ID;
2027 }
satok723a27e2010-11-11 14:58:11 +09002028 return getSubtypeIdFromHashCode(imi, subtypeId);
2029 }
2030
2031 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002032 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002033 final int subtypeCount = imi.getSubtypeCount();
2034 for (int i = 0; i < subtypeCount; ++i) {
2035 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002036 if (subtypeHashCode == ims.hashCode()) {
2037 return i;
2038 }
satokab751aa2010-09-14 19:17:36 +09002039 }
2040 }
2041 return NOT_A_SUBTYPE_ID;
2042 }
2043
satokdf31ae62011-01-15 06:19:44 +09002044 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2045 Resources res, List<InputMethodSubtype> subtypes) {
2046 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002047 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2048 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2049 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002050 final int N = subtypes.size();
2051 boolean containsKeyboardSubtype = false;
2052 for (int i = 0; i < N; ++i) {
2053 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002054 final String locale = subtype.getLocale();
2055 final String mode = subtype.getMode();
2056 // When system locale starts with subtype's locale, that subtype will be applicable
2057 // for system locale
2058 // For instance, it's clearly applicable for cases like system locale = en_US and
2059 // subtype = en, but it is not necessarily considered applicable for cases like system
2060 // locale = en and subtype = en_US.
2061 // We just call systemLocale.startsWith(locale) in this function because there is no
2062 // need to find applicable subtypes aggressively unlike
2063 // findLastResortApplicableSubtypeLocked.
2064 if (systemLocale.startsWith(locale)) {
2065 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2066 // If more applicable subtypes are contained, skip.
2067 if (applicableSubtype != null
2068 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2069 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002070 if (!containsKeyboardSubtype
2071 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2072 containsKeyboardSubtype = true;
2073 }
2074 }
2075 }
satok3da92232011-01-11 22:46:30 +09002076 ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2077 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002078 if (!containsKeyboardSubtype) {
2079 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002080 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002081 if (lastResortKeyboardSubtype != null) {
2082 applicableSubtypes.add(lastResortKeyboardSubtype);
2083 }
2084 }
2085 return applicableSubtypes;
2086 }
2087
satok4e4569d2010-11-19 18:45:53 +09002088 /**
2089 * If there are no selected subtypes, tries finding the most applicable one according to the
2090 * given locale.
2091 * @param subtypes this function will search the most applicable subtype in subtypes
2092 * @param mode subtypes will be filtered by mode
2093 * @param locale subtypes will be filtered by locale
satok7599a7fb2010-12-22 13:45:23 +09002094 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2095 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002096 * @return the most applicable subtypeId
2097 */
satokdf31ae62011-01-15 06:19:44 +09002098 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2099 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7fb2010-12-22 13:45:23 +09002100 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002101 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002102 return null;
satok8fbb1e82010-11-02 23:15:58 +09002103 }
satok4e4569d2010-11-19 18:45:53 +09002104 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002105 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002106 }
satok8fbb1e82010-11-02 23:15:58 +09002107 final String language = locale.substring(0, 2);
2108 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002109 InputMethodSubtype applicableSubtype = null;
satok7599a7fb2010-12-22 13:45:23 +09002110 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002111 final int N = subtypes.size();
2112 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002113 InputMethodSubtype subtype = subtypes.get(i);
2114 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002115 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2116 // and all subtypes with all modes can be candidates.
2117 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7fb2010-12-22 13:45:23 +09002118 if (firstMatchedModeSubtype == null) {
2119 firstMatchedModeSubtype = subtype;
2120 }
satok9ef02832010-11-04 21:17:48 +09002121 if (locale.equals(subtypeLocale)) {
2122 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002123 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002124 break;
2125 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2126 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002127 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002128 partialMatchFound = true;
2129 }
satok8fbb1e82010-11-02 23:15:58 +09002130 }
2131 }
2132
satok7599a7fb2010-12-22 13:45:23 +09002133 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2134 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002135 }
2136
satok8fbb1e82010-11-02 23:15:58 +09002137 // The first subtype applicable to the system locale will be defined as the most applicable
2138 // subtype.
2139 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002140 if (applicableSubtype != null) {
2141 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2142 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2143 }
satok8fbb1e82010-11-02 23:15:58 +09002144 }
satokcd7cd292010-11-20 15:46:23 +09002145 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002146 }
2147
satok4e4569d2010-11-19 18:45:53 +09002148 // If there are no selected shortcuts, tries finding the most applicable ones.
2149 private Pair<InputMethodInfo, InputMethodSubtype>
2150 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2151 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2152 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002153 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002154 boolean foundInSystemIME = false;
2155
2156 // Search applicable subtype for each InputMethodInfo
2157 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002158 final String imiId = imi.getId();
2159 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2160 continue;
2161 }
satokcd7cd292010-11-20 15:46:23 +09002162 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002163 final List<InputMethodSubtype> enabledSubtypes =
2164 getEnabledInputMethodSubtypeList(imi, true);
2165 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002166 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002167 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002168 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002169 }
satokdf31ae62011-01-15 06:19:44 +09002170 // 2. Search by the system locale from enabledSubtypes.
2171 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002172 if (subtype == null) {
2173 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002174 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002175 }
satok7599a7fb2010-12-22 13:45:23 +09002176 // 4. Search by the current subtype's locale from all subtypes.
2177 if (subtype == null && mCurrentSubtype != null) {
2178 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002179 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09002180 }
2181 // 5. Search by the system locale from all subtypes.
2182 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002183 if (subtype == null) {
satok7599a7fb2010-12-22 13:45:23 +09002184 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002185 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002186 }
satokcd7cd292010-11-20 15:46:23 +09002187 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09002188 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002189 // The current input method is the most applicable IME.
2190 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002191 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002192 break;
satok7599a7fb2010-12-22 13:45:23 +09002193 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002194 // The system input method is 2nd applicable IME.
2195 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002196 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09002197 if ((imi.getServiceInfo().applicationInfo.flags
2198 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2199 foundInSystemIME = true;
2200 }
satok4e4569d2010-11-19 18:45:53 +09002201 }
2202 }
2203 }
2204 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002205 if (mostApplicableIMI != null) {
2206 Slog.w(TAG, "Most applicable shortcut input method was:"
2207 + mostApplicableIMI.getId());
2208 if (mostApplicableSubtype != null) {
2209 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2210 + "," + mostApplicableSubtype.getMode() + ","
2211 + mostApplicableSubtype.getLocale());
2212 }
2213 }
satok4e4569d2010-11-19 18:45:53 +09002214 }
satokcd7cd292010-11-20 15:46:23 +09002215 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002216 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002217 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002218 } else {
2219 return null;
2220 }
2221 }
2222
satok4d733292011-01-31 22:14:32 +09002223 private static long getSystemSettingsVersion() {
2224 return SystemProperties.getLong(Settings.Secure.SYS_PROP_SETTING_VERSION, 0);
2225 }
2226
satokab751aa2010-09-14 19:17:36 +09002227 /**
2228 * @return Return the current subtype of this input method.
2229 */
2230 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002231 boolean subtypeIsSelected = false;
2232 try {
2233 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2234 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2235 } catch (SettingNotFoundException e) {
2236 }
satok3ef8b292010-11-23 06:06:29 +09002237 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002238 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002239 String lastInputMethodId = Settings.Secure.getString(
2240 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002241 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2242 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002243 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2244 if (imi != null) {
2245 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002246 // the most applicable subtype from explicitly or implicitly enabled
2247 // subtypes.
2248 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2249 getEnabledInputMethodSubtypeList(imi, true);
2250 // If there is only one explicitly or implicitly enabled subtype,
2251 // just returns it.
2252 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2253 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2254 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2255 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2256 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2257 SUBTYPE_MODE_KEYBOARD, null, true);
2258 if (mCurrentSubtype == null) {
2259 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2260 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2261 true);
2262 }
2263 }
satok4e4569d2010-11-19 18:45:53 +09002264 }
satokcd7cd292010-11-20 15:46:23 +09002265 } else {
satok3ef8b292010-11-23 06:06:29 +09002266 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002267 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002268 }
satok8fbb1e82010-11-02 23:15:58 +09002269 }
satok3ef8b292010-11-23 06:06:29 +09002270 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002271 }
satokab751aa2010-09-14 19:17:36 +09002272 }
2273
satokf3db1af2010-11-23 13:34:33 +09002274 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2275 InputMethodSubtype subtype) {
2276 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2277 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2278 } else {
2279 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2280 subtypes.add(subtype);
2281 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2282 }
2283 }
2284
satok4e4569d2010-11-19 18:45:53 +09002285 // TODO: We should change the return type from List to List<Parcelable>
2286 public List getShortcutInputMethodsAndSubtypes() {
2287 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002288 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002289 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002290 // If there are no selected shortcut subtypes, the framework will try to find
2291 // the most applicable subtype from all subtypes whose mode is
2292 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002293 Pair<InputMethodInfo, InputMethodSubtype> info =
2294 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2295 SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09002296 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002297 ret.add(info.first);
2298 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09002299 }
satok3da92232011-01-11 22:46:30 +09002300 return ret;
satokf3db1af2010-11-23 13:34:33 +09002301 }
satokf3db1af2010-11-23 13:34:33 +09002302 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2303 ret.add(imi);
2304 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2305 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002306 }
2307 }
satokf3db1af2010-11-23 13:34:33 +09002308 return ret;
satok4e4569d2010-11-19 18:45:53 +09002309 }
2310 }
2311
satokb66d2872010-11-10 01:04:04 +09002312 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2313 synchronized (mMethodMap) {
2314 if (subtype != null && mCurMethodId != null) {
2315 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2316 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2317 if (subtypeId != NOT_A_SUBTYPE_ID) {
2318 setInputMethodLocked(mCurMethodId, subtypeId);
2319 return true;
2320 }
2321 }
2322 return false;
2323 }
2324 }
2325
satokd87c2592010-09-29 11:52:06 +09002326 /**
2327 * Utility class for putting and getting settings for InputMethod
2328 * TODO: Move all putters and getters of settings to this class.
2329 */
2330 private static class InputMethodSettings {
2331 // The string for enabled input method is saved as follows:
2332 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2333 private static final char INPUT_METHOD_SEPARATER = ':';
2334 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002335 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002336 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2337
satok723a27e2010-11-11 14:58:11 +09002338 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002339 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2340
satokdf31ae62011-01-15 06:19:44 +09002341 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002342 private final ContentResolver mResolver;
2343 private final HashMap<String, InputMethodInfo> mMethodMap;
2344 private final ArrayList<InputMethodInfo> mMethodList;
2345
2346 private String mEnabledInputMethodsStrCache;
2347
2348 private static void buildEnabledInputMethodsSettingString(
2349 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2350 String id = pair.first;
2351 ArrayList<String> subtypes = pair.second;
2352 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002353 // Inputmethod and subtypes are saved in the settings as follows:
2354 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2355 for (String subtypeId: subtypes) {
2356 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002357 }
2358 }
2359
2360 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002361 Resources res, ContentResolver resolver,
2362 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2363 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002364 mResolver = resolver;
2365 mMethodMap = methodMap;
2366 mMethodList = methodList;
2367 }
2368
2369 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2370 return createEnabledInputMethodListLocked(
2371 getEnabledInputMethodsAndSubtypeListLocked());
2372 }
2373
satok7f35c8c2010-10-07 21:13:11 +09002374 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002375 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2376 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002377 getEnabledInputMethodsAndSubtypeListLocked());
2378 }
2379
satok67ddf9c2010-11-17 09:45:54 +09002380 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2381 InputMethodInfo imi) {
2382 List<Pair<String, ArrayList<String>>> imsList =
2383 getEnabledInputMethodsAndSubtypeListLocked();
2384 ArrayList<InputMethodSubtype> enabledSubtypes =
2385 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002386 if (imi != null) {
2387 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2388 InputMethodInfo info = mMethodMap.get(imsPair.first);
2389 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002390 final int subtypeCount = info.getSubtypeCount();
2391 for (int i = 0; i < subtypeCount; ++i) {
2392 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002393 for (String s: imsPair.second) {
2394 if (String.valueOf(ims.hashCode()).equals(s)) {
2395 enabledSubtypes.add(ims);
2396 }
satok67ddf9c2010-11-17 09:45:54 +09002397 }
2398 }
satok884ef9a2010-11-18 10:39:46 +09002399 break;
satok67ddf9c2010-11-17 09:45:54 +09002400 }
satok67ddf9c2010-11-17 09:45:54 +09002401 }
2402 }
2403 return enabledSubtypes;
2404 }
2405
satokd87c2592010-09-29 11:52:06 +09002406 // At the initial boot, the settings for input methods are not set,
2407 // so we need to enable IME in that case.
2408 public void enableAllIMEsIfThereIsNoEnabledIME() {
2409 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2410 StringBuilder sb = new StringBuilder();
2411 final int N = mMethodList.size();
2412 for (int i = 0; i < N; i++) {
2413 InputMethodInfo imi = mMethodList.get(i);
2414 Slog.i(TAG, "Adding: " + imi.getId());
2415 if (i > 0) sb.append(':');
2416 sb.append(imi.getId());
2417 }
2418 putEnabledInputMethodsStr(sb.toString());
2419 }
2420 }
2421
satokbb4aa062011-01-19 21:40:27 +09002422 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002423 ArrayList<Pair<String, ArrayList<String>>> imsList
2424 = new ArrayList<Pair<String, ArrayList<String>>>();
2425 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2426 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2427 return imsList;
2428 }
satok723a27e2010-11-11 14:58:11 +09002429 mInputMethodSplitter.setString(enabledInputMethodsStr);
2430 while (mInputMethodSplitter.hasNext()) {
2431 String nextImsStr = mInputMethodSplitter.next();
2432 mSubtypeSplitter.setString(nextImsStr);
2433 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002434 ArrayList<String> subtypeHashes = new ArrayList<String>();
2435 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002436 String imeId = mSubtypeSplitter.next();
2437 while (mSubtypeSplitter.hasNext()) {
2438 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002439 }
2440 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2441 }
2442 }
2443 return imsList;
2444 }
2445
2446 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2447 if (reloadInputMethodStr) {
2448 getEnabledInputMethodsStr();
2449 }
2450 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2451 // Add in the newly enabled input method.
2452 putEnabledInputMethodsStr(id);
2453 } else {
2454 putEnabledInputMethodsStr(
2455 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2456 }
2457 }
2458
2459 /**
2460 * Build and put a string of EnabledInputMethods with removing specified Id.
2461 * @return the specified id was removed or not.
2462 */
2463 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2464 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2465 boolean isRemoved = false;
2466 boolean needsAppendSeparator = false;
2467 for (Pair<String, ArrayList<String>> ims: imsList) {
2468 String curId = ims.first;
2469 if (curId.equals(id)) {
2470 // We are disabling this input method, and it is
2471 // currently enabled. Skip it to remove from the
2472 // new list.
2473 isRemoved = true;
2474 } else {
2475 if (needsAppendSeparator) {
2476 builder.append(INPUT_METHOD_SEPARATER);
2477 } else {
2478 needsAppendSeparator = true;
2479 }
2480 buildEnabledInputMethodsSettingString(builder, ims);
2481 }
2482 }
2483 if (isRemoved) {
2484 // Update the setting with the new list of input methods.
2485 putEnabledInputMethodsStr(builder.toString());
2486 }
2487 return isRemoved;
2488 }
2489
2490 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2491 List<Pair<String, ArrayList<String>>> imsList) {
2492 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2493 for (Pair<String, ArrayList<String>> ims: imsList) {
2494 InputMethodInfo info = mMethodMap.get(ims.first);
2495 if (info != null) {
2496 res.add(info);
2497 }
2498 }
2499 return res;
2500 }
2501
satok7f35c8c2010-10-07 21:13:11 +09002502 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002503 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002504 List<Pair<String, ArrayList<String>>> imsList) {
2505 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2506 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2507 for (Pair<String, ArrayList<String>> ims : imsList) {
2508 InputMethodInfo info = mMethodMap.get(ims.first);
2509 if (info != null) {
2510 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2511 }
2512 }
2513 return res;
2514 }
2515
satokd87c2592010-09-29 11:52:06 +09002516 private void putEnabledInputMethodsStr(String str) {
2517 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2518 mEnabledInputMethodsStrCache = str;
2519 }
2520
2521 private String getEnabledInputMethodsStr() {
2522 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2523 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002524 if (DEBUG) {
2525 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2526 }
satokd87c2592010-09-29 11:52:06 +09002527 return mEnabledInputMethodsStrCache;
2528 }
satok723a27e2010-11-11 14:58:11 +09002529
2530 private void saveSubtypeHistory(
2531 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2532 StringBuilder builder = new StringBuilder();
2533 boolean isImeAdded = false;
2534 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2535 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2536 newSubtypeId);
2537 isImeAdded = true;
2538 }
2539 for (Pair<String, String> ime: savedImes) {
2540 String imeId = ime.first;
2541 String subtypeId = ime.second;
2542 if (TextUtils.isEmpty(subtypeId)) {
2543 subtypeId = NOT_A_SUBTYPE_ID_STR;
2544 }
2545 if (isImeAdded) {
2546 builder.append(INPUT_METHOD_SEPARATER);
2547 } else {
2548 isImeAdded = true;
2549 }
2550 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2551 subtypeId);
2552 }
2553 // Remove the last INPUT_METHOD_SEPARATER
2554 putSubtypeHistoryStr(builder.toString());
2555 }
2556
2557 public void addSubtypeToHistory(String imeId, String subtypeId) {
2558 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2559 for (Pair<String, String> ime: subtypeHistory) {
2560 if (ime.first.equals(imeId)) {
2561 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002562 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002563 + ime.second);
2564 }
2565 // We should break here
2566 subtypeHistory.remove(ime);
2567 break;
2568 }
2569 }
satokbb4aa062011-01-19 21:40:27 +09002570 if (DEBUG) {
2571 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2572 }
satok723a27e2010-11-11 14:58:11 +09002573 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2574 }
2575
2576 private void putSubtypeHistoryStr(String str) {
2577 if (DEBUG) {
2578 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2579 }
2580 Settings.Secure.putString(
2581 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2582 }
2583
2584 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2585 // Gets the first one from the history
2586 return getLastSubtypeForInputMethodLockedInternal(null);
2587 }
2588
2589 public String getLastSubtypeForInputMethodLocked(String imeId) {
2590 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2591 if (ime != null) {
2592 return ime.second;
2593 } else {
2594 return null;
2595 }
2596 }
2597
2598 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2599 List<Pair<String, ArrayList<String>>> enabledImes =
2600 getEnabledInputMethodsAndSubtypeListLocked();
2601 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2602 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2603 final String imeInTheHistory = imeAndSubtype.first;
2604 // If imeId is empty, returns the first IME and subtype in the history
2605 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2606 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002607 final String subtypeHashCode =
2608 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2609 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002610 if (!TextUtils.isEmpty(subtypeHashCode)) {
2611 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002612 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002613 }
2614 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2615 }
2616 }
2617 }
2618 if (DEBUG) {
2619 Slog.d(TAG, "No enabled IME found in the history");
2620 }
2621 return null;
2622 }
2623
satokdf31ae62011-01-15 06:19:44 +09002624 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002625 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2626 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2627 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002628 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2629 if (explicitlyEnabledSubtypes.size() == 0) {
2630 // If there are no explicitly enabled subtypes, applicable subtypes are
2631 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002632 InputMethodInfo ime = mMethodMap.get(imeId);
2633 // If IME is enabled and no subtypes are enabled, applicable subtypes
2634 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002635 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002636 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002637 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002638 if (implicitlySelectedSubtypes != null) {
2639 final int N = implicitlySelectedSubtypes.size();
2640 for (int i = 0; i < N; ++i) {
2641 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2642 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2643 return subtypeHashCode;
2644 }
2645 }
2646 }
2647 }
2648 } else {
satokf6cafb62011-01-17 16:29:02 +09002649 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002650 if (s.equals(subtypeHashCode)) {
2651 // If both imeId and subtypeId are enabled, return subtypeId.
2652 return s;
2653 }
satok723a27e2010-11-11 14:58:11 +09002654 }
2655 }
2656 // If imeId was enabled but subtypeId was disabled.
2657 return NOT_A_SUBTYPE_ID_STR;
2658 }
2659 }
2660 // If both imeId and subtypeId are disabled, return null
2661 return null;
2662 }
2663
2664 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2665 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2666 final String subtypeHistoryStr = getSubtypeHistoryStr();
2667 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2668 return imsList;
2669 }
2670 mInputMethodSplitter.setString(subtypeHistoryStr);
2671 while (mInputMethodSplitter.hasNext()) {
2672 String nextImsStr = mInputMethodSplitter.next();
2673 mSubtypeSplitter.setString(nextImsStr);
2674 if (mSubtypeSplitter.hasNext()) {
2675 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2676 // The first element is ime id.
2677 String imeId = mSubtypeSplitter.next();
2678 while (mSubtypeSplitter.hasNext()) {
2679 subtypeId = mSubtypeSplitter.next();
2680 break;
2681 }
2682 imsList.add(new Pair<String, String>(imeId, subtypeId));
2683 }
2684 }
2685 return imsList;
2686 }
2687
2688 private String getSubtypeHistoryStr() {
2689 if (DEBUG) {
2690 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2691 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2692 }
2693 return Settings.Secure.getString(
2694 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2695 }
2696
2697 public void putSelectedInputMethod(String imeId) {
2698 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2699 }
2700
2701 public void putSelectedSubtype(int subtypeId) {
2702 Settings.Secure.putInt(
2703 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2704 }
satokd87c2592010-09-29 11:52:06 +09002705 }
2706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 @Override
2710 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2711 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2712 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2715 + Binder.getCallingPid()
2716 + ", uid=" + Binder.getCallingUid());
2717 return;
2718 }
2719
2720 IInputMethod method;
2721 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 synchronized (mMethodMap) {
2726 p.println("Current Input Method Manager state:");
2727 int N = mMethodList.size();
2728 p.println(" Input Methods:");
2729 for (int i=0; i<N; i++) {
2730 InputMethodInfo info = mMethodList.get(i);
2731 p.println(" InputMethod #" + i + ":");
2732 info.dump(p, " ");
2733 }
2734 p.println(" Clients:");
2735 for (ClientState ci : mClients.values()) {
2736 p.println(" Client " + ci + ":");
2737 p.println(" client=" + ci.client);
2738 p.println(" inputContext=" + ci.inputContext);
2739 p.println(" sessionRequested=" + ci.sessionRequested);
2740 p.println(" curSession=" + ci.curSession);
2741 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002742 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002744 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2745 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2747 + " mBoundToMethod=" + mBoundToMethod);
2748 p.println(" mCurToken=" + mCurToken);
2749 p.println(" mCurIntent=" + mCurIntent);
2750 method = mCurMethod;
2751 p.println(" mCurMethod=" + mCurMethod);
2752 p.println(" mEnabledSession=" + mEnabledSession);
2753 p.println(" mShowRequested=" + mShowRequested
2754 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2755 + " mShowForced=" + mShowForced
2756 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002757 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002759
Jeff Brownb88102f2010-09-08 11:49:43 -07002760 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 pw.flush();
2763 try {
2764 client.client.asBinder().dump(fd, args);
2765 } catch (RemoteException e) {
2766 p.println("Input method client dead: " + e);
2767 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002768 } else {
2769 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002771
Jeff Brownb88102f2010-09-08 11:49:43 -07002772 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 pw.flush();
2775 try {
2776 method.asBinder().dump(fd, args);
2777 } catch (RemoteException e) {
2778 p.println("Input method service dead: " + e);
2779 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002780 } else {
2781 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 }
2783 }
2784}