blob: 51e9b0077b59c2b198fc857dd223ec3c0659f8ea [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 }
satok7265d9b2011-02-14 15:47:30 +0900599 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900600 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900601 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
602 enabledSubtypes = getApplicableSubtypesLocked(mRes, getSubtypes(imi));
satokbb4aa062011-01-19 21:40:27 +0900603 }
satok7265d9b2011-02-14 15:47:30 +0900604 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900605 }
606
satok16331c82010-12-20 23:48:46 +0900607 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
608 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900609 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900610 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900611 }
612 }
613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 public void addClient(IInputMethodClient client,
615 IInputContext inputContext, int uid, int pid) {
616 synchronized (mMethodMap) {
617 mClients.put(client.asBinder(), new ClientState(client,
618 inputContext, uid, pid));
619 }
620 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 public void removeClient(IInputMethodClient client) {
623 synchronized (mMethodMap) {
624 mClients.remove(client.asBinder());
625 }
626 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 void executeOrSendMessage(IInterface target, Message msg) {
629 if (target.asBinder() instanceof Binder) {
630 mCaller.sendMessage(msg);
631 } else {
632 handleMessage(msg);
633 msg.recycle();
634 }
635 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800636
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700637 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800639 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 + mCurClient.client.asBinder());
641 if (mBoundToMethod) {
642 mBoundToMethod = false;
643 if (mCurMethod != null) {
644 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
645 MSG_UNBIND_INPUT, mCurMethod));
646 }
647 }
648 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
649 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
650 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 // Call setActive(false) on the old client
653 try {
654 mCurClient.client.setActive(false);
655 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800656 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 + mCurClient.pid + " uid " + mCurClient.uid);
658 }
659 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800660
The Android Open Source Project10592532009-03-18 17:39:46 -0700661 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 }
663 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 private int getImeShowFlags() {
666 int flags = 0;
667 if (mShowForced) {
668 flags |= InputMethod.SHOW_FORCED
669 | InputMethod.SHOW_EXPLICIT;
670 } else if (mShowExplicitlyRequested) {
671 flags |= InputMethod.SHOW_EXPLICIT;
672 }
673 return flags;
674 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 private int getAppShowFlags() {
677 int flags = 0;
678 if (mShowForced) {
679 flags |= InputMethodManager.SHOW_FORCED;
680 } else if (!mShowExplicitlyRequested) {
681 flags |= InputMethodManager.SHOW_IMPLICIT;
682 }
683 return flags;
684 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
687 if (!mBoundToMethod) {
688 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
689 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
690 mBoundToMethod = true;
691 }
692 final SessionState session = mCurClient.curSession;
693 if (initial) {
694 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
695 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
696 } else {
697 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
698 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
699 }
700 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800701 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800702 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 }
704 return needResult
705 ? new InputBindResult(session.session, mCurId, mCurSeq)
706 : null;
707 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 InputBindResult startInputLocked(IInputMethodClient client,
710 IInputContext inputContext, EditorInfo attribute,
711 boolean initial, boolean needResult) {
712 // If no method is currently selected, do nothing.
713 if (mCurMethodId == null) {
714 return mNoBinding;
715 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 ClientState cs = mClients.get(client.asBinder());
718 if (cs == null) {
719 throw new IllegalArgumentException("unknown client "
720 + client.asBinder());
721 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 try {
724 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
725 // Check with the window manager to make sure this client actually
726 // has a window with focus. If not, reject. This is thread safe
727 // because if the focus changes some time before or after, the
728 // next client receiving focus that has any interest in input will
729 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800730 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
732 return null;
733 }
734 } catch (RemoteException e) {
735 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 if (mCurClient != cs) {
738 // If the client is changing, we need to switch over to the new
739 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700740 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800741 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 + cs.client.asBinder());
743
744 // If the screen is on, inform the new client it is active
745 if (mScreenOn) {
746 try {
747 cs.client.setActive(mScreenOn);
748 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800749 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 + cs.pid + " uid " + cs.uid);
751 }
752 }
753 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 // Bump up the sequence for this client and attach it.
756 mCurSeq++;
757 if (mCurSeq <= 0) mCurSeq = 1;
758 mCurClient = cs;
759 mCurInputContext = inputContext;
760 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 // Check if the input method is changing.
763 if (mCurId != null && mCurId.equals(mCurMethodId)) {
764 if (cs.curSession != null) {
765 // Fast case: if we are already connected to the input method,
766 // then just return it.
767 return attachNewInputLocked(initial, needResult);
768 }
769 if (mHaveConnection) {
770 if (mCurMethod != null) {
771 if (!cs.sessionRequested) {
772 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800773 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
775 MSG_CREATE_SESSION, mCurMethod,
776 new MethodCallback(mCurMethod)));
777 }
778 // Return to client, and we will get back with it when
779 // we have had a session made for it.
780 return new InputBindResult(null, mCurId, mCurSeq);
781 } else if (SystemClock.uptimeMillis()
782 < (mLastBindTime+TIME_TO_RECONNECT)) {
783 // In this case we have connected to the service, but
784 // don't yet have its interface. If it hasn't been too
785 // long since we did the connection, we'll return to
786 // the client and wait to get the service interface so
787 // we can report back. If it has been too long, we want
788 // to fall through so we can try a disconnect/reconnect
789 // to see if we can get back in touch with the service.
790 return new InputBindResult(null, mCurId, mCurSeq);
791 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800792 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
793 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 }
795 }
796 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800797
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700798 return startInputInnerLocked();
799 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800800
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700801 InputBindResult startInputInnerLocked() {
802 if (mCurMethodId == null) {
803 return mNoBinding;
804 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800805
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700806 if (!mSystemReady) {
807 // If the system is not yet ready, we shouldn't be running third
808 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700809 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700810 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 InputMethodInfo info = mMethodMap.get(mCurMethodId);
813 if (info == null) {
814 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
815 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800816
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700817 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
820 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700821 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
822 com.android.internal.R.string.input_method_binding_label);
823 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
824 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
826 mLastBindTime = SystemClock.uptimeMillis();
827 mHaveConnection = true;
828 mCurId = info.getId();
829 mCurToken = new Binder();
830 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800831 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 mIWindowManager.addWindowToken(mCurToken,
833 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
834 } catch (RemoteException e) {
835 }
836 return new InputBindResult(null, mCurId, mCurSeq);
837 } else {
838 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800839 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 + mCurIntent);
841 }
842 return null;
843 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 public InputBindResult startInput(IInputMethodClient client,
846 IInputContext inputContext, EditorInfo attribute,
847 boolean initial, boolean needResult) {
848 synchronized (mMethodMap) {
849 final long ident = Binder.clearCallingIdentity();
850 try {
851 return startInputLocked(client, inputContext, attribute,
852 initial, needResult);
853 } finally {
854 Binder.restoreCallingIdentity(ident);
855 }
856 }
857 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 public void finishInput(IInputMethodClient client) {
860 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 public void onServiceConnected(ComponentName name, IBinder service) {
863 synchronized (mMethodMap) {
864 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
865 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700866 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800867 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700868 unbindCurrentMethodLocked(false);
869 return;
870 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800871 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700872 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
873 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800875 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700876 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700878 MSG_CREATE_SESSION, mCurMethod,
879 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 }
881 }
882 }
883 }
884
885 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
886 synchronized (mMethodMap) {
887 if (mCurMethod != null && method != null
888 && mCurMethod.asBinder() == method.asBinder()) {
889 if (mCurClient != null) {
890 mCurClient.curSession = new SessionState(mCurClient,
891 method, session);
892 mCurClient.sessionRequested = false;
893 InputBindResult res = attachNewInputLocked(true, true);
894 if (res.method != null) {
895 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
896 MSG_BIND_METHOD, mCurClient.client, res));
897 }
898 }
899 }
900 }
901 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800902
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700903 void unbindCurrentMethodLocked(boolean reportToClient) {
904 if (mHaveConnection) {
905 mContext.unbindService(this);
906 mHaveConnection = false;
907 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800908
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700909 if (mCurToken != null) {
910 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800911 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700912 mIWindowManager.removeWindowToken(mCurToken);
913 } catch (RemoteException e) {
914 }
915 mCurToken = null;
916 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800917
The Android Open Source Project10592532009-03-18 17:39:46 -0700918 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700919 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800920
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700921 if (reportToClient && mCurClient != null) {
922 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
923 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
924 }
925 }
926
Devin Taylor0c33ed22010-02-23 13:26:46 -0600927 private void finishSession(SessionState sessionState) {
928 if (sessionState != null && sessionState.session != null) {
929 try {
930 sessionState.session.finishSession();
931 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700932 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600933 }
934 }
935 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800936
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700937 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 if (mCurMethod != null) {
939 for (ClientState cs : mClients.values()) {
940 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600941 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 cs.curSession = null;
943 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600944
945 finishSession(mEnabledSession);
946 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 mCurMethod = null;
948 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700949 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 public void onServiceDisconnected(ComponentName name) {
953 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800954 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 + " mCurIntent=" + mCurIntent);
956 if (mCurMethod != null && mCurIntent != null
957 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700958 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 // We consider this to be a new bind attempt, since the system
960 // should now try to restart the service for us.
961 mLastBindTime = SystemClock.uptimeMillis();
962 mShowRequested = mInputShown;
963 mInputShown = false;
964 if (mCurClient != null) {
965 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
966 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
967 }
968 }
969 }
970 }
971
972 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700973 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 long ident = Binder.clearCallingIdentity();
975 try {
976 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700977 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 return;
979 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 synchronized (mMethodMap) {
982 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800983 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700984 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800986 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700987 mStatusBar.setIcon("ime", packageName, iconId, 0);
988 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 }
990 }
991 } finally {
992 Binder.restoreCallingIdentity(ident);
993 }
994 }
995
Joe Onorato857fd9b2011-01-27 15:08:35 -0800996 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +0900997 int uid = Binder.getCallingUid();
998 long ident = Binder.clearCallingIdentity();
999 try {
1000 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001001 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001002 return;
1003 }
1004
1005 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001006 mImeWindowVis = vis;
1007 mBackDisposition = backDisposition;
1008 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
satok06487a52010-10-29 11:37:18 +09001009 }
1010 } finally {
1011 Binder.restoreCallingIdentity(ident);
1012 }
1013 }
1014
satok4d733292011-01-31 22:14:32 +09001015 // TODO: Investigate and fix why are settings changes getting processed before the settings seq
1016 // number is updated?
1017 // TODO: Change this stuff to not rely on modifying settings for normal user interactions.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 void updateFromSettingsLocked() {
satok4d733292011-01-31 22:14:32 +09001019 long newSystemSettingsVersion = getSystemSettingsVersion();
1020 // This is a workaround to avoid a situation that old cached value in Settings.Secure
1021 // will be handled.
1022 if (newSystemSettingsVersion == mOldSystemSettingsVersion) return;
1023
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001024 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1025 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1026 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1027 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001029 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001030 // There is no input method selected, try to choose new applicable input method.
1031 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1032 id = Settings.Secure.getString(mContext.getContentResolver(),
1033 Settings.Secure.DEFAULT_INPUT_METHOD);
1034 }
1035 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 try {
satokab751aa2010-09-14 19:17:36 +09001037 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001039 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001040 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001041 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 }
satokf3db1af2010-11-23 13:34:33 +09001043 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001044 } else {
1045 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001046 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001047 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 }
1049 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001050
satokab751aa2010-09-14 19:17:36 +09001051 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 InputMethodInfo info = mMethodMap.get(id);
1053 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001054 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001058 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001059 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1060 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001061 }
1062 if (subtype != mCurrentSubtype) {
1063 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001064 if (subtype != null) {
1065 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1066 }
satokcd7cd292010-11-20 15:46:23 +09001067 if (mCurMethod != null) {
1068 try {
satoke40dea02011-01-30 01:14:02 +09001069 final Configuration conf = mRes.getConfiguration();
1070 final boolean haveHardKeyboard = conf.keyboard
1071 != Configuration.KEYBOARD_NOKEYS;
1072 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001073 && conf.hardKeyboardHidden
1074 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001075 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1076 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1077 : 0;
Joe Onorato857fd9b2011-01-27 15:08:35 -08001078 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1079 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001080 // If subtype is null, try to find the most applicable one from
1081 // getCurrentInputMethodSubtype.
1082 if (subtype == null) {
1083 subtype = getCurrentInputMethodSubtype();
1084 }
1085 mCurMethod.changeInputMethodSubtype(subtype);
1086 } catch (RemoteException e) {
1087 return;
satokab751aa2010-09-14 19:17:36 +09001088 }
1089 }
1090 }
1091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 return;
1093 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 final long ident = Binder.clearCallingIdentity();
1096 try {
satokab751aa2010-09-14 19:17:36 +09001097 // Set a subtype to this input method.
1098 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001099 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1100 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1101 // because mCurMethodId is stored as a history in
1102 // setSelectedInputMethodAndSubtypeLocked().
1103 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104
1105 if (ActivityManagerNative.isSystemReady()) {
1106 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001107 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 intent.putExtra("input_method_id", id);
1109 mContext.sendBroadcast(intent);
1110 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001111 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 } finally {
1113 Binder.restoreCallingIdentity(ident);
1114 }
1115 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001116
The Android Open Source Project4df24232009-03-05 14:34:35 -08001117 public boolean showSoftInput(IInputMethodClient client, int flags,
1118 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001119 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 long ident = Binder.clearCallingIdentity();
1121 try {
1122 synchronized (mMethodMap) {
1123 if (mCurClient == null || client == null
1124 || mCurClient.client.asBinder() != client.asBinder()) {
1125 try {
1126 // We need to check if this is the current client with
1127 // focus in the window manager, to allow this call to
1128 // be made before input is started in it.
1129 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001130 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001131 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 }
1133 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001134 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 }
1136 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001137
Joe Onorato8a9b2202010-02-26 18:56:32 -08001138 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001139 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 }
1141 } finally {
1142 Binder.restoreCallingIdentity(ident);
1143 }
1144 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001145
The Android Open Source Project4df24232009-03-05 14:34:35 -08001146 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 mShowRequested = true;
1148 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1149 mShowExplicitlyRequested = true;
1150 }
1151 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1152 mShowExplicitlyRequested = true;
1153 mShowForced = true;
1154 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001155
Dianne Hackborncc278702009-09-02 23:07:23 -07001156 if (!mSystemReady) {
1157 return false;
1158 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001159
The Android Open Source Project4df24232009-03-05 14:34:35 -08001160 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001162 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1163 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1164 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001166 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 } else if (mHaveConnection && SystemClock.uptimeMillis()
1168 < (mLastBindTime+TIME_TO_RECONNECT)) {
1169 // The client has asked to have the input method shown, but
1170 // we have been sitting here too long with a connection to the
1171 // service and no interface received, so let's disconnect/connect
1172 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001173 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 SystemClock.uptimeMillis()-mLastBindTime,1);
1175 mContext.unbindService(this);
1176 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1177 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001178
The Android Open Source Project4df24232009-03-05 14:34:35 -08001179 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001181
The Android Open Source Project4df24232009-03-05 14:34:35 -08001182 public boolean hideSoftInput(IInputMethodClient client, int flags,
1183 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001184 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 long ident = Binder.clearCallingIdentity();
1186 try {
1187 synchronized (mMethodMap) {
1188 if (mCurClient == null || client == null
1189 || mCurClient.client.asBinder() != client.asBinder()) {
1190 try {
1191 // We need to check if this is the current client with
1192 // focus in the window manager, to allow this call to
1193 // be made before input is started in it.
1194 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001195 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1196 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001197 mImeWindowVis = 0;
1198 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1199 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001200 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
1202 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001203 mImeWindowVis = 0;
1204 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001205 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 }
1207 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001208
Joe Onorato8a9b2202010-02-26 18:56:32 -08001209 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001210 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
1212 } finally {
1213 Binder.restoreCallingIdentity(ident);
1214 }
1215 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001216
The Android Open Source Project4df24232009-03-05 14:34:35 -08001217 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1219 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001220 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001222 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 }
1224 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001225 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001227 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001229 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001231 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1232 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1233 res = true;
1234 } else {
1235 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 }
1237 mInputShown = false;
1238 mShowRequested = false;
1239 mShowExplicitlyRequested = false;
1240 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001241 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001243
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001244 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1246 boolean first, int windowFlags) {
1247 long ident = Binder.clearCallingIdentity();
1248 try {
1249 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001250 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 + " viewHasFocus=" + viewHasFocus
1252 + " isTextEditor=" + isTextEditor
1253 + " softInputMode=#" + Integer.toHexString(softInputMode)
1254 + " first=" + first + " flags=#"
1255 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 if (mCurClient == null || client == null
1258 || mCurClient.client.asBinder() != client.asBinder()) {
1259 try {
1260 // We need to check if this is the current client with
1261 // focus in the window manager, to allow this call to
1262 // be made before input is started in it.
1263 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001264 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 return;
1266 }
1267 } catch (RemoteException e) {
1268 }
1269 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001270
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001271 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001272 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001273 return;
1274 }
1275 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001276
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001277 // Should we auto-show the IME even if the caller has not
1278 // specified what should be done with it?
1279 // We only do this automatically if the window can resize
1280 // to accommodate the IME (so what the user sees will give
1281 // them good context without input information being obscured
1282 // by the IME) or if running on a large screen where there
1283 // is more room for the target window + IME.
1284 final boolean doAutoShow =
1285 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1286 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1287 || mRes.getConfiguration().isLayoutSizeAtLeast(
1288 Configuration.SCREENLAYOUT_SIZE_LARGE);
1289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1291 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001292 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1294 // There is no focus view, and this window will
1295 // be behind any soft input window, so hide the
1296 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001297 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001298 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001300 } else if (isTextEditor && doAutoShow && (softInputMode &
1301 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 // There is a focus view, and we are navigating forward
1303 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001304 // We only do this automatically if the window an resize
1305 // to accomodate the IME (so what the user sees will give
1306 // them good context without input information being obscured
1307 // by the IME) or if running on a large screen where there
1308 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001309 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001310 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 }
1312 break;
1313 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1314 // Do nothing.
1315 break;
1316 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1317 if ((softInputMode &
1318 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001319 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001320 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 }
1322 break;
1323 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001324 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001325 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 break;
1327 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1328 if ((softInputMode &
1329 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001330 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001331 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 }
1333 break;
1334 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001335 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001336 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 break;
1338 }
1339 }
1340 } finally {
1341 Binder.restoreCallingIdentity(ident);
1342 }
1343 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1346 synchronized (mMethodMap) {
1347 if (mCurClient == null || client == null
1348 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001349 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001350 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 }
1352
satok440aab52010-11-25 09:43:11 +09001353 // Always call subtype picker, because subtype picker is a superset of input method
1354 // picker.
satokab751aa2010-09-14 19:17:36 +09001355 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1356 }
1357 }
1358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001360 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1361 }
1362
1363 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1364 synchronized (mMethodMap) {
1365 if (subtype != null) {
1366 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1367 mMethodMap.get(id), subtype.hashCode()));
1368 } else {
1369 setInputMethod(token, id);
1370 }
1371 }
satokab751aa2010-09-14 19:17:36 +09001372 }
1373
satokb416a71e2010-11-25 20:42:14 +09001374 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001375 IInputMethodClient client, String inputMethodId) {
satokb416a71e2010-11-25 20:42:14 +09001376 synchronized (mMethodMap) {
1377 if (mCurClient == null || client == null
1378 || mCurClient.client.asBinder() != client.asBinder()) {
1379 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1380 }
satok7fee71f2010-12-17 18:54:26 +09001381 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1382 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09001383 }
1384 }
1385
satok735cf382010-11-11 20:40:09 +09001386 public boolean switchToLastInputMethod(IBinder token) {
1387 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001388 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1389 if (lastIme == null) return false;
1390 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1391 if (lastImi == null) return false;
1392
1393 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1394 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1395 // If the last IME is the same as the current IME and the last subtype is not defined,
1396 // there is no need to switch to the last IME.
1397 if (imiIdIsSame && lastSubtypeHash == NOT_A_SUBTYPE_ID) return false;
1398
1399 int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1400 : mCurrentSubtype.hashCode();
1401 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1402 if (DEBUG) {
1403 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second + ", from: "
1404 + mCurMethodId + ", " + currentSubtypeHash);
satok735cf382010-11-11 20:40:09 +09001405 }
satokc445bcd2011-01-25 18:57:24 +09001406 setInputMethodWithSubtypeId(token, lastIme.first, getSubtypeIdFromHashCode(
1407 lastImi, lastSubtypeHash));
1408 return true;
satok735cf382010-11-11 20:40:09 +09001409 }
1410 return false;
1411 }
1412 }
1413
satok28203512010-11-24 11:06:49 +09001414 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 synchronized (mMethodMap) {
1416 if (token == null) {
1417 if (mContext.checkCallingOrSelfPermission(
1418 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1419 != PackageManager.PERMISSION_GRANTED) {
1420 throw new SecurityException(
1421 "Using null token requires permission "
1422 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1423 }
1424 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001425 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1426 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 return;
1428 }
1429
1430 long ident = Binder.clearCallingIdentity();
1431 try {
satokab751aa2010-09-14 19:17:36 +09001432 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 } finally {
1434 Binder.restoreCallingIdentity(ident);
1435 }
1436 }
1437 }
1438
1439 public void hideMySoftInput(IBinder token, int flags) {
1440 synchronized (mMethodMap) {
1441 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001442 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1443 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 return;
1445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 long ident = Binder.clearCallingIdentity();
1447 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001448 hideCurrentInputLocked(flags, null);
1449 } finally {
1450 Binder.restoreCallingIdentity(ident);
1451 }
1452 }
1453 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001454
The Android Open Source Project4df24232009-03-05 14:34:35 -08001455 public void showMySoftInput(IBinder token, int flags) {
1456 synchronized (mMethodMap) {
1457 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001458 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1459 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001460 return;
1461 }
1462 long ident = Binder.clearCallingIdentity();
1463 try {
1464 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 } finally {
1466 Binder.restoreCallingIdentity(ident);
1467 }
1468 }
1469 }
1470
1471 void setEnabledSessionInMainThread(SessionState session) {
1472 if (mEnabledSession != session) {
1473 if (mEnabledSession != null) {
1474 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001475 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 mEnabledSession.method.setSessionEnabled(
1477 mEnabledSession.session, false);
1478 } catch (RemoteException e) {
1479 }
1480 }
1481 mEnabledSession = session;
1482 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001483 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 session.method.setSessionEnabled(
1485 session.session, true);
1486 } catch (RemoteException e) {
1487 }
1488 }
1489 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 public boolean handleMessage(Message msg) {
1492 HandlerCaller.SomeArgs args;
1493 switch (msg.what) {
1494 case MSG_SHOW_IM_PICKER:
1495 showInputMethodMenu();
1496 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001497
satokab751aa2010-09-14 19:17:36 +09001498 case MSG_SHOW_IM_SUBTYPE_PICKER:
1499 showInputMethodSubtypeMenu();
1500 return true;
1501
satok47a44912010-10-06 16:03:58 +09001502 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001503 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001504 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001505 return true;
1506
1507 case MSG_SHOW_IM_CONFIG:
1508 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001509 return true;
1510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 case MSG_UNBIND_INPUT:
1514 try {
1515 ((IInputMethod)msg.obj).unbindInput();
1516 } catch (RemoteException e) {
1517 // There is nothing interesting about the method dying.
1518 }
1519 return true;
1520 case MSG_BIND_INPUT:
1521 args = (HandlerCaller.SomeArgs)msg.obj;
1522 try {
1523 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1524 } catch (RemoteException e) {
1525 }
1526 return true;
1527 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001528 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001530 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1531 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 } catch (RemoteException e) {
1533 }
1534 return true;
1535 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001536 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001538 ((IInputMethod)args.arg1).hideSoftInput(0,
1539 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 } catch (RemoteException e) {
1541 }
1542 return true;
1543 case MSG_ATTACH_TOKEN:
1544 args = (HandlerCaller.SomeArgs)msg.obj;
1545 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001546 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1548 } catch (RemoteException e) {
1549 }
1550 return true;
1551 case MSG_CREATE_SESSION:
1552 args = (HandlerCaller.SomeArgs)msg.obj;
1553 try {
1554 ((IInputMethod)args.arg1).createSession(
1555 (IInputMethodCallback)args.arg2);
1556 } catch (RemoteException e) {
1557 }
1558 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 case MSG_START_INPUT:
1562 args = (HandlerCaller.SomeArgs)msg.obj;
1563 try {
1564 SessionState session = (SessionState)args.arg1;
1565 setEnabledSessionInMainThread(session);
1566 session.method.startInput((IInputContext)args.arg2,
1567 (EditorInfo)args.arg3);
1568 } catch (RemoteException e) {
1569 }
1570 return true;
1571 case MSG_RESTART_INPUT:
1572 args = (HandlerCaller.SomeArgs)msg.obj;
1573 try {
1574 SessionState session = (SessionState)args.arg1;
1575 setEnabledSessionInMainThread(session);
1576 session.method.restartInput((IInputContext)args.arg2,
1577 (EditorInfo)args.arg3);
1578 } catch (RemoteException e) {
1579 }
1580 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 case MSG_UNBIND_METHOD:
1585 try {
1586 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1587 } catch (RemoteException e) {
1588 // There is nothing interesting about the last client dying.
1589 }
1590 return true;
1591 case MSG_BIND_METHOD:
1592 args = (HandlerCaller.SomeArgs)msg.obj;
1593 try {
1594 ((IInputMethodClient)args.arg1).onBindMethod(
1595 (InputBindResult)args.arg2);
1596 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001597 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 }
1599 return true;
1600 }
1601 return false;
1602 }
1603
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001604 private boolean isSystemIme(InputMethodInfo inputMethod) {
1605 return (inputMethod.getServiceInfo().applicationInfo.flags
1606 & ApplicationInfo.FLAG_SYSTEM) != 0;
1607 }
1608
Ken Wakasa586f0512011-01-20 22:31:01 +09001609 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1610 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1611 final int subtypeCount = imi.getSubtypeCount();
1612 for (int i = 0; i < subtypeCount; ++i) {
1613 subtypes.add(imi.getSubtypeAt(i));
1614 }
1615 return subtypes;
1616 }
1617
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001618 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001619 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001620 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001621 // We'd prefer to fall back on a system IME, since that is safer.
1622 int i=enabled.size();
1623 while (i > 0) {
1624 i--;
1625 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1626 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1627 break;
1628 }
1629 }
satokab751aa2010-09-14 19:17:36 +09001630 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001631 if (DEBUG) {
1632 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1633 }
satok723a27e2010-11-11 14:58:11 +09001634 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001635 return true;
1636 }
1637
1638 return false;
1639 }
1640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1642 HashMap<String, InputMethodInfo> map) {
1643 list.clear();
1644 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001647 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001648 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1649 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1650 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1651 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652
1653 List<ResolveInfo> services = pm.queryIntentServices(
1654 new Intent(InputMethod.SERVICE_INTERFACE),
1655 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 for (int i = 0; i < services.size(); ++i) {
1658 ResolveInfo ri = services.get(i);
1659 ServiceInfo si = ri.serviceInfo;
1660 ComponentName compName = new ComponentName(si.packageName, si.name);
1661 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1662 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001663 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 + ": it does not require the permission "
1665 + android.Manifest.permission.BIND_INPUT_METHOD);
1666 continue;
1667 }
1668
Joe Onorato8a9b2202010-02-26 18:56:32 -08001669 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670
1671 try {
1672 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1673 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001674 final String id = p.getId();
1675 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676
Amith Yamasanie861ec12010-03-24 21:39:27 -07001677 // System IMEs are enabled by default, unless there's a hard keyboard
1678 // and the system IME was explicitly disabled
1679 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1680 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001681 }
1682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001684 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001688 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001690 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 }
1692 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001693
1694 String defaultIme = Settings.Secure.getString(mContext
1695 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001696 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001697 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001698 updateFromSettingsLocked();
1699 }
1700 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001704
satokab751aa2010-09-14 19:17:36 +09001705 private void showInputMethodMenu() {
1706 showInputMethodMenuInternal(false);
1707 }
1708
1709 private void showInputMethodSubtypeMenu() {
1710 showInputMethodMenuInternal(true);
1711 }
1712
satok217f5482010-12-15 05:19:19 +09001713 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001714 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001715 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001716 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1717 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001718 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001719 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001720 }
satok217f5482010-12-15 05:19:19 +09001721 mContext.startActivity(intent);
1722 }
1723
1724 private void showConfigureInputMethods() {
1725 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1726 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1727 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1728 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001729 mContext.startActivity(intent);
1730 }
1731
satokab751aa2010-09-14 19:17:36 +09001732 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001733 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 String lastInputMethodId = Settings.Secure.getString(context
1740 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001741 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001742 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001743
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001744 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09001745 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
1746 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001747 if (immis == null || immis.size() == 0) {
1748 return;
1749 }
1750
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001751 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752
satokab751aa2010-09-14 19:17:36 +09001753 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1754 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001755
satokbb4aa062011-01-19 21:40:27 +09001756 for (InputMethodInfo imi: immis.keySet()) {
1757 if (imi == null) continue;
1758 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09001759 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09001760 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
1761 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09001762 }
satokbb4aa062011-01-19 21:40:27 +09001763 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
1764 CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001765 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09001766 final int subtypeCount = imi.getSubtypeCount();
Ken Wakasa586f0512011-01-20 22:31:01 +09001767 for (int j = 0; j < subtypeCount; ++j) {
satokbb4aa062011-01-19 21:40:27 +09001768 InputMethodSubtype subtype = imi.getSubtypeAt(j);
satok7f35c8c2010-10-07 21:13:11 +09001769 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1770 CharSequence title;
1771 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001772 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001773 if (nameResId != 0) {
satokbb4aa062011-01-19 21:40:27 +09001774 title = pm.getText(imi.getPackageName(), nameResId,
1775 imi.getServiceInfo().applicationInfo);
satok7f35c8c2010-10-07 21:13:11 +09001776 } else {
1777 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001778 // TODO: Use more friendly Title and UI
1779 title = label + "," + (mode == null ? "" : mode) + ","
1780 + (language == null ? "" : language);
1781 }
satokbb4aa062011-01-19 21:40:27 +09001782 imMap.put(title, new Pair<InputMethodInfo, Integer>(imi, j));
satokab751aa2010-09-14 19:17:36 +09001783 }
satokab751aa2010-09-14 19:17:36 +09001784 }
1785 } else {
1786 imMap.put(label,
satokbb4aa062011-01-19 21:40:27 +09001787 new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID));
satokab751aa2010-09-14 19:17:36 +09001788 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001789 }
satok913a8922010-08-26 21:53:41 +09001790
satokbb4aa062011-01-19 21:40:27 +09001791 final int N = imMap.size();
satok913a8922010-08-26 21:53:41 +09001792 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001793 mIms = new InputMethodInfo[N];
1794 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001795 int checkedItem = 0;
1796 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001797 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1798 mIms[i] = value.first;
1799 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001800 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001801 int subtypeId = mSubtypeIds[i];
1802 if ((subtypeId == NOT_A_SUBTYPE_ID)
1803 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1804 || (subtypeId == lastInputMethodSubtypeId)) {
1805 checkedItem = i;
1806 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001807 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 }
satokab751aa2010-09-14 19:17:36 +09001809
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001810 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1811 public void onClick(DialogInterface dialog, int which) {
1812 hideInputMethodMenu();
1813 }
1814 };
satokd87c2592010-09-29 11:52:06 +09001815
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001816 TypedArray a = context.obtainStyledAttributes(null,
1817 com.android.internal.R.styleable.DialogPreference,
1818 com.android.internal.R.attr.alertDialogStyle, 0);
1819 mDialogBuilder = new AlertDialog.Builder(context)
1820 .setTitle(com.android.internal.R.string.select_input_method)
1821 .setOnCancelListener(new OnCancelListener() {
1822 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001824 }
1825 })
1826 .setIcon(a.getDrawable(
1827 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1828 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001829
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001830 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1831 new AlertDialog.OnClickListener() {
1832 public void onClick(DialogInterface dialog, int which) {
1833 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001834 if (mIms == null || mIms.length <= which
1835 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001836 return;
1837 }
1838 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001839 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001840 hideInputMethodMenu();
1841 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001842 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09001843 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09001844 subtypeId = NOT_A_SUBTYPE_ID;
1845 }
1846 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001847 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001850 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851
satok7f35c8c2010-10-07 21:13:11 +09001852 if (showSubtypes) {
satok82beadf2010-12-27 19:03:06 +09001853 mDialogBuilder.setPositiveButton(
1854 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09001855 new DialogInterface.OnClickListener() {
1856 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09001857 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09001858 }
1859 });
1860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 mSwitchingDialog = mDialogBuilder.create();
1862 mSwitchingDialog.getWindow().setType(
1863 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1864 mSwitchingDialog.show();
1865 }
1866 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001869 synchronized (mMethodMap) {
1870 hideInputMethodMenuLocked();
1871 }
1872 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001873
The Android Open Source Project10592532009-03-18 17:39:46 -07001874 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001875 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876
The Android Open Source Project10592532009-03-18 17:39:46 -07001877 if (mSwitchingDialog != null) {
1878 mSwitchingDialog.dismiss();
1879 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001881
The Android Open Source Project10592532009-03-18 17:39:46 -07001882 mDialogBuilder = null;
1883 mItems = null;
1884 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 public boolean setInputMethodEnabled(String id, boolean enabled) {
1890 synchronized (mMethodMap) {
1891 if (mContext.checkCallingOrSelfPermission(
1892 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1893 != PackageManager.PERMISSION_GRANTED) {
1894 throw new SecurityException(
1895 "Requires permission "
1896 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1897 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 long ident = Binder.clearCallingIdentity();
1900 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001901 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 } finally {
1903 Binder.restoreCallingIdentity(ident);
1904 }
1905 }
1906 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001907
1908 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1909 // Make sure this is a valid input method.
1910 InputMethodInfo imm = mMethodMap.get(id);
1911 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001912 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001913 }
1914
satokd87c2592010-09-29 11:52:06 +09001915 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1916 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001917
satokd87c2592010-09-29 11:52:06 +09001918 if (enabled) {
1919 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1920 if (pair.first.equals(id)) {
1921 // We are enabling this input method, but it is already enabled.
1922 // Nothing to do. The previous state was enabled.
1923 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001924 }
1925 }
satokd87c2592010-09-29 11:52:06 +09001926 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1927 // Previous state was disabled.
1928 return false;
1929 } else {
1930 StringBuilder builder = new StringBuilder();
1931 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1932 builder, enabledInputMethodsList, id)) {
1933 // Disabled input method is currently selected, switch to another one.
1934 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1935 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001936 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
1937 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
1938 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09001939 }
1940 // Previous state was enabled.
1941 return true;
1942 } else {
1943 // We are disabling the input method but it is already disabled.
1944 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001945 return false;
1946 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001947 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001948 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001949
satok57ffc002011-01-25 00:11:47 +09001950 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
1951 if (subtype == null) return true;
Ken Wakasaa5e9ffa2011-02-16 11:55:23 +09001952 return !subtype.containsExtraValueKey(SUBTYPE_EXTRAVALUE_EXCLUDE_FROM_LAST_IME);
satok57ffc002011-01-25 00:11:47 +09001953 }
1954
satok723a27e2010-11-11 14:58:11 +09001955 private void saveCurrentInputMethodAndSubtypeToHistory() {
1956 String subtypeId = NOT_A_SUBTYPE_ID_STR;
1957 if (mCurrentSubtype != null) {
1958 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
1959 }
satok57ffc002011-01-25 00:11:47 +09001960 if (canAddToLastInputMethod(mCurrentSubtype)) {
1961 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
1962 }
satokab751aa2010-09-14 19:17:36 +09001963 }
1964
satok723a27e2010-11-11 14:58:11 +09001965 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
1966 boolean setSubtypeOnly) {
satok4d733292011-01-31 22:14:32 +09001967 mOldSystemSettingsVersion = getSystemSettingsVersion();
satok723a27e2010-11-11 14:58:11 +09001968 // Update the history of InputMethod and Subtype
1969 saveCurrentInputMethodAndSubtypeToHistory();
1970
1971 // Set Subtype here
1972 if (imi == null || subtypeId < 0) {
1973 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001974 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09001975 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09001976 if (subtypeId < imi.getSubtypeCount()) {
1977 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
1978 mSettings.putSelectedSubtype(subtype.hashCode());
1979 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09001980 } else {
1981 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
1982 mCurrentSubtype = null;
1983 }
satokab751aa2010-09-14 19:17:36 +09001984 }
satok723a27e2010-11-11 14:58:11 +09001985
1986 if (!setSubtypeOnly) {
1987 // Set InputMethod here
1988 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
1989 }
1990 }
1991
1992 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
1993 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
1994 int lastSubtypeId = NOT_A_SUBTYPE_ID;
1995 // newDefaultIme is empty when there is no candidate for the selected IME.
1996 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
1997 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
1998 if (subtypeHashCode != null) {
1999 try {
2000 lastSubtypeId = getSubtypeIdFromHashCode(
2001 imi, Integer.valueOf(subtypeHashCode));
2002 } catch (NumberFormatException e) {
2003 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2004 }
2005 }
2006 }
2007 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002008 }
2009
2010 private int getSelectedInputMethodSubtypeId(String id) {
2011 InputMethodInfo imi = mMethodMap.get(id);
2012 if (imi == null) {
2013 return NOT_A_SUBTYPE_ID;
2014 }
satokab751aa2010-09-14 19:17:36 +09002015 int subtypeId;
2016 try {
2017 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2018 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2019 } catch (SettingNotFoundException e) {
2020 return NOT_A_SUBTYPE_ID;
2021 }
satok723a27e2010-11-11 14:58:11 +09002022 return getSubtypeIdFromHashCode(imi, subtypeId);
2023 }
2024
2025 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002026 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002027 final int subtypeCount = imi.getSubtypeCount();
2028 for (int i = 0; i < subtypeCount; ++i) {
2029 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002030 if (subtypeHashCode == ims.hashCode()) {
2031 return i;
2032 }
satokab751aa2010-09-14 19:17:36 +09002033 }
2034 }
2035 return NOT_A_SUBTYPE_ID;
2036 }
2037
satokdf31ae62011-01-15 06:19:44 +09002038 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2039 Resources res, List<InputMethodSubtype> subtypes) {
2040 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002041 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2042 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2043 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002044 final int N = subtypes.size();
2045 boolean containsKeyboardSubtype = false;
2046 for (int i = 0; i < N; ++i) {
2047 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002048 final String locale = subtype.getLocale();
2049 final String mode = subtype.getMode();
2050 // When system locale starts with subtype's locale, that subtype will be applicable
2051 // for system locale
2052 // For instance, it's clearly applicable for cases like system locale = en_US and
2053 // subtype = en, but it is not necessarily considered applicable for cases like system
2054 // locale = en and subtype = en_US.
2055 // We just call systemLocale.startsWith(locale) in this function because there is no
2056 // need to find applicable subtypes aggressively unlike
2057 // findLastResortApplicableSubtypeLocked.
2058 if (systemLocale.startsWith(locale)) {
2059 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2060 // If more applicable subtypes are contained, skip.
2061 if (applicableSubtype != null
2062 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2063 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002064 if (!containsKeyboardSubtype
2065 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2066 containsKeyboardSubtype = true;
2067 }
2068 }
2069 }
satok3da92232011-01-11 22:46:30 +09002070 ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2071 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002072 if (!containsKeyboardSubtype) {
2073 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002074 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002075 if (lastResortKeyboardSubtype != null) {
2076 applicableSubtypes.add(lastResortKeyboardSubtype);
2077 }
2078 }
2079 return applicableSubtypes;
2080 }
2081
satok4e4569d2010-11-19 18:45:53 +09002082 /**
2083 * If there are no selected subtypes, tries finding the most applicable one according to the
2084 * given locale.
2085 * @param subtypes this function will search the most applicable subtype in subtypes
2086 * @param mode subtypes will be filtered by mode
2087 * @param locale subtypes will be filtered by locale
satok7599a7fb2010-12-22 13:45:23 +09002088 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2089 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002090 * @return the most applicable subtypeId
2091 */
satokdf31ae62011-01-15 06:19:44 +09002092 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2093 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7fb2010-12-22 13:45:23 +09002094 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002095 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002096 return null;
satok8fbb1e82010-11-02 23:15:58 +09002097 }
satok4e4569d2010-11-19 18:45:53 +09002098 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002099 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002100 }
satok8fbb1e82010-11-02 23:15:58 +09002101 final String language = locale.substring(0, 2);
2102 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002103 InputMethodSubtype applicableSubtype = null;
satok7599a7fb2010-12-22 13:45:23 +09002104 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002105 final int N = subtypes.size();
2106 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002107 InputMethodSubtype subtype = subtypes.get(i);
2108 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002109 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2110 // and all subtypes with all modes can be candidates.
2111 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7fb2010-12-22 13:45:23 +09002112 if (firstMatchedModeSubtype == null) {
2113 firstMatchedModeSubtype = subtype;
2114 }
satok9ef02832010-11-04 21:17:48 +09002115 if (locale.equals(subtypeLocale)) {
2116 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002117 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002118 break;
2119 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2120 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002121 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002122 partialMatchFound = true;
2123 }
satok8fbb1e82010-11-02 23:15:58 +09002124 }
2125 }
2126
satok7599a7fb2010-12-22 13:45:23 +09002127 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2128 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002129 }
2130
satok8fbb1e82010-11-02 23:15:58 +09002131 // The first subtype applicable to the system locale will be defined as the most applicable
2132 // subtype.
2133 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002134 if (applicableSubtype != null) {
2135 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2136 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2137 }
satok8fbb1e82010-11-02 23:15:58 +09002138 }
satokcd7cd292010-11-20 15:46:23 +09002139 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002140 }
2141
satok4e4569d2010-11-19 18:45:53 +09002142 // If there are no selected shortcuts, tries finding the most applicable ones.
2143 private Pair<InputMethodInfo, InputMethodSubtype>
2144 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2145 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2146 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002147 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002148 boolean foundInSystemIME = false;
2149
2150 // Search applicable subtype for each InputMethodInfo
2151 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002152 final String imiId = imi.getId();
2153 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2154 continue;
2155 }
satokcd7cd292010-11-20 15:46:23 +09002156 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002157 final List<InputMethodSubtype> enabledSubtypes =
2158 getEnabledInputMethodSubtypeList(imi, true);
2159 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002160 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002161 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002162 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002163 }
satokdf31ae62011-01-15 06:19:44 +09002164 // 2. Search by the system locale from enabledSubtypes.
2165 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002166 if (subtype == null) {
2167 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002168 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002169 }
satok7599a7fb2010-12-22 13:45:23 +09002170 // 4. Search by the current subtype's locale from all subtypes.
2171 if (subtype == null && mCurrentSubtype != null) {
2172 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002173 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09002174 }
2175 // 5. Search by the system locale from all subtypes.
2176 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002177 if (subtype == null) {
satok7599a7fb2010-12-22 13:45:23 +09002178 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002179 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002180 }
satokcd7cd292010-11-20 15:46:23 +09002181 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09002182 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002183 // The current input method is the most applicable IME.
2184 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002185 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002186 break;
satok7599a7fb2010-12-22 13:45:23 +09002187 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002188 // The system input method is 2nd applicable IME.
2189 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002190 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09002191 if ((imi.getServiceInfo().applicationInfo.flags
2192 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2193 foundInSystemIME = true;
2194 }
satok4e4569d2010-11-19 18:45:53 +09002195 }
2196 }
2197 }
2198 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002199 if (mostApplicableIMI != null) {
2200 Slog.w(TAG, "Most applicable shortcut input method was:"
2201 + mostApplicableIMI.getId());
2202 if (mostApplicableSubtype != null) {
2203 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2204 + "," + mostApplicableSubtype.getMode() + ","
2205 + mostApplicableSubtype.getLocale());
2206 }
2207 }
satok4e4569d2010-11-19 18:45:53 +09002208 }
satokcd7cd292010-11-20 15:46:23 +09002209 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002210 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002211 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002212 } else {
2213 return null;
2214 }
2215 }
2216
satok4d733292011-01-31 22:14:32 +09002217 private static long getSystemSettingsVersion() {
2218 return SystemProperties.getLong(Settings.Secure.SYS_PROP_SETTING_VERSION, 0);
2219 }
2220
satokab751aa2010-09-14 19:17:36 +09002221 /**
2222 * @return Return the current subtype of this input method.
2223 */
2224 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002225 boolean subtypeIsSelected = false;
2226 try {
2227 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2228 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2229 } catch (SettingNotFoundException e) {
2230 }
satok3ef8b292010-11-23 06:06:29 +09002231 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002232 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002233 String lastInputMethodId = Settings.Secure.getString(
2234 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002235 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2236 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002237 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2238 if (imi != null) {
2239 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002240 // the most applicable subtype from explicitly or implicitly enabled
2241 // subtypes.
2242 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2243 getEnabledInputMethodSubtypeList(imi, true);
2244 // If there is only one explicitly or implicitly enabled subtype,
2245 // just returns it.
2246 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2247 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2248 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2249 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2250 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2251 SUBTYPE_MODE_KEYBOARD, null, true);
2252 if (mCurrentSubtype == null) {
2253 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2254 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2255 true);
2256 }
2257 }
satok4e4569d2010-11-19 18:45:53 +09002258 }
satokcd7cd292010-11-20 15:46:23 +09002259 } else {
satok3ef8b292010-11-23 06:06:29 +09002260 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002261 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002262 }
satok8fbb1e82010-11-02 23:15:58 +09002263 }
satok3ef8b292010-11-23 06:06:29 +09002264 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002265 }
satokab751aa2010-09-14 19:17:36 +09002266 }
2267
satokf3db1af2010-11-23 13:34:33 +09002268 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2269 InputMethodSubtype subtype) {
2270 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2271 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2272 } else {
2273 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2274 subtypes.add(subtype);
2275 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2276 }
2277 }
2278
satok4e4569d2010-11-19 18:45:53 +09002279 // TODO: We should change the return type from List to List<Parcelable>
2280 public List getShortcutInputMethodsAndSubtypes() {
2281 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002282 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002283 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002284 // If there are no selected shortcut subtypes, the framework will try to find
2285 // the most applicable subtype from all subtypes whose mode is
2286 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002287 Pair<InputMethodInfo, InputMethodSubtype> info =
2288 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2289 SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09002290 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002291 ret.add(info.first);
2292 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09002293 }
satok3da92232011-01-11 22:46:30 +09002294 return ret;
satokf3db1af2010-11-23 13:34:33 +09002295 }
satokf3db1af2010-11-23 13:34:33 +09002296 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2297 ret.add(imi);
2298 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2299 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002300 }
2301 }
satokf3db1af2010-11-23 13:34:33 +09002302 return ret;
satok4e4569d2010-11-19 18:45:53 +09002303 }
2304 }
2305
satokb66d2872010-11-10 01:04:04 +09002306 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2307 synchronized (mMethodMap) {
2308 if (subtype != null && mCurMethodId != null) {
2309 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2310 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2311 if (subtypeId != NOT_A_SUBTYPE_ID) {
2312 setInputMethodLocked(mCurMethodId, subtypeId);
2313 return true;
2314 }
2315 }
2316 return false;
2317 }
2318 }
2319
satokd87c2592010-09-29 11:52:06 +09002320 /**
2321 * Utility class for putting and getting settings for InputMethod
2322 * TODO: Move all putters and getters of settings to this class.
2323 */
2324 private static class InputMethodSettings {
2325 // The string for enabled input method is saved as follows:
2326 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2327 private static final char INPUT_METHOD_SEPARATER = ':';
2328 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002329 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002330 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2331
satok723a27e2010-11-11 14:58:11 +09002332 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002333 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2334
satokdf31ae62011-01-15 06:19:44 +09002335 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002336 private final ContentResolver mResolver;
2337 private final HashMap<String, InputMethodInfo> mMethodMap;
2338 private final ArrayList<InputMethodInfo> mMethodList;
2339
2340 private String mEnabledInputMethodsStrCache;
2341
2342 private static void buildEnabledInputMethodsSettingString(
2343 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2344 String id = pair.first;
2345 ArrayList<String> subtypes = pair.second;
2346 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002347 // Inputmethod and subtypes are saved in the settings as follows:
2348 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2349 for (String subtypeId: subtypes) {
2350 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002351 }
2352 }
2353
2354 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002355 Resources res, ContentResolver resolver,
2356 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2357 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002358 mResolver = resolver;
2359 mMethodMap = methodMap;
2360 mMethodList = methodList;
2361 }
2362
2363 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2364 return createEnabledInputMethodListLocked(
2365 getEnabledInputMethodsAndSubtypeListLocked());
2366 }
2367
satok7f35c8c2010-10-07 21:13:11 +09002368 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002369 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2370 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002371 getEnabledInputMethodsAndSubtypeListLocked());
2372 }
2373
satok67ddf9c2010-11-17 09:45:54 +09002374 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2375 InputMethodInfo imi) {
2376 List<Pair<String, ArrayList<String>>> imsList =
2377 getEnabledInputMethodsAndSubtypeListLocked();
2378 ArrayList<InputMethodSubtype> enabledSubtypes =
2379 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002380 if (imi != null) {
2381 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2382 InputMethodInfo info = mMethodMap.get(imsPair.first);
2383 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002384 final int subtypeCount = info.getSubtypeCount();
2385 for (int i = 0; i < subtypeCount; ++i) {
2386 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002387 for (String s: imsPair.second) {
2388 if (String.valueOf(ims.hashCode()).equals(s)) {
2389 enabledSubtypes.add(ims);
2390 }
satok67ddf9c2010-11-17 09:45:54 +09002391 }
2392 }
satok884ef9a2010-11-18 10:39:46 +09002393 break;
satok67ddf9c2010-11-17 09:45:54 +09002394 }
satok67ddf9c2010-11-17 09:45:54 +09002395 }
2396 }
2397 return enabledSubtypes;
2398 }
2399
satokd87c2592010-09-29 11:52:06 +09002400 // At the initial boot, the settings for input methods are not set,
2401 // so we need to enable IME in that case.
2402 public void enableAllIMEsIfThereIsNoEnabledIME() {
2403 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2404 StringBuilder sb = new StringBuilder();
2405 final int N = mMethodList.size();
2406 for (int i = 0; i < N; i++) {
2407 InputMethodInfo imi = mMethodList.get(i);
2408 Slog.i(TAG, "Adding: " + imi.getId());
2409 if (i > 0) sb.append(':');
2410 sb.append(imi.getId());
2411 }
2412 putEnabledInputMethodsStr(sb.toString());
2413 }
2414 }
2415
satokbb4aa062011-01-19 21:40:27 +09002416 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002417 ArrayList<Pair<String, ArrayList<String>>> imsList
2418 = new ArrayList<Pair<String, ArrayList<String>>>();
2419 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2420 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2421 return imsList;
2422 }
satok723a27e2010-11-11 14:58:11 +09002423 mInputMethodSplitter.setString(enabledInputMethodsStr);
2424 while (mInputMethodSplitter.hasNext()) {
2425 String nextImsStr = mInputMethodSplitter.next();
2426 mSubtypeSplitter.setString(nextImsStr);
2427 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002428 ArrayList<String> subtypeHashes = new ArrayList<String>();
2429 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002430 String imeId = mSubtypeSplitter.next();
2431 while (mSubtypeSplitter.hasNext()) {
2432 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002433 }
2434 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2435 }
2436 }
2437 return imsList;
2438 }
2439
2440 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2441 if (reloadInputMethodStr) {
2442 getEnabledInputMethodsStr();
2443 }
2444 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2445 // Add in the newly enabled input method.
2446 putEnabledInputMethodsStr(id);
2447 } else {
2448 putEnabledInputMethodsStr(
2449 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2450 }
2451 }
2452
2453 /**
2454 * Build and put a string of EnabledInputMethods with removing specified Id.
2455 * @return the specified id was removed or not.
2456 */
2457 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2458 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2459 boolean isRemoved = false;
2460 boolean needsAppendSeparator = false;
2461 for (Pair<String, ArrayList<String>> ims: imsList) {
2462 String curId = ims.first;
2463 if (curId.equals(id)) {
2464 // We are disabling this input method, and it is
2465 // currently enabled. Skip it to remove from the
2466 // new list.
2467 isRemoved = true;
2468 } else {
2469 if (needsAppendSeparator) {
2470 builder.append(INPUT_METHOD_SEPARATER);
2471 } else {
2472 needsAppendSeparator = true;
2473 }
2474 buildEnabledInputMethodsSettingString(builder, ims);
2475 }
2476 }
2477 if (isRemoved) {
2478 // Update the setting with the new list of input methods.
2479 putEnabledInputMethodsStr(builder.toString());
2480 }
2481 return isRemoved;
2482 }
2483
2484 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2485 List<Pair<String, ArrayList<String>>> imsList) {
2486 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2487 for (Pair<String, ArrayList<String>> ims: imsList) {
2488 InputMethodInfo info = mMethodMap.get(ims.first);
2489 if (info != null) {
2490 res.add(info);
2491 }
2492 }
2493 return res;
2494 }
2495
satok7f35c8c2010-10-07 21:13:11 +09002496 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002497 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002498 List<Pair<String, ArrayList<String>>> imsList) {
2499 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2500 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2501 for (Pair<String, ArrayList<String>> ims : imsList) {
2502 InputMethodInfo info = mMethodMap.get(ims.first);
2503 if (info != null) {
2504 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2505 }
2506 }
2507 return res;
2508 }
2509
satokd87c2592010-09-29 11:52:06 +09002510 private void putEnabledInputMethodsStr(String str) {
2511 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2512 mEnabledInputMethodsStrCache = str;
2513 }
2514
2515 private String getEnabledInputMethodsStr() {
2516 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2517 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002518 if (DEBUG) {
2519 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2520 }
satokd87c2592010-09-29 11:52:06 +09002521 return mEnabledInputMethodsStrCache;
2522 }
satok723a27e2010-11-11 14:58:11 +09002523
2524 private void saveSubtypeHistory(
2525 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2526 StringBuilder builder = new StringBuilder();
2527 boolean isImeAdded = false;
2528 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2529 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2530 newSubtypeId);
2531 isImeAdded = true;
2532 }
2533 for (Pair<String, String> ime: savedImes) {
2534 String imeId = ime.first;
2535 String subtypeId = ime.second;
2536 if (TextUtils.isEmpty(subtypeId)) {
2537 subtypeId = NOT_A_SUBTYPE_ID_STR;
2538 }
2539 if (isImeAdded) {
2540 builder.append(INPUT_METHOD_SEPARATER);
2541 } else {
2542 isImeAdded = true;
2543 }
2544 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2545 subtypeId);
2546 }
2547 // Remove the last INPUT_METHOD_SEPARATER
2548 putSubtypeHistoryStr(builder.toString());
2549 }
2550
2551 public void addSubtypeToHistory(String imeId, String subtypeId) {
2552 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2553 for (Pair<String, String> ime: subtypeHistory) {
2554 if (ime.first.equals(imeId)) {
2555 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002556 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002557 + ime.second);
2558 }
2559 // We should break here
2560 subtypeHistory.remove(ime);
2561 break;
2562 }
2563 }
satokbb4aa062011-01-19 21:40:27 +09002564 if (DEBUG) {
2565 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2566 }
satok723a27e2010-11-11 14:58:11 +09002567 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2568 }
2569
2570 private void putSubtypeHistoryStr(String str) {
2571 if (DEBUG) {
2572 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2573 }
2574 Settings.Secure.putString(
2575 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2576 }
2577
2578 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2579 // Gets the first one from the history
2580 return getLastSubtypeForInputMethodLockedInternal(null);
2581 }
2582
2583 public String getLastSubtypeForInputMethodLocked(String imeId) {
2584 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2585 if (ime != null) {
2586 return ime.second;
2587 } else {
2588 return null;
2589 }
2590 }
2591
2592 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2593 List<Pair<String, ArrayList<String>>> enabledImes =
2594 getEnabledInputMethodsAndSubtypeListLocked();
2595 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2596 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2597 final String imeInTheHistory = imeAndSubtype.first;
2598 // If imeId is empty, returns the first IME and subtype in the history
2599 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2600 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002601 final String subtypeHashCode =
2602 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2603 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002604 if (!TextUtils.isEmpty(subtypeHashCode)) {
2605 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002606 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002607 }
2608 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2609 }
2610 }
2611 }
2612 if (DEBUG) {
2613 Slog.d(TAG, "No enabled IME found in the history");
2614 }
2615 return null;
2616 }
2617
satokdf31ae62011-01-15 06:19:44 +09002618 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002619 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2620 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2621 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002622 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2623 if (explicitlyEnabledSubtypes.size() == 0) {
2624 // If there are no explicitly enabled subtypes, applicable subtypes are
2625 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002626 InputMethodInfo ime = mMethodMap.get(imeId);
2627 // If IME is enabled and no subtypes are enabled, applicable subtypes
2628 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002629 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002630 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002631 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002632 if (implicitlySelectedSubtypes != null) {
2633 final int N = implicitlySelectedSubtypes.size();
2634 for (int i = 0; i < N; ++i) {
2635 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2636 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2637 return subtypeHashCode;
2638 }
2639 }
2640 }
2641 }
2642 } else {
satokf6cafb62011-01-17 16:29:02 +09002643 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002644 if (s.equals(subtypeHashCode)) {
2645 // If both imeId and subtypeId are enabled, return subtypeId.
2646 return s;
2647 }
satok723a27e2010-11-11 14:58:11 +09002648 }
2649 }
2650 // If imeId was enabled but subtypeId was disabled.
2651 return NOT_A_SUBTYPE_ID_STR;
2652 }
2653 }
2654 // If both imeId and subtypeId are disabled, return null
2655 return null;
2656 }
2657
2658 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2659 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2660 final String subtypeHistoryStr = getSubtypeHistoryStr();
2661 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2662 return imsList;
2663 }
2664 mInputMethodSplitter.setString(subtypeHistoryStr);
2665 while (mInputMethodSplitter.hasNext()) {
2666 String nextImsStr = mInputMethodSplitter.next();
2667 mSubtypeSplitter.setString(nextImsStr);
2668 if (mSubtypeSplitter.hasNext()) {
2669 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2670 // The first element is ime id.
2671 String imeId = mSubtypeSplitter.next();
2672 while (mSubtypeSplitter.hasNext()) {
2673 subtypeId = mSubtypeSplitter.next();
2674 break;
2675 }
2676 imsList.add(new Pair<String, String>(imeId, subtypeId));
2677 }
2678 }
2679 return imsList;
2680 }
2681
2682 private String getSubtypeHistoryStr() {
2683 if (DEBUG) {
2684 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2685 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2686 }
2687 return Settings.Secure.getString(
2688 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2689 }
2690
2691 public void putSelectedInputMethod(String imeId) {
2692 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2693 }
2694
2695 public void putSelectedSubtype(int subtypeId) {
2696 Settings.Secure.putInt(
2697 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2698 }
satokd87c2592010-09-29 11:52:06 +09002699 }
2700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 @Override
2704 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2705 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2706 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2709 + Binder.getCallingPid()
2710 + ", uid=" + Binder.getCallingUid());
2711 return;
2712 }
2713
2714 IInputMethod method;
2715 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 synchronized (mMethodMap) {
2720 p.println("Current Input Method Manager state:");
2721 int N = mMethodList.size();
2722 p.println(" Input Methods:");
2723 for (int i=0; i<N; i++) {
2724 InputMethodInfo info = mMethodList.get(i);
2725 p.println(" InputMethod #" + i + ":");
2726 info.dump(p, " ");
2727 }
2728 p.println(" Clients:");
2729 for (ClientState ci : mClients.values()) {
2730 p.println(" Client " + ci + ":");
2731 p.println(" client=" + ci.client);
2732 p.println(" inputContext=" + ci.inputContext);
2733 p.println(" sessionRequested=" + ci.sessionRequested);
2734 p.println(" curSession=" + ci.curSession);
2735 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002736 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002738 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2739 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2741 + " mBoundToMethod=" + mBoundToMethod);
2742 p.println(" mCurToken=" + mCurToken);
2743 p.println(" mCurIntent=" + mCurIntent);
2744 method = mCurMethod;
2745 p.println(" mCurMethod=" + mCurMethod);
2746 p.println(" mEnabledSession=" + mEnabledSession);
2747 p.println(" mShowRequested=" + mShowRequested
2748 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2749 + " mShowForced=" + mShowForced
2750 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002751 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002753
Jeff Brownb88102f2010-09-08 11:49:43 -07002754 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 pw.flush();
2757 try {
2758 client.client.asBinder().dump(fd, args);
2759 } catch (RemoteException e) {
2760 p.println("Input method client dead: " + e);
2761 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002762 } else {
2763 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002765
Jeff Brownb88102f2010-09-08 11:49:43 -07002766 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 pw.flush();
2769 try {
2770 method.asBinder().dump(fd, args);
2771 } catch (RemoteException e) {
2772 p.println("Input method service dead: " + e);
2773 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002774 } else {
2775 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 }
2777 }
2778}