blob: 586d222498b40a149bd763bfc4370bd7bb10e1e6 [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;
63import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070064import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090065import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.text.TextUtils;
67import android.util.EventLog;
satokab751aa2010-09-14 19:17:36 +090068import android.util.Pair;
Joe Onorato8a9b2202010-02-26 18:56:32 -080069import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.util.PrintWriterPrinter;
71import android.util.Printer;
72import android.view.IWindowManager;
73import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputBinding;
76import android.view.inputmethod.InputMethod;
77import android.view.inputmethod.InputMethodInfo;
78import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090079import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
81import java.io.FileDescriptor;
82import java.io.IOException;
83import java.io.PrintWriter;
satok913a8922010-08-26 21:53:41 +090084import java.text.Collator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import java.util.ArrayList;
86import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +090087import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import java.util.List;
satok913a8922010-08-26 21:53:41 +090089import java.util.Map;
90import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92/**
93 * This class provides a system service that manages input methods.
94 */
95public class InputMethodManagerService extends IInputMethodManager.Stub
96 implements ServiceConnection, Handler.Callback {
97 static final boolean DEBUG = false;
98 static final String TAG = "InputManagerService";
99
100 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900101 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900102 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900103 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 static final int MSG_UNBIND_INPUT = 1000;
106 static final int MSG_BIND_INPUT = 1010;
107 static final int MSG_SHOW_SOFT_INPUT = 1020;
108 static final int MSG_HIDE_SOFT_INPUT = 1030;
109 static final int MSG_ATTACH_TOKEN = 1040;
110 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 static final int MSG_START_INPUT = 2000;
113 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 static final int MSG_UNBIND_METHOD = 3000;
116 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800119
satokab751aa2010-09-14 19:17:36 +0900120 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900121 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900122 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
123 private static final String SUBTYPE_MODE_VOICE = "voice";
124
satok57ffc002011-01-25 00:11:47 +0900125 // TODO: Will formalize this value as API
126 private static final String SUBTYPE_EXTRAVALUE_EXCLUDE_FROM_LAST_IME =
127 "excludeFromLastInputMethod";
128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800130 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900132 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700134 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 final IWindowManager mIWindowManager;
136 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 // All known input methods. mMethodMap also serves as the global
141 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900142 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
143 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 class SessionState {
146 final ClientState client;
147 final IInputMethod method;
148 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 @Override
151 public String toString() {
152 return "SessionState{uid " + client.uid + " pid " + client.pid
153 + " method " + Integer.toHexString(
154 System.identityHashCode(method))
155 + " session " + Integer.toHexString(
156 System.identityHashCode(session))
157 + "}";
158 }
159
160 SessionState(ClientState _client, IInputMethod _method,
161 IInputMethodSession _session) {
162 client = _client;
163 method = _method;
164 session = _session;
165 }
166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 class ClientState {
169 final IInputMethodClient client;
170 final IInputContext inputContext;
171 final int uid;
172 final int pid;
173 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 boolean sessionRequested;
176 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 @Override
179 public String toString() {
180 return "ClientState{" + Integer.toHexString(
181 System.identityHashCode(this)) + " uid " + uid
182 + " pid " + pid + "}";
183 }
184
185 ClientState(IInputMethodClient _client, IInputContext _inputContext,
186 int _uid, int _pid) {
187 client = _client;
188 inputContext = _inputContext;
189 uid = _uid;
190 pid = _pid;
191 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
192 }
193 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 final HashMap<IBinder, ClientState> mClients
196 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700199 * Set once the system is ready to run third party code.
200 */
201 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800202
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700203 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 * Id of the currently selected input method.
205 */
206 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 /**
209 * The current binding sequence number, incremented every time there is
210 * a new bind performed.
211 */
212 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 /**
215 * The client that is currently bound to an input method.
216 */
217 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700220 * The last window token that gained focus.
221 */
222 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800223
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700224 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 * The input context last provided by the current client.
226 */
227 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 /**
230 * The attributes last provided by the current client.
231 */
232 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 /**
235 * The input method ID of the input method service that we are currently
236 * connected to or in the process of connecting to.
237 */
238 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 /**
satokab751aa2010-09-14 19:17:36 +0900241 * The current subtype of the current input method.
242 */
243 private InputMethodSubtype mCurrentSubtype;
244
satok4e4569d2010-11-19 18:45:53 +0900245 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900246 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
247 mShortcutInputMethodsAndSubtypes =
248 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900249
250 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 * Set to true if our ServiceConnection is currently actively bound to
252 * a service (whether or not we have gotten its IBinder back yet).
253 */
254 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 /**
257 * Set if the client has asked for the input method to be shown.
258 */
259 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 /**
262 * Set if we were explicitly told to show the input method.
263 */
264 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 /**
267 * Set if we were forced to be shown.
268 */
269 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 /**
272 * Set if we last told the input method to show itself.
273 */
274 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 /**
277 * The Intent used to connect to the current input method.
278 */
279 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 /**
282 * The token we have made for the currently active input method, to
283 * identify it in the future.
284 */
285 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 /**
288 * If non-null, this is the input method service we are currently connected
289 * to.
290 */
291 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 /**
294 * Time that we last initiated a bind to the input method, to determine
295 * if we should try to disconnect and reconnect to it.
296 */
297 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * Have we called mCurMethod.bindInput()?
301 */
302 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 /**
305 * Currently enabled session. Only touched by service thread, not
306 * protected by a lock.
307 */
308 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 /**
311 * True if the screen is on. The value is true initially.
312 */
313 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800314
Joe Onorato857fd9b2011-01-27 15:08:35 -0800315 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
316 int mImeWindowVis;
317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 AlertDialog.Builder mDialogBuilder;
319 AlertDialog mSwitchingDialog;
320 InputMethodInfo[] mIms;
321 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900322 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 class SettingsObserver extends ContentObserver {
325 SettingsObserver(Handler handler) {
326 super(handler);
327 ContentResolver resolver = mContext.getContentResolver();
328 resolver.registerContentObserver(Settings.Secure.getUriFor(
329 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900330 resolver.registerContentObserver(Settings.Secure.getUriFor(
331 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 @Override public void onChange(boolean selfChange) {
335 synchronized (mMethodMap) {
336 updateFromSettingsLocked();
337 }
338 }
339 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
342 @Override
343 public void onReceive(Context context, Intent intent) {
344 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
345 mScreenOn = true;
346 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
347 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700348 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
349 hideInputMethodMenu();
350 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800352 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 }
354
355 // Inform the current client of the change in active status
356 try {
357 if (mCurClient != null && mCurClient.client != null) {
358 mCurClient.client.setActive(mScreenOn);
359 }
360 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800361 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 + mCurClient.pid + " uid " + mCurClient.uid);
363 }
364 }
365 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800366
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800367 class MyPackageMonitor extends PackageMonitor {
368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800370 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800372 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
374 final int N = mMethodList.size();
375 if (curInputMethodId != null) {
376 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800377 InputMethodInfo imi = mMethodList.get(i);
378 if (imi.getId().equals(curInputMethodId)) {
379 for (String pkg : packages) {
380 if (imi.getPackageName().equals(pkg)) {
381 if (!doit) {
382 return true;
383 }
satok723a27e2010-11-11 14:58:11 +0900384 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800385 chooseNewDefaultIMELocked();
386 return true;
387 }
388 }
389 }
390 }
391 }
392 }
393 return false;
394 }
395
396 @Override
397 public void onSomePackagesChanged() {
398 synchronized (mMethodMap) {
399 InputMethodInfo curIm = null;
400 String curInputMethodId = Settings.Secure.getString(mContext
401 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
402 final int N = mMethodList.size();
403 if (curInputMethodId != null) {
404 for (int i=0; i<N; i++) {
405 InputMethodInfo imi = mMethodList.get(i);
406 if (imi.getId().equals(curInputMethodId)) {
407 curIm = imi;
408 }
409 int change = isPackageDisappearing(imi.getPackageName());
410 if (change == PACKAGE_TEMPORARY_CHANGE
411 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800412 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800413 + imi.getComponent());
414 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 }
416 }
417 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800418
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800419 buildInputMethodListLocked(mMethodList, mMethodMap);
420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800422
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800423 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800424 int change = isPackageDisappearing(curIm.getPackageName());
425 if (change == PACKAGE_TEMPORARY_CHANGE
426 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800427 ServiceInfo si = null;
428 try {
429 si = mContext.getPackageManager().getServiceInfo(
430 curIm.getComponent(), 0);
431 } catch (PackageManager.NameNotFoundException ex) {
432 }
433 if (si == null) {
434 // Uh oh, current input method is no longer around!
435 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800436 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800437 mImeWindowVis = 0;
438 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
439 mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800440 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800441 changed = true;
442 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800443 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900444 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800445 }
446 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800447 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800448 }
satokab751aa2010-09-14 19:17:36 +0900449
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800450 if (curIm == null) {
451 // We currently don't have a default input method... is
452 // one now available?
453 changed = chooseNewDefaultIMELocked();
454 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800455
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800456 if (changed) {
457 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 }
459 }
460 }
461 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 class MethodCallback extends IInputMethodCallback.Stub {
464 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 MethodCallback(IInputMethod method) {
467 mMethod = method;
468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 public void finishedEvent(int seq, boolean handled) throws RemoteException {
471 }
472
473 public void sessionCreated(IInputMethodSession session) throws RemoteException {
474 onSessionCreated(mMethod, session);
475 }
476 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800477
Joe Onorato089de882010-04-12 08:18:45 -0700478 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800480 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 mHandler = new Handler(this);
482 mIWindowManager = IWindowManager.Stub.asInterface(
483 ServiceManager.getService(Context.WINDOW_SERVICE));
484 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
485 public void executeMessage(Message msg) {
486 handleMessage(msg);
487 }
488 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800489
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800490 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 IntentFilter screenOnOffFilt = new IntentFilter();
493 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
494 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700495 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800497
satok913a8922010-08-26 21:53:41 +0900498 mStatusBar = statusBar;
499 statusBar.setIconVisibility("ime", false);
500
satokd87c2592010-09-29 11:52:06 +0900501 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900502 mSettings = new InputMethodSettings(
503 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900505 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506
satokd87c2592010-09-29 11:52:06 +0900507 if (TextUtils.isEmpty(Settings.Secure.getString(
508 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900510 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
512 try {
satokd87c2592010-09-29 11:52:06 +0900513 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 imi.getPackageName(), 0).getResources();
515 if (res.getBoolean(imi.getIsDefaultResourceId())) {
516 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800517 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 }
519 } catch (PackageManager.NameNotFoundException ex) {
520 } catch (Resources.NotFoundException ex) {
521 }
522 }
523 }
satokd87c2592010-09-29 11:52:06 +0900524 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800526 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900529 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 }
531 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 mSettingsObserver = new SettingsObserver(mHandler);
534 updateFromSettingsLocked();
535 }
536
537 @Override
538 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
539 throws RemoteException {
540 try {
541 return super.onTransact(code, data, reply, flags);
542 } catch (RuntimeException e) {
543 // The input method manager only throws security exceptions, so let's
544 // log all others.
545 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800546 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 }
548 throw e;
549 }
550 }
551
552 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700553 synchronized (mMethodMap) {
554 if (!mSystemReady) {
555 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700556 try {
557 startInputInnerLocked();
558 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800559 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700560 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700561 }
562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 public List<InputMethodInfo> getInputMethodList() {
566 synchronized (mMethodMap) {
567 return new ArrayList<InputMethodInfo>(mMethodList);
568 }
569 }
570
571 public List<InputMethodInfo> getEnabledInputMethodList() {
572 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900573 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 }
575 }
576
satokbb4aa062011-01-19 21:40:27 +0900577 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
578 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
579 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
580 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
581 for (InputMethodInfo imi: getEnabledInputMethodList()) {
582 enabledInputMethodAndSubtypes.put(
583 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
584 }
585 return enabledInputMethodAndSubtypes;
586 }
587
588 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
589 boolean allowsImplicitlySelectedSubtypes) {
590 if (imi == null && mCurMethodId != null) {
591 imi = mMethodMap.get(mCurMethodId);
592 }
593 final List<InputMethodSubtype> enabledSubtypes =
594 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
595 if (!allowsImplicitlySelectedSubtypes || enabledSubtypes.size() > 0) {
596 return enabledSubtypes;
597 } else {
598 return getApplicableSubtypesLocked(mRes, getSubtypes(imi));
599 }
600 }
601
satok16331c82010-12-20 23:48:46 +0900602 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
603 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900604 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900605 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900606 }
607 }
608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 public void addClient(IInputMethodClient client,
610 IInputContext inputContext, int uid, int pid) {
611 synchronized (mMethodMap) {
612 mClients.put(client.asBinder(), new ClientState(client,
613 inputContext, uid, pid));
614 }
615 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 public void removeClient(IInputMethodClient client) {
618 synchronized (mMethodMap) {
619 mClients.remove(client.asBinder());
620 }
621 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 void executeOrSendMessage(IInterface target, Message msg) {
624 if (target.asBinder() instanceof Binder) {
625 mCaller.sendMessage(msg);
626 } else {
627 handleMessage(msg);
628 msg.recycle();
629 }
630 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800631
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700632 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800634 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 + mCurClient.client.asBinder());
636 if (mBoundToMethod) {
637 mBoundToMethod = false;
638 if (mCurMethod != null) {
639 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
640 MSG_UNBIND_INPUT, mCurMethod));
641 }
642 }
643 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
644 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
645 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 // Call setActive(false) on the old client
648 try {
649 mCurClient.client.setActive(false);
650 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800651 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 + mCurClient.pid + " uid " + mCurClient.uid);
653 }
654 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800655
The Android Open Source Project10592532009-03-18 17:39:46 -0700656 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 }
658 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 private int getImeShowFlags() {
661 int flags = 0;
662 if (mShowForced) {
663 flags |= InputMethod.SHOW_FORCED
664 | InputMethod.SHOW_EXPLICIT;
665 } else if (mShowExplicitlyRequested) {
666 flags |= InputMethod.SHOW_EXPLICIT;
667 }
668 return flags;
669 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 private int getAppShowFlags() {
672 int flags = 0;
673 if (mShowForced) {
674 flags |= InputMethodManager.SHOW_FORCED;
675 } else if (!mShowExplicitlyRequested) {
676 flags |= InputMethodManager.SHOW_IMPLICIT;
677 }
678 return flags;
679 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
682 if (!mBoundToMethod) {
683 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
684 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
685 mBoundToMethod = true;
686 }
687 final SessionState session = mCurClient.curSession;
688 if (initial) {
689 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
690 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
691 } else {
692 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
693 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
694 }
695 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800696 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800697 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 }
699 return needResult
700 ? new InputBindResult(session.session, mCurId, mCurSeq)
701 : null;
702 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 InputBindResult startInputLocked(IInputMethodClient client,
705 IInputContext inputContext, EditorInfo attribute,
706 boolean initial, boolean needResult) {
707 // If no method is currently selected, do nothing.
708 if (mCurMethodId == null) {
709 return mNoBinding;
710 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 ClientState cs = mClients.get(client.asBinder());
713 if (cs == null) {
714 throw new IllegalArgumentException("unknown client "
715 + client.asBinder());
716 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 try {
719 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
720 // Check with the window manager to make sure this client actually
721 // has a window with focus. If not, reject. This is thread safe
722 // because if the focus changes some time before or after, the
723 // next client receiving focus that has any interest in input will
724 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800725 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
727 return null;
728 }
729 } catch (RemoteException e) {
730 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 if (mCurClient != cs) {
733 // If the client is changing, we need to switch over to the new
734 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700735 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800736 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 + cs.client.asBinder());
738
739 // If the screen is on, inform the new client it is active
740 if (mScreenOn) {
741 try {
742 cs.client.setActive(mScreenOn);
743 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800744 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 + cs.pid + " uid " + cs.uid);
746 }
747 }
748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 // Bump up the sequence for this client and attach it.
751 mCurSeq++;
752 if (mCurSeq <= 0) mCurSeq = 1;
753 mCurClient = cs;
754 mCurInputContext = inputContext;
755 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 // Check if the input method is changing.
758 if (mCurId != null && mCurId.equals(mCurMethodId)) {
759 if (cs.curSession != null) {
760 // Fast case: if we are already connected to the input method,
761 // then just return it.
762 return attachNewInputLocked(initial, needResult);
763 }
764 if (mHaveConnection) {
765 if (mCurMethod != null) {
766 if (!cs.sessionRequested) {
767 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800768 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
770 MSG_CREATE_SESSION, mCurMethod,
771 new MethodCallback(mCurMethod)));
772 }
773 // Return to client, and we will get back with it when
774 // we have had a session made for it.
775 return new InputBindResult(null, mCurId, mCurSeq);
776 } else if (SystemClock.uptimeMillis()
777 < (mLastBindTime+TIME_TO_RECONNECT)) {
778 // In this case we have connected to the service, but
779 // don't yet have its interface. If it hasn't been too
780 // long since we did the connection, we'll return to
781 // the client and wait to get the service interface so
782 // we can report back. If it has been too long, we want
783 // to fall through so we can try a disconnect/reconnect
784 // to see if we can get back in touch with the service.
785 return new InputBindResult(null, mCurId, mCurSeq);
786 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800787 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
788 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 }
790 }
791 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800792
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700793 return startInputInnerLocked();
794 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800795
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700796 InputBindResult startInputInnerLocked() {
797 if (mCurMethodId == null) {
798 return mNoBinding;
799 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800800
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700801 if (!mSystemReady) {
802 // If the system is not yet ready, we shouldn't be running third
803 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700804 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700805 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 InputMethodInfo info = mMethodMap.get(mCurMethodId);
808 if (info == null) {
809 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
810 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800811
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700812 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
815 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700816 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
817 com.android.internal.R.string.input_method_binding_label);
818 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
819 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
821 mLastBindTime = SystemClock.uptimeMillis();
822 mHaveConnection = true;
823 mCurId = info.getId();
824 mCurToken = new Binder();
825 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800826 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 mIWindowManager.addWindowToken(mCurToken,
828 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
829 } catch (RemoteException e) {
830 }
831 return new InputBindResult(null, mCurId, mCurSeq);
832 } else {
833 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800834 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 + mCurIntent);
836 }
837 return null;
838 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 public InputBindResult startInput(IInputMethodClient client,
841 IInputContext inputContext, EditorInfo attribute,
842 boolean initial, boolean needResult) {
843 synchronized (mMethodMap) {
844 final long ident = Binder.clearCallingIdentity();
845 try {
846 return startInputLocked(client, inputContext, attribute,
847 initial, needResult);
848 } finally {
849 Binder.restoreCallingIdentity(ident);
850 }
851 }
852 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 public void finishInput(IInputMethodClient client) {
855 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 public void onServiceConnected(ComponentName name, IBinder service) {
858 synchronized (mMethodMap) {
859 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
860 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700861 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800862 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700863 unbindCurrentMethodLocked(false);
864 return;
865 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800866 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700867 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
868 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800870 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700871 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700873 MSG_CREATE_SESSION, mCurMethod,
874 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 }
876 }
877 }
878 }
879
880 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
881 synchronized (mMethodMap) {
882 if (mCurMethod != null && method != null
883 && mCurMethod.asBinder() == method.asBinder()) {
884 if (mCurClient != null) {
885 mCurClient.curSession = new SessionState(mCurClient,
886 method, session);
887 mCurClient.sessionRequested = false;
888 InputBindResult res = attachNewInputLocked(true, true);
889 if (res.method != null) {
890 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
891 MSG_BIND_METHOD, mCurClient.client, res));
892 }
893 }
894 }
895 }
896 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800897
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700898 void unbindCurrentMethodLocked(boolean reportToClient) {
899 if (mHaveConnection) {
900 mContext.unbindService(this);
901 mHaveConnection = false;
902 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800903
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700904 if (mCurToken != null) {
905 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800906 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700907 mIWindowManager.removeWindowToken(mCurToken);
908 } catch (RemoteException e) {
909 }
910 mCurToken = null;
911 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800912
The Android Open Source Project10592532009-03-18 17:39:46 -0700913 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700914 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800915
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700916 if (reportToClient && mCurClient != null) {
917 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
918 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
919 }
920 }
921
Devin Taylor0c33ed22010-02-23 13:26:46 -0600922 private void finishSession(SessionState sessionState) {
923 if (sessionState != null && sessionState.session != null) {
924 try {
925 sessionState.session.finishSession();
926 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700927 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600928 }
929 }
930 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800931
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700932 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 if (mCurMethod != null) {
934 for (ClientState cs : mClients.values()) {
935 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600936 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 cs.curSession = null;
938 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600939
940 finishSession(mEnabledSession);
941 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 mCurMethod = null;
943 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700944 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 public void onServiceDisconnected(ComponentName name) {
948 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800949 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 + " mCurIntent=" + mCurIntent);
951 if (mCurMethod != null && mCurIntent != null
952 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700953 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 // We consider this to be a new bind attempt, since the system
955 // should now try to restart the service for us.
956 mLastBindTime = SystemClock.uptimeMillis();
957 mShowRequested = mInputShown;
958 mInputShown = false;
959 if (mCurClient != null) {
960 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
961 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
962 }
963 }
964 }
965 }
966
967 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700968 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 long ident = Binder.clearCallingIdentity();
970 try {
971 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700972 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 return;
974 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 synchronized (mMethodMap) {
977 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800978 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700979 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800981 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700982 mStatusBar.setIcon("ime", packageName, iconId, 0);
983 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 }
985 }
986 } finally {
987 Binder.restoreCallingIdentity(ident);
988 }
989 }
990
Joe Onorato857fd9b2011-01-27 15:08:35 -0800991 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +0900992 int uid = Binder.getCallingUid();
993 long ident = Binder.clearCallingIdentity();
994 try {
995 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -0800996 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +0900997 return;
998 }
999
1000 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001001 mImeWindowVis = vis;
1002 mBackDisposition = backDisposition;
1003 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
satok06487a52010-10-29 11:37:18 +09001004 }
1005 } finally {
1006 Binder.restoreCallingIdentity(ident);
1007 }
1008 }
1009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001011 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1012 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1013 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1014 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001016 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001017 // There is no input method selected, try to choose new applicable input method.
1018 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1019 id = Settings.Secure.getString(mContext.getContentResolver(),
1020 Settings.Secure.DEFAULT_INPUT_METHOD);
1021 }
1022 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 try {
satokab751aa2010-09-14 19:17:36 +09001024 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001026 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001027 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001028 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 }
satokf3db1af2010-11-23 13:34:33 +09001030 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001031 } else {
1032 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001033 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001034 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 }
1036 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001037
satokab751aa2010-09-14 19:17:36 +09001038 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 InputMethodInfo info = mMethodMap.get(id);
1040 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001041 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001045 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001046 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1047 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001048 }
1049 if (subtype != mCurrentSubtype) {
1050 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001051 if (subtype != null) {
1052 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1053 }
satokcd7cd292010-11-20 15:46:23 +09001054 if (mCurMethod != null) {
1055 try {
satoke40dea02011-01-30 01:14:02 +09001056 final Configuration conf = mRes.getConfiguration();
1057 final boolean haveHardKeyboard = conf.keyboard
1058 != Configuration.KEYBOARD_NOKEYS;
1059 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001060 && conf.hardKeyboardHidden
1061 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001062 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1063 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1064 : 0;
Joe Onorato857fd9b2011-01-27 15:08:35 -08001065 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1066 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001067 // If subtype is null, try to find the most applicable one from
1068 // getCurrentInputMethodSubtype.
1069 if (subtype == null) {
1070 subtype = getCurrentInputMethodSubtype();
1071 }
1072 mCurMethod.changeInputMethodSubtype(subtype);
1073 } catch (RemoteException e) {
1074 return;
satokab751aa2010-09-14 19:17:36 +09001075 }
1076 }
1077 }
1078 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 return;
1080 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 final long ident = Binder.clearCallingIdentity();
1083 try {
satokab751aa2010-09-14 19:17:36 +09001084 // Set a subtype to this input method.
1085 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001086 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1087 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1088 // because mCurMethodId is stored as a history in
1089 // setSelectedInputMethodAndSubtypeLocked().
1090 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091
1092 if (ActivityManagerNative.isSystemReady()) {
1093 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001094 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 intent.putExtra("input_method_id", id);
1096 mContext.sendBroadcast(intent);
1097 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001098 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 } finally {
1100 Binder.restoreCallingIdentity(ident);
1101 }
1102 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001103
The Android Open Source Project4df24232009-03-05 14:34:35 -08001104 public boolean showSoftInput(IInputMethodClient client, int flags,
1105 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001106 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 long ident = Binder.clearCallingIdentity();
1108 try {
1109 synchronized (mMethodMap) {
1110 if (mCurClient == null || client == null
1111 || mCurClient.client.asBinder() != client.asBinder()) {
1112 try {
1113 // We need to check if this is the current client with
1114 // focus in the window manager, to allow this call to
1115 // be made before input is started in it.
1116 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001117 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001118 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 }
1120 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001121 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 }
1123 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001124
Joe Onorato8a9b2202010-02-26 18:56:32 -08001125 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001126 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 }
1128 } finally {
1129 Binder.restoreCallingIdentity(ident);
1130 }
1131 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001132
The Android Open Source Project4df24232009-03-05 14:34:35 -08001133 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 mShowRequested = true;
1135 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1136 mShowExplicitlyRequested = true;
1137 }
1138 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1139 mShowExplicitlyRequested = true;
1140 mShowForced = true;
1141 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001142
Dianne Hackborncc278702009-09-02 23:07:23 -07001143 if (!mSystemReady) {
1144 return false;
1145 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001146
The Android Open Source Project4df24232009-03-05 14:34:35 -08001147 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001149 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1150 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1151 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001153 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 } else if (mHaveConnection && SystemClock.uptimeMillis()
1155 < (mLastBindTime+TIME_TO_RECONNECT)) {
1156 // The client has asked to have the input method shown, but
1157 // we have been sitting here too long with a connection to the
1158 // service and no interface received, so let's disconnect/connect
1159 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001160 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 SystemClock.uptimeMillis()-mLastBindTime,1);
1162 mContext.unbindService(this);
1163 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1164 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001165
The Android Open Source Project4df24232009-03-05 14:34:35 -08001166 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001168
The Android Open Source Project4df24232009-03-05 14:34:35 -08001169 public boolean hideSoftInput(IInputMethodClient client, int flags,
1170 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001171 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 long ident = Binder.clearCallingIdentity();
1173 try {
1174 synchronized (mMethodMap) {
1175 if (mCurClient == null || client == null
1176 || mCurClient.client.asBinder() != client.asBinder()) {
1177 try {
1178 // We need to check if this is the current client with
1179 // focus in the window manager, to allow this call to
1180 // be made before input is started in it.
1181 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001182 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1183 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001184 mImeWindowVis = 0;
1185 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1186 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001187 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 }
1189 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001190 mImeWindowVis = 0;
1191 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001192 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 }
1194 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001195
Joe Onorato8a9b2202010-02-26 18:56:32 -08001196 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001197 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 }
1199 } finally {
1200 Binder.restoreCallingIdentity(ident);
1201 }
1202 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001203
The Android Open Source Project4df24232009-03-05 14:34:35 -08001204 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1206 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001207 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001209 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 }
1211 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001212 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001214 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001216 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001218 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1219 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1220 res = true;
1221 } else {
1222 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 }
1224 mInputShown = false;
1225 mShowRequested = false;
1226 mShowExplicitlyRequested = false;
1227 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001228 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001230
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001231 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1233 boolean first, int windowFlags) {
1234 long ident = Binder.clearCallingIdentity();
1235 try {
1236 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001237 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 + " viewHasFocus=" + viewHasFocus
1239 + " isTextEditor=" + isTextEditor
1240 + " softInputMode=#" + Integer.toHexString(softInputMode)
1241 + " first=" + first + " flags=#"
1242 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 if (mCurClient == null || client == null
1245 || mCurClient.client.asBinder() != client.asBinder()) {
1246 try {
1247 // We need to check if this is the current client with
1248 // focus in the window manager, to allow this call to
1249 // be made before input is started in it.
1250 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001251 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 return;
1253 }
1254 } catch (RemoteException e) {
1255 }
1256 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001257
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001258 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001259 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001260 return;
1261 }
1262 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001263
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001264 // Should we auto-show the IME even if the caller has not
1265 // specified what should be done with it?
1266 // We only do this automatically if the window can resize
1267 // to accommodate the IME (so what the user sees will give
1268 // them good context without input information being obscured
1269 // by the IME) or if running on a large screen where there
1270 // is more room for the target window + IME.
1271 final boolean doAutoShow =
1272 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1273 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1274 || mRes.getConfiguration().isLayoutSizeAtLeast(
1275 Configuration.SCREENLAYOUT_SIZE_LARGE);
1276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1278 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001279 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1281 // There is no focus view, and this window will
1282 // be behind any soft input window, so hide the
1283 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001284 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001285 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001287 } else if (isTextEditor && doAutoShow && (softInputMode &
1288 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 // There is a focus view, and we are navigating forward
1290 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001291 // We only do this automatically if the window an resize
1292 // to accomodate the IME (so what the user sees will give
1293 // them good context without input information being obscured
1294 // by the IME) or if running on a large screen where there
1295 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001296 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001297 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 }
1299 break;
1300 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1301 // Do nothing.
1302 break;
1303 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1304 if ((softInputMode &
1305 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001306 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001307 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 }
1309 break;
1310 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001311 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001312 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 break;
1314 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1315 if ((softInputMode &
1316 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001317 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001318 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 }
1320 break;
1321 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001322 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001323 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 break;
1325 }
1326 }
1327 } finally {
1328 Binder.restoreCallingIdentity(ident);
1329 }
1330 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1333 synchronized (mMethodMap) {
1334 if (mCurClient == null || client == null
1335 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001336 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001337 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 }
1339
satok440aab52010-11-25 09:43:11 +09001340 // Always call subtype picker, because subtype picker is a superset of input method
1341 // picker.
satokab751aa2010-09-14 19:17:36 +09001342 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1343 }
1344 }
1345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001347 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1348 }
1349
1350 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1351 synchronized (mMethodMap) {
1352 if (subtype != null) {
1353 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1354 mMethodMap.get(id), subtype.hashCode()));
1355 } else {
1356 setInputMethod(token, id);
1357 }
1358 }
satokab751aa2010-09-14 19:17:36 +09001359 }
1360
satokb416a71e2010-11-25 20:42:14 +09001361 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001362 IInputMethodClient client, String inputMethodId) {
satokb416a71e2010-11-25 20:42:14 +09001363 synchronized (mMethodMap) {
1364 if (mCurClient == null || client == null
1365 || mCurClient.client.asBinder() != client.asBinder()) {
1366 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1367 }
satok7fee71f2010-12-17 18:54:26 +09001368 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1369 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09001370 }
1371 }
1372
satok735cf382010-11-11 20:40:09 +09001373 public boolean switchToLastInputMethod(IBinder token) {
1374 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001375 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1376 if (lastIme == null) return false;
1377 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1378 if (lastImi == null) return false;
1379
1380 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1381 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1382 // If the last IME is the same as the current IME and the last subtype is not defined,
1383 // there is no need to switch to the last IME.
1384 if (imiIdIsSame && lastSubtypeHash == NOT_A_SUBTYPE_ID) return false;
1385
1386 int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1387 : mCurrentSubtype.hashCode();
1388 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1389 if (DEBUG) {
1390 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second + ", from: "
1391 + mCurMethodId + ", " + currentSubtypeHash);
satok735cf382010-11-11 20:40:09 +09001392 }
satokc445bcd2011-01-25 18:57:24 +09001393 setInputMethodWithSubtypeId(token, lastIme.first, getSubtypeIdFromHashCode(
1394 lastImi, lastSubtypeHash));
1395 return true;
satok735cf382010-11-11 20:40:09 +09001396 }
1397 return false;
1398 }
1399 }
1400
satok28203512010-11-24 11:06:49 +09001401 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 synchronized (mMethodMap) {
1403 if (token == null) {
1404 if (mContext.checkCallingOrSelfPermission(
1405 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1406 != PackageManager.PERMISSION_GRANTED) {
1407 throw new SecurityException(
1408 "Using null token requires permission "
1409 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1410 }
1411 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001412 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1413 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 return;
1415 }
1416
1417 long ident = Binder.clearCallingIdentity();
1418 try {
satokab751aa2010-09-14 19:17:36 +09001419 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 } finally {
1421 Binder.restoreCallingIdentity(ident);
1422 }
1423 }
1424 }
1425
1426 public void hideMySoftInput(IBinder token, int flags) {
1427 synchronized (mMethodMap) {
1428 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001429 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1430 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 return;
1432 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 long ident = Binder.clearCallingIdentity();
1434 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001435 hideCurrentInputLocked(flags, null);
1436 } finally {
1437 Binder.restoreCallingIdentity(ident);
1438 }
1439 }
1440 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001441
The Android Open Source Project4df24232009-03-05 14:34:35 -08001442 public void showMySoftInput(IBinder token, int flags) {
1443 synchronized (mMethodMap) {
1444 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001445 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1446 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001447 return;
1448 }
1449 long ident = Binder.clearCallingIdentity();
1450 try {
1451 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 } finally {
1453 Binder.restoreCallingIdentity(ident);
1454 }
1455 }
1456 }
1457
1458 void setEnabledSessionInMainThread(SessionState session) {
1459 if (mEnabledSession != session) {
1460 if (mEnabledSession != null) {
1461 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001462 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 mEnabledSession.method.setSessionEnabled(
1464 mEnabledSession.session, false);
1465 } catch (RemoteException e) {
1466 }
1467 }
1468 mEnabledSession = session;
1469 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001470 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 session.method.setSessionEnabled(
1472 session.session, true);
1473 } catch (RemoteException e) {
1474 }
1475 }
1476 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 public boolean handleMessage(Message msg) {
1479 HandlerCaller.SomeArgs args;
1480 switch (msg.what) {
1481 case MSG_SHOW_IM_PICKER:
1482 showInputMethodMenu();
1483 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001484
satokab751aa2010-09-14 19:17:36 +09001485 case MSG_SHOW_IM_SUBTYPE_PICKER:
1486 showInputMethodSubtypeMenu();
1487 return true;
1488
satok47a44912010-10-06 16:03:58 +09001489 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001490 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001491 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001492 return true;
1493
1494 case MSG_SHOW_IM_CONFIG:
1495 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001496 return true;
1497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 case MSG_UNBIND_INPUT:
1501 try {
1502 ((IInputMethod)msg.obj).unbindInput();
1503 } catch (RemoteException e) {
1504 // There is nothing interesting about the method dying.
1505 }
1506 return true;
1507 case MSG_BIND_INPUT:
1508 args = (HandlerCaller.SomeArgs)msg.obj;
1509 try {
1510 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1511 } catch (RemoteException e) {
1512 }
1513 return true;
1514 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001515 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001517 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1518 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 } catch (RemoteException e) {
1520 }
1521 return true;
1522 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001523 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001525 ((IInputMethod)args.arg1).hideSoftInput(0,
1526 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 } catch (RemoteException e) {
1528 }
1529 return true;
1530 case MSG_ATTACH_TOKEN:
1531 args = (HandlerCaller.SomeArgs)msg.obj;
1532 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001533 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1535 } catch (RemoteException e) {
1536 }
1537 return true;
1538 case MSG_CREATE_SESSION:
1539 args = (HandlerCaller.SomeArgs)msg.obj;
1540 try {
1541 ((IInputMethod)args.arg1).createSession(
1542 (IInputMethodCallback)args.arg2);
1543 } catch (RemoteException e) {
1544 }
1545 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 case MSG_START_INPUT:
1549 args = (HandlerCaller.SomeArgs)msg.obj;
1550 try {
1551 SessionState session = (SessionState)args.arg1;
1552 setEnabledSessionInMainThread(session);
1553 session.method.startInput((IInputContext)args.arg2,
1554 (EditorInfo)args.arg3);
1555 } catch (RemoteException e) {
1556 }
1557 return true;
1558 case MSG_RESTART_INPUT:
1559 args = (HandlerCaller.SomeArgs)msg.obj;
1560 try {
1561 SessionState session = (SessionState)args.arg1;
1562 setEnabledSessionInMainThread(session);
1563 session.method.restartInput((IInputContext)args.arg2,
1564 (EditorInfo)args.arg3);
1565 } catch (RemoteException e) {
1566 }
1567 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 case MSG_UNBIND_METHOD:
1572 try {
1573 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1574 } catch (RemoteException e) {
1575 // There is nothing interesting about the last client dying.
1576 }
1577 return true;
1578 case MSG_BIND_METHOD:
1579 args = (HandlerCaller.SomeArgs)msg.obj;
1580 try {
1581 ((IInputMethodClient)args.arg1).onBindMethod(
1582 (InputBindResult)args.arg2);
1583 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001584 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 }
1586 return true;
1587 }
1588 return false;
1589 }
1590
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001591 private boolean isSystemIme(InputMethodInfo inputMethod) {
1592 return (inputMethod.getServiceInfo().applicationInfo.flags
1593 & ApplicationInfo.FLAG_SYSTEM) != 0;
1594 }
1595
Ken Wakasa586f0512011-01-20 22:31:01 +09001596 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1597 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1598 final int subtypeCount = imi.getSubtypeCount();
1599 for (int i = 0; i < subtypeCount; ++i) {
1600 subtypes.add(imi.getSubtypeAt(i));
1601 }
1602 return subtypes;
1603 }
1604
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001605 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001606 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001607 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001608 // We'd prefer to fall back on a system IME, since that is safer.
1609 int i=enabled.size();
1610 while (i > 0) {
1611 i--;
1612 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1613 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1614 break;
1615 }
1616 }
satokab751aa2010-09-14 19:17:36 +09001617 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001618 if (DEBUG) {
1619 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1620 }
satok723a27e2010-11-11 14:58:11 +09001621 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001622 return true;
1623 }
1624
1625 return false;
1626 }
1627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1629 HashMap<String, InputMethodInfo> map) {
1630 list.clear();
1631 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001634 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001635 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1636 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1637 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1638 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639
1640 List<ResolveInfo> services = pm.queryIntentServices(
1641 new Intent(InputMethod.SERVICE_INTERFACE),
1642 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 for (int i = 0; i < services.size(); ++i) {
1645 ResolveInfo ri = services.get(i);
1646 ServiceInfo si = ri.serviceInfo;
1647 ComponentName compName = new ComponentName(si.packageName, si.name);
1648 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1649 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001650 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 + ": it does not require the permission "
1652 + android.Manifest.permission.BIND_INPUT_METHOD);
1653 continue;
1654 }
1655
Joe Onorato8a9b2202010-02-26 18:56:32 -08001656 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657
1658 try {
1659 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1660 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001661 final String id = p.getId();
1662 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663
Amith Yamasanie861ec12010-03-24 21:39:27 -07001664 // System IMEs are enabled by default, unless there's a hard keyboard
1665 // and the system IME was explicitly disabled
1666 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1667 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001668 }
1669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001671 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001675 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001677 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 }
1679 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001680
1681 String defaultIme = Settings.Secure.getString(mContext
1682 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001683 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001684 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001685 updateFromSettingsLocked();
1686 }
1687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001691
satokab751aa2010-09-14 19:17:36 +09001692 private void showInputMethodMenu() {
1693 showInputMethodMenuInternal(false);
1694 }
1695
1696 private void showInputMethodSubtypeMenu() {
1697 showInputMethodMenuInternal(true);
1698 }
1699
satok217f5482010-12-15 05:19:19 +09001700 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001701 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001702 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001703 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1704 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001705 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001706 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001707 }
satok217f5482010-12-15 05:19:19 +09001708 mContext.startActivity(intent);
1709 }
1710
1711 private void showConfigureInputMethods() {
1712 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1713 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1714 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1715 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001716 mContext.startActivity(intent);
1717 }
1718
satokab751aa2010-09-14 19:17:36 +09001719 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001720 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 String lastInputMethodId = Settings.Secure.getString(context
1727 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001728 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001729 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001730
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001731 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09001732 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
1733 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001734 if (immis == null || immis.size() == 0) {
1735 return;
1736 }
1737
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001738 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739
satokab751aa2010-09-14 19:17:36 +09001740 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1741 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001742
satokbb4aa062011-01-19 21:40:27 +09001743 for (InputMethodInfo imi: immis.keySet()) {
1744 if (imi == null) continue;
1745 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09001746 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09001747 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
1748 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09001749 }
satokbb4aa062011-01-19 21:40:27 +09001750 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
1751 CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001752 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09001753 final int subtypeCount = imi.getSubtypeCount();
Ken Wakasa586f0512011-01-20 22:31:01 +09001754 for (int j = 0; j < subtypeCount; ++j) {
satokbb4aa062011-01-19 21:40:27 +09001755 InputMethodSubtype subtype = imi.getSubtypeAt(j);
satok7f35c8c2010-10-07 21:13:11 +09001756 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1757 CharSequence title;
1758 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001759 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001760 if (nameResId != 0) {
satokbb4aa062011-01-19 21:40:27 +09001761 title = pm.getText(imi.getPackageName(), nameResId,
1762 imi.getServiceInfo().applicationInfo);
satok7f35c8c2010-10-07 21:13:11 +09001763 } else {
1764 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001765 // TODO: Use more friendly Title and UI
1766 title = label + "," + (mode == null ? "" : mode) + ","
1767 + (language == null ? "" : language);
1768 }
satokbb4aa062011-01-19 21:40:27 +09001769 imMap.put(title, new Pair<InputMethodInfo, Integer>(imi, j));
satokab751aa2010-09-14 19:17:36 +09001770 }
satokab751aa2010-09-14 19:17:36 +09001771 }
1772 } else {
1773 imMap.put(label,
satokbb4aa062011-01-19 21:40:27 +09001774 new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID));
satokab751aa2010-09-14 19:17:36 +09001775 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001776 }
satok913a8922010-08-26 21:53:41 +09001777
satokbb4aa062011-01-19 21:40:27 +09001778 final int N = imMap.size();
satok913a8922010-08-26 21:53:41 +09001779 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001780 mIms = new InputMethodInfo[N];
1781 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001782 int checkedItem = 0;
1783 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001784 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1785 mIms[i] = value.first;
1786 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001787 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001788 int subtypeId = mSubtypeIds[i];
1789 if ((subtypeId == NOT_A_SUBTYPE_ID)
1790 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1791 || (subtypeId == lastInputMethodSubtypeId)) {
1792 checkedItem = i;
1793 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 }
satokab751aa2010-09-14 19:17:36 +09001796
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001797 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1798 public void onClick(DialogInterface dialog, int which) {
1799 hideInputMethodMenu();
1800 }
1801 };
satokd87c2592010-09-29 11:52:06 +09001802
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001803 TypedArray a = context.obtainStyledAttributes(null,
1804 com.android.internal.R.styleable.DialogPreference,
1805 com.android.internal.R.attr.alertDialogStyle, 0);
1806 mDialogBuilder = new AlertDialog.Builder(context)
1807 .setTitle(com.android.internal.R.string.select_input_method)
1808 .setOnCancelListener(new OnCancelListener() {
1809 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001811 }
1812 })
1813 .setIcon(a.getDrawable(
1814 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1815 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001816
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001817 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1818 new AlertDialog.OnClickListener() {
1819 public void onClick(DialogInterface dialog, int which) {
1820 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001821 if (mIms == null || mIms.length <= which
1822 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001823 return;
1824 }
1825 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001826 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001827 hideInputMethodMenu();
1828 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001829 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09001830 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09001831 subtypeId = NOT_A_SUBTYPE_ID;
1832 }
1833 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001834 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001837 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838
satok7f35c8c2010-10-07 21:13:11 +09001839 if (showSubtypes) {
satok82beadf2010-12-27 19:03:06 +09001840 mDialogBuilder.setPositiveButton(
1841 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09001842 new DialogInterface.OnClickListener() {
1843 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09001844 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09001845 }
1846 });
1847 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 mSwitchingDialog = mDialogBuilder.create();
1849 mSwitchingDialog.getWindow().setType(
1850 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1851 mSwitchingDialog.show();
1852 }
1853 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001856 synchronized (mMethodMap) {
1857 hideInputMethodMenuLocked();
1858 }
1859 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001860
The Android Open Source Project10592532009-03-18 17:39:46 -07001861 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001862 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863
The Android Open Source Project10592532009-03-18 17:39:46 -07001864 if (mSwitchingDialog != null) {
1865 mSwitchingDialog.dismiss();
1866 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001868
The Android Open Source Project10592532009-03-18 17:39:46 -07001869 mDialogBuilder = null;
1870 mItems = null;
1871 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 public boolean setInputMethodEnabled(String id, boolean enabled) {
1877 synchronized (mMethodMap) {
1878 if (mContext.checkCallingOrSelfPermission(
1879 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1880 != PackageManager.PERMISSION_GRANTED) {
1881 throw new SecurityException(
1882 "Requires permission "
1883 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1884 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 long ident = Binder.clearCallingIdentity();
1887 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001888 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 } finally {
1890 Binder.restoreCallingIdentity(ident);
1891 }
1892 }
1893 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001894
1895 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1896 // Make sure this is a valid input method.
1897 InputMethodInfo imm = mMethodMap.get(id);
1898 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001899 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001900 }
1901
satokd87c2592010-09-29 11:52:06 +09001902 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1903 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001904
satokd87c2592010-09-29 11:52:06 +09001905 if (enabled) {
1906 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1907 if (pair.first.equals(id)) {
1908 // We are enabling this input method, but it is already enabled.
1909 // Nothing to do. The previous state was enabled.
1910 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001911 }
1912 }
satokd87c2592010-09-29 11:52:06 +09001913 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1914 // Previous state was disabled.
1915 return false;
1916 } else {
1917 StringBuilder builder = new StringBuilder();
1918 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1919 builder, enabledInputMethodsList, id)) {
1920 // Disabled input method is currently selected, switch to another one.
1921 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1922 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001923 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
1924 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
1925 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09001926 }
1927 // Previous state was enabled.
1928 return true;
1929 } else {
1930 // We are disabling the input method but it is already disabled.
1931 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001932 return false;
1933 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001934 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001935 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001936
satok57ffc002011-01-25 00:11:47 +09001937 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
1938 if (subtype == null) return true;
1939 String[] extraValues = subtype.getExtraValue().split(",");
1940 final int N = extraValues.length;
1941 for (int i = 0; i < N; ++i) {
1942 if (SUBTYPE_EXTRAVALUE_EXCLUDE_FROM_LAST_IME.equals(extraValues[i])) {
1943 return false;
1944 }
1945 }
1946 return true;
1947 }
1948
satok723a27e2010-11-11 14:58:11 +09001949 private void saveCurrentInputMethodAndSubtypeToHistory() {
1950 String subtypeId = NOT_A_SUBTYPE_ID_STR;
1951 if (mCurrentSubtype != null) {
1952 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
1953 }
satok57ffc002011-01-25 00:11:47 +09001954 if (canAddToLastInputMethod(mCurrentSubtype)) {
1955 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
1956 }
satokab751aa2010-09-14 19:17:36 +09001957 }
1958
satok723a27e2010-11-11 14:58:11 +09001959 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
1960 boolean setSubtypeOnly) {
1961 // Update the history of InputMethod and Subtype
1962 saveCurrentInputMethodAndSubtypeToHistory();
1963
1964 // Set Subtype here
1965 if (imi == null || subtypeId < 0) {
1966 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001967 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09001968 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09001969 if (subtypeId < imi.getSubtypeCount()) {
1970 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
1971 mSettings.putSelectedSubtype(subtype.hashCode());
1972 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09001973 } else {
1974 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
1975 mCurrentSubtype = null;
1976 }
satokab751aa2010-09-14 19:17:36 +09001977 }
satok723a27e2010-11-11 14:58:11 +09001978
1979 if (!setSubtypeOnly) {
1980 // Set InputMethod here
1981 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
1982 }
1983 }
1984
1985 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
1986 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
1987 int lastSubtypeId = NOT_A_SUBTYPE_ID;
1988 // newDefaultIme is empty when there is no candidate for the selected IME.
1989 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
1990 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
1991 if (subtypeHashCode != null) {
1992 try {
1993 lastSubtypeId = getSubtypeIdFromHashCode(
1994 imi, Integer.valueOf(subtypeHashCode));
1995 } catch (NumberFormatException e) {
1996 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
1997 }
1998 }
1999 }
2000 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002001 }
2002
2003 private int getSelectedInputMethodSubtypeId(String id) {
2004 InputMethodInfo imi = mMethodMap.get(id);
2005 if (imi == null) {
2006 return NOT_A_SUBTYPE_ID;
2007 }
satokab751aa2010-09-14 19:17:36 +09002008 int subtypeId;
2009 try {
2010 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2011 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2012 } catch (SettingNotFoundException e) {
2013 return NOT_A_SUBTYPE_ID;
2014 }
satok723a27e2010-11-11 14:58:11 +09002015 return getSubtypeIdFromHashCode(imi, subtypeId);
2016 }
2017
2018 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002019 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002020 final int subtypeCount = imi.getSubtypeCount();
2021 for (int i = 0; i < subtypeCount; ++i) {
2022 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002023 if (subtypeHashCode == ims.hashCode()) {
2024 return i;
2025 }
satokab751aa2010-09-14 19:17:36 +09002026 }
2027 }
2028 return NOT_A_SUBTYPE_ID;
2029 }
2030
satokdf31ae62011-01-15 06:19:44 +09002031 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2032 Resources res, List<InputMethodSubtype> subtypes) {
2033 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002034 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2035 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2036 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002037 final int N = subtypes.size();
2038 boolean containsKeyboardSubtype = false;
2039 for (int i = 0; i < N; ++i) {
2040 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002041 final String locale = subtype.getLocale();
2042 final String mode = subtype.getMode();
2043 // When system locale starts with subtype's locale, that subtype will be applicable
2044 // for system locale
2045 // For instance, it's clearly applicable for cases like system locale = en_US and
2046 // subtype = en, but it is not necessarily considered applicable for cases like system
2047 // locale = en and subtype = en_US.
2048 // We just call systemLocale.startsWith(locale) in this function because there is no
2049 // need to find applicable subtypes aggressively unlike
2050 // findLastResortApplicableSubtypeLocked.
2051 if (systemLocale.startsWith(locale)) {
2052 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2053 // If more applicable subtypes are contained, skip.
2054 if (applicableSubtype != null
2055 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2056 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002057 if (!containsKeyboardSubtype
2058 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2059 containsKeyboardSubtype = true;
2060 }
2061 }
2062 }
satok3da92232011-01-11 22:46:30 +09002063 ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2064 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002065 if (!containsKeyboardSubtype) {
2066 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002067 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002068 if (lastResortKeyboardSubtype != null) {
2069 applicableSubtypes.add(lastResortKeyboardSubtype);
2070 }
2071 }
2072 return applicableSubtypes;
2073 }
2074
satok4e4569d2010-11-19 18:45:53 +09002075 /**
2076 * If there are no selected subtypes, tries finding the most applicable one according to the
2077 * given locale.
2078 * @param subtypes this function will search the most applicable subtype in subtypes
2079 * @param mode subtypes will be filtered by mode
2080 * @param locale subtypes will be filtered by locale
satok7599a7fb2010-12-22 13:45:23 +09002081 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2082 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002083 * @return the most applicable subtypeId
2084 */
satokdf31ae62011-01-15 06:19:44 +09002085 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2086 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7fb2010-12-22 13:45:23 +09002087 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002088 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002089 return null;
satok8fbb1e82010-11-02 23:15:58 +09002090 }
satok4e4569d2010-11-19 18:45:53 +09002091 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002092 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002093 }
satok8fbb1e82010-11-02 23:15:58 +09002094 final String language = locale.substring(0, 2);
2095 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002096 InputMethodSubtype applicableSubtype = null;
satok7599a7fb2010-12-22 13:45:23 +09002097 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002098 final int N = subtypes.size();
2099 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002100 InputMethodSubtype subtype = subtypes.get(i);
2101 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002102 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2103 // and all subtypes with all modes can be candidates.
2104 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7fb2010-12-22 13:45:23 +09002105 if (firstMatchedModeSubtype == null) {
2106 firstMatchedModeSubtype = subtype;
2107 }
satok9ef02832010-11-04 21:17:48 +09002108 if (locale.equals(subtypeLocale)) {
2109 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002110 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002111 break;
2112 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2113 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002114 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002115 partialMatchFound = true;
2116 }
satok8fbb1e82010-11-02 23:15:58 +09002117 }
2118 }
2119
satok7599a7fb2010-12-22 13:45:23 +09002120 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2121 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002122 }
2123
satok8fbb1e82010-11-02 23:15:58 +09002124 // The first subtype applicable to the system locale will be defined as the most applicable
2125 // subtype.
2126 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002127 if (applicableSubtype != null) {
2128 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2129 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2130 }
satok8fbb1e82010-11-02 23:15:58 +09002131 }
satokcd7cd292010-11-20 15:46:23 +09002132 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002133 }
2134
satok4e4569d2010-11-19 18:45:53 +09002135 // If there are no selected shortcuts, tries finding the most applicable ones.
2136 private Pair<InputMethodInfo, InputMethodSubtype>
2137 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2138 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2139 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002140 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002141 boolean foundInSystemIME = false;
2142
2143 // Search applicable subtype for each InputMethodInfo
2144 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002145 final String imiId = imi.getId();
2146 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2147 continue;
2148 }
satokcd7cd292010-11-20 15:46:23 +09002149 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002150 final List<InputMethodSubtype> enabledSubtypes =
2151 getEnabledInputMethodSubtypeList(imi, true);
2152 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002153 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002154 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002155 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002156 }
satokdf31ae62011-01-15 06:19:44 +09002157 // 2. Search by the system locale from enabledSubtypes.
2158 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002159 if (subtype == null) {
2160 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002161 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002162 }
satok7599a7fb2010-12-22 13:45:23 +09002163 // 4. Search by the current subtype's locale from all subtypes.
2164 if (subtype == null && mCurrentSubtype != null) {
2165 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002166 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09002167 }
2168 // 5. Search by the system locale from all subtypes.
2169 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002170 if (subtype == null) {
satok7599a7fb2010-12-22 13:45:23 +09002171 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002172 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002173 }
satokcd7cd292010-11-20 15:46:23 +09002174 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09002175 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002176 // The current input method is the most applicable IME.
2177 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002178 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002179 break;
satok7599a7fb2010-12-22 13:45:23 +09002180 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002181 // The system input method is 2nd applicable IME.
2182 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002183 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09002184 if ((imi.getServiceInfo().applicationInfo.flags
2185 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2186 foundInSystemIME = true;
2187 }
satok4e4569d2010-11-19 18:45:53 +09002188 }
2189 }
2190 }
2191 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002192 if (mostApplicableIMI != null) {
2193 Slog.w(TAG, "Most applicable shortcut input method was:"
2194 + mostApplicableIMI.getId());
2195 if (mostApplicableSubtype != null) {
2196 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2197 + "," + mostApplicableSubtype.getMode() + ","
2198 + mostApplicableSubtype.getLocale());
2199 }
2200 }
satok4e4569d2010-11-19 18:45:53 +09002201 }
satokcd7cd292010-11-20 15:46:23 +09002202 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002203 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002204 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002205 } else {
2206 return null;
2207 }
2208 }
2209
satokab751aa2010-09-14 19:17:36 +09002210 /**
2211 * @return Return the current subtype of this input method.
2212 */
2213 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002214 boolean subtypeIsSelected = false;
2215 try {
2216 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2217 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2218 } catch (SettingNotFoundException e) {
2219 }
satok3ef8b292010-11-23 06:06:29 +09002220 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002221 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002222 String lastInputMethodId = Settings.Secure.getString(
2223 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002224 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2225 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002226 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2227 if (imi != null) {
2228 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002229 // the most applicable subtype from explicitly or implicitly enabled
2230 // subtypes.
2231 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2232 getEnabledInputMethodSubtypeList(imi, true);
2233 // If there is only one explicitly or implicitly enabled subtype,
2234 // just returns it.
2235 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2236 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2237 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2238 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2239 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2240 SUBTYPE_MODE_KEYBOARD, null, true);
2241 if (mCurrentSubtype == null) {
2242 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2243 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2244 true);
2245 }
2246 }
satok4e4569d2010-11-19 18:45:53 +09002247 }
satokcd7cd292010-11-20 15:46:23 +09002248 } else {
satok3ef8b292010-11-23 06:06:29 +09002249 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002250 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002251 }
satok8fbb1e82010-11-02 23:15:58 +09002252 }
satok3ef8b292010-11-23 06:06:29 +09002253 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002254 }
satokab751aa2010-09-14 19:17:36 +09002255 }
2256
satokf3db1af2010-11-23 13:34:33 +09002257 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2258 InputMethodSubtype subtype) {
2259 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2260 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2261 } else {
2262 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2263 subtypes.add(subtype);
2264 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2265 }
2266 }
2267
satok4e4569d2010-11-19 18:45:53 +09002268 // TODO: We should change the return type from List to List<Parcelable>
2269 public List getShortcutInputMethodsAndSubtypes() {
2270 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002271 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002272 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002273 // If there are no selected shortcut subtypes, the framework will try to find
2274 // the most applicable subtype from all subtypes whose mode is
2275 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002276 Pair<InputMethodInfo, InputMethodSubtype> info =
2277 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2278 SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09002279 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002280 ret.add(info.first);
2281 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09002282 }
satok3da92232011-01-11 22:46:30 +09002283 return ret;
satokf3db1af2010-11-23 13:34:33 +09002284 }
satokf3db1af2010-11-23 13:34:33 +09002285 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2286 ret.add(imi);
2287 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2288 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002289 }
2290 }
satokf3db1af2010-11-23 13:34:33 +09002291 return ret;
satok4e4569d2010-11-19 18:45:53 +09002292 }
2293 }
2294
satokb66d2872010-11-10 01:04:04 +09002295 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2296 synchronized (mMethodMap) {
2297 if (subtype != null && mCurMethodId != null) {
2298 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2299 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2300 if (subtypeId != NOT_A_SUBTYPE_ID) {
2301 setInputMethodLocked(mCurMethodId, subtypeId);
2302 return true;
2303 }
2304 }
2305 return false;
2306 }
2307 }
2308
satokd87c2592010-09-29 11:52:06 +09002309 /**
2310 * Utility class for putting and getting settings for InputMethod
2311 * TODO: Move all putters and getters of settings to this class.
2312 */
2313 private static class InputMethodSettings {
2314 // The string for enabled input method is saved as follows:
2315 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2316 private static final char INPUT_METHOD_SEPARATER = ':';
2317 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002318 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002319 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2320
satok723a27e2010-11-11 14:58:11 +09002321 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002322 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2323
satokdf31ae62011-01-15 06:19:44 +09002324 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002325 private final ContentResolver mResolver;
2326 private final HashMap<String, InputMethodInfo> mMethodMap;
2327 private final ArrayList<InputMethodInfo> mMethodList;
2328
2329 private String mEnabledInputMethodsStrCache;
2330
2331 private static void buildEnabledInputMethodsSettingString(
2332 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2333 String id = pair.first;
2334 ArrayList<String> subtypes = pair.second;
2335 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002336 // Inputmethod and subtypes are saved in the settings as follows:
2337 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2338 for (String subtypeId: subtypes) {
2339 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002340 }
2341 }
2342
2343 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002344 Resources res, ContentResolver resolver,
2345 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2346 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002347 mResolver = resolver;
2348 mMethodMap = methodMap;
2349 mMethodList = methodList;
2350 }
2351
2352 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2353 return createEnabledInputMethodListLocked(
2354 getEnabledInputMethodsAndSubtypeListLocked());
2355 }
2356
satok7f35c8c2010-10-07 21:13:11 +09002357 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002358 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2359 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002360 getEnabledInputMethodsAndSubtypeListLocked());
2361 }
2362
satok67ddf9c2010-11-17 09:45:54 +09002363 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2364 InputMethodInfo imi) {
2365 List<Pair<String, ArrayList<String>>> imsList =
2366 getEnabledInputMethodsAndSubtypeListLocked();
2367 ArrayList<InputMethodSubtype> enabledSubtypes =
2368 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002369 if (imi != null) {
2370 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2371 InputMethodInfo info = mMethodMap.get(imsPair.first);
2372 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002373 final int subtypeCount = info.getSubtypeCount();
2374 for (int i = 0; i < subtypeCount; ++i) {
2375 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002376 for (String s: imsPair.second) {
2377 if (String.valueOf(ims.hashCode()).equals(s)) {
2378 enabledSubtypes.add(ims);
2379 }
satok67ddf9c2010-11-17 09:45:54 +09002380 }
2381 }
satok884ef9a2010-11-18 10:39:46 +09002382 break;
satok67ddf9c2010-11-17 09:45:54 +09002383 }
satok67ddf9c2010-11-17 09:45:54 +09002384 }
2385 }
2386 return enabledSubtypes;
2387 }
2388
satokd87c2592010-09-29 11:52:06 +09002389 // At the initial boot, the settings for input methods are not set,
2390 // so we need to enable IME in that case.
2391 public void enableAllIMEsIfThereIsNoEnabledIME() {
2392 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2393 StringBuilder sb = new StringBuilder();
2394 final int N = mMethodList.size();
2395 for (int i = 0; i < N; i++) {
2396 InputMethodInfo imi = mMethodList.get(i);
2397 Slog.i(TAG, "Adding: " + imi.getId());
2398 if (i > 0) sb.append(':');
2399 sb.append(imi.getId());
2400 }
2401 putEnabledInputMethodsStr(sb.toString());
2402 }
2403 }
2404
satokbb4aa062011-01-19 21:40:27 +09002405 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002406 ArrayList<Pair<String, ArrayList<String>>> imsList
2407 = new ArrayList<Pair<String, ArrayList<String>>>();
2408 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2409 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2410 return imsList;
2411 }
satok723a27e2010-11-11 14:58:11 +09002412 mInputMethodSplitter.setString(enabledInputMethodsStr);
2413 while (mInputMethodSplitter.hasNext()) {
2414 String nextImsStr = mInputMethodSplitter.next();
2415 mSubtypeSplitter.setString(nextImsStr);
2416 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002417 ArrayList<String> subtypeHashes = new ArrayList<String>();
2418 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002419 String imeId = mSubtypeSplitter.next();
2420 while (mSubtypeSplitter.hasNext()) {
2421 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002422 }
2423 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2424 }
2425 }
2426 return imsList;
2427 }
2428
2429 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2430 if (reloadInputMethodStr) {
2431 getEnabledInputMethodsStr();
2432 }
2433 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2434 // Add in the newly enabled input method.
2435 putEnabledInputMethodsStr(id);
2436 } else {
2437 putEnabledInputMethodsStr(
2438 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2439 }
2440 }
2441
2442 /**
2443 * Build and put a string of EnabledInputMethods with removing specified Id.
2444 * @return the specified id was removed or not.
2445 */
2446 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2447 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2448 boolean isRemoved = false;
2449 boolean needsAppendSeparator = false;
2450 for (Pair<String, ArrayList<String>> ims: imsList) {
2451 String curId = ims.first;
2452 if (curId.equals(id)) {
2453 // We are disabling this input method, and it is
2454 // currently enabled. Skip it to remove from the
2455 // new list.
2456 isRemoved = true;
2457 } else {
2458 if (needsAppendSeparator) {
2459 builder.append(INPUT_METHOD_SEPARATER);
2460 } else {
2461 needsAppendSeparator = true;
2462 }
2463 buildEnabledInputMethodsSettingString(builder, ims);
2464 }
2465 }
2466 if (isRemoved) {
2467 // Update the setting with the new list of input methods.
2468 putEnabledInputMethodsStr(builder.toString());
2469 }
2470 return isRemoved;
2471 }
2472
2473 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2474 List<Pair<String, ArrayList<String>>> imsList) {
2475 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2476 for (Pair<String, ArrayList<String>> ims: imsList) {
2477 InputMethodInfo info = mMethodMap.get(ims.first);
2478 if (info != null) {
2479 res.add(info);
2480 }
2481 }
2482 return res;
2483 }
2484
satok7f35c8c2010-10-07 21:13:11 +09002485 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002486 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002487 List<Pair<String, ArrayList<String>>> imsList) {
2488 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2489 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2490 for (Pair<String, ArrayList<String>> ims : imsList) {
2491 InputMethodInfo info = mMethodMap.get(ims.first);
2492 if (info != null) {
2493 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2494 }
2495 }
2496 return res;
2497 }
2498
satokd87c2592010-09-29 11:52:06 +09002499 private void putEnabledInputMethodsStr(String str) {
2500 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2501 mEnabledInputMethodsStrCache = str;
2502 }
2503
2504 private String getEnabledInputMethodsStr() {
2505 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2506 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002507 if (DEBUG) {
2508 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2509 }
satokd87c2592010-09-29 11:52:06 +09002510 return mEnabledInputMethodsStrCache;
2511 }
satok723a27e2010-11-11 14:58:11 +09002512
2513 private void saveSubtypeHistory(
2514 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2515 StringBuilder builder = new StringBuilder();
2516 boolean isImeAdded = false;
2517 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2518 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2519 newSubtypeId);
2520 isImeAdded = true;
2521 }
2522 for (Pair<String, String> ime: savedImes) {
2523 String imeId = ime.first;
2524 String subtypeId = ime.second;
2525 if (TextUtils.isEmpty(subtypeId)) {
2526 subtypeId = NOT_A_SUBTYPE_ID_STR;
2527 }
2528 if (isImeAdded) {
2529 builder.append(INPUT_METHOD_SEPARATER);
2530 } else {
2531 isImeAdded = true;
2532 }
2533 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2534 subtypeId);
2535 }
2536 // Remove the last INPUT_METHOD_SEPARATER
2537 putSubtypeHistoryStr(builder.toString());
2538 }
2539
2540 public void addSubtypeToHistory(String imeId, String subtypeId) {
2541 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2542 for (Pair<String, String> ime: subtypeHistory) {
2543 if (ime.first.equals(imeId)) {
2544 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002545 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002546 + ime.second);
2547 }
2548 // We should break here
2549 subtypeHistory.remove(ime);
2550 break;
2551 }
2552 }
satokbb4aa062011-01-19 21:40:27 +09002553 if (DEBUG) {
2554 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2555 }
satok723a27e2010-11-11 14:58:11 +09002556 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2557 }
2558
2559 private void putSubtypeHistoryStr(String str) {
2560 if (DEBUG) {
2561 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2562 }
2563 Settings.Secure.putString(
2564 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2565 }
2566
2567 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2568 // Gets the first one from the history
2569 return getLastSubtypeForInputMethodLockedInternal(null);
2570 }
2571
2572 public String getLastSubtypeForInputMethodLocked(String imeId) {
2573 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2574 if (ime != null) {
2575 return ime.second;
2576 } else {
2577 return null;
2578 }
2579 }
2580
2581 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2582 List<Pair<String, ArrayList<String>>> enabledImes =
2583 getEnabledInputMethodsAndSubtypeListLocked();
2584 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2585 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2586 final String imeInTheHistory = imeAndSubtype.first;
2587 // If imeId is empty, returns the first IME and subtype in the history
2588 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2589 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002590 final String subtypeHashCode =
2591 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2592 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002593 if (!TextUtils.isEmpty(subtypeHashCode)) {
2594 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002595 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002596 }
2597 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2598 }
2599 }
2600 }
2601 if (DEBUG) {
2602 Slog.d(TAG, "No enabled IME found in the history");
2603 }
2604 return null;
2605 }
2606
satokdf31ae62011-01-15 06:19:44 +09002607 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002608 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2609 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2610 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002611 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2612 if (explicitlyEnabledSubtypes.size() == 0) {
2613 // If there are no explicitly enabled subtypes, applicable subtypes are
2614 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002615 InputMethodInfo ime = mMethodMap.get(imeId);
2616 // If IME is enabled and no subtypes are enabled, applicable subtypes
2617 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002618 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002619 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002620 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002621 if (implicitlySelectedSubtypes != null) {
2622 final int N = implicitlySelectedSubtypes.size();
2623 for (int i = 0; i < N; ++i) {
2624 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2625 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2626 return subtypeHashCode;
2627 }
2628 }
2629 }
2630 }
2631 } else {
satokf6cafb62011-01-17 16:29:02 +09002632 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002633 if (s.equals(subtypeHashCode)) {
2634 // If both imeId and subtypeId are enabled, return subtypeId.
2635 return s;
2636 }
satok723a27e2010-11-11 14:58:11 +09002637 }
2638 }
2639 // If imeId was enabled but subtypeId was disabled.
2640 return NOT_A_SUBTYPE_ID_STR;
2641 }
2642 }
2643 // If both imeId and subtypeId are disabled, return null
2644 return null;
2645 }
2646
2647 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2648 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2649 final String subtypeHistoryStr = getSubtypeHistoryStr();
2650 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2651 return imsList;
2652 }
2653 mInputMethodSplitter.setString(subtypeHistoryStr);
2654 while (mInputMethodSplitter.hasNext()) {
2655 String nextImsStr = mInputMethodSplitter.next();
2656 mSubtypeSplitter.setString(nextImsStr);
2657 if (mSubtypeSplitter.hasNext()) {
2658 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2659 // The first element is ime id.
2660 String imeId = mSubtypeSplitter.next();
2661 while (mSubtypeSplitter.hasNext()) {
2662 subtypeId = mSubtypeSplitter.next();
2663 break;
2664 }
2665 imsList.add(new Pair<String, String>(imeId, subtypeId));
2666 }
2667 }
2668 return imsList;
2669 }
2670
2671 private String getSubtypeHistoryStr() {
2672 if (DEBUG) {
2673 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2674 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2675 }
2676 return Settings.Secure.getString(
2677 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2678 }
2679
2680 public void putSelectedInputMethod(String imeId) {
2681 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2682 }
2683
2684 public void putSelectedSubtype(int subtypeId) {
2685 Settings.Secure.putInt(
2686 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2687 }
satokd87c2592010-09-29 11:52:06 +09002688 }
2689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 @Override
2693 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2694 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2695 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2698 + Binder.getCallingPid()
2699 + ", uid=" + Binder.getCallingUid());
2700 return;
2701 }
2702
2703 IInputMethod method;
2704 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 synchronized (mMethodMap) {
2709 p.println("Current Input Method Manager state:");
2710 int N = mMethodList.size();
2711 p.println(" Input Methods:");
2712 for (int i=0; i<N; i++) {
2713 InputMethodInfo info = mMethodList.get(i);
2714 p.println(" InputMethod #" + i + ":");
2715 info.dump(p, " ");
2716 }
2717 p.println(" Clients:");
2718 for (ClientState ci : mClients.values()) {
2719 p.println(" Client " + ci + ":");
2720 p.println(" client=" + ci.client);
2721 p.println(" inputContext=" + ci.inputContext);
2722 p.println(" sessionRequested=" + ci.sessionRequested);
2723 p.println(" curSession=" + ci.curSession);
2724 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002725 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002727 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2728 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2730 + " mBoundToMethod=" + mBoundToMethod);
2731 p.println(" mCurToken=" + mCurToken);
2732 p.println(" mCurIntent=" + mCurIntent);
2733 method = mCurMethod;
2734 p.println(" mCurMethod=" + mCurMethod);
2735 p.println(" mEnabledSession=" + mEnabledSession);
2736 p.println(" mShowRequested=" + mShowRequested
2737 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2738 + " mShowForced=" + mShowForced
2739 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002740 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002742
Jeff Brownb88102f2010-09-08 11:49:43 -07002743 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 pw.flush();
2746 try {
2747 client.client.asBinder().dump(fd, args);
2748 } catch (RemoteException e) {
2749 p.println("Input method client dead: " + e);
2750 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002751 } else {
2752 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002754
Jeff Brownb88102f2010-09-08 11:49:43 -07002755 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 pw.flush();
2758 try {
2759 method.asBinder().dump(fd, args);
2760 } catch (RemoteException e) {
2761 p.println("Input method service dead: " + e);
2762 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002763 } else {
2764 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 }
2766 }
2767}