blob: bc196836e4b66cb574cbab75cdab7283c9a1705d [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(
satokb6109bb2011-02-03 22:24:54 +0900333 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
334 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900335 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 @Override public void onChange(boolean selfChange) {
339 synchronized (mMethodMap) {
340 updateFromSettingsLocked();
341 }
342 }
343 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
346 @Override
347 public void onReceive(Context context, Intent intent) {
348 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
349 mScreenOn = true;
350 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
351 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700352 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
353 hideInputMethodMenu();
354 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800356 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 }
358
359 // Inform the current client of the change in active status
360 try {
361 if (mCurClient != null && mCurClient.client != null) {
362 mCurClient.client.setActive(mScreenOn);
363 }
364 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800365 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 + mCurClient.pid + " uid " + mCurClient.uid);
367 }
368 }
369 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800371 class MyPackageMonitor extends PackageMonitor {
372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800374 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800376 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
378 final int N = mMethodList.size();
379 if (curInputMethodId != null) {
380 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800381 InputMethodInfo imi = mMethodList.get(i);
382 if (imi.getId().equals(curInputMethodId)) {
383 for (String pkg : packages) {
384 if (imi.getPackageName().equals(pkg)) {
385 if (!doit) {
386 return true;
387 }
satok723a27e2010-11-11 14:58:11 +0900388 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800389 chooseNewDefaultIMELocked();
390 return true;
391 }
392 }
393 }
394 }
395 }
396 }
397 return false;
398 }
399
400 @Override
401 public void onSomePackagesChanged() {
402 synchronized (mMethodMap) {
403 InputMethodInfo curIm = null;
404 String curInputMethodId = Settings.Secure.getString(mContext
405 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
406 final int N = mMethodList.size();
407 if (curInputMethodId != null) {
408 for (int i=0; i<N; i++) {
409 InputMethodInfo imi = mMethodList.get(i);
410 if (imi.getId().equals(curInputMethodId)) {
411 curIm = imi;
412 }
413 int change = isPackageDisappearing(imi.getPackageName());
414 if (change == PACKAGE_TEMPORARY_CHANGE
415 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800416 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800417 + imi.getComponent());
418 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 }
420 }
421 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800422
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800423 buildInputMethodListLocked(mMethodList, mMethodMap);
424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800426
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800427 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800428 int change = isPackageDisappearing(curIm.getPackageName());
429 if (change == PACKAGE_TEMPORARY_CHANGE
430 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800431 ServiceInfo si = null;
432 try {
433 si = mContext.getPackageManager().getServiceInfo(
434 curIm.getComponent(), 0);
435 } catch (PackageManager.NameNotFoundException ex) {
436 }
437 if (si == null) {
438 // Uh oh, current input method is no longer around!
439 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800440 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800441 mImeWindowVis = 0;
442 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
443 mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800444 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800445 changed = true;
446 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800447 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900448 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800449 }
450 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800451 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800452 }
satokab751aa2010-09-14 19:17:36 +0900453
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800454 if (curIm == null) {
455 // We currently don't have a default input method... is
456 // one now available?
457 changed = chooseNewDefaultIMELocked();
458 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800459
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800460 if (changed) {
461 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 }
463 }
464 }
465 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 class MethodCallback extends IInputMethodCallback.Stub {
468 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 MethodCallback(IInputMethod method) {
471 mMethod = method;
472 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 public void finishedEvent(int seq, boolean handled) throws RemoteException {
475 }
476
477 public void sessionCreated(IInputMethodSession session) throws RemoteException {
478 onSessionCreated(mMethod, session);
479 }
480 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800481
Joe Onorato089de882010-04-12 08:18:45 -0700482 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800484 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 mHandler = new Handler(this);
486 mIWindowManager = IWindowManager.Stub.asInterface(
487 ServiceManager.getService(Context.WINDOW_SERVICE));
488 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
489 public void executeMessage(Message msg) {
490 handleMessage(msg);
491 }
492 });
satok4d733292011-01-31 22:14:32 +0900493 // Initialize the system settings version to undefined.
494 mOldSystemSettingsVersion = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800495
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800496 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 IntentFilter screenOnOffFilt = new IntentFilter();
499 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
500 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700501 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800503
satok913a8922010-08-26 21:53:41 +0900504 mStatusBar = statusBar;
505 statusBar.setIconVisibility("ime", false);
506
satokd87c2592010-09-29 11:52:06 +0900507 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900508 mSettings = new InputMethodSettings(
509 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900511 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512
satokd87c2592010-09-29 11:52:06 +0900513 if (TextUtils.isEmpty(Settings.Secure.getString(
514 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900516 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
518 try {
satokd87c2592010-09-29 11:52:06 +0900519 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 imi.getPackageName(), 0).getResources();
521 if (res.getBoolean(imi.getIsDefaultResourceId())) {
522 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800523 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 }
525 } catch (PackageManager.NameNotFoundException ex) {
526 } catch (Resources.NotFoundException ex) {
527 }
528 }
529 }
satokd87c2592010-09-29 11:52:06 +0900530 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800532 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900535 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 }
537 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 mSettingsObserver = new SettingsObserver(mHandler);
540 updateFromSettingsLocked();
541 }
542
543 @Override
544 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
545 throws RemoteException {
546 try {
547 return super.onTransact(code, data, reply, flags);
548 } catch (RuntimeException e) {
549 // The input method manager only throws security exceptions, so let's
550 // log all others.
551 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800552 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
554 throw e;
555 }
556 }
557
558 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700559 synchronized (mMethodMap) {
560 if (!mSystemReady) {
561 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700562 try {
563 startInputInnerLocked();
564 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800565 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700566 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700567 }
568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 public List<InputMethodInfo> getInputMethodList() {
572 synchronized (mMethodMap) {
573 return new ArrayList<InputMethodInfo>(mMethodList);
574 }
575 }
576
577 public List<InputMethodInfo> getEnabledInputMethodList() {
578 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900579 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 }
581 }
582
satokbb4aa062011-01-19 21:40:27 +0900583 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
584 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
585 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
586 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
587 for (InputMethodInfo imi: getEnabledInputMethodList()) {
588 enabledInputMethodAndSubtypes.put(
589 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
590 }
591 return enabledInputMethodAndSubtypes;
592 }
593
594 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
595 boolean allowsImplicitlySelectedSubtypes) {
596 if (imi == null && mCurMethodId != null) {
597 imi = mMethodMap.get(mCurMethodId);
598 }
599 final List<InputMethodSubtype> enabledSubtypes =
600 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
601 if (!allowsImplicitlySelectedSubtypes || enabledSubtypes.size() > 0) {
602 return enabledSubtypes;
603 } else {
604 return getApplicableSubtypesLocked(mRes, getSubtypes(imi));
605 }
606 }
607
satok16331c82010-12-20 23:48:46 +0900608 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
609 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900610 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900611 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900612 }
613 }
614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 public void addClient(IInputMethodClient client,
616 IInputContext inputContext, int uid, int pid) {
617 synchronized (mMethodMap) {
618 mClients.put(client.asBinder(), new ClientState(client,
619 inputContext, uid, pid));
620 }
621 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 public void removeClient(IInputMethodClient client) {
624 synchronized (mMethodMap) {
625 mClients.remove(client.asBinder());
626 }
627 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 void executeOrSendMessage(IInterface target, Message msg) {
630 if (target.asBinder() instanceof Binder) {
631 mCaller.sendMessage(msg);
632 } else {
633 handleMessage(msg);
634 msg.recycle();
635 }
636 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800637
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700638 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800640 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 + mCurClient.client.asBinder());
642 if (mBoundToMethod) {
643 mBoundToMethod = false;
644 if (mCurMethod != null) {
645 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
646 MSG_UNBIND_INPUT, mCurMethod));
647 }
648 }
649 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
650 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
651 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 // Call setActive(false) on the old client
654 try {
655 mCurClient.client.setActive(false);
656 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800657 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 + mCurClient.pid + " uid " + mCurClient.uid);
659 }
660 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800661
The Android Open Source Project10592532009-03-18 17:39:46 -0700662 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 }
664 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 private int getImeShowFlags() {
667 int flags = 0;
668 if (mShowForced) {
669 flags |= InputMethod.SHOW_FORCED
670 | InputMethod.SHOW_EXPLICIT;
671 } else if (mShowExplicitlyRequested) {
672 flags |= InputMethod.SHOW_EXPLICIT;
673 }
674 return flags;
675 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 private int getAppShowFlags() {
678 int flags = 0;
679 if (mShowForced) {
680 flags |= InputMethodManager.SHOW_FORCED;
681 } else if (!mShowExplicitlyRequested) {
682 flags |= InputMethodManager.SHOW_IMPLICIT;
683 }
684 return flags;
685 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
688 if (!mBoundToMethod) {
689 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
690 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
691 mBoundToMethod = true;
692 }
693 final SessionState session = mCurClient.curSession;
694 if (initial) {
695 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
696 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
697 } else {
698 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
699 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
700 }
701 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800702 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800703 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 }
705 return needResult
706 ? new InputBindResult(session.session, mCurId, mCurSeq)
707 : null;
708 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 InputBindResult startInputLocked(IInputMethodClient client,
711 IInputContext inputContext, EditorInfo attribute,
712 boolean initial, boolean needResult) {
713 // If no method is currently selected, do nothing.
714 if (mCurMethodId == null) {
715 return mNoBinding;
716 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 ClientState cs = mClients.get(client.asBinder());
719 if (cs == null) {
720 throw new IllegalArgumentException("unknown client "
721 + client.asBinder());
722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 try {
725 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
726 // Check with the window manager to make sure this client actually
727 // has a window with focus. If not, reject. This is thread safe
728 // because if the focus changes some time before or after, the
729 // next client receiving focus that has any interest in input will
730 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800731 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
733 return null;
734 }
735 } catch (RemoteException e) {
736 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 if (mCurClient != cs) {
739 // If the client is changing, we need to switch over to the new
740 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700741 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800742 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 + cs.client.asBinder());
744
745 // If the screen is on, inform the new client it is active
746 if (mScreenOn) {
747 try {
748 cs.client.setActive(mScreenOn);
749 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800750 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 + cs.pid + " uid " + cs.uid);
752 }
753 }
754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 // Bump up the sequence for this client and attach it.
757 mCurSeq++;
758 if (mCurSeq <= 0) mCurSeq = 1;
759 mCurClient = cs;
760 mCurInputContext = inputContext;
761 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 // Check if the input method is changing.
764 if (mCurId != null && mCurId.equals(mCurMethodId)) {
765 if (cs.curSession != null) {
766 // Fast case: if we are already connected to the input method,
767 // then just return it.
768 return attachNewInputLocked(initial, needResult);
769 }
770 if (mHaveConnection) {
771 if (mCurMethod != null) {
772 if (!cs.sessionRequested) {
773 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800774 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
776 MSG_CREATE_SESSION, mCurMethod,
777 new MethodCallback(mCurMethod)));
778 }
779 // Return to client, and we will get back with it when
780 // we have had a session made for it.
781 return new InputBindResult(null, mCurId, mCurSeq);
782 } else if (SystemClock.uptimeMillis()
783 < (mLastBindTime+TIME_TO_RECONNECT)) {
784 // In this case we have connected to the service, but
785 // don't yet have its interface. If it hasn't been too
786 // long since we did the connection, we'll return to
787 // the client and wait to get the service interface so
788 // we can report back. If it has been too long, we want
789 // to fall through so we can try a disconnect/reconnect
790 // to see if we can get back in touch with the service.
791 return new InputBindResult(null, mCurId, mCurSeq);
792 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800793 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
794 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 }
796 }
797 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800798
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700799 return startInputInnerLocked();
800 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800801
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700802 InputBindResult startInputInnerLocked() {
803 if (mCurMethodId == null) {
804 return mNoBinding;
805 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800806
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700807 if (!mSystemReady) {
808 // If the system is not yet ready, we shouldn't be running third
809 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700810 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700811 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 InputMethodInfo info = mMethodMap.get(mCurMethodId);
814 if (info == null) {
815 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
816 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800817
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700818 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
821 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700822 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
823 com.android.internal.R.string.input_method_binding_label);
824 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
825 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
827 mLastBindTime = SystemClock.uptimeMillis();
828 mHaveConnection = true;
829 mCurId = info.getId();
830 mCurToken = new Binder();
831 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800832 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 mIWindowManager.addWindowToken(mCurToken,
834 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
835 } catch (RemoteException e) {
836 }
837 return new InputBindResult(null, mCurId, mCurSeq);
838 } else {
839 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800840 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 + mCurIntent);
842 }
843 return null;
844 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 public InputBindResult startInput(IInputMethodClient client,
847 IInputContext inputContext, EditorInfo attribute,
848 boolean initial, boolean needResult) {
849 synchronized (mMethodMap) {
850 final long ident = Binder.clearCallingIdentity();
851 try {
852 return startInputLocked(client, inputContext, attribute,
853 initial, needResult);
854 } finally {
855 Binder.restoreCallingIdentity(ident);
856 }
857 }
858 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 public void finishInput(IInputMethodClient client) {
861 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 public void onServiceConnected(ComponentName name, IBinder service) {
864 synchronized (mMethodMap) {
865 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
866 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700867 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800868 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700869 unbindCurrentMethodLocked(false);
870 return;
871 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800872 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700873 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
874 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800876 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700877 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700879 MSG_CREATE_SESSION, mCurMethod,
880 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 }
882 }
883 }
884 }
885
886 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
887 synchronized (mMethodMap) {
888 if (mCurMethod != null && method != null
889 && mCurMethod.asBinder() == method.asBinder()) {
890 if (mCurClient != null) {
891 mCurClient.curSession = new SessionState(mCurClient,
892 method, session);
893 mCurClient.sessionRequested = false;
894 InputBindResult res = attachNewInputLocked(true, true);
895 if (res.method != null) {
896 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
897 MSG_BIND_METHOD, mCurClient.client, res));
898 }
899 }
900 }
901 }
902 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800903
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700904 void unbindCurrentMethodLocked(boolean reportToClient) {
905 if (mHaveConnection) {
906 mContext.unbindService(this);
907 mHaveConnection = false;
908 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800909
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700910 if (mCurToken != null) {
911 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800912 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700913 mIWindowManager.removeWindowToken(mCurToken);
914 } catch (RemoteException e) {
915 }
916 mCurToken = null;
917 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800918
The Android Open Source Project10592532009-03-18 17:39:46 -0700919 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700920 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800921
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700922 if (reportToClient && mCurClient != null) {
923 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
924 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
925 }
926 }
927
Devin Taylor0c33ed22010-02-23 13:26:46 -0600928 private void finishSession(SessionState sessionState) {
929 if (sessionState != null && sessionState.session != null) {
930 try {
931 sessionState.session.finishSession();
932 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700933 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600934 }
935 }
936 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800937
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700938 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 if (mCurMethod != null) {
940 for (ClientState cs : mClients.values()) {
941 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600942 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 cs.curSession = null;
944 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600945
946 finishSession(mEnabledSession);
947 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 mCurMethod = null;
949 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700950 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 public void onServiceDisconnected(ComponentName name) {
954 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800955 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 + " mCurIntent=" + mCurIntent);
957 if (mCurMethod != null && mCurIntent != null
958 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700959 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 // We consider this to be a new bind attempt, since the system
961 // should now try to restart the service for us.
962 mLastBindTime = SystemClock.uptimeMillis();
963 mShowRequested = mInputShown;
964 mInputShown = false;
965 if (mCurClient != null) {
966 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
967 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
968 }
969 }
970 }
971 }
972
973 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700974 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 long ident = Binder.clearCallingIdentity();
976 try {
977 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700978 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 return;
980 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 synchronized (mMethodMap) {
983 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800984 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700985 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800987 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700988 mStatusBar.setIcon("ime", packageName, iconId, 0);
989 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 }
991 }
992 } finally {
993 Binder.restoreCallingIdentity(ident);
994 }
995 }
996
Joe Onorato857fd9b2011-01-27 15:08:35 -0800997 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +0900998 int uid = Binder.getCallingUid();
999 long ident = Binder.clearCallingIdentity();
1000 try {
1001 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001002 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001003 return;
1004 }
1005
1006 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001007 mImeWindowVis = vis;
1008 mBackDisposition = backDisposition;
1009 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
satok06487a52010-10-29 11:37:18 +09001010 }
1011 } finally {
1012 Binder.restoreCallingIdentity(ident);
1013 }
1014 }
1015
satok4d733292011-01-31 22:14:32 +09001016 // TODO: Investigate and fix why are settings changes getting processed before the settings seq
1017 // number is updated?
1018 // TODO: Change this stuff to not rely on modifying settings for normal user interactions.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 void updateFromSettingsLocked() {
satok4d733292011-01-31 22:14:32 +09001020 long newSystemSettingsVersion = getSystemSettingsVersion();
1021 // This is a workaround to avoid a situation that old cached value in Settings.Secure
1022 // will be handled.
1023 if (newSystemSettingsVersion == mOldSystemSettingsVersion) return;
1024
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001025 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1026 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1027 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1028 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001030 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001031 // There is no input method selected, try to choose new applicable input method.
1032 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1033 id = Settings.Secure.getString(mContext.getContentResolver(),
1034 Settings.Secure.DEFAULT_INPUT_METHOD);
1035 }
1036 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 try {
satokab751aa2010-09-14 19:17:36 +09001038 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001040 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001041 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001042 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 }
satokf3db1af2010-11-23 13:34:33 +09001044 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001045 } else {
1046 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001047 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001048 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 }
1050 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001051
satokab751aa2010-09-14 19:17:36 +09001052 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 InputMethodInfo info = mMethodMap.get(id);
1054 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001055 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001059 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001060 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1061 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001062 }
1063 if (subtype != mCurrentSubtype) {
1064 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001065 if (subtype != null) {
1066 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1067 }
satokcd7cd292010-11-20 15:46:23 +09001068 if (mCurMethod != null) {
1069 try {
satoke40dea02011-01-30 01:14:02 +09001070 final Configuration conf = mRes.getConfiguration();
1071 final boolean haveHardKeyboard = conf.keyboard
1072 != Configuration.KEYBOARD_NOKEYS;
1073 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001074 && conf.hardKeyboardHidden
1075 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001076 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1077 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1078 : 0;
Joe Onorato857fd9b2011-01-27 15:08:35 -08001079 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1080 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001081 // If subtype is null, try to find the most applicable one from
1082 // getCurrentInputMethodSubtype.
1083 if (subtype == null) {
1084 subtype = getCurrentInputMethodSubtype();
1085 }
1086 mCurMethod.changeInputMethodSubtype(subtype);
1087 } catch (RemoteException e) {
1088 return;
satokab751aa2010-09-14 19:17:36 +09001089 }
1090 }
1091 }
1092 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 return;
1094 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 final long ident = Binder.clearCallingIdentity();
1097 try {
satokab751aa2010-09-14 19:17:36 +09001098 // Set a subtype to this input method.
1099 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001100 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1101 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1102 // because mCurMethodId is stored as a history in
1103 // setSelectedInputMethodAndSubtypeLocked().
1104 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105
1106 if (ActivityManagerNative.isSystemReady()) {
1107 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001108 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 intent.putExtra("input_method_id", id);
1110 mContext.sendBroadcast(intent);
1111 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001112 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 } finally {
1114 Binder.restoreCallingIdentity(ident);
1115 }
1116 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001117
The Android Open Source Project4df24232009-03-05 14:34:35 -08001118 public boolean showSoftInput(IInputMethodClient client, int flags,
1119 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001120 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 long ident = Binder.clearCallingIdentity();
1122 try {
1123 synchronized (mMethodMap) {
1124 if (mCurClient == null || client == null
1125 || mCurClient.client.asBinder() != client.asBinder()) {
1126 try {
1127 // We need to check if this is the current client with
1128 // focus in the window manager, to allow this call to
1129 // be made before input is started in it.
1130 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001131 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001132 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 }
1134 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001135 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 }
1137 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001138
Joe Onorato8a9b2202010-02-26 18:56:32 -08001139 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001140 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 }
1142 } finally {
1143 Binder.restoreCallingIdentity(ident);
1144 }
1145 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001146
The Android Open Source Project4df24232009-03-05 14:34:35 -08001147 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 mShowRequested = true;
1149 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1150 mShowExplicitlyRequested = true;
1151 }
1152 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1153 mShowExplicitlyRequested = true;
1154 mShowForced = true;
1155 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001156
Dianne Hackborncc278702009-09-02 23:07:23 -07001157 if (!mSystemReady) {
1158 return false;
1159 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001160
The Android Open Source Project4df24232009-03-05 14:34:35 -08001161 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001163 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1164 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1165 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001167 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 } else if (mHaveConnection && SystemClock.uptimeMillis()
1169 < (mLastBindTime+TIME_TO_RECONNECT)) {
1170 // The client has asked to have the input method shown, but
1171 // we have been sitting here too long with a connection to the
1172 // service and no interface received, so let's disconnect/connect
1173 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001174 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 SystemClock.uptimeMillis()-mLastBindTime,1);
1176 mContext.unbindService(this);
1177 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1178 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001179
The Android Open Source Project4df24232009-03-05 14:34:35 -08001180 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001182
The Android Open Source Project4df24232009-03-05 14:34:35 -08001183 public boolean hideSoftInput(IInputMethodClient client, int flags,
1184 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001185 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 long ident = Binder.clearCallingIdentity();
1187 try {
1188 synchronized (mMethodMap) {
1189 if (mCurClient == null || client == null
1190 || mCurClient.client.asBinder() != client.asBinder()) {
1191 try {
1192 // We need to check if this is the current client with
1193 // focus in the window manager, to allow this call to
1194 // be made before input is started in it.
1195 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001196 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1197 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001198 mImeWindowVis = 0;
1199 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1200 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001201 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 }
1203 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001204 mImeWindowVis = 0;
1205 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001206 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 }
1208 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001209
Joe Onorato8a9b2202010-02-26 18:56:32 -08001210 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001211 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 }
1213 } finally {
1214 Binder.restoreCallingIdentity(ident);
1215 }
1216 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001217
The Android Open Source Project4df24232009-03-05 14:34:35 -08001218 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1220 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001221 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001223 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 }
1225 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001226 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001228 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001230 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001232 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1233 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1234 res = true;
1235 } else {
1236 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 }
1238 mInputShown = false;
1239 mShowRequested = false;
1240 mShowExplicitlyRequested = false;
1241 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001242 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001244
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001245 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1247 boolean first, int windowFlags) {
1248 long ident = Binder.clearCallingIdentity();
1249 try {
1250 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001251 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 + " viewHasFocus=" + viewHasFocus
1253 + " isTextEditor=" + isTextEditor
1254 + " softInputMode=#" + Integer.toHexString(softInputMode)
1255 + " first=" + first + " flags=#"
1256 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 if (mCurClient == null || client == null
1259 || mCurClient.client.asBinder() != client.asBinder()) {
1260 try {
1261 // We need to check if this is the current client with
1262 // focus in the window manager, to allow this call to
1263 // be made before input is started in it.
1264 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001265 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 return;
1267 }
1268 } catch (RemoteException e) {
1269 }
1270 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001271
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001272 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001273 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001274 return;
1275 }
1276 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001277
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001278 // Should we auto-show the IME even if the caller has not
1279 // specified what should be done with it?
1280 // We only do this automatically if the window can resize
1281 // to accommodate the IME (so what the user sees will give
1282 // them good context without input information being obscured
1283 // by the IME) or if running on a large screen where there
1284 // is more room for the target window + IME.
1285 final boolean doAutoShow =
1286 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1287 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1288 || mRes.getConfiguration().isLayoutSizeAtLeast(
1289 Configuration.SCREENLAYOUT_SIZE_LARGE);
1290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1292 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001293 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1295 // There is no focus view, and this window will
1296 // be behind any soft input window, so hide the
1297 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001298 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001299 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001301 } else if (isTextEditor && doAutoShow && (softInputMode &
1302 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 // There is a focus view, and we are navigating forward
1304 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001305 // We only do this automatically if the window an resize
1306 // to accomodate the IME (so what the user sees will give
1307 // them good context without input information being obscured
1308 // by the IME) or if running on a large screen where there
1309 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001310 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001311 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 }
1313 break;
1314 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1315 // Do nothing.
1316 break;
1317 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1318 if ((softInputMode &
1319 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001320 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001321 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 }
1323 break;
1324 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001325 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001326 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 break;
1328 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1329 if ((softInputMode &
1330 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001331 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001332 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 }
1334 break;
1335 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001336 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001337 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 break;
1339 }
1340 }
1341 } finally {
1342 Binder.restoreCallingIdentity(ident);
1343 }
1344 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1347 synchronized (mMethodMap) {
1348 if (mCurClient == null || client == null
1349 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001350 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001351 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 }
1353
satok440aab52010-11-25 09:43:11 +09001354 // Always call subtype picker, because subtype picker is a superset of input method
1355 // picker.
satokab751aa2010-09-14 19:17:36 +09001356 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1357 }
1358 }
1359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001361 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1362 }
1363
1364 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1365 synchronized (mMethodMap) {
1366 if (subtype != null) {
1367 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1368 mMethodMap.get(id), subtype.hashCode()));
1369 } else {
1370 setInputMethod(token, id);
1371 }
1372 }
satokab751aa2010-09-14 19:17:36 +09001373 }
1374
satokb416a712010-11-25 20:42:14 +09001375 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001376 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001377 synchronized (mMethodMap) {
1378 if (mCurClient == null || client == null
1379 || mCurClient.client.asBinder() != client.asBinder()) {
1380 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1381 }
satok7fee71f2010-12-17 18:54:26 +09001382 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1383 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001384 }
1385 }
1386
satok735cf382010-11-11 20:40:09 +09001387 public boolean switchToLastInputMethod(IBinder token) {
1388 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001389 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1390 if (lastIme == null) return false;
1391 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1392 if (lastImi == null) return false;
1393
1394 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1395 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1396 // If the last IME is the same as the current IME and the last subtype is not defined,
1397 // there is no need to switch to the last IME.
1398 if (imiIdIsSame && lastSubtypeHash == NOT_A_SUBTYPE_ID) return false;
1399
1400 int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1401 : mCurrentSubtype.hashCode();
1402 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1403 if (DEBUG) {
1404 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second + ", from: "
1405 + mCurMethodId + ", " + currentSubtypeHash);
satok735cf382010-11-11 20:40:09 +09001406 }
satokc445bcd2011-01-25 18:57:24 +09001407 setInputMethodWithSubtypeId(token, lastIme.first, getSubtypeIdFromHashCode(
1408 lastImi, lastSubtypeHash));
1409 return true;
satok735cf382010-11-11 20:40:09 +09001410 }
1411 return false;
1412 }
1413 }
1414
satok28203512010-11-24 11:06:49 +09001415 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 synchronized (mMethodMap) {
1417 if (token == null) {
1418 if (mContext.checkCallingOrSelfPermission(
1419 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1420 != PackageManager.PERMISSION_GRANTED) {
1421 throw new SecurityException(
1422 "Using null token requires permission "
1423 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1424 }
1425 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001426 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1427 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 return;
1429 }
1430
1431 long ident = Binder.clearCallingIdentity();
1432 try {
satokab751aa2010-09-14 19:17:36 +09001433 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 } finally {
1435 Binder.restoreCallingIdentity(ident);
1436 }
1437 }
1438 }
1439
1440 public void hideMySoftInput(IBinder token, int flags) {
1441 synchronized (mMethodMap) {
1442 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001443 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1444 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 return;
1446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 long ident = Binder.clearCallingIdentity();
1448 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001449 hideCurrentInputLocked(flags, null);
1450 } finally {
1451 Binder.restoreCallingIdentity(ident);
1452 }
1453 }
1454 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001455
The Android Open Source Project4df24232009-03-05 14:34:35 -08001456 public void showMySoftInput(IBinder token, int flags) {
1457 synchronized (mMethodMap) {
1458 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001459 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1460 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001461 return;
1462 }
1463 long ident = Binder.clearCallingIdentity();
1464 try {
1465 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 } finally {
1467 Binder.restoreCallingIdentity(ident);
1468 }
1469 }
1470 }
1471
1472 void setEnabledSessionInMainThread(SessionState session) {
1473 if (mEnabledSession != session) {
1474 if (mEnabledSession != null) {
1475 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001476 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 mEnabledSession.method.setSessionEnabled(
1478 mEnabledSession.session, false);
1479 } catch (RemoteException e) {
1480 }
1481 }
1482 mEnabledSession = session;
1483 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001484 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 session.method.setSessionEnabled(
1486 session.session, true);
1487 } catch (RemoteException e) {
1488 }
1489 }
1490 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 public boolean handleMessage(Message msg) {
1493 HandlerCaller.SomeArgs args;
1494 switch (msg.what) {
1495 case MSG_SHOW_IM_PICKER:
1496 showInputMethodMenu();
1497 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001498
satokab751aa2010-09-14 19:17:36 +09001499 case MSG_SHOW_IM_SUBTYPE_PICKER:
1500 showInputMethodSubtypeMenu();
1501 return true;
1502
satok47a44912010-10-06 16:03:58 +09001503 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001504 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001505 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001506 return true;
1507
1508 case MSG_SHOW_IM_CONFIG:
1509 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001510 return true;
1511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 case MSG_UNBIND_INPUT:
1515 try {
1516 ((IInputMethod)msg.obj).unbindInput();
1517 } catch (RemoteException e) {
1518 // There is nothing interesting about the method dying.
1519 }
1520 return true;
1521 case MSG_BIND_INPUT:
1522 args = (HandlerCaller.SomeArgs)msg.obj;
1523 try {
1524 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1525 } catch (RemoteException e) {
1526 }
1527 return true;
1528 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001529 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001531 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1532 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 } catch (RemoteException e) {
1534 }
1535 return true;
1536 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001537 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001539 ((IInputMethod)args.arg1).hideSoftInput(0,
1540 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 } catch (RemoteException e) {
1542 }
1543 return true;
1544 case MSG_ATTACH_TOKEN:
1545 args = (HandlerCaller.SomeArgs)msg.obj;
1546 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001547 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1549 } catch (RemoteException e) {
1550 }
1551 return true;
1552 case MSG_CREATE_SESSION:
1553 args = (HandlerCaller.SomeArgs)msg.obj;
1554 try {
1555 ((IInputMethod)args.arg1).createSession(
1556 (IInputMethodCallback)args.arg2);
1557 } catch (RemoteException e) {
1558 }
1559 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 case MSG_START_INPUT:
1563 args = (HandlerCaller.SomeArgs)msg.obj;
1564 try {
1565 SessionState session = (SessionState)args.arg1;
1566 setEnabledSessionInMainThread(session);
1567 session.method.startInput((IInputContext)args.arg2,
1568 (EditorInfo)args.arg3);
1569 } catch (RemoteException e) {
1570 }
1571 return true;
1572 case MSG_RESTART_INPUT:
1573 args = (HandlerCaller.SomeArgs)msg.obj;
1574 try {
1575 SessionState session = (SessionState)args.arg1;
1576 setEnabledSessionInMainThread(session);
1577 session.method.restartInput((IInputContext)args.arg2,
1578 (EditorInfo)args.arg3);
1579 } catch (RemoteException e) {
1580 }
1581 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 case MSG_UNBIND_METHOD:
1586 try {
1587 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1588 } catch (RemoteException e) {
1589 // There is nothing interesting about the last client dying.
1590 }
1591 return true;
1592 case MSG_BIND_METHOD:
1593 args = (HandlerCaller.SomeArgs)msg.obj;
1594 try {
1595 ((IInputMethodClient)args.arg1).onBindMethod(
1596 (InputBindResult)args.arg2);
1597 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001598 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 }
1600 return true;
1601 }
1602 return false;
1603 }
1604
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001605 private boolean isSystemIme(InputMethodInfo inputMethod) {
1606 return (inputMethod.getServiceInfo().applicationInfo.flags
1607 & ApplicationInfo.FLAG_SYSTEM) != 0;
1608 }
1609
Ken Wakasa586f0512011-01-20 22:31:01 +09001610 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1611 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1612 final int subtypeCount = imi.getSubtypeCount();
1613 for (int i = 0; i < subtypeCount; ++i) {
1614 subtypes.add(imi.getSubtypeAt(i));
1615 }
1616 return subtypes;
1617 }
1618
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001619 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001620 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001621 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001622 // We'd prefer to fall back on a system IME, since that is safer.
1623 int i=enabled.size();
1624 while (i > 0) {
1625 i--;
1626 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1627 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1628 break;
1629 }
1630 }
satokab751aa2010-09-14 19:17:36 +09001631 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001632 if (DEBUG) {
1633 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1634 }
satok723a27e2010-11-11 14:58:11 +09001635 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001636 return true;
1637 }
1638
1639 return false;
1640 }
1641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1643 HashMap<String, InputMethodInfo> map) {
1644 list.clear();
1645 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001648 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001649 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1650 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1651 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1652 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653
1654 List<ResolveInfo> services = pm.queryIntentServices(
1655 new Intent(InputMethod.SERVICE_INTERFACE),
1656 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 for (int i = 0; i < services.size(); ++i) {
1659 ResolveInfo ri = services.get(i);
1660 ServiceInfo si = ri.serviceInfo;
1661 ComponentName compName = new ComponentName(si.packageName, si.name);
1662 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1663 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001664 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 + ": it does not require the permission "
1666 + android.Manifest.permission.BIND_INPUT_METHOD);
1667 continue;
1668 }
1669
Joe Onorato8a9b2202010-02-26 18:56:32 -08001670 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671
1672 try {
1673 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1674 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001675 final String id = p.getId();
1676 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677
Amith Yamasanie861ec12010-03-24 21:39:27 -07001678 // System IMEs are enabled by default, unless there's a hard keyboard
1679 // and the system IME was explicitly disabled
1680 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1681 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001682 }
1683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001685 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 } catch (XmlPullParserException 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 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001691 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 }
1693 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001694
1695 String defaultIme = Settings.Secure.getString(mContext
1696 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001697 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001698 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001699 updateFromSettingsLocked();
1700 }
1701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001705
satokab751aa2010-09-14 19:17:36 +09001706 private void showInputMethodMenu() {
1707 showInputMethodMenuInternal(false);
1708 }
1709
1710 private void showInputMethodSubtypeMenu() {
1711 showInputMethodMenuInternal(true);
1712 }
1713
satok217f5482010-12-15 05:19:19 +09001714 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001715 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001716 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001717 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1718 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001719 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001720 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001721 }
satok217f5482010-12-15 05:19:19 +09001722 mContext.startActivity(intent);
1723 }
1724
1725 private void showConfigureInputMethods() {
1726 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1727 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1728 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1729 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001730 mContext.startActivity(intent);
1731 }
1732
satokab751aa2010-09-14 19:17:36 +09001733 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001734 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 String lastInputMethodId = Settings.Secure.getString(context
1741 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001742 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001743 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001744
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001745 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09001746 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
1747 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001748 if (immis == null || immis.size() == 0) {
1749 return;
1750 }
1751
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001752 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753
satokab751aa2010-09-14 19:17:36 +09001754 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1755 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001756
satokbb4aa062011-01-19 21:40:27 +09001757 for (InputMethodInfo imi: immis.keySet()) {
1758 if (imi == null) continue;
1759 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09001760 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09001761 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
1762 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09001763 }
satokbb4aa062011-01-19 21:40:27 +09001764 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
1765 CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001766 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09001767 final int subtypeCount = imi.getSubtypeCount();
Ken Wakasa586f0512011-01-20 22:31:01 +09001768 for (int j = 0; j < subtypeCount; ++j) {
satokbb4aa062011-01-19 21:40:27 +09001769 InputMethodSubtype subtype = imi.getSubtypeAt(j);
satok7f35c8c2010-10-07 21:13:11 +09001770 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1771 CharSequence title;
1772 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001773 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001774 if (nameResId != 0) {
satokbb4aa062011-01-19 21:40:27 +09001775 title = pm.getText(imi.getPackageName(), nameResId,
1776 imi.getServiceInfo().applicationInfo);
satok7f35c8c2010-10-07 21:13:11 +09001777 } else {
1778 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001779 // TODO: Use more friendly Title and UI
1780 title = label + "," + (mode == null ? "" : mode) + ","
1781 + (language == null ? "" : language);
1782 }
satokbb4aa062011-01-19 21:40:27 +09001783 imMap.put(title, new Pair<InputMethodInfo, Integer>(imi, j));
satokab751aa2010-09-14 19:17:36 +09001784 }
satokab751aa2010-09-14 19:17:36 +09001785 }
1786 } else {
1787 imMap.put(label,
satokbb4aa062011-01-19 21:40:27 +09001788 new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID));
satokab751aa2010-09-14 19:17:36 +09001789 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001790 }
satok913a8922010-08-26 21:53:41 +09001791
satokbb4aa062011-01-19 21:40:27 +09001792 final int N = imMap.size();
satok913a8922010-08-26 21:53:41 +09001793 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001794 mIms = new InputMethodInfo[N];
1795 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001796 int checkedItem = 0;
1797 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001798 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1799 mIms[i] = value.first;
1800 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001801 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001802 int subtypeId = mSubtypeIds[i];
1803 if ((subtypeId == NOT_A_SUBTYPE_ID)
1804 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1805 || (subtypeId == lastInputMethodSubtypeId)) {
1806 checkedItem = i;
1807 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 }
satokab751aa2010-09-14 19:17:36 +09001810
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001811 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1812 public void onClick(DialogInterface dialog, int which) {
1813 hideInputMethodMenu();
1814 }
1815 };
satokd87c2592010-09-29 11:52:06 +09001816
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001817 TypedArray a = context.obtainStyledAttributes(null,
1818 com.android.internal.R.styleable.DialogPreference,
1819 com.android.internal.R.attr.alertDialogStyle, 0);
1820 mDialogBuilder = new AlertDialog.Builder(context)
1821 .setTitle(com.android.internal.R.string.select_input_method)
1822 .setOnCancelListener(new OnCancelListener() {
1823 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001825 }
1826 })
1827 .setIcon(a.getDrawable(
1828 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1829 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001830
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001831 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1832 new AlertDialog.OnClickListener() {
1833 public void onClick(DialogInterface dialog, int which) {
1834 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001835 if (mIms == null || mIms.length <= which
1836 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001837 return;
1838 }
1839 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001840 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001841 hideInputMethodMenu();
1842 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001843 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09001844 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09001845 subtypeId = NOT_A_SUBTYPE_ID;
1846 }
1847 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001848 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001851 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852
satok7f35c8c2010-10-07 21:13:11 +09001853 if (showSubtypes) {
satok82beadf2010-12-27 19:03:06 +09001854 mDialogBuilder.setPositiveButton(
1855 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09001856 new DialogInterface.OnClickListener() {
1857 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09001858 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09001859 }
1860 });
1861 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 mSwitchingDialog = mDialogBuilder.create();
1863 mSwitchingDialog.getWindow().setType(
1864 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1865 mSwitchingDialog.show();
1866 }
1867 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001870 synchronized (mMethodMap) {
1871 hideInputMethodMenuLocked();
1872 }
1873 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001874
The Android Open Source Project10592532009-03-18 17:39:46 -07001875 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001876 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877
The Android Open Source Project10592532009-03-18 17:39:46 -07001878 if (mSwitchingDialog != null) {
1879 mSwitchingDialog.dismiss();
1880 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001882
The Android Open Source Project10592532009-03-18 17:39:46 -07001883 mDialogBuilder = null;
1884 mItems = null;
1885 mIms = null;
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 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 public boolean setInputMethodEnabled(String id, boolean enabled) {
1891 synchronized (mMethodMap) {
1892 if (mContext.checkCallingOrSelfPermission(
1893 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1894 != PackageManager.PERMISSION_GRANTED) {
1895 throw new SecurityException(
1896 "Requires permission "
1897 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1898 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 long ident = Binder.clearCallingIdentity();
1901 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001902 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 } finally {
1904 Binder.restoreCallingIdentity(ident);
1905 }
1906 }
1907 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001908
1909 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1910 // Make sure this is a valid input method.
1911 InputMethodInfo imm = mMethodMap.get(id);
1912 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001913 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001914 }
1915
satokd87c2592010-09-29 11:52:06 +09001916 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1917 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001918
satokd87c2592010-09-29 11:52:06 +09001919 if (enabled) {
1920 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1921 if (pair.first.equals(id)) {
1922 // We are enabling this input method, but it is already enabled.
1923 // Nothing to do. The previous state was enabled.
1924 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001925 }
1926 }
satokd87c2592010-09-29 11:52:06 +09001927 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1928 // Previous state was disabled.
1929 return false;
1930 } else {
1931 StringBuilder builder = new StringBuilder();
1932 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1933 builder, enabledInputMethodsList, id)) {
1934 // Disabled input method is currently selected, switch to another one.
1935 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1936 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001937 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
1938 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
1939 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09001940 }
1941 // Previous state was enabled.
1942 return true;
1943 } else {
1944 // We are disabling the input method but it is already disabled.
1945 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001946 return false;
1947 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001948 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001949 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001950
satok57ffc002011-01-25 00:11:47 +09001951 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
1952 if (subtype == null) return true;
1953 String[] extraValues = subtype.getExtraValue().split(",");
1954 final int N = extraValues.length;
1955 for (int i = 0; i < N; ++i) {
1956 if (SUBTYPE_EXTRAVALUE_EXCLUDE_FROM_LAST_IME.equals(extraValues[i])) {
1957 return false;
1958 }
1959 }
1960 return true;
1961 }
1962
satok723a27e2010-11-11 14:58:11 +09001963 private void saveCurrentInputMethodAndSubtypeToHistory() {
1964 String subtypeId = NOT_A_SUBTYPE_ID_STR;
1965 if (mCurrentSubtype != null) {
1966 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
1967 }
satok57ffc002011-01-25 00:11:47 +09001968 if (canAddToLastInputMethod(mCurrentSubtype)) {
1969 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
1970 }
satokab751aa2010-09-14 19:17:36 +09001971 }
1972
satok723a27e2010-11-11 14:58:11 +09001973 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
1974 boolean setSubtypeOnly) {
satok4d733292011-01-31 22:14:32 +09001975 mOldSystemSettingsVersion = getSystemSettingsVersion();
satok723a27e2010-11-11 14:58:11 +09001976 // Update the history of InputMethod and Subtype
1977 saveCurrentInputMethodAndSubtypeToHistory();
1978
1979 // Set Subtype here
1980 if (imi == null || subtypeId < 0) {
1981 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001982 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09001983 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09001984 if (subtypeId < imi.getSubtypeCount()) {
1985 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
1986 mSettings.putSelectedSubtype(subtype.hashCode());
1987 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09001988 } else {
1989 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
1990 mCurrentSubtype = null;
1991 }
satokab751aa2010-09-14 19:17:36 +09001992 }
satok723a27e2010-11-11 14:58:11 +09001993
1994 if (!setSubtypeOnly) {
1995 // Set InputMethod here
1996 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
1997 }
1998 }
1999
2000 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2001 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2002 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2003 // newDefaultIme is empty when there is no candidate for the selected IME.
2004 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2005 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2006 if (subtypeHashCode != null) {
2007 try {
2008 lastSubtypeId = getSubtypeIdFromHashCode(
2009 imi, Integer.valueOf(subtypeHashCode));
2010 } catch (NumberFormatException e) {
2011 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2012 }
2013 }
2014 }
2015 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002016 }
2017
2018 private int getSelectedInputMethodSubtypeId(String id) {
2019 InputMethodInfo imi = mMethodMap.get(id);
2020 if (imi == null) {
2021 return NOT_A_SUBTYPE_ID;
2022 }
satokab751aa2010-09-14 19:17:36 +09002023 int subtypeId;
2024 try {
2025 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2026 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2027 } catch (SettingNotFoundException e) {
2028 return NOT_A_SUBTYPE_ID;
2029 }
satok723a27e2010-11-11 14:58:11 +09002030 return getSubtypeIdFromHashCode(imi, subtypeId);
2031 }
2032
2033 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002034 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002035 final int subtypeCount = imi.getSubtypeCount();
2036 for (int i = 0; i < subtypeCount; ++i) {
2037 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002038 if (subtypeHashCode == ims.hashCode()) {
2039 return i;
2040 }
satokab751aa2010-09-14 19:17:36 +09002041 }
2042 }
2043 return NOT_A_SUBTYPE_ID;
2044 }
2045
satokdf31ae62011-01-15 06:19:44 +09002046 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2047 Resources res, List<InputMethodSubtype> subtypes) {
2048 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002049 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2050 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2051 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002052 final int N = subtypes.size();
2053 boolean containsKeyboardSubtype = false;
2054 for (int i = 0; i < N; ++i) {
2055 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002056 final String locale = subtype.getLocale();
2057 final String mode = subtype.getMode();
2058 // When system locale starts with subtype's locale, that subtype will be applicable
2059 // for system locale
2060 // For instance, it's clearly applicable for cases like system locale = en_US and
2061 // subtype = en, but it is not necessarily considered applicable for cases like system
2062 // locale = en and subtype = en_US.
2063 // We just call systemLocale.startsWith(locale) in this function because there is no
2064 // need to find applicable subtypes aggressively unlike
2065 // findLastResortApplicableSubtypeLocked.
2066 if (systemLocale.startsWith(locale)) {
2067 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2068 // If more applicable subtypes are contained, skip.
2069 if (applicableSubtype != null
2070 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2071 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002072 if (!containsKeyboardSubtype
2073 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2074 containsKeyboardSubtype = true;
2075 }
2076 }
2077 }
satok3da92232011-01-11 22:46:30 +09002078 ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2079 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002080 if (!containsKeyboardSubtype) {
2081 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002082 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002083 if (lastResortKeyboardSubtype != null) {
2084 applicableSubtypes.add(lastResortKeyboardSubtype);
2085 }
2086 }
2087 return applicableSubtypes;
2088 }
2089
satok4e4569d2010-11-19 18:45:53 +09002090 /**
2091 * If there are no selected subtypes, tries finding the most applicable one according to the
2092 * given locale.
2093 * @param subtypes this function will search the most applicable subtype in subtypes
2094 * @param mode subtypes will be filtered by mode
2095 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002096 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2097 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002098 * @return the most applicable subtypeId
2099 */
satokdf31ae62011-01-15 06:19:44 +09002100 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2101 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002102 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002103 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002104 return null;
satok8fbb1e82010-11-02 23:15:58 +09002105 }
satok4e4569d2010-11-19 18:45:53 +09002106 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002107 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002108 }
satok8fbb1e82010-11-02 23:15:58 +09002109 final String language = locale.substring(0, 2);
2110 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002111 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002112 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002113 final int N = subtypes.size();
2114 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002115 InputMethodSubtype subtype = subtypes.get(i);
2116 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002117 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2118 // and all subtypes with all modes can be candidates.
2119 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002120 if (firstMatchedModeSubtype == null) {
2121 firstMatchedModeSubtype = subtype;
2122 }
satok9ef02832010-11-04 21:17:48 +09002123 if (locale.equals(subtypeLocale)) {
2124 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002125 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002126 break;
2127 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2128 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002129 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002130 partialMatchFound = true;
2131 }
satok8fbb1e82010-11-02 23:15:58 +09002132 }
2133 }
2134
satok7599a7f2010-12-22 13:45:23 +09002135 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2136 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002137 }
2138
satok8fbb1e82010-11-02 23:15:58 +09002139 // The first subtype applicable to the system locale will be defined as the most applicable
2140 // subtype.
2141 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002142 if (applicableSubtype != null) {
2143 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2144 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2145 }
satok8fbb1e82010-11-02 23:15:58 +09002146 }
satokcd7cd292010-11-20 15:46:23 +09002147 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002148 }
2149
satok4e4569d2010-11-19 18:45:53 +09002150 // If there are no selected shortcuts, tries finding the most applicable ones.
2151 private Pair<InputMethodInfo, InputMethodSubtype>
2152 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2153 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2154 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002155 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002156 boolean foundInSystemIME = false;
2157
2158 // Search applicable subtype for each InputMethodInfo
2159 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002160 final String imiId = imi.getId();
2161 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2162 continue;
2163 }
satokcd7cd292010-11-20 15:46:23 +09002164 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002165 final List<InputMethodSubtype> enabledSubtypes =
2166 getEnabledInputMethodSubtypeList(imi, true);
2167 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002168 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002169 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002170 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002171 }
satokdf31ae62011-01-15 06:19:44 +09002172 // 2. Search by the system locale from enabledSubtypes.
2173 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002174 if (subtype == null) {
2175 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002176 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002177 }
satok7599a7f2010-12-22 13:45:23 +09002178 // 4. Search by the current subtype's locale from all subtypes.
2179 if (subtype == null && mCurrentSubtype != null) {
2180 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002181 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002182 }
2183 // 5. Search by the system locale from all subtypes.
2184 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002185 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002186 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002187 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002188 }
satokcd7cd292010-11-20 15:46:23 +09002189 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002190 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002191 // The current input method is the most applicable IME.
2192 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002193 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002194 break;
satok7599a7f2010-12-22 13:45:23 +09002195 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002196 // The system input method is 2nd applicable IME.
2197 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002198 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002199 if ((imi.getServiceInfo().applicationInfo.flags
2200 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2201 foundInSystemIME = true;
2202 }
satok4e4569d2010-11-19 18:45:53 +09002203 }
2204 }
2205 }
2206 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002207 if (mostApplicableIMI != null) {
2208 Slog.w(TAG, "Most applicable shortcut input method was:"
2209 + mostApplicableIMI.getId());
2210 if (mostApplicableSubtype != null) {
2211 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2212 + "," + mostApplicableSubtype.getMode() + ","
2213 + mostApplicableSubtype.getLocale());
2214 }
2215 }
satok4e4569d2010-11-19 18:45:53 +09002216 }
satokcd7cd292010-11-20 15:46:23 +09002217 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002218 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002219 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002220 } else {
2221 return null;
2222 }
2223 }
2224
satok4d733292011-01-31 22:14:32 +09002225 private static long getSystemSettingsVersion() {
2226 return SystemProperties.getLong(Settings.Secure.SYS_PROP_SETTING_VERSION, 0);
2227 }
2228
satokab751aa2010-09-14 19:17:36 +09002229 /**
2230 * @return Return the current subtype of this input method.
2231 */
2232 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002233 boolean subtypeIsSelected = false;
2234 try {
2235 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2236 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2237 } catch (SettingNotFoundException e) {
2238 }
satok3ef8b292010-11-23 06:06:29 +09002239 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002240 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002241 String lastInputMethodId = Settings.Secure.getString(
2242 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002243 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2244 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002245 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2246 if (imi != null) {
2247 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002248 // the most applicable subtype from explicitly or implicitly enabled
2249 // subtypes.
2250 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2251 getEnabledInputMethodSubtypeList(imi, true);
2252 // If there is only one explicitly or implicitly enabled subtype,
2253 // just returns it.
2254 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2255 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2256 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2257 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2258 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2259 SUBTYPE_MODE_KEYBOARD, null, true);
2260 if (mCurrentSubtype == null) {
2261 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2262 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2263 true);
2264 }
2265 }
satok4e4569d2010-11-19 18:45:53 +09002266 }
satokcd7cd292010-11-20 15:46:23 +09002267 } else {
satok3ef8b292010-11-23 06:06:29 +09002268 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002269 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002270 }
satok8fbb1e82010-11-02 23:15:58 +09002271 }
satok3ef8b292010-11-23 06:06:29 +09002272 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002273 }
satokab751aa2010-09-14 19:17:36 +09002274 }
2275
satokf3db1af2010-11-23 13:34:33 +09002276 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2277 InputMethodSubtype subtype) {
2278 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2279 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2280 } else {
2281 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2282 subtypes.add(subtype);
2283 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2284 }
2285 }
2286
satok4e4569d2010-11-19 18:45:53 +09002287 // TODO: We should change the return type from List to List<Parcelable>
2288 public List getShortcutInputMethodsAndSubtypes() {
2289 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002290 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002291 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002292 // If there are no selected shortcut subtypes, the framework will try to find
2293 // the most applicable subtype from all subtypes whose mode is
2294 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002295 Pair<InputMethodInfo, InputMethodSubtype> info =
2296 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2297 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002298 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002299 ret.add(info.first);
2300 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002301 }
satok3da92232011-01-11 22:46:30 +09002302 return ret;
satokf3db1af2010-11-23 13:34:33 +09002303 }
satokf3db1af2010-11-23 13:34:33 +09002304 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2305 ret.add(imi);
2306 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2307 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002308 }
2309 }
satokf3db1af2010-11-23 13:34:33 +09002310 return ret;
satok4e4569d2010-11-19 18:45:53 +09002311 }
2312 }
2313
satokb66d2872010-11-10 01:04:04 +09002314 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2315 synchronized (mMethodMap) {
2316 if (subtype != null && mCurMethodId != null) {
2317 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2318 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2319 if (subtypeId != NOT_A_SUBTYPE_ID) {
2320 setInputMethodLocked(mCurMethodId, subtypeId);
2321 return true;
2322 }
2323 }
2324 return false;
2325 }
2326 }
2327
satokd87c2592010-09-29 11:52:06 +09002328 /**
2329 * Utility class for putting and getting settings for InputMethod
2330 * TODO: Move all putters and getters of settings to this class.
2331 */
2332 private static class InputMethodSettings {
2333 // The string for enabled input method is saved as follows:
2334 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2335 private static final char INPUT_METHOD_SEPARATER = ':';
2336 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002337 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002338 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2339
satok723a27e2010-11-11 14:58:11 +09002340 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002341 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2342
satokdf31ae62011-01-15 06:19:44 +09002343 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002344 private final ContentResolver mResolver;
2345 private final HashMap<String, InputMethodInfo> mMethodMap;
2346 private final ArrayList<InputMethodInfo> mMethodList;
2347
2348 private String mEnabledInputMethodsStrCache;
2349
2350 private static void buildEnabledInputMethodsSettingString(
2351 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2352 String id = pair.first;
2353 ArrayList<String> subtypes = pair.second;
2354 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002355 // Inputmethod and subtypes are saved in the settings as follows:
2356 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2357 for (String subtypeId: subtypes) {
2358 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002359 }
2360 }
2361
2362 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002363 Resources res, ContentResolver resolver,
2364 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2365 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002366 mResolver = resolver;
2367 mMethodMap = methodMap;
2368 mMethodList = methodList;
2369 }
2370
2371 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2372 return createEnabledInputMethodListLocked(
2373 getEnabledInputMethodsAndSubtypeListLocked());
2374 }
2375
satok7f35c8c2010-10-07 21:13:11 +09002376 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002377 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2378 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002379 getEnabledInputMethodsAndSubtypeListLocked());
2380 }
2381
satok67ddf9c2010-11-17 09:45:54 +09002382 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2383 InputMethodInfo imi) {
2384 List<Pair<String, ArrayList<String>>> imsList =
2385 getEnabledInputMethodsAndSubtypeListLocked();
2386 ArrayList<InputMethodSubtype> enabledSubtypes =
2387 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002388 if (imi != null) {
2389 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2390 InputMethodInfo info = mMethodMap.get(imsPair.first);
2391 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002392 final int subtypeCount = info.getSubtypeCount();
2393 for (int i = 0; i < subtypeCount; ++i) {
2394 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002395 for (String s: imsPair.second) {
2396 if (String.valueOf(ims.hashCode()).equals(s)) {
2397 enabledSubtypes.add(ims);
2398 }
satok67ddf9c2010-11-17 09:45:54 +09002399 }
2400 }
satok884ef9a2010-11-18 10:39:46 +09002401 break;
satok67ddf9c2010-11-17 09:45:54 +09002402 }
satok67ddf9c2010-11-17 09:45:54 +09002403 }
2404 }
2405 return enabledSubtypes;
2406 }
2407
satokd87c2592010-09-29 11:52:06 +09002408 // At the initial boot, the settings for input methods are not set,
2409 // so we need to enable IME in that case.
2410 public void enableAllIMEsIfThereIsNoEnabledIME() {
2411 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2412 StringBuilder sb = new StringBuilder();
2413 final int N = mMethodList.size();
2414 for (int i = 0; i < N; i++) {
2415 InputMethodInfo imi = mMethodList.get(i);
2416 Slog.i(TAG, "Adding: " + imi.getId());
2417 if (i > 0) sb.append(':');
2418 sb.append(imi.getId());
2419 }
2420 putEnabledInputMethodsStr(sb.toString());
2421 }
2422 }
2423
satokbb4aa062011-01-19 21:40:27 +09002424 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002425 ArrayList<Pair<String, ArrayList<String>>> imsList
2426 = new ArrayList<Pair<String, ArrayList<String>>>();
2427 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2428 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2429 return imsList;
2430 }
satok723a27e2010-11-11 14:58:11 +09002431 mInputMethodSplitter.setString(enabledInputMethodsStr);
2432 while (mInputMethodSplitter.hasNext()) {
2433 String nextImsStr = mInputMethodSplitter.next();
2434 mSubtypeSplitter.setString(nextImsStr);
2435 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002436 ArrayList<String> subtypeHashes = new ArrayList<String>();
2437 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002438 String imeId = mSubtypeSplitter.next();
2439 while (mSubtypeSplitter.hasNext()) {
2440 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002441 }
2442 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2443 }
2444 }
2445 return imsList;
2446 }
2447
2448 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2449 if (reloadInputMethodStr) {
2450 getEnabledInputMethodsStr();
2451 }
2452 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2453 // Add in the newly enabled input method.
2454 putEnabledInputMethodsStr(id);
2455 } else {
2456 putEnabledInputMethodsStr(
2457 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2458 }
2459 }
2460
2461 /**
2462 * Build and put a string of EnabledInputMethods with removing specified Id.
2463 * @return the specified id was removed or not.
2464 */
2465 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2466 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2467 boolean isRemoved = false;
2468 boolean needsAppendSeparator = false;
2469 for (Pair<String, ArrayList<String>> ims: imsList) {
2470 String curId = ims.first;
2471 if (curId.equals(id)) {
2472 // We are disabling this input method, and it is
2473 // currently enabled. Skip it to remove from the
2474 // new list.
2475 isRemoved = true;
2476 } else {
2477 if (needsAppendSeparator) {
2478 builder.append(INPUT_METHOD_SEPARATER);
2479 } else {
2480 needsAppendSeparator = true;
2481 }
2482 buildEnabledInputMethodsSettingString(builder, ims);
2483 }
2484 }
2485 if (isRemoved) {
2486 // Update the setting with the new list of input methods.
2487 putEnabledInputMethodsStr(builder.toString());
2488 }
2489 return isRemoved;
2490 }
2491
2492 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2493 List<Pair<String, ArrayList<String>>> imsList) {
2494 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2495 for (Pair<String, ArrayList<String>> ims: imsList) {
2496 InputMethodInfo info = mMethodMap.get(ims.first);
2497 if (info != null) {
2498 res.add(info);
2499 }
2500 }
2501 return res;
2502 }
2503
satok7f35c8c2010-10-07 21:13:11 +09002504 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002505 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002506 List<Pair<String, ArrayList<String>>> imsList) {
2507 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2508 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2509 for (Pair<String, ArrayList<String>> ims : imsList) {
2510 InputMethodInfo info = mMethodMap.get(ims.first);
2511 if (info != null) {
2512 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2513 }
2514 }
2515 return res;
2516 }
2517
satokd87c2592010-09-29 11:52:06 +09002518 private void putEnabledInputMethodsStr(String str) {
2519 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2520 mEnabledInputMethodsStrCache = str;
2521 }
2522
2523 private String getEnabledInputMethodsStr() {
2524 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2525 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002526 if (DEBUG) {
2527 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2528 }
satokd87c2592010-09-29 11:52:06 +09002529 return mEnabledInputMethodsStrCache;
2530 }
satok723a27e2010-11-11 14:58:11 +09002531
2532 private void saveSubtypeHistory(
2533 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2534 StringBuilder builder = new StringBuilder();
2535 boolean isImeAdded = false;
2536 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2537 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2538 newSubtypeId);
2539 isImeAdded = true;
2540 }
2541 for (Pair<String, String> ime: savedImes) {
2542 String imeId = ime.first;
2543 String subtypeId = ime.second;
2544 if (TextUtils.isEmpty(subtypeId)) {
2545 subtypeId = NOT_A_SUBTYPE_ID_STR;
2546 }
2547 if (isImeAdded) {
2548 builder.append(INPUT_METHOD_SEPARATER);
2549 } else {
2550 isImeAdded = true;
2551 }
2552 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2553 subtypeId);
2554 }
2555 // Remove the last INPUT_METHOD_SEPARATER
2556 putSubtypeHistoryStr(builder.toString());
2557 }
2558
2559 public void addSubtypeToHistory(String imeId, String subtypeId) {
2560 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2561 for (Pair<String, String> ime: subtypeHistory) {
2562 if (ime.first.equals(imeId)) {
2563 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002564 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002565 + ime.second);
2566 }
2567 // We should break here
2568 subtypeHistory.remove(ime);
2569 break;
2570 }
2571 }
satokbb4aa062011-01-19 21:40:27 +09002572 if (DEBUG) {
2573 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2574 }
satok723a27e2010-11-11 14:58:11 +09002575 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2576 }
2577
2578 private void putSubtypeHistoryStr(String str) {
2579 if (DEBUG) {
2580 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2581 }
2582 Settings.Secure.putString(
2583 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2584 }
2585
2586 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2587 // Gets the first one from the history
2588 return getLastSubtypeForInputMethodLockedInternal(null);
2589 }
2590
2591 public String getLastSubtypeForInputMethodLocked(String imeId) {
2592 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2593 if (ime != null) {
2594 return ime.second;
2595 } else {
2596 return null;
2597 }
2598 }
2599
2600 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2601 List<Pair<String, ArrayList<String>>> enabledImes =
2602 getEnabledInputMethodsAndSubtypeListLocked();
2603 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2604 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2605 final String imeInTheHistory = imeAndSubtype.first;
2606 // If imeId is empty, returns the first IME and subtype in the history
2607 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2608 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002609 final String subtypeHashCode =
2610 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2611 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002612 if (!TextUtils.isEmpty(subtypeHashCode)) {
2613 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002614 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002615 }
2616 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2617 }
2618 }
2619 }
2620 if (DEBUG) {
2621 Slog.d(TAG, "No enabled IME found in the history");
2622 }
2623 return null;
2624 }
2625
satokdf31ae62011-01-15 06:19:44 +09002626 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002627 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2628 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2629 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002630 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2631 if (explicitlyEnabledSubtypes.size() == 0) {
2632 // If there are no explicitly enabled subtypes, applicable subtypes are
2633 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002634 InputMethodInfo ime = mMethodMap.get(imeId);
2635 // If IME is enabled and no subtypes are enabled, applicable subtypes
2636 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002637 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002638 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002639 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002640 if (implicitlySelectedSubtypes != null) {
2641 final int N = implicitlySelectedSubtypes.size();
2642 for (int i = 0; i < N; ++i) {
2643 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2644 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2645 return subtypeHashCode;
2646 }
2647 }
2648 }
2649 }
2650 } else {
satokf6cafb62011-01-17 16:29:02 +09002651 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002652 if (s.equals(subtypeHashCode)) {
2653 // If both imeId and subtypeId are enabled, return subtypeId.
2654 return s;
2655 }
satok723a27e2010-11-11 14:58:11 +09002656 }
2657 }
2658 // If imeId was enabled but subtypeId was disabled.
2659 return NOT_A_SUBTYPE_ID_STR;
2660 }
2661 }
2662 // If both imeId and subtypeId are disabled, return null
2663 return null;
2664 }
2665
2666 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2667 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2668 final String subtypeHistoryStr = getSubtypeHistoryStr();
2669 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2670 return imsList;
2671 }
2672 mInputMethodSplitter.setString(subtypeHistoryStr);
2673 while (mInputMethodSplitter.hasNext()) {
2674 String nextImsStr = mInputMethodSplitter.next();
2675 mSubtypeSplitter.setString(nextImsStr);
2676 if (mSubtypeSplitter.hasNext()) {
2677 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2678 // The first element is ime id.
2679 String imeId = mSubtypeSplitter.next();
2680 while (mSubtypeSplitter.hasNext()) {
2681 subtypeId = mSubtypeSplitter.next();
2682 break;
2683 }
2684 imsList.add(new Pair<String, String>(imeId, subtypeId));
2685 }
2686 }
2687 return imsList;
2688 }
2689
2690 private String getSubtypeHistoryStr() {
2691 if (DEBUG) {
2692 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2693 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2694 }
2695 return Settings.Secure.getString(
2696 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2697 }
2698
2699 public void putSelectedInputMethod(String imeId) {
2700 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2701 }
2702
2703 public void putSelectedSubtype(int subtypeId) {
2704 Settings.Secure.putInt(
2705 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2706 }
satokd87c2592010-09-29 11:52:06 +09002707 }
2708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 @Override
2712 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2713 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2714 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2717 + Binder.getCallingPid()
2718 + ", uid=" + Binder.getCallingUid());
2719 return;
2720 }
2721
2722 IInputMethod method;
2723 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 synchronized (mMethodMap) {
2728 p.println("Current Input Method Manager state:");
2729 int N = mMethodList.size();
2730 p.println(" Input Methods:");
2731 for (int i=0; i<N; i++) {
2732 InputMethodInfo info = mMethodList.get(i);
2733 p.println(" InputMethod #" + i + ":");
2734 info.dump(p, " ");
2735 }
2736 p.println(" Clients:");
2737 for (ClientState ci : mClients.values()) {
2738 p.println(" Client " + ci + ":");
2739 p.println(" client=" + ci.client);
2740 p.println(" inputContext=" + ci.inputContext);
2741 p.println(" sessionRequested=" + ci.sessionRequested);
2742 p.println(" curSession=" + ci.curSession);
2743 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002744 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002746 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2747 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2749 + " mBoundToMethod=" + mBoundToMethod);
2750 p.println(" mCurToken=" + mCurToken);
2751 p.println(" mCurIntent=" + mCurIntent);
2752 method = mCurMethod;
2753 p.println(" mCurMethod=" + mCurMethod);
2754 p.println(" mEnabledSession=" + mEnabledSession);
2755 p.println(" mShowRequested=" + mShowRequested
2756 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2757 + " mShowForced=" + mShowForced
2758 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002759 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002761
Jeff Brownb88102f2010-09-08 11:49:43 -07002762 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 pw.flush();
2765 try {
2766 client.client.asBinder().dump(fd, args);
2767 } catch (RemoteException e) {
2768 p.println("Input method client dead: " + e);
2769 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002770 } else {
2771 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002773
Jeff Brownb88102f2010-09-08 11:49:43 -07002774 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 pw.flush();
2777 try {
2778 method.asBinder().dump(fd, args);
2779 } catch (RemoteException e) {
2780 p.println("Input method service dead: " + e);
2781 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002782 } else {
2783 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 }
2785 }
2786}