blob: 8d6d3a1644b6a61d4cce87ad9b56a9499caf62de [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 {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001056 mImeWindowVis = 0;
1057 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1058 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001059 // If subtype is null, try to find the most applicable one from
1060 // getCurrentInputMethodSubtype.
1061 if (subtype == null) {
1062 subtype = getCurrentInputMethodSubtype();
1063 }
1064 mCurMethod.changeInputMethodSubtype(subtype);
1065 } catch (RemoteException e) {
1066 return;
satokab751aa2010-09-14 19:17:36 +09001067 }
1068 }
1069 }
1070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 return;
1072 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 final long ident = Binder.clearCallingIdentity();
1075 try {
satokab751aa2010-09-14 19:17:36 +09001076 // Set a subtype to this input method.
1077 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001078 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1079 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1080 // because mCurMethodId is stored as a history in
1081 // setSelectedInputMethodAndSubtypeLocked().
1082 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083
1084 if (ActivityManagerNative.isSystemReady()) {
1085 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001086 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 intent.putExtra("input_method_id", id);
1088 mContext.sendBroadcast(intent);
1089 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001090 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 } finally {
1092 Binder.restoreCallingIdentity(ident);
1093 }
1094 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001095
The Android Open Source Project4df24232009-03-05 14:34:35 -08001096 public boolean showSoftInput(IInputMethodClient client, int flags,
1097 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001098 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 long ident = Binder.clearCallingIdentity();
1100 try {
1101 synchronized (mMethodMap) {
1102 if (mCurClient == null || client == null
1103 || mCurClient.client.asBinder() != client.asBinder()) {
1104 try {
1105 // We need to check if this is the current client with
1106 // focus in the window manager, to allow this call to
1107 // be made before input is started in it.
1108 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001109 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001110 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 }
1112 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001113 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 }
1115 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001116
Joe Onorato8a9b2202010-02-26 18:56:32 -08001117 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001118 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 }
1120 } finally {
1121 Binder.restoreCallingIdentity(ident);
1122 }
1123 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001124
The Android Open Source Project4df24232009-03-05 14:34:35 -08001125 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 mShowRequested = true;
1127 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1128 mShowExplicitlyRequested = true;
1129 }
1130 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1131 mShowExplicitlyRequested = true;
1132 mShowForced = true;
1133 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001134
Dianne Hackborncc278702009-09-02 23:07:23 -07001135 if (!mSystemReady) {
1136 return false;
1137 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001138
The Android Open Source Project4df24232009-03-05 14:34:35 -08001139 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001141 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1142 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1143 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001145 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 } else if (mHaveConnection && SystemClock.uptimeMillis()
1147 < (mLastBindTime+TIME_TO_RECONNECT)) {
1148 // The client has asked to have the input method shown, but
1149 // we have been sitting here too long with a connection to the
1150 // service and no interface received, so let's disconnect/connect
1151 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001152 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 SystemClock.uptimeMillis()-mLastBindTime,1);
1154 mContext.unbindService(this);
1155 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1156 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001157
The Android Open Source Project4df24232009-03-05 14:34:35 -08001158 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001160
The Android Open Source Project4df24232009-03-05 14:34:35 -08001161 public boolean hideSoftInput(IInputMethodClient client, int flags,
1162 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001163 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 long ident = Binder.clearCallingIdentity();
1165 try {
1166 synchronized (mMethodMap) {
1167 if (mCurClient == null || client == null
1168 || mCurClient.client.asBinder() != client.asBinder()) {
1169 try {
1170 // We need to check if this is the current client with
1171 // focus in the window manager, to allow this call to
1172 // be made before input is started in it.
1173 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001174 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1175 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001176 mImeWindowVis = 0;
1177 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1178 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001179 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 }
1181 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001182 mImeWindowVis = 0;
1183 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001184 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 }
1186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001187
Joe Onorato8a9b2202010-02-26 18:56:32 -08001188 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001189 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 }
1191 } finally {
1192 Binder.restoreCallingIdentity(ident);
1193 }
1194 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001195
The Android Open Source Project4df24232009-03-05 14:34:35 -08001196 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1198 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001199 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001201 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 }
1203 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001204 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001206 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001208 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001210 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1211 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1212 res = true;
1213 } else {
1214 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 }
1216 mInputShown = false;
1217 mShowRequested = false;
1218 mShowExplicitlyRequested = false;
1219 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001220 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001222
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001223 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1225 boolean first, int windowFlags) {
1226 long ident = Binder.clearCallingIdentity();
1227 try {
1228 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001229 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 + " viewHasFocus=" + viewHasFocus
1231 + " isTextEditor=" + isTextEditor
1232 + " softInputMode=#" + Integer.toHexString(softInputMode)
1233 + " first=" + first + " flags=#"
1234 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 if (mCurClient == null || client == null
1237 || mCurClient.client.asBinder() != client.asBinder()) {
1238 try {
1239 // We need to check if this is the current client with
1240 // focus in the window manager, to allow this call to
1241 // be made before input is started in it.
1242 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001243 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 return;
1245 }
1246 } catch (RemoteException e) {
1247 }
1248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001249
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001250 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001251 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001252 return;
1253 }
1254 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001255
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001256 // Should we auto-show the IME even if the caller has not
1257 // specified what should be done with it?
1258 // We only do this automatically if the window can resize
1259 // to accommodate the IME (so what the user sees will give
1260 // them good context without input information being obscured
1261 // by the IME) or if running on a large screen where there
1262 // is more room for the target window + IME.
1263 final boolean doAutoShow =
1264 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1265 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1266 || mRes.getConfiguration().isLayoutSizeAtLeast(
1267 Configuration.SCREENLAYOUT_SIZE_LARGE);
1268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1270 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001271 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1273 // There is no focus view, and this window will
1274 // be behind any soft input window, so hide the
1275 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001276 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001277 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001279 } else if (isTextEditor && doAutoShow && (softInputMode &
1280 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 // There is a focus view, and we are navigating forward
1282 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001283 // We only do this automatically if the window an resize
1284 // to accomodate the IME (so what the user sees will give
1285 // them good context without input information being obscured
1286 // by the IME) or if running on a large screen where there
1287 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001288 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001289 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 }
1291 break;
1292 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1293 // Do nothing.
1294 break;
1295 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1296 if ((softInputMode &
1297 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001298 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001299 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 }
1301 break;
1302 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001303 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001304 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 break;
1306 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1307 if ((softInputMode &
1308 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001309 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001310 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 }
1312 break;
1313 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001314 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001315 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 break;
1317 }
1318 }
1319 } finally {
1320 Binder.restoreCallingIdentity(ident);
1321 }
1322 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1325 synchronized (mMethodMap) {
1326 if (mCurClient == null || client == null
1327 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001328 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001329 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 }
1331
satok440aab52010-11-25 09:43:11 +09001332 // Always call subtype picker, because subtype picker is a superset of input method
1333 // picker.
satokab751aa2010-09-14 19:17:36 +09001334 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1335 }
1336 }
1337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001339 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1340 }
1341
1342 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1343 synchronized (mMethodMap) {
1344 if (subtype != null) {
1345 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1346 mMethodMap.get(id), subtype.hashCode()));
1347 } else {
1348 setInputMethod(token, id);
1349 }
1350 }
satokab751aa2010-09-14 19:17:36 +09001351 }
1352
satokb416a71e2010-11-25 20:42:14 +09001353 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001354 IInputMethodClient client, String inputMethodId) {
satokb416a71e2010-11-25 20:42:14 +09001355 synchronized (mMethodMap) {
1356 if (mCurClient == null || client == null
1357 || mCurClient.client.asBinder() != client.asBinder()) {
1358 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1359 }
satok7fee71f2010-12-17 18:54:26 +09001360 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1361 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09001362 }
1363 }
1364
satok735cf382010-11-11 20:40:09 +09001365 public boolean switchToLastInputMethod(IBinder token) {
1366 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001367 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1368 if (lastIme == null) return false;
1369 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1370 if (lastImi == null) return false;
1371
1372 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1373 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1374 // If the last IME is the same as the current IME and the last subtype is not defined,
1375 // there is no need to switch to the last IME.
1376 if (imiIdIsSame && lastSubtypeHash == NOT_A_SUBTYPE_ID) return false;
1377
1378 int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1379 : mCurrentSubtype.hashCode();
1380 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1381 if (DEBUG) {
1382 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second + ", from: "
1383 + mCurMethodId + ", " + currentSubtypeHash);
satok735cf382010-11-11 20:40:09 +09001384 }
satokc445bcd2011-01-25 18:57:24 +09001385 setInputMethodWithSubtypeId(token, lastIme.first, getSubtypeIdFromHashCode(
1386 lastImi, lastSubtypeHash));
1387 return true;
satok735cf382010-11-11 20:40:09 +09001388 }
1389 return false;
1390 }
1391 }
1392
satok28203512010-11-24 11:06:49 +09001393 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 synchronized (mMethodMap) {
1395 if (token == null) {
1396 if (mContext.checkCallingOrSelfPermission(
1397 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1398 != PackageManager.PERMISSION_GRANTED) {
1399 throw new SecurityException(
1400 "Using null token requires permission "
1401 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1402 }
1403 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001404 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1405 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 return;
1407 }
1408
1409 long ident = Binder.clearCallingIdentity();
1410 try {
satokab751aa2010-09-14 19:17:36 +09001411 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 } finally {
1413 Binder.restoreCallingIdentity(ident);
1414 }
1415 }
1416 }
1417
1418 public void hideMySoftInput(IBinder token, int flags) {
1419 synchronized (mMethodMap) {
1420 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001421 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1422 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 return;
1424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 long ident = Binder.clearCallingIdentity();
1426 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001427 hideCurrentInputLocked(flags, null);
1428 } finally {
1429 Binder.restoreCallingIdentity(ident);
1430 }
1431 }
1432 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001433
The Android Open Source Project4df24232009-03-05 14:34:35 -08001434 public void showMySoftInput(IBinder token, int flags) {
1435 synchronized (mMethodMap) {
1436 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001437 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1438 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001439 return;
1440 }
1441 long ident = Binder.clearCallingIdentity();
1442 try {
1443 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 } finally {
1445 Binder.restoreCallingIdentity(ident);
1446 }
1447 }
1448 }
1449
1450 void setEnabledSessionInMainThread(SessionState session) {
1451 if (mEnabledSession != session) {
1452 if (mEnabledSession != null) {
1453 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001454 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 mEnabledSession.method.setSessionEnabled(
1456 mEnabledSession.session, false);
1457 } catch (RemoteException e) {
1458 }
1459 }
1460 mEnabledSession = session;
1461 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001462 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 session.method.setSessionEnabled(
1464 session.session, true);
1465 } catch (RemoteException e) {
1466 }
1467 }
1468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 public boolean handleMessage(Message msg) {
1471 HandlerCaller.SomeArgs args;
1472 switch (msg.what) {
1473 case MSG_SHOW_IM_PICKER:
1474 showInputMethodMenu();
1475 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001476
satokab751aa2010-09-14 19:17:36 +09001477 case MSG_SHOW_IM_SUBTYPE_PICKER:
1478 showInputMethodSubtypeMenu();
1479 return true;
1480
satok47a44912010-10-06 16:03:58 +09001481 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001482 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001483 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001484 return true;
1485
1486 case MSG_SHOW_IM_CONFIG:
1487 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001488 return true;
1489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 case MSG_UNBIND_INPUT:
1493 try {
1494 ((IInputMethod)msg.obj).unbindInput();
1495 } catch (RemoteException e) {
1496 // There is nothing interesting about the method dying.
1497 }
1498 return true;
1499 case MSG_BIND_INPUT:
1500 args = (HandlerCaller.SomeArgs)msg.obj;
1501 try {
1502 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1503 } catch (RemoteException e) {
1504 }
1505 return true;
1506 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001507 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001509 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1510 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 } catch (RemoteException e) {
1512 }
1513 return true;
1514 case MSG_HIDE_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).hideSoftInput(0,
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_ATTACH_TOKEN:
1523 args = (HandlerCaller.SomeArgs)msg.obj;
1524 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001525 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1527 } catch (RemoteException e) {
1528 }
1529 return true;
1530 case MSG_CREATE_SESSION:
1531 args = (HandlerCaller.SomeArgs)msg.obj;
1532 try {
1533 ((IInputMethod)args.arg1).createSession(
1534 (IInputMethodCallback)args.arg2);
1535 } catch (RemoteException e) {
1536 }
1537 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 case MSG_START_INPUT:
1541 args = (HandlerCaller.SomeArgs)msg.obj;
1542 try {
1543 SessionState session = (SessionState)args.arg1;
1544 setEnabledSessionInMainThread(session);
1545 session.method.startInput((IInputContext)args.arg2,
1546 (EditorInfo)args.arg3);
1547 } catch (RemoteException e) {
1548 }
1549 return true;
1550 case MSG_RESTART_INPUT:
1551 args = (HandlerCaller.SomeArgs)msg.obj;
1552 try {
1553 SessionState session = (SessionState)args.arg1;
1554 setEnabledSessionInMainThread(session);
1555 session.method.restartInput((IInputContext)args.arg2,
1556 (EditorInfo)args.arg3);
1557 } catch (RemoteException e) {
1558 }
1559 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 case MSG_UNBIND_METHOD:
1564 try {
1565 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1566 } catch (RemoteException e) {
1567 // There is nothing interesting about the last client dying.
1568 }
1569 return true;
1570 case MSG_BIND_METHOD:
1571 args = (HandlerCaller.SomeArgs)msg.obj;
1572 try {
1573 ((IInputMethodClient)args.arg1).onBindMethod(
1574 (InputBindResult)args.arg2);
1575 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001576 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 }
1578 return true;
1579 }
1580 return false;
1581 }
1582
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001583 private boolean isSystemIme(InputMethodInfo inputMethod) {
1584 return (inputMethod.getServiceInfo().applicationInfo.flags
1585 & ApplicationInfo.FLAG_SYSTEM) != 0;
1586 }
1587
Ken Wakasa586f0512011-01-20 22:31:01 +09001588 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1589 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1590 final int subtypeCount = imi.getSubtypeCount();
1591 for (int i = 0; i < subtypeCount; ++i) {
1592 subtypes.add(imi.getSubtypeAt(i));
1593 }
1594 return subtypes;
1595 }
1596
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001597 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001598 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001599 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001600 // We'd prefer to fall back on a system IME, since that is safer.
1601 int i=enabled.size();
1602 while (i > 0) {
1603 i--;
1604 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1605 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1606 break;
1607 }
1608 }
satokab751aa2010-09-14 19:17:36 +09001609 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001610 if (DEBUG) {
1611 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1612 }
satok723a27e2010-11-11 14:58:11 +09001613 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001614 return true;
1615 }
1616
1617 return false;
1618 }
1619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1621 HashMap<String, InputMethodInfo> map) {
1622 list.clear();
1623 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001626 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001627 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1628 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1629 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1630 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631
1632 List<ResolveInfo> services = pm.queryIntentServices(
1633 new Intent(InputMethod.SERVICE_INTERFACE),
1634 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 for (int i = 0; i < services.size(); ++i) {
1637 ResolveInfo ri = services.get(i);
1638 ServiceInfo si = ri.serviceInfo;
1639 ComponentName compName = new ComponentName(si.packageName, si.name);
1640 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1641 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001642 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 + ": it does not require the permission "
1644 + android.Manifest.permission.BIND_INPUT_METHOD);
1645 continue;
1646 }
1647
Joe Onorato8a9b2202010-02-26 18:56:32 -08001648 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649
1650 try {
1651 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1652 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001653 final String id = p.getId();
1654 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655
Amith Yamasanie861ec12010-03-24 21:39:27 -07001656 // System IMEs are enabled by default, unless there's a hard keyboard
1657 // and the system IME was explicitly disabled
1658 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1659 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001660 }
1661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001663 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001667 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001669 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 }
1671 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001672
1673 String defaultIme = Settings.Secure.getString(mContext
1674 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001675 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001676 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001677 updateFromSettingsLocked();
1678 }
1679 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001683
satokab751aa2010-09-14 19:17:36 +09001684 private void showInputMethodMenu() {
1685 showInputMethodMenuInternal(false);
1686 }
1687
1688 private void showInputMethodSubtypeMenu() {
1689 showInputMethodMenuInternal(true);
1690 }
1691
satok217f5482010-12-15 05:19:19 +09001692 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001693 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001694 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001695 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1696 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001697 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001698 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001699 }
satok217f5482010-12-15 05:19:19 +09001700 mContext.startActivity(intent);
1701 }
1702
1703 private void showConfigureInputMethods() {
1704 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1705 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1706 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1707 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001708 mContext.startActivity(intent);
1709 }
1710
satokab751aa2010-09-14 19:17:36 +09001711 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001712 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 String lastInputMethodId = Settings.Secure.getString(context
1719 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001720 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001721 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001722
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001723 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09001724 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
1725 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001726 if (immis == null || immis.size() == 0) {
1727 return;
1728 }
1729
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001730 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731
satokab751aa2010-09-14 19:17:36 +09001732 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1733 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001734
satokbb4aa062011-01-19 21:40:27 +09001735 for (InputMethodInfo imi: immis.keySet()) {
1736 if (imi == null) continue;
1737 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09001738 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09001739 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
1740 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09001741 }
satokbb4aa062011-01-19 21:40:27 +09001742 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
1743 CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001744 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09001745 final int subtypeCount = imi.getSubtypeCount();
Ken Wakasa586f0512011-01-20 22:31:01 +09001746 for (int j = 0; j < subtypeCount; ++j) {
satokbb4aa062011-01-19 21:40:27 +09001747 InputMethodSubtype subtype = imi.getSubtypeAt(j);
satok7f35c8c2010-10-07 21:13:11 +09001748 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1749 CharSequence title;
1750 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001751 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001752 if (nameResId != 0) {
satokbb4aa062011-01-19 21:40:27 +09001753 title = pm.getText(imi.getPackageName(), nameResId,
1754 imi.getServiceInfo().applicationInfo);
satok7f35c8c2010-10-07 21:13:11 +09001755 } else {
1756 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001757 // TODO: Use more friendly Title and UI
1758 title = label + "," + (mode == null ? "" : mode) + ","
1759 + (language == null ? "" : language);
1760 }
satokbb4aa062011-01-19 21:40:27 +09001761 imMap.put(title, new Pair<InputMethodInfo, Integer>(imi, j));
satokab751aa2010-09-14 19:17:36 +09001762 }
satokab751aa2010-09-14 19:17:36 +09001763 }
1764 } else {
1765 imMap.put(label,
satokbb4aa062011-01-19 21:40:27 +09001766 new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID));
satokab751aa2010-09-14 19:17:36 +09001767 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001768 }
satok913a8922010-08-26 21:53:41 +09001769
satokbb4aa062011-01-19 21:40:27 +09001770 final int N = imMap.size();
satok913a8922010-08-26 21:53:41 +09001771 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001772 mIms = new InputMethodInfo[N];
1773 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001774 int checkedItem = 0;
1775 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001776 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1777 mIms[i] = value.first;
1778 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001779 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001780 int subtypeId = mSubtypeIds[i];
1781 if ((subtypeId == NOT_A_SUBTYPE_ID)
1782 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1783 || (subtypeId == lastInputMethodSubtypeId)) {
1784 checkedItem = i;
1785 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 }
satokab751aa2010-09-14 19:17:36 +09001788
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001789 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1790 public void onClick(DialogInterface dialog, int which) {
1791 hideInputMethodMenu();
1792 }
1793 };
satokd87c2592010-09-29 11:52:06 +09001794
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001795 TypedArray a = context.obtainStyledAttributes(null,
1796 com.android.internal.R.styleable.DialogPreference,
1797 com.android.internal.R.attr.alertDialogStyle, 0);
1798 mDialogBuilder = new AlertDialog.Builder(context)
1799 .setTitle(com.android.internal.R.string.select_input_method)
1800 .setOnCancelListener(new OnCancelListener() {
1801 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001803 }
1804 })
1805 .setIcon(a.getDrawable(
1806 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1807 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001808
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001809 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1810 new AlertDialog.OnClickListener() {
1811 public void onClick(DialogInterface dialog, int which) {
1812 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001813 if (mIms == null || mIms.length <= which
1814 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001815 return;
1816 }
1817 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001818 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001819 hideInputMethodMenu();
1820 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001821 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09001822 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09001823 subtypeId = NOT_A_SUBTYPE_ID;
1824 }
1825 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001826 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001827 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001829 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830
satok7f35c8c2010-10-07 21:13:11 +09001831 if (showSubtypes) {
satok82beadf2010-12-27 19:03:06 +09001832 mDialogBuilder.setPositiveButton(
1833 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09001834 new DialogInterface.OnClickListener() {
1835 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09001836 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09001837 }
1838 });
1839 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 mSwitchingDialog = mDialogBuilder.create();
1841 mSwitchingDialog.getWindow().setType(
1842 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1843 mSwitchingDialog.show();
1844 }
1845 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001848 synchronized (mMethodMap) {
1849 hideInputMethodMenuLocked();
1850 }
1851 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001852
The Android Open Source Project10592532009-03-18 17:39:46 -07001853 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001854 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855
The Android Open Source Project10592532009-03-18 17:39:46 -07001856 if (mSwitchingDialog != null) {
1857 mSwitchingDialog.dismiss();
1858 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001860
The Android Open Source Project10592532009-03-18 17:39:46 -07001861 mDialogBuilder = null;
1862 mItems = null;
1863 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 public boolean setInputMethodEnabled(String id, boolean enabled) {
1869 synchronized (mMethodMap) {
1870 if (mContext.checkCallingOrSelfPermission(
1871 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1872 != PackageManager.PERMISSION_GRANTED) {
1873 throw new SecurityException(
1874 "Requires permission "
1875 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1876 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 long ident = Binder.clearCallingIdentity();
1879 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001880 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 } finally {
1882 Binder.restoreCallingIdentity(ident);
1883 }
1884 }
1885 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001886
1887 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1888 // Make sure this is a valid input method.
1889 InputMethodInfo imm = mMethodMap.get(id);
1890 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001891 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001892 }
1893
satokd87c2592010-09-29 11:52:06 +09001894 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1895 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001896
satokd87c2592010-09-29 11:52:06 +09001897 if (enabled) {
1898 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1899 if (pair.first.equals(id)) {
1900 // We are enabling this input method, but it is already enabled.
1901 // Nothing to do. The previous state was enabled.
1902 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001903 }
1904 }
satokd87c2592010-09-29 11:52:06 +09001905 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1906 // Previous state was disabled.
1907 return false;
1908 } else {
1909 StringBuilder builder = new StringBuilder();
1910 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1911 builder, enabledInputMethodsList, id)) {
1912 // Disabled input method is currently selected, switch to another one.
1913 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1914 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001915 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
1916 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
1917 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09001918 }
1919 // Previous state was enabled.
1920 return true;
1921 } else {
1922 // We are disabling the input method but it is already disabled.
1923 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001924 return false;
1925 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001926 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001927 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001928
satok57ffc002011-01-25 00:11:47 +09001929 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
1930 if (subtype == null) return true;
1931 String[] extraValues = subtype.getExtraValue().split(",");
1932 final int N = extraValues.length;
1933 for (int i = 0; i < N; ++i) {
1934 if (SUBTYPE_EXTRAVALUE_EXCLUDE_FROM_LAST_IME.equals(extraValues[i])) {
1935 return false;
1936 }
1937 }
1938 return true;
1939 }
1940
satok723a27e2010-11-11 14:58:11 +09001941 private void saveCurrentInputMethodAndSubtypeToHistory() {
1942 String subtypeId = NOT_A_SUBTYPE_ID_STR;
1943 if (mCurrentSubtype != null) {
1944 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
1945 }
satok57ffc002011-01-25 00:11:47 +09001946 if (canAddToLastInputMethod(mCurrentSubtype)) {
1947 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
1948 }
satokab751aa2010-09-14 19:17:36 +09001949 }
1950
satok723a27e2010-11-11 14:58:11 +09001951 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
1952 boolean setSubtypeOnly) {
1953 // Update the history of InputMethod and Subtype
1954 saveCurrentInputMethodAndSubtypeToHistory();
1955
1956 // Set Subtype here
1957 if (imi == null || subtypeId < 0) {
1958 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001959 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09001960 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09001961 if (subtypeId < imi.getSubtypeCount()) {
1962 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
1963 mSettings.putSelectedSubtype(subtype.hashCode());
1964 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09001965 } else {
1966 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
1967 mCurrentSubtype = null;
1968 }
satokab751aa2010-09-14 19:17:36 +09001969 }
satok723a27e2010-11-11 14:58:11 +09001970
1971 if (!setSubtypeOnly) {
1972 // Set InputMethod here
1973 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
1974 }
1975 }
1976
1977 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
1978 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
1979 int lastSubtypeId = NOT_A_SUBTYPE_ID;
1980 // newDefaultIme is empty when there is no candidate for the selected IME.
1981 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
1982 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
1983 if (subtypeHashCode != null) {
1984 try {
1985 lastSubtypeId = getSubtypeIdFromHashCode(
1986 imi, Integer.valueOf(subtypeHashCode));
1987 } catch (NumberFormatException e) {
1988 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
1989 }
1990 }
1991 }
1992 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09001993 }
1994
1995 private int getSelectedInputMethodSubtypeId(String id) {
1996 InputMethodInfo imi = mMethodMap.get(id);
1997 if (imi == null) {
1998 return NOT_A_SUBTYPE_ID;
1999 }
satokab751aa2010-09-14 19:17:36 +09002000 int subtypeId;
2001 try {
2002 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2003 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2004 } catch (SettingNotFoundException e) {
2005 return NOT_A_SUBTYPE_ID;
2006 }
satok723a27e2010-11-11 14:58:11 +09002007 return getSubtypeIdFromHashCode(imi, subtypeId);
2008 }
2009
2010 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002011 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002012 final int subtypeCount = imi.getSubtypeCount();
2013 for (int i = 0; i < subtypeCount; ++i) {
2014 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002015 if (subtypeHashCode == ims.hashCode()) {
2016 return i;
2017 }
satokab751aa2010-09-14 19:17:36 +09002018 }
2019 }
2020 return NOT_A_SUBTYPE_ID;
2021 }
2022
satokdf31ae62011-01-15 06:19:44 +09002023 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2024 Resources res, List<InputMethodSubtype> subtypes) {
2025 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002026 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2027 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2028 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002029 final int N = subtypes.size();
2030 boolean containsKeyboardSubtype = false;
2031 for (int i = 0; i < N; ++i) {
2032 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002033 final String locale = subtype.getLocale();
2034 final String mode = subtype.getMode();
2035 // When system locale starts with subtype's locale, that subtype will be applicable
2036 // for system locale
2037 // For instance, it's clearly applicable for cases like system locale = en_US and
2038 // subtype = en, but it is not necessarily considered applicable for cases like system
2039 // locale = en and subtype = en_US.
2040 // We just call systemLocale.startsWith(locale) in this function because there is no
2041 // need to find applicable subtypes aggressively unlike
2042 // findLastResortApplicableSubtypeLocked.
2043 if (systemLocale.startsWith(locale)) {
2044 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2045 // If more applicable subtypes are contained, skip.
2046 if (applicableSubtype != null
2047 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2048 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002049 if (!containsKeyboardSubtype
2050 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2051 containsKeyboardSubtype = true;
2052 }
2053 }
2054 }
satok3da92232011-01-11 22:46:30 +09002055 ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2056 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002057 if (!containsKeyboardSubtype) {
2058 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002059 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002060 if (lastResortKeyboardSubtype != null) {
2061 applicableSubtypes.add(lastResortKeyboardSubtype);
2062 }
2063 }
2064 return applicableSubtypes;
2065 }
2066
satok4e4569d2010-11-19 18:45:53 +09002067 /**
2068 * If there are no selected subtypes, tries finding the most applicable one according to the
2069 * given locale.
2070 * @param subtypes this function will search the most applicable subtype in subtypes
2071 * @param mode subtypes will be filtered by mode
2072 * @param locale subtypes will be filtered by locale
satok7599a7fb2010-12-22 13:45:23 +09002073 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2074 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002075 * @return the most applicable subtypeId
2076 */
satokdf31ae62011-01-15 06:19:44 +09002077 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2078 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7fb2010-12-22 13:45:23 +09002079 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002080 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002081 return null;
satok8fbb1e82010-11-02 23:15:58 +09002082 }
satok4e4569d2010-11-19 18:45:53 +09002083 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002084 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002085 }
satok8fbb1e82010-11-02 23:15:58 +09002086 final String language = locale.substring(0, 2);
2087 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002088 InputMethodSubtype applicableSubtype = null;
satok7599a7fb2010-12-22 13:45:23 +09002089 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002090 final int N = subtypes.size();
2091 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002092 InputMethodSubtype subtype = subtypes.get(i);
2093 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002094 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2095 // and all subtypes with all modes can be candidates.
2096 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7fb2010-12-22 13:45:23 +09002097 if (firstMatchedModeSubtype == null) {
2098 firstMatchedModeSubtype = subtype;
2099 }
satok9ef02832010-11-04 21:17:48 +09002100 if (locale.equals(subtypeLocale)) {
2101 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002102 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002103 break;
2104 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2105 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002106 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002107 partialMatchFound = true;
2108 }
satok8fbb1e82010-11-02 23:15:58 +09002109 }
2110 }
2111
satok7599a7fb2010-12-22 13:45:23 +09002112 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2113 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002114 }
2115
satok8fbb1e82010-11-02 23:15:58 +09002116 // The first subtype applicable to the system locale will be defined as the most applicable
2117 // subtype.
2118 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002119 if (applicableSubtype != null) {
2120 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2121 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2122 }
satok8fbb1e82010-11-02 23:15:58 +09002123 }
satokcd7cd292010-11-20 15:46:23 +09002124 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002125 }
2126
satok4e4569d2010-11-19 18:45:53 +09002127 // If there are no selected shortcuts, tries finding the most applicable ones.
2128 private Pair<InputMethodInfo, InputMethodSubtype>
2129 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2130 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2131 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002132 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002133 boolean foundInSystemIME = false;
2134
2135 // Search applicable subtype for each InputMethodInfo
2136 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002137 final String imiId = imi.getId();
2138 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2139 continue;
2140 }
satokcd7cd292010-11-20 15:46:23 +09002141 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002142 final List<InputMethodSubtype> enabledSubtypes =
2143 getEnabledInputMethodSubtypeList(imi, true);
2144 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002145 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002146 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002147 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002148 }
satokdf31ae62011-01-15 06:19:44 +09002149 // 2. Search by the system locale from enabledSubtypes.
2150 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002151 if (subtype == null) {
2152 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002153 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002154 }
satok7599a7fb2010-12-22 13:45:23 +09002155 // 4. Search by the current subtype's locale from all subtypes.
2156 if (subtype == null && mCurrentSubtype != null) {
2157 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002158 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09002159 }
2160 // 5. Search by the system locale from all subtypes.
2161 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002162 if (subtype == null) {
satok7599a7fb2010-12-22 13:45:23 +09002163 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002164 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002165 }
satokcd7cd292010-11-20 15:46:23 +09002166 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09002167 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002168 // The current input method is the most applicable IME.
2169 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002170 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002171 break;
satok7599a7fb2010-12-22 13:45:23 +09002172 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002173 // The system input method is 2nd applicable IME.
2174 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002175 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09002176 if ((imi.getServiceInfo().applicationInfo.flags
2177 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2178 foundInSystemIME = true;
2179 }
satok4e4569d2010-11-19 18:45:53 +09002180 }
2181 }
2182 }
2183 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002184 if (mostApplicableIMI != null) {
2185 Slog.w(TAG, "Most applicable shortcut input method was:"
2186 + mostApplicableIMI.getId());
2187 if (mostApplicableSubtype != null) {
2188 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2189 + "," + mostApplicableSubtype.getMode() + ","
2190 + mostApplicableSubtype.getLocale());
2191 }
2192 }
satok4e4569d2010-11-19 18:45:53 +09002193 }
satokcd7cd292010-11-20 15:46:23 +09002194 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002195 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002196 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002197 } else {
2198 return null;
2199 }
2200 }
2201
satokab751aa2010-09-14 19:17:36 +09002202 /**
2203 * @return Return the current subtype of this input method.
2204 */
2205 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002206 boolean subtypeIsSelected = false;
2207 try {
2208 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2209 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2210 } catch (SettingNotFoundException e) {
2211 }
satok3ef8b292010-11-23 06:06:29 +09002212 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002213 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002214 String lastInputMethodId = Settings.Secure.getString(
2215 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002216 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2217 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002218 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2219 if (imi != null) {
2220 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002221 // the most applicable subtype from explicitly or implicitly enabled
2222 // subtypes.
2223 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2224 getEnabledInputMethodSubtypeList(imi, true);
2225 // If there is only one explicitly or implicitly enabled subtype,
2226 // just returns it.
2227 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2228 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2229 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2230 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2231 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2232 SUBTYPE_MODE_KEYBOARD, null, true);
2233 if (mCurrentSubtype == null) {
2234 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2235 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2236 true);
2237 }
2238 }
satok4e4569d2010-11-19 18:45:53 +09002239 }
satokcd7cd292010-11-20 15:46:23 +09002240 } else {
satok3ef8b292010-11-23 06:06:29 +09002241 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002242 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002243 }
satok8fbb1e82010-11-02 23:15:58 +09002244 }
satok3ef8b292010-11-23 06:06:29 +09002245 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002246 }
satokab751aa2010-09-14 19:17:36 +09002247 }
2248
satokf3db1af2010-11-23 13:34:33 +09002249 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2250 InputMethodSubtype subtype) {
2251 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2252 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2253 } else {
2254 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2255 subtypes.add(subtype);
2256 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2257 }
2258 }
2259
satok4e4569d2010-11-19 18:45:53 +09002260 // TODO: We should change the return type from List to List<Parcelable>
2261 public List getShortcutInputMethodsAndSubtypes() {
2262 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002263 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002264 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002265 // If there are no selected shortcut subtypes, the framework will try to find
2266 // the most applicable subtype from all subtypes whose mode is
2267 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002268 Pair<InputMethodInfo, InputMethodSubtype> info =
2269 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2270 SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09002271 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002272 ret.add(info.first);
2273 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09002274 }
satok3da92232011-01-11 22:46:30 +09002275 return ret;
satokf3db1af2010-11-23 13:34:33 +09002276 }
satokf3db1af2010-11-23 13:34:33 +09002277 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2278 ret.add(imi);
2279 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2280 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002281 }
2282 }
satokf3db1af2010-11-23 13:34:33 +09002283 return ret;
satok4e4569d2010-11-19 18:45:53 +09002284 }
2285 }
2286
satokb66d2872010-11-10 01:04:04 +09002287 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2288 synchronized (mMethodMap) {
2289 if (subtype != null && mCurMethodId != null) {
2290 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2291 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2292 if (subtypeId != NOT_A_SUBTYPE_ID) {
2293 setInputMethodLocked(mCurMethodId, subtypeId);
2294 return true;
2295 }
2296 }
2297 return false;
2298 }
2299 }
2300
satokd87c2592010-09-29 11:52:06 +09002301 /**
2302 * Utility class for putting and getting settings for InputMethod
2303 * TODO: Move all putters and getters of settings to this class.
2304 */
2305 private static class InputMethodSettings {
2306 // The string for enabled input method is saved as follows:
2307 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2308 private static final char INPUT_METHOD_SEPARATER = ':';
2309 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002310 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002311 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2312
satok723a27e2010-11-11 14:58:11 +09002313 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002314 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2315
satokdf31ae62011-01-15 06:19:44 +09002316 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002317 private final ContentResolver mResolver;
2318 private final HashMap<String, InputMethodInfo> mMethodMap;
2319 private final ArrayList<InputMethodInfo> mMethodList;
2320
2321 private String mEnabledInputMethodsStrCache;
2322
2323 private static void buildEnabledInputMethodsSettingString(
2324 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2325 String id = pair.first;
2326 ArrayList<String> subtypes = pair.second;
2327 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002328 // Inputmethod and subtypes are saved in the settings as follows:
2329 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2330 for (String subtypeId: subtypes) {
2331 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002332 }
2333 }
2334
2335 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002336 Resources res, ContentResolver resolver,
2337 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2338 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002339 mResolver = resolver;
2340 mMethodMap = methodMap;
2341 mMethodList = methodList;
2342 }
2343
2344 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2345 return createEnabledInputMethodListLocked(
2346 getEnabledInputMethodsAndSubtypeListLocked());
2347 }
2348
satok7f35c8c2010-10-07 21:13:11 +09002349 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002350 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2351 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002352 getEnabledInputMethodsAndSubtypeListLocked());
2353 }
2354
satok67ddf9c2010-11-17 09:45:54 +09002355 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2356 InputMethodInfo imi) {
2357 List<Pair<String, ArrayList<String>>> imsList =
2358 getEnabledInputMethodsAndSubtypeListLocked();
2359 ArrayList<InputMethodSubtype> enabledSubtypes =
2360 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002361 if (imi != null) {
2362 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2363 InputMethodInfo info = mMethodMap.get(imsPair.first);
2364 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002365 final int subtypeCount = info.getSubtypeCount();
2366 for (int i = 0; i < subtypeCount; ++i) {
2367 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002368 for (String s: imsPair.second) {
2369 if (String.valueOf(ims.hashCode()).equals(s)) {
2370 enabledSubtypes.add(ims);
2371 }
satok67ddf9c2010-11-17 09:45:54 +09002372 }
2373 }
satok884ef9a2010-11-18 10:39:46 +09002374 break;
satok67ddf9c2010-11-17 09:45:54 +09002375 }
satok67ddf9c2010-11-17 09:45:54 +09002376 }
2377 }
2378 return enabledSubtypes;
2379 }
2380
satokd87c2592010-09-29 11:52:06 +09002381 // At the initial boot, the settings for input methods are not set,
2382 // so we need to enable IME in that case.
2383 public void enableAllIMEsIfThereIsNoEnabledIME() {
2384 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2385 StringBuilder sb = new StringBuilder();
2386 final int N = mMethodList.size();
2387 for (int i = 0; i < N; i++) {
2388 InputMethodInfo imi = mMethodList.get(i);
2389 Slog.i(TAG, "Adding: " + imi.getId());
2390 if (i > 0) sb.append(':');
2391 sb.append(imi.getId());
2392 }
2393 putEnabledInputMethodsStr(sb.toString());
2394 }
2395 }
2396
satokbb4aa062011-01-19 21:40:27 +09002397 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002398 ArrayList<Pair<String, ArrayList<String>>> imsList
2399 = new ArrayList<Pair<String, ArrayList<String>>>();
2400 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2401 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2402 return imsList;
2403 }
satok723a27e2010-11-11 14:58:11 +09002404 mInputMethodSplitter.setString(enabledInputMethodsStr);
2405 while (mInputMethodSplitter.hasNext()) {
2406 String nextImsStr = mInputMethodSplitter.next();
2407 mSubtypeSplitter.setString(nextImsStr);
2408 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002409 ArrayList<String> subtypeHashes = new ArrayList<String>();
2410 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002411 String imeId = mSubtypeSplitter.next();
2412 while (mSubtypeSplitter.hasNext()) {
2413 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002414 }
2415 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2416 }
2417 }
2418 return imsList;
2419 }
2420
2421 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2422 if (reloadInputMethodStr) {
2423 getEnabledInputMethodsStr();
2424 }
2425 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2426 // Add in the newly enabled input method.
2427 putEnabledInputMethodsStr(id);
2428 } else {
2429 putEnabledInputMethodsStr(
2430 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2431 }
2432 }
2433
2434 /**
2435 * Build and put a string of EnabledInputMethods with removing specified Id.
2436 * @return the specified id was removed or not.
2437 */
2438 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2439 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2440 boolean isRemoved = false;
2441 boolean needsAppendSeparator = false;
2442 for (Pair<String, ArrayList<String>> ims: imsList) {
2443 String curId = ims.first;
2444 if (curId.equals(id)) {
2445 // We are disabling this input method, and it is
2446 // currently enabled. Skip it to remove from the
2447 // new list.
2448 isRemoved = true;
2449 } else {
2450 if (needsAppendSeparator) {
2451 builder.append(INPUT_METHOD_SEPARATER);
2452 } else {
2453 needsAppendSeparator = true;
2454 }
2455 buildEnabledInputMethodsSettingString(builder, ims);
2456 }
2457 }
2458 if (isRemoved) {
2459 // Update the setting with the new list of input methods.
2460 putEnabledInputMethodsStr(builder.toString());
2461 }
2462 return isRemoved;
2463 }
2464
2465 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2466 List<Pair<String, ArrayList<String>>> imsList) {
2467 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2468 for (Pair<String, ArrayList<String>> ims: imsList) {
2469 InputMethodInfo info = mMethodMap.get(ims.first);
2470 if (info != null) {
2471 res.add(info);
2472 }
2473 }
2474 return res;
2475 }
2476
satok7f35c8c2010-10-07 21:13:11 +09002477 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002478 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002479 List<Pair<String, ArrayList<String>>> imsList) {
2480 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2481 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2482 for (Pair<String, ArrayList<String>> ims : imsList) {
2483 InputMethodInfo info = mMethodMap.get(ims.first);
2484 if (info != null) {
2485 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2486 }
2487 }
2488 return res;
2489 }
2490
satokd87c2592010-09-29 11:52:06 +09002491 private void putEnabledInputMethodsStr(String str) {
2492 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2493 mEnabledInputMethodsStrCache = str;
2494 }
2495
2496 private String getEnabledInputMethodsStr() {
2497 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2498 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002499 if (DEBUG) {
2500 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2501 }
satokd87c2592010-09-29 11:52:06 +09002502 return mEnabledInputMethodsStrCache;
2503 }
satok723a27e2010-11-11 14:58:11 +09002504
2505 private void saveSubtypeHistory(
2506 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2507 StringBuilder builder = new StringBuilder();
2508 boolean isImeAdded = false;
2509 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2510 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2511 newSubtypeId);
2512 isImeAdded = true;
2513 }
2514 for (Pair<String, String> ime: savedImes) {
2515 String imeId = ime.first;
2516 String subtypeId = ime.second;
2517 if (TextUtils.isEmpty(subtypeId)) {
2518 subtypeId = NOT_A_SUBTYPE_ID_STR;
2519 }
2520 if (isImeAdded) {
2521 builder.append(INPUT_METHOD_SEPARATER);
2522 } else {
2523 isImeAdded = true;
2524 }
2525 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2526 subtypeId);
2527 }
2528 // Remove the last INPUT_METHOD_SEPARATER
2529 putSubtypeHistoryStr(builder.toString());
2530 }
2531
2532 public void addSubtypeToHistory(String imeId, String subtypeId) {
2533 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2534 for (Pair<String, String> ime: subtypeHistory) {
2535 if (ime.first.equals(imeId)) {
2536 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002537 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002538 + ime.second);
2539 }
2540 // We should break here
2541 subtypeHistory.remove(ime);
2542 break;
2543 }
2544 }
satokbb4aa062011-01-19 21:40:27 +09002545 if (DEBUG) {
2546 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2547 }
satok723a27e2010-11-11 14:58:11 +09002548 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2549 }
2550
2551 private void putSubtypeHistoryStr(String str) {
2552 if (DEBUG) {
2553 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2554 }
2555 Settings.Secure.putString(
2556 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2557 }
2558
2559 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2560 // Gets the first one from the history
2561 return getLastSubtypeForInputMethodLockedInternal(null);
2562 }
2563
2564 public String getLastSubtypeForInputMethodLocked(String imeId) {
2565 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2566 if (ime != null) {
2567 return ime.second;
2568 } else {
2569 return null;
2570 }
2571 }
2572
2573 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2574 List<Pair<String, ArrayList<String>>> enabledImes =
2575 getEnabledInputMethodsAndSubtypeListLocked();
2576 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2577 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2578 final String imeInTheHistory = imeAndSubtype.first;
2579 // If imeId is empty, returns the first IME and subtype in the history
2580 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2581 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002582 final String subtypeHashCode =
2583 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2584 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002585 if (!TextUtils.isEmpty(subtypeHashCode)) {
2586 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002587 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002588 }
2589 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2590 }
2591 }
2592 }
2593 if (DEBUG) {
2594 Slog.d(TAG, "No enabled IME found in the history");
2595 }
2596 return null;
2597 }
2598
satokdf31ae62011-01-15 06:19:44 +09002599 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002600 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2601 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2602 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002603 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2604 if (explicitlyEnabledSubtypes.size() == 0) {
2605 // If there are no explicitly enabled subtypes, applicable subtypes are
2606 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002607 InputMethodInfo ime = mMethodMap.get(imeId);
2608 // If IME is enabled and no subtypes are enabled, applicable subtypes
2609 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002610 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002611 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002612 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002613 if (implicitlySelectedSubtypes != null) {
2614 final int N = implicitlySelectedSubtypes.size();
2615 for (int i = 0; i < N; ++i) {
2616 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2617 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2618 return subtypeHashCode;
2619 }
2620 }
2621 }
2622 }
2623 } else {
satokf6cafb62011-01-17 16:29:02 +09002624 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002625 if (s.equals(subtypeHashCode)) {
2626 // If both imeId and subtypeId are enabled, return subtypeId.
2627 return s;
2628 }
satok723a27e2010-11-11 14:58:11 +09002629 }
2630 }
2631 // If imeId was enabled but subtypeId was disabled.
2632 return NOT_A_SUBTYPE_ID_STR;
2633 }
2634 }
2635 // If both imeId and subtypeId are disabled, return null
2636 return null;
2637 }
2638
2639 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2640 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2641 final String subtypeHistoryStr = getSubtypeHistoryStr();
2642 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2643 return imsList;
2644 }
2645 mInputMethodSplitter.setString(subtypeHistoryStr);
2646 while (mInputMethodSplitter.hasNext()) {
2647 String nextImsStr = mInputMethodSplitter.next();
2648 mSubtypeSplitter.setString(nextImsStr);
2649 if (mSubtypeSplitter.hasNext()) {
2650 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2651 // The first element is ime id.
2652 String imeId = mSubtypeSplitter.next();
2653 while (mSubtypeSplitter.hasNext()) {
2654 subtypeId = mSubtypeSplitter.next();
2655 break;
2656 }
2657 imsList.add(new Pair<String, String>(imeId, subtypeId));
2658 }
2659 }
2660 return imsList;
2661 }
2662
2663 private String getSubtypeHistoryStr() {
2664 if (DEBUG) {
2665 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2666 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2667 }
2668 return Settings.Secure.getString(
2669 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2670 }
2671
2672 public void putSelectedInputMethod(String imeId) {
2673 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2674 }
2675
2676 public void putSelectedSubtype(int subtypeId) {
2677 Settings.Secure.putInt(
2678 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2679 }
satokd87c2592010-09-29 11:52:06 +09002680 }
2681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 @Override
2685 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2686 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2687 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2690 + Binder.getCallingPid()
2691 + ", uid=" + Binder.getCallingUid());
2692 return;
2693 }
2694
2695 IInputMethod method;
2696 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 synchronized (mMethodMap) {
2701 p.println("Current Input Method Manager state:");
2702 int N = mMethodList.size();
2703 p.println(" Input Methods:");
2704 for (int i=0; i<N; i++) {
2705 InputMethodInfo info = mMethodList.get(i);
2706 p.println(" InputMethod #" + i + ":");
2707 info.dump(p, " ");
2708 }
2709 p.println(" Clients:");
2710 for (ClientState ci : mClients.values()) {
2711 p.println(" Client " + ci + ":");
2712 p.println(" client=" + ci.client);
2713 p.println(" inputContext=" + ci.inputContext);
2714 p.println(" sessionRequested=" + ci.sessionRequested);
2715 p.println(" curSession=" + ci.curSession);
2716 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002717 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002719 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2720 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2722 + " mBoundToMethod=" + mBoundToMethod);
2723 p.println(" mCurToken=" + mCurToken);
2724 p.println(" mCurIntent=" + mCurIntent);
2725 method = mCurMethod;
2726 p.println(" mCurMethod=" + mCurMethod);
2727 p.println(" mEnabledSession=" + mEnabledSession);
2728 p.println(" mShowRequested=" + mShowRequested
2729 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2730 + " mShowForced=" + mShowForced
2731 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002732 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002734
Jeff Brownb88102f2010-09-08 11:49:43 -07002735 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 pw.flush();
2738 try {
2739 client.client.asBinder().dump(fd, args);
2740 } catch (RemoteException e) {
2741 p.println("Input method client dead: " + e);
2742 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002743 } else {
2744 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002746
Jeff Brownb88102f2010-09-08 11:49:43 -07002747 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 pw.flush();
2750 try {
2751 method.asBinder().dump(fd, args);
2752 } catch (RemoteException e) {
2753 p.println("Input method service dead: " + e);
2754 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002755 } else {
2756 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 }
2758 }
2759}