blob: 4d406209e74b06098e893f5036b38dfef112fb28 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Binder;
53import android.os.Handler;
54import android.os.IBinder;
55import android.os.IInterface;
56import android.os.Message;
57import android.os.Parcel;
58import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080059import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.ServiceManager;
61import android.os.SystemClock;
62import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070063import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090064import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.text.TextUtils;
66import android.util.EventLog;
satokab751aa2010-09-14 19:17:36 +090067import android.util.Pair;
Joe Onorato8a9b2202010-02-26 18:56:32 -080068import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.util.PrintWriterPrinter;
70import android.util.Printer;
71import android.view.IWindowManager;
72import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090073import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.view.inputmethod.InputBinding;
75import android.view.inputmethod.InputMethod;
76import android.view.inputmethod.InputMethodInfo;
77import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090078import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80import java.io.FileDescriptor;
81import java.io.IOException;
82import java.io.PrintWriter;
satok913a8922010-08-26 21:53:41 +090083import java.text.Collator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import java.util.ArrayList;
85import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +090086import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import java.util.List;
satok913a8922010-08-26 21:53:41 +090088import java.util.Map;
89import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91/**
92 * This class provides a system service that manages input methods.
93 */
94public class InputMethodManagerService extends IInputMethodManager.Stub
95 implements ServiceConnection, Handler.Callback {
96 static final boolean DEBUG = false;
97 static final String TAG = "InputManagerService";
98
99 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900100 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900101 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900102 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 static final int MSG_UNBIND_INPUT = 1000;
105 static final int MSG_BIND_INPUT = 1010;
106 static final int MSG_SHOW_SOFT_INPUT = 1020;
107 static final int MSG_HIDE_SOFT_INPUT = 1030;
108 static final int MSG_ATTACH_TOKEN = 1040;
109 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 static final int MSG_START_INPUT = 2000;
112 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 static final int MSG_UNBIND_METHOD = 3000;
115 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800118
satokab751aa2010-09-14 19:17:36 +0900119 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900120 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok217f5482010-12-15 05:19:19 +0900121 private static final String EXTRA_INPUT_METHOD_ID = "input_method_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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800126 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900128 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700130 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 final IWindowManager mIWindowManager;
132 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 // All known input methods. mMethodMap also serves as the global
137 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900138 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
139 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 class SessionState {
142 final ClientState client;
143 final IInputMethod method;
144 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 @Override
147 public String toString() {
148 return "SessionState{uid " + client.uid + " pid " + client.pid
149 + " method " + Integer.toHexString(
150 System.identityHashCode(method))
151 + " session " + Integer.toHexString(
152 System.identityHashCode(session))
153 + "}";
154 }
155
156 SessionState(ClientState _client, IInputMethod _method,
157 IInputMethodSession _session) {
158 client = _client;
159 method = _method;
160 session = _session;
161 }
162 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 class ClientState {
165 final IInputMethodClient client;
166 final IInputContext inputContext;
167 final int uid;
168 final int pid;
169 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 boolean sessionRequested;
172 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 @Override
175 public String toString() {
176 return "ClientState{" + Integer.toHexString(
177 System.identityHashCode(this)) + " uid " + uid
178 + " pid " + pid + "}";
179 }
180
181 ClientState(IInputMethodClient _client, IInputContext _inputContext,
182 int _uid, int _pid) {
183 client = _client;
184 inputContext = _inputContext;
185 uid = _uid;
186 pid = _pid;
187 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
188 }
189 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 final HashMap<IBinder, ClientState> mClients
192 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700195 * Set once the system is ready to run third party code.
196 */
197 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800198
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700199 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * Id of the currently selected input method.
201 */
202 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 /**
205 * The current binding sequence number, incremented every time there is
206 * a new bind performed.
207 */
208 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 /**
211 * The client that is currently bound to an input method.
212 */
213 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700216 * The last window token that gained focus.
217 */
218 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800219
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700220 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 * The input context last provided by the current client.
222 */
223 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 /**
226 * The attributes last provided by the current client.
227 */
228 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 /**
231 * The input method ID of the input method service that we are currently
232 * connected to or in the process of connecting to.
233 */
234 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 /**
satokab751aa2010-09-14 19:17:36 +0900237 * The current subtype of the current input method.
238 */
239 private InputMethodSubtype mCurrentSubtype;
240
satok4e4569d2010-11-19 18:45:53 +0900241 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900242 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
243 mShortcutInputMethodsAndSubtypes =
244 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900245
246 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * Set to true if our ServiceConnection is currently actively bound to
248 * a service (whether or not we have gotten its IBinder back yet).
249 */
250 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 /**
253 * Set if the client has asked for the input method to be shown.
254 */
255 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 /**
258 * Set if we were explicitly told to show the input method.
259 */
260 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
263 * Set if we were forced to be shown.
264 */
265 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 /**
268 * Set if we last told the input method to show itself.
269 */
270 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 /**
273 * The Intent used to connect to the current input method.
274 */
275 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
278 * The token we have made for the currently active input method, to
279 * identify it in the future.
280 */
281 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 /**
284 * If non-null, this is the input method service we are currently connected
285 * to.
286 */
287 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 /**
290 * Time that we last initiated a bind to the input method, to determine
291 * if we should try to disconnect and reconnect to it.
292 */
293 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 /**
296 * Have we called mCurMethod.bindInput()?
297 */
298 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 /**
301 * Currently enabled session. Only touched by service thread, not
302 * protected by a lock.
303 */
304 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 /**
307 * True if the screen is on. The value is true initially.
308 */
309 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 AlertDialog.Builder mDialogBuilder;
312 AlertDialog mSwitchingDialog;
313 InputMethodInfo[] mIms;
314 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900315 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 class SettingsObserver extends ContentObserver {
318 SettingsObserver(Handler handler) {
319 super(handler);
320 ContentResolver resolver = mContext.getContentResolver();
321 resolver.registerContentObserver(Settings.Secure.getUriFor(
322 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900323 resolver.registerContentObserver(Settings.Secure.getUriFor(
324 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 @Override public void onChange(boolean selfChange) {
328 synchronized (mMethodMap) {
329 updateFromSettingsLocked();
330 }
331 }
332 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
335 @Override
336 public void onReceive(Context context, Intent intent) {
337 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
338 mScreenOn = true;
339 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
340 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700341 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
342 hideInputMethodMenu();
343 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800345 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 }
347
348 // Inform the current client of the change in active status
349 try {
350 if (mCurClient != null && mCurClient.client != null) {
351 mCurClient.client.setActive(mScreenOn);
352 }
353 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800354 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 + mCurClient.pid + " uid " + mCurClient.uid);
356 }
357 }
358 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800359
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800360 class MyPackageMonitor extends PackageMonitor {
361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800363 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800365 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
367 final int N = mMethodList.size();
368 if (curInputMethodId != null) {
369 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800370 InputMethodInfo imi = mMethodList.get(i);
371 if (imi.getId().equals(curInputMethodId)) {
372 for (String pkg : packages) {
373 if (imi.getPackageName().equals(pkg)) {
374 if (!doit) {
375 return true;
376 }
satok723a27e2010-11-11 14:58:11 +0900377 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800378 chooseNewDefaultIMELocked();
379 return true;
380 }
381 }
382 }
383 }
384 }
385 }
386 return false;
387 }
388
389 @Override
390 public void onSomePackagesChanged() {
391 synchronized (mMethodMap) {
392 InputMethodInfo curIm = null;
393 String curInputMethodId = Settings.Secure.getString(mContext
394 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
395 final int N = mMethodList.size();
396 if (curInputMethodId != null) {
397 for (int i=0; i<N; i++) {
398 InputMethodInfo imi = mMethodList.get(i);
399 if (imi.getId().equals(curInputMethodId)) {
400 curIm = imi;
401 }
402 int change = isPackageDisappearing(imi.getPackageName());
403 if (change == PACKAGE_TEMPORARY_CHANGE
404 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800405 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800406 + imi.getComponent());
407 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 }
409 }
410 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800411
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800412 buildInputMethodListLocked(mMethodList, mMethodMap);
413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800415
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800416 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800417 int change = isPackageDisappearing(curIm.getPackageName());
418 if (change == PACKAGE_TEMPORARY_CHANGE
419 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800420 ServiceInfo si = null;
421 try {
422 si = mContext.getPackageManager().getServiceInfo(
423 curIm.getComponent(), 0);
424 } catch (PackageManager.NameNotFoundException ex) {
425 }
426 if (si == null) {
427 // Uh oh, current input method is no longer around!
428 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800429 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satokca830212011-01-13 21:15:04 +0900430 mStatusBar.setIMEButtonVisible(mCurToken, false);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800431 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800432 changed = true;
433 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800434 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900435 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800436 }
437 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800438 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800439 }
satokab751aa2010-09-14 19:17:36 +0900440
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800441 if (curIm == null) {
442 // We currently don't have a default input method... is
443 // one now available?
444 changed = chooseNewDefaultIMELocked();
445 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800446
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800447 if (changed) {
448 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
450 }
451 }
452 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 class MethodCallback extends IInputMethodCallback.Stub {
455 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 MethodCallback(IInputMethod method) {
458 mMethod = method;
459 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 public void finishedEvent(int seq, boolean handled) throws RemoteException {
462 }
463
464 public void sessionCreated(IInputMethodSession session) throws RemoteException {
465 onSessionCreated(mMethod, session);
466 }
467 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800468
Joe Onorato089de882010-04-12 08:18:45 -0700469 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800471 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 mHandler = new Handler(this);
473 mIWindowManager = IWindowManager.Stub.asInterface(
474 ServiceManager.getService(Context.WINDOW_SERVICE));
475 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
476 public void executeMessage(Message msg) {
477 handleMessage(msg);
478 }
479 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800480
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800481 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 IntentFilter screenOnOffFilt = new IntentFilter();
484 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
485 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700486 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800488
satok913a8922010-08-26 21:53:41 +0900489 mStatusBar = statusBar;
490 statusBar.setIconVisibility("ime", false);
491
satokd87c2592010-09-29 11:52:06 +0900492 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900493 mSettings = new InputMethodSettings(
494 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900496 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497
satokd87c2592010-09-29 11:52:06 +0900498 if (TextUtils.isEmpty(Settings.Secure.getString(
499 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900501 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
503 try {
satokd87c2592010-09-29 11:52:06 +0900504 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 imi.getPackageName(), 0).getResources();
506 if (res.getBoolean(imi.getIsDefaultResourceId())) {
507 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800508 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 }
510 } catch (PackageManager.NameNotFoundException ex) {
511 } catch (Resources.NotFoundException ex) {
512 }
513 }
514 }
satokd87c2592010-09-29 11:52:06 +0900515 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800517 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900520 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 }
522 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 mSettingsObserver = new SettingsObserver(mHandler);
525 updateFromSettingsLocked();
526 }
527
528 @Override
529 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
530 throws RemoteException {
531 try {
532 return super.onTransact(code, data, reply, flags);
533 } catch (RuntimeException e) {
534 // The input method manager only throws security exceptions, so let's
535 // log all others.
536 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800537 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 }
539 throw e;
540 }
541 }
542
543 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700544 synchronized (mMethodMap) {
545 if (!mSystemReady) {
546 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700547 try {
548 startInputInnerLocked();
549 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800550 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700551 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700552 }
553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 public List<InputMethodInfo> getInputMethodList() {
557 synchronized (mMethodMap) {
558 return new ArrayList<InputMethodInfo>(mMethodList);
559 }
560 }
561
562 public List<InputMethodInfo> getEnabledInputMethodList() {
563 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900564 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566 }
567
satok16331c82010-12-20 23:48:46 +0900568 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
569 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900570 synchronized (mMethodMap) {
satok884ef9a2010-11-18 10:39:46 +0900571 if (imi == null && mCurMethodId != null) {
572 imi = mMethodMap.get(mCurMethodId);
573 }
satok16331c82010-12-20 23:48:46 +0900574 final List<InputMethodSubtype> enabledSubtypes =
575 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
576 if (!allowsImplicitlySelectedSubtypes || enabledSubtypes.size() > 0) {
577 return enabledSubtypes;
578 } else {
satokdf31ae62011-01-15 06:19:44 +0900579 return getApplicableSubtypesLocked(mRes, imi.getSubtypes());
satok16331c82010-12-20 23:48:46 +0900580 }
satok67ddf9c2010-11-17 09:45:54 +0900581 }
582 }
583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 public void addClient(IInputMethodClient client,
585 IInputContext inputContext, int uid, int pid) {
586 synchronized (mMethodMap) {
587 mClients.put(client.asBinder(), new ClientState(client,
588 inputContext, uid, pid));
589 }
590 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 public void removeClient(IInputMethodClient client) {
593 synchronized (mMethodMap) {
594 mClients.remove(client.asBinder());
595 }
596 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 void executeOrSendMessage(IInterface target, Message msg) {
599 if (target.asBinder() instanceof Binder) {
600 mCaller.sendMessage(msg);
601 } else {
602 handleMessage(msg);
603 msg.recycle();
604 }
605 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800606
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700607 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800609 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 + mCurClient.client.asBinder());
611 if (mBoundToMethod) {
612 mBoundToMethod = false;
613 if (mCurMethod != null) {
614 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
615 MSG_UNBIND_INPUT, mCurMethod));
616 }
617 }
618 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
619 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
620 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 // Call setActive(false) on the old client
623 try {
624 mCurClient.client.setActive(false);
625 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800626 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 + mCurClient.pid + " uid " + mCurClient.uid);
628 }
629 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800630
The Android Open Source Project10592532009-03-18 17:39:46 -0700631 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 }
633 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 private int getImeShowFlags() {
636 int flags = 0;
637 if (mShowForced) {
638 flags |= InputMethod.SHOW_FORCED
639 | InputMethod.SHOW_EXPLICIT;
640 } else if (mShowExplicitlyRequested) {
641 flags |= InputMethod.SHOW_EXPLICIT;
642 }
643 return flags;
644 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 private int getAppShowFlags() {
647 int flags = 0;
648 if (mShowForced) {
649 flags |= InputMethodManager.SHOW_FORCED;
650 } else if (!mShowExplicitlyRequested) {
651 flags |= InputMethodManager.SHOW_IMPLICIT;
652 }
653 return flags;
654 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
657 if (!mBoundToMethod) {
658 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
659 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
660 mBoundToMethod = true;
661 }
662 final SessionState session = mCurClient.curSession;
663 if (initial) {
664 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
665 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
666 } else {
667 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
668 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
669 }
670 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800671 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800672 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 }
674 return needResult
675 ? new InputBindResult(session.session, mCurId, mCurSeq)
676 : null;
677 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 InputBindResult startInputLocked(IInputMethodClient client,
680 IInputContext inputContext, EditorInfo attribute,
681 boolean initial, boolean needResult) {
682 // If no method is currently selected, do nothing.
683 if (mCurMethodId == null) {
684 return mNoBinding;
685 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 ClientState cs = mClients.get(client.asBinder());
688 if (cs == null) {
689 throw new IllegalArgumentException("unknown client "
690 + client.asBinder());
691 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 try {
694 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
695 // Check with the window manager to make sure this client actually
696 // has a window with focus. If not, reject. This is thread safe
697 // because if the focus changes some time before or after, the
698 // next client receiving focus that has any interest in input will
699 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800700 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
702 return null;
703 }
704 } catch (RemoteException e) {
705 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 if (mCurClient != cs) {
708 // If the client is changing, we need to switch over to the new
709 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700710 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800711 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 + cs.client.asBinder());
713
714 // If the screen is on, inform the new client it is active
715 if (mScreenOn) {
716 try {
717 cs.client.setActive(mScreenOn);
718 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800719 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 + cs.pid + " uid " + cs.uid);
721 }
722 }
723 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 // Bump up the sequence for this client and attach it.
726 mCurSeq++;
727 if (mCurSeq <= 0) mCurSeq = 1;
728 mCurClient = cs;
729 mCurInputContext = inputContext;
730 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 // Check if the input method is changing.
733 if (mCurId != null && mCurId.equals(mCurMethodId)) {
734 if (cs.curSession != null) {
735 // Fast case: if we are already connected to the input method,
736 // then just return it.
737 return attachNewInputLocked(initial, needResult);
738 }
739 if (mHaveConnection) {
740 if (mCurMethod != null) {
741 if (!cs.sessionRequested) {
742 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800743 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
745 MSG_CREATE_SESSION, mCurMethod,
746 new MethodCallback(mCurMethod)));
747 }
748 // Return to client, and we will get back with it when
749 // we have had a session made for it.
750 return new InputBindResult(null, mCurId, mCurSeq);
751 } else if (SystemClock.uptimeMillis()
752 < (mLastBindTime+TIME_TO_RECONNECT)) {
753 // In this case we have connected to the service, but
754 // don't yet have its interface. If it hasn't been too
755 // long since we did the connection, we'll return to
756 // the client and wait to get the service interface so
757 // we can report back. If it has been too long, we want
758 // to fall through so we can try a disconnect/reconnect
759 // to see if we can get back in touch with the service.
760 return new InputBindResult(null, mCurId, mCurSeq);
761 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800762 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
763 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 }
765 }
766 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800767
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700768 return startInputInnerLocked();
769 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800770
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700771 InputBindResult startInputInnerLocked() {
772 if (mCurMethodId == null) {
773 return mNoBinding;
774 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800775
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700776 if (!mSystemReady) {
777 // If the system is not yet ready, we shouldn't be running third
778 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700779 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700780 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 InputMethodInfo info = mMethodMap.get(mCurMethodId);
783 if (info == null) {
784 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
785 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800786
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700787 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
790 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700791 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
792 com.android.internal.R.string.input_method_binding_label);
793 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
794 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
796 mLastBindTime = SystemClock.uptimeMillis();
797 mHaveConnection = true;
798 mCurId = info.getId();
799 mCurToken = new Binder();
800 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800801 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 mIWindowManager.addWindowToken(mCurToken,
803 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
804 } catch (RemoteException e) {
805 }
806 return new InputBindResult(null, mCurId, mCurSeq);
807 } else {
808 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800809 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 + mCurIntent);
811 }
812 return null;
813 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 public InputBindResult startInput(IInputMethodClient client,
816 IInputContext inputContext, EditorInfo attribute,
817 boolean initial, boolean needResult) {
818 synchronized (mMethodMap) {
819 final long ident = Binder.clearCallingIdentity();
820 try {
821 return startInputLocked(client, inputContext, attribute,
822 initial, needResult);
823 } finally {
824 Binder.restoreCallingIdentity(ident);
825 }
826 }
827 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 public void finishInput(IInputMethodClient client) {
830 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 public void onServiceConnected(ComponentName name, IBinder service) {
833 synchronized (mMethodMap) {
834 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
835 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700836 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800837 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700838 unbindCurrentMethodLocked(false);
839 return;
840 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800841 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700842 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
843 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800845 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700846 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700848 MSG_CREATE_SESSION, mCurMethod,
849 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 }
851 }
852 }
853 }
854
855 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
856 synchronized (mMethodMap) {
857 if (mCurMethod != null && method != null
858 && mCurMethod.asBinder() == method.asBinder()) {
859 if (mCurClient != null) {
860 mCurClient.curSession = new SessionState(mCurClient,
861 method, session);
862 mCurClient.sessionRequested = false;
863 InputBindResult res = attachNewInputLocked(true, true);
864 if (res.method != null) {
865 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
866 MSG_BIND_METHOD, mCurClient.client, res));
867 }
868 }
869 }
870 }
871 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800872
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700873 void unbindCurrentMethodLocked(boolean reportToClient) {
874 if (mHaveConnection) {
875 mContext.unbindService(this);
876 mHaveConnection = false;
877 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800878
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700879 if (mCurToken != null) {
880 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800881 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700882 mIWindowManager.removeWindowToken(mCurToken);
883 } catch (RemoteException e) {
884 }
885 mCurToken = null;
886 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800887
The Android Open Source Project10592532009-03-18 17:39:46 -0700888 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700889 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800890
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700891 if (reportToClient && mCurClient != null) {
892 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
893 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
894 }
895 }
896
Devin Taylor0c33ed22010-02-23 13:26:46 -0600897 private void finishSession(SessionState sessionState) {
898 if (sessionState != null && sessionState.session != null) {
899 try {
900 sessionState.session.finishSession();
901 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700902 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600903 }
904 }
905 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800906
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700907 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 if (mCurMethod != null) {
909 for (ClientState cs : mClients.values()) {
910 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600911 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 cs.curSession = null;
913 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600914
915 finishSession(mEnabledSession);
916 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 mCurMethod = null;
918 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700919 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 public void onServiceDisconnected(ComponentName name) {
923 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800924 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 + " mCurIntent=" + mCurIntent);
926 if (mCurMethod != null && mCurIntent != null
927 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700928 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 // We consider this to be a new bind attempt, since the system
930 // should now try to restart the service for us.
931 mLastBindTime = SystemClock.uptimeMillis();
932 mShowRequested = mInputShown;
933 mInputShown = false;
934 if (mCurClient != null) {
935 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
936 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
937 }
938 }
939 }
940 }
941
942 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700943 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 long ident = Binder.clearCallingIdentity();
945 try {
946 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700947 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 return;
949 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 synchronized (mMethodMap) {
952 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800953 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700954 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800956 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700957 mStatusBar.setIcon("ime", packageName, iconId, 0);
958 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 }
960 }
961 } finally {
962 Binder.restoreCallingIdentity(ident);
963 }
964 }
965
satok06487a52010-10-29 11:37:18 +0900966 public void setIMEButtonVisible(IBinder token, boolean visible) {
967 int uid = Binder.getCallingUid();
968 long ident = Binder.clearCallingIdentity();
969 try {
970 if (token == null || mCurToken != token) {
971 Slog.w(TAG, "Ignoring setIMEButtonVisible of uid " + uid + " token: " + token);
972 return;
973 }
974
975 synchronized (mMethodMap) {
satokcd7cd292010-11-20 15:46:23 +0900976 mStatusBar.setIMEButtonVisible(token, visible);
satok06487a52010-10-29 11:37:18 +0900977 }
978 } finally {
979 Binder.restoreCallingIdentity(ident);
980 }
981 }
982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700984 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
985 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
986 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
987 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +0900989 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +0900990 // There is no input method selected, try to choose new applicable input method.
991 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
992 id = Settings.Secure.getString(mContext.getContentResolver(),
993 Settings.Secure.DEFAULT_INPUT_METHOD);
994 }
995 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 try {
satokab751aa2010-09-14 19:17:36 +0900997 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800999 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001000 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001001 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
satokf3db1af2010-11-23 13:34:33 +09001003 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001004 } else {
1005 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001006 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001007 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
1009 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001010
satokab751aa2010-09-14 19:17:36 +09001011 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 InputMethodInfo info = mMethodMap.get(id);
1013 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001014 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 if (id.equals(mCurMethodId)) {
satokb66d2872010-11-10 01:04:04 +09001018 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
satokcd7cd292010-11-20 15:46:23 +09001019 InputMethodSubtype subtype = null;
satokb66d2872010-11-10 01:04:04 +09001020 if (subtypeId >= 0 && subtypeId < subtypes.size()) {
satokcd7cd292010-11-20 15:46:23 +09001021 subtype = subtypes.get(subtypeId);
1022 }
1023 if (subtype != mCurrentSubtype) {
1024 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001025 if (subtype != null) {
1026 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1027 }
satokcd7cd292010-11-20 15:46:23 +09001028 if (mCurMethod != null) {
1029 try {
satokcd7cd292010-11-20 15:46:23 +09001030 if (mInputShown) {
1031 // If mInputShown is false, there is no IME button on the
1032 // system bar.
1033 // Thus there is no need to make it invisible explicitly.
1034 mStatusBar.setIMEButtonVisible(mCurToken, true);
satokab751aa2010-09-14 19:17:36 +09001035 }
satokcd7cd292010-11-20 15:46:23 +09001036 // If subtype is null, try to find the most applicable one from
1037 // getCurrentInputMethodSubtype.
1038 if (subtype == null) {
1039 subtype = getCurrentInputMethodSubtype();
1040 }
1041 mCurMethod.changeInputMethodSubtype(subtype);
1042 } catch (RemoteException e) {
1043 return;
satokab751aa2010-09-14 19:17:36 +09001044 }
1045 }
1046 }
1047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 return;
1049 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 final long ident = Binder.clearCallingIdentity();
1052 try {
satokab751aa2010-09-14 19:17:36 +09001053 // Set a subtype to this input method.
1054 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001055 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1056 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1057 // because mCurMethodId is stored as a history in
1058 // setSelectedInputMethodAndSubtypeLocked().
1059 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060
1061 if (ActivityManagerNative.isSystemReady()) {
1062 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001063 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 intent.putExtra("input_method_id", id);
1065 mContext.sendBroadcast(intent);
1066 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001067 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 } finally {
1069 Binder.restoreCallingIdentity(ident);
1070 }
1071 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001072
The Android Open Source Project4df24232009-03-05 14:34:35 -08001073 public boolean showSoftInput(IInputMethodClient client, int flags,
1074 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001075 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 long ident = Binder.clearCallingIdentity();
1077 try {
1078 synchronized (mMethodMap) {
1079 if (mCurClient == null || client == null
1080 || mCurClient.client.asBinder() != client.asBinder()) {
1081 try {
1082 // We need to check if this is the current client with
1083 // focus in the window manager, to allow this call to
1084 // be made before input is started in it.
1085 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001086 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001087 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 }
1089 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001090 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 }
1092 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001093
Joe Onorato8a9b2202010-02-26 18:56:32 -08001094 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001095 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 }
1097 } finally {
1098 Binder.restoreCallingIdentity(ident);
1099 }
1100 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001101
The Android Open Source Project4df24232009-03-05 14:34:35 -08001102 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 mShowRequested = true;
1104 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1105 mShowExplicitlyRequested = true;
1106 }
1107 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1108 mShowExplicitlyRequested = true;
1109 mShowForced = true;
1110 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001111
Dianne Hackborncc278702009-09-02 23:07:23 -07001112 if (!mSystemReady) {
1113 return false;
1114 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001115
The Android Open Source Project4df24232009-03-05 14:34:35 -08001116 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001118 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1119 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1120 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001122 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 } else if (mHaveConnection && SystemClock.uptimeMillis()
1124 < (mLastBindTime+TIME_TO_RECONNECT)) {
1125 // The client has asked to have the input method shown, but
1126 // we have been sitting here too long with a connection to the
1127 // service and no interface received, so let's disconnect/connect
1128 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001129 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 SystemClock.uptimeMillis()-mLastBindTime,1);
1131 mContext.unbindService(this);
1132 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1133 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001134
The Android Open Source Project4df24232009-03-05 14:34:35 -08001135 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001137
The Android Open Source Project4df24232009-03-05 14:34:35 -08001138 public boolean hideSoftInput(IInputMethodClient client, int flags,
1139 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001140 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 long ident = Binder.clearCallingIdentity();
1142 try {
1143 synchronized (mMethodMap) {
1144 if (mCurClient == null || client == null
1145 || mCurClient.client.asBinder() != client.asBinder()) {
1146 try {
1147 // We need to check if this is the current client with
1148 // focus in the window manager, to allow this call to
1149 // be made before input is started in it.
1150 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001151 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1152 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001153 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 }
1155 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001156 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 }
1158 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001159
Joe Onorato8a9b2202010-02-26 18:56:32 -08001160 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001161 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 }
1163 } finally {
1164 Binder.restoreCallingIdentity(ident);
1165 }
1166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001167
The Android Open Source Project4df24232009-03-05 14:34:35 -08001168 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1170 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001171 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001173 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 }
1175 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001176 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001178 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001180 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001182 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1183 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1184 res = true;
1185 } else {
1186 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 }
1188 mInputShown = false;
1189 mShowRequested = false;
1190 mShowExplicitlyRequested = false;
1191 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001192 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001194
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001195 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1197 boolean first, int windowFlags) {
1198 long ident = Binder.clearCallingIdentity();
1199 try {
1200 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001201 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 + " viewHasFocus=" + viewHasFocus
1203 + " isTextEditor=" + isTextEditor
1204 + " softInputMode=#" + Integer.toHexString(softInputMode)
1205 + " first=" + first + " flags=#"
1206 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 if (mCurClient == null || client == null
1209 || mCurClient.client.asBinder() != client.asBinder()) {
1210 try {
1211 // We need to check if this is the current client with
1212 // focus in the window manager, to allow this call to
1213 // be made before input is started in it.
1214 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001215 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 return;
1217 }
1218 } catch (RemoteException e) {
1219 }
1220 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001221
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001222 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001223 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001224 return;
1225 }
1226 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001227
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001228 // Should we auto-show the IME even if the caller has not
1229 // specified what should be done with it?
1230 // We only do this automatically if the window can resize
1231 // to accommodate the IME (so what the user sees will give
1232 // them good context without input information being obscured
1233 // by the IME) or if running on a large screen where there
1234 // is more room for the target window + IME.
1235 final boolean doAutoShow =
1236 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1237 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1238 || mRes.getConfiguration().isLayoutSizeAtLeast(
1239 Configuration.SCREENLAYOUT_SIZE_LARGE);
1240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1242 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001243 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1245 // There is no focus view, and this window will
1246 // be behind any soft input window, so hide the
1247 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001248 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001249 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001251 } else if (isTextEditor && doAutoShow && (softInputMode &
1252 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 // There is a focus view, and we are navigating forward
1254 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001255 // We only do this automatically if the window an resize
1256 // to accomodate the IME (so what the user sees will give
1257 // them good context without input information being obscured
1258 // by the IME) or if running on a large screen where there
1259 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001260 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001261 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 }
1263 break;
1264 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1265 // Do nothing.
1266 break;
1267 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1268 if ((softInputMode &
1269 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001270 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001271 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 }
1273 break;
1274 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001275 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001276 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 break;
1278 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1279 if ((softInputMode &
1280 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001281 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001282 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 }
1284 break;
1285 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001286 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001287 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 break;
1289 }
1290 }
1291 } finally {
1292 Binder.restoreCallingIdentity(ident);
1293 }
1294 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1297 synchronized (mMethodMap) {
1298 if (mCurClient == null || client == null
1299 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001300 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001301 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 }
1303
satok440aab52010-11-25 09:43:11 +09001304 // Always call subtype picker, because subtype picker is a superset of input method
1305 // picker.
satokab751aa2010-09-14 19:17:36 +09001306 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1307 }
1308 }
1309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001311 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1312 }
1313
1314 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1315 synchronized (mMethodMap) {
1316 if (subtype != null) {
1317 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1318 mMethodMap.get(id), subtype.hashCode()));
1319 } else {
1320 setInputMethod(token, id);
1321 }
1322 }
satokab751aa2010-09-14 19:17:36 +09001323 }
1324
satokb416a71e2010-11-25 20:42:14 +09001325 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001326 IInputMethodClient client, String inputMethodId) {
satokb416a71e2010-11-25 20:42:14 +09001327 synchronized (mMethodMap) {
1328 if (mCurClient == null || client == null
1329 || mCurClient.client.asBinder() != client.asBinder()) {
1330 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1331 }
satok7fee71f2010-12-17 18:54:26 +09001332 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1333 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09001334 }
1335 }
1336
satok735cf382010-11-11 20:40:09 +09001337 public boolean switchToLastInputMethod(IBinder token) {
1338 synchronized (mMethodMap) {
1339 Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1340 if (lastIme != null) {
1341 InputMethodInfo imi = mMethodMap.get(lastIme.first);
1342 if (imi != null) {
satok28203512010-11-24 11:06:49 +09001343 setInputMethodWithSubtypeId(token, lastIme.first, getSubtypeIdFromHashCode(
satok735cf382010-11-11 20:40:09 +09001344 imi, Integer.valueOf(lastIme.second)));
1345 return true;
1346 }
1347 }
1348 return false;
1349 }
1350 }
1351
satok28203512010-11-24 11:06:49 +09001352 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 synchronized (mMethodMap) {
1354 if (token == null) {
1355 if (mContext.checkCallingOrSelfPermission(
1356 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1357 != PackageManager.PERMISSION_GRANTED) {
1358 throw new SecurityException(
1359 "Using null token requires permission "
1360 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1361 }
1362 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001363 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1364 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 return;
1366 }
1367
1368 long ident = Binder.clearCallingIdentity();
1369 try {
satokab751aa2010-09-14 19:17:36 +09001370 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 } finally {
1372 Binder.restoreCallingIdentity(ident);
1373 }
1374 }
1375 }
1376
1377 public void hideMySoftInput(IBinder token, int flags) {
1378 synchronized (mMethodMap) {
1379 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001380 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1381 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 return;
1383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 long ident = Binder.clearCallingIdentity();
1385 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001386 hideCurrentInputLocked(flags, null);
1387 } finally {
1388 Binder.restoreCallingIdentity(ident);
1389 }
1390 }
1391 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001392
The Android Open Source Project4df24232009-03-05 14:34:35 -08001393 public void showMySoftInput(IBinder token, int flags) {
1394 synchronized (mMethodMap) {
1395 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001396 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1397 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001398 return;
1399 }
1400 long ident = Binder.clearCallingIdentity();
1401 try {
1402 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 } finally {
1404 Binder.restoreCallingIdentity(ident);
1405 }
1406 }
1407 }
1408
1409 void setEnabledSessionInMainThread(SessionState session) {
1410 if (mEnabledSession != session) {
1411 if (mEnabledSession != null) {
1412 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001413 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 mEnabledSession.method.setSessionEnabled(
1415 mEnabledSession.session, false);
1416 } catch (RemoteException e) {
1417 }
1418 }
1419 mEnabledSession = session;
1420 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001421 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 session.method.setSessionEnabled(
1423 session.session, true);
1424 } catch (RemoteException e) {
1425 }
1426 }
1427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 public boolean handleMessage(Message msg) {
1430 HandlerCaller.SomeArgs args;
1431 switch (msg.what) {
1432 case MSG_SHOW_IM_PICKER:
1433 showInputMethodMenu();
1434 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001435
satokab751aa2010-09-14 19:17:36 +09001436 case MSG_SHOW_IM_SUBTYPE_PICKER:
1437 showInputMethodSubtypeMenu();
1438 return true;
1439
satok47a44912010-10-06 16:03:58 +09001440 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001441 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001442 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001443 return true;
1444
1445 case MSG_SHOW_IM_CONFIG:
1446 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001447 return true;
1448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 case MSG_UNBIND_INPUT:
1452 try {
1453 ((IInputMethod)msg.obj).unbindInput();
1454 } catch (RemoteException e) {
1455 // There is nothing interesting about the method dying.
1456 }
1457 return true;
1458 case MSG_BIND_INPUT:
1459 args = (HandlerCaller.SomeArgs)msg.obj;
1460 try {
1461 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1462 } catch (RemoteException e) {
1463 }
1464 return true;
1465 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001466 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001468 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1469 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 } catch (RemoteException e) {
1471 }
1472 return true;
1473 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001474 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001476 ((IInputMethod)args.arg1).hideSoftInput(0,
1477 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 } catch (RemoteException e) {
1479 }
1480 return true;
1481 case MSG_ATTACH_TOKEN:
1482 args = (HandlerCaller.SomeArgs)msg.obj;
1483 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001484 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1486 } catch (RemoteException e) {
1487 }
1488 return true;
1489 case MSG_CREATE_SESSION:
1490 args = (HandlerCaller.SomeArgs)msg.obj;
1491 try {
1492 ((IInputMethod)args.arg1).createSession(
1493 (IInputMethodCallback)args.arg2);
1494 } catch (RemoteException e) {
1495 }
1496 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 case MSG_START_INPUT:
1500 args = (HandlerCaller.SomeArgs)msg.obj;
1501 try {
1502 SessionState session = (SessionState)args.arg1;
1503 setEnabledSessionInMainThread(session);
1504 session.method.startInput((IInputContext)args.arg2,
1505 (EditorInfo)args.arg3);
1506 } catch (RemoteException e) {
1507 }
1508 return true;
1509 case MSG_RESTART_INPUT:
1510 args = (HandlerCaller.SomeArgs)msg.obj;
1511 try {
1512 SessionState session = (SessionState)args.arg1;
1513 setEnabledSessionInMainThread(session);
1514 session.method.restartInput((IInputContext)args.arg2,
1515 (EditorInfo)args.arg3);
1516 } catch (RemoteException e) {
1517 }
1518 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 case MSG_UNBIND_METHOD:
1523 try {
1524 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1525 } catch (RemoteException e) {
1526 // There is nothing interesting about the last client dying.
1527 }
1528 return true;
1529 case MSG_BIND_METHOD:
1530 args = (HandlerCaller.SomeArgs)msg.obj;
1531 try {
1532 ((IInputMethodClient)args.arg1).onBindMethod(
1533 (InputBindResult)args.arg2);
1534 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001535 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 }
1537 return true;
1538 }
1539 return false;
1540 }
1541
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001542 private boolean isSystemIme(InputMethodInfo inputMethod) {
1543 return (inputMethod.getServiceInfo().applicationInfo.flags
1544 & ApplicationInfo.FLAG_SYSTEM) != 0;
1545 }
1546
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001547 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001548 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001549 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001550 // We'd prefer to fall back on a system IME, since that is safer.
1551 int i=enabled.size();
1552 while (i > 0) {
1553 i--;
1554 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1555 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1556 break;
1557 }
1558 }
satokab751aa2010-09-14 19:17:36 +09001559 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001560 if (DEBUG) {
1561 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1562 }
satok723a27e2010-11-11 14:58:11 +09001563 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001564 return true;
1565 }
1566
1567 return false;
1568 }
1569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1571 HashMap<String, InputMethodInfo> map) {
1572 list.clear();
1573 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001576 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001577 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1578 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1579 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1580 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581
1582 List<ResolveInfo> services = pm.queryIntentServices(
1583 new Intent(InputMethod.SERVICE_INTERFACE),
1584 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 for (int i = 0; i < services.size(); ++i) {
1587 ResolveInfo ri = services.get(i);
1588 ServiceInfo si = ri.serviceInfo;
1589 ComponentName compName = new ComponentName(si.packageName, si.name);
1590 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1591 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001592 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 + ": it does not require the permission "
1594 + android.Manifest.permission.BIND_INPUT_METHOD);
1595 continue;
1596 }
1597
Joe Onorato8a9b2202010-02-26 18:56:32 -08001598 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599
1600 try {
1601 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1602 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001603 final String id = p.getId();
1604 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605
Amith Yamasanie861ec12010-03-24 21:39:27 -07001606 // System IMEs are enabled by default, unless there's a hard keyboard
1607 // and the system IME was explicitly disabled
1608 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1609 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001610 }
1611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001613 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001617 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001619 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 }
1621 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001622
1623 String defaultIme = Settings.Secure.getString(mContext
1624 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001625 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001626 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001627 updateFromSettingsLocked();
1628 }
1629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001633
satokab751aa2010-09-14 19:17:36 +09001634 private void showInputMethodMenu() {
1635 showInputMethodMenuInternal(false);
1636 }
1637
1638 private void showInputMethodSubtypeMenu() {
1639 showInputMethodMenuInternal(true);
1640 }
1641
satok217f5482010-12-15 05:19:19 +09001642 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
satok86417ea2010-10-27 14:11:03 +09001643 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_AND_SUBTYPE_ENABLER);
satok47a44912010-10-06 16:03:58 +09001644 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001645 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1646 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001647 if (!TextUtils.isEmpty(inputMethodId)) {
1648 intent.putExtra(EXTRA_INPUT_METHOD_ID, inputMethodId);
1649 }
satok217f5482010-12-15 05:19:19 +09001650 mContext.startActivity(intent);
1651 }
1652
1653 private void showConfigureInputMethods() {
1654 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1655 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1656 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1657 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001658 mContext.startActivity(intent);
1659 }
1660
satokab751aa2010-09-14 19:17:36 +09001661 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001662 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 String lastInputMethodId = Settings.Secure.getString(context
1669 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001670 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001671 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001672
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001673 synchronized (mMethodMap) {
satok7f35c8c2010-10-07 21:13:11 +09001674 final List<Pair<InputMethodInfo, ArrayList<String>>> immis =
satok67ddf9c2010-11-17 09:45:54 +09001675 mSettings.getEnabledInputMethodAndSubtypeHashCodeListLocked();
satok16331c82010-12-20 23:48:46 +09001676 int N = immis.size();
1677
1678 // Add applicable subtypes if no subtype for each IME is enabled.
1679 for (int i = 0; i < N; ++i) {
1680 InputMethodInfo imi = immis.get(i).first;
1681 ArrayList<String> subtypes = immis.get(i).second;
1682 if (subtypes != null && subtypes.size() == 0) {
1683 ArrayList<InputMethodSubtype> applicableSubtypes =
satokdf31ae62011-01-15 06:19:44 +09001684 getApplicableSubtypesLocked(mRes, imi.getSubtypes());
satok16331c82010-12-20 23:48:46 +09001685 final int numSubtypes = applicableSubtypes.size();
1686 for (int j = 0; j < numSubtypes; ++j) {
1687 subtypes.add(String.valueOf(applicableSubtypes.get(j).hashCode()));
1688 }
1689 }
1690 }
1691
satok7f35c8c2010-10-07 21:13:11 +09001692 ArrayList<Integer> subtypeIds = new ArrayList<Integer>();
1693
1694 if (immis == null || immis.size() == 0) {
1695 return;
1696 }
1697
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001698 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699
satok913a8922010-08-26 21:53:41 +09001700
satokab751aa2010-09-14 19:17:36 +09001701 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1702 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001703
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001704 for (int i = 0; i < N; ++i) {
satok7f35c8c2010-10-07 21:13:11 +09001705 InputMethodInfo property = immis.get(i).first;
1706 final ArrayList<String> enabledSubtypeIds = immis.get(i).second;
1707 HashSet<String> enabledSubtypeSet = new HashSet<String>();
1708 for (String s : enabledSubtypeIds) {
1709 enabledSubtypeSet.add(s);
1710 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001711 if (property == null) {
1712 continue;
1713 }
satokab751aa2010-09-14 19:17:36 +09001714 ArrayList<InputMethodSubtype> subtypes = property.getSubtypes();
1715 CharSequence label = property.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001716 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokab751aa2010-09-14 19:17:36 +09001717 for (int j = 0; j < subtypes.size(); ++j) {
1718 InputMethodSubtype subtype = subtypes.get(j);
satok7f35c8c2010-10-07 21:13:11 +09001719 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1720 CharSequence title;
1721 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001722 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001723 if (nameResId != 0) {
1724 title = pm.getText(property.getPackageName(), nameResId,
1725 property.getServiceInfo().applicationInfo);
1726 } else {
1727 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001728 // TODO: Use more friendly Title and UI
1729 title = label + "," + (mode == null ? "" : mode) + ","
1730 + (language == null ? "" : language);
1731 }
1732 imMap.put(title, new Pair<InputMethodInfo, Integer>(property, j));
satokab751aa2010-09-14 19:17:36 +09001733 }
satokab751aa2010-09-14 19:17:36 +09001734 }
1735 } else {
1736 imMap.put(label,
1737 new Pair<InputMethodInfo, Integer>(property, NOT_A_SUBTYPE_ID));
1738 subtypeIds.add(0);
1739 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001740 }
satok913a8922010-08-26 21:53:41 +09001741
1742 N = imMap.size();
1743 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001744 mIms = new InputMethodInfo[N];
1745 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001746 int checkedItem = 0;
1747 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001748 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1749 mIms[i] = value.first;
1750 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001751 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001752 int subtypeId = mSubtypeIds[i];
1753 if ((subtypeId == NOT_A_SUBTYPE_ID)
1754 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1755 || (subtypeId == lastInputMethodSubtypeId)) {
1756 checkedItem = i;
1757 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
satokab751aa2010-09-14 19:17:36 +09001760
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001761 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1762 public void onClick(DialogInterface dialog, int which) {
1763 hideInputMethodMenu();
1764 }
1765 };
satokd87c2592010-09-29 11:52:06 +09001766
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001767 TypedArray a = context.obtainStyledAttributes(null,
1768 com.android.internal.R.styleable.DialogPreference,
1769 com.android.internal.R.attr.alertDialogStyle, 0);
1770 mDialogBuilder = new AlertDialog.Builder(context)
1771 .setTitle(com.android.internal.R.string.select_input_method)
1772 .setOnCancelListener(new OnCancelListener() {
1773 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001775 }
1776 })
1777 .setIcon(a.getDrawable(
1778 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1779 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001780
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001781 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1782 new AlertDialog.OnClickListener() {
1783 public void onClick(DialogInterface dialog, int which) {
1784 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001785 if (mIms == null || mIms.length <= which
1786 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001787 return;
1788 }
1789 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001790 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001791 hideInputMethodMenu();
1792 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001793 if ((subtypeId < 0)
1794 || (subtypeId >= im.getSubtypes().size())) {
1795 subtypeId = NOT_A_SUBTYPE_ID;
1796 }
1797 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001798 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001799 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001801 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802
satok7f35c8c2010-10-07 21:13:11 +09001803 if (showSubtypes) {
satok82beadf2010-12-27 19:03:06 +09001804 mDialogBuilder.setPositiveButton(
1805 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09001806 new DialogInterface.OnClickListener() {
1807 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09001808 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09001809 }
1810 });
1811 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 mSwitchingDialog = mDialogBuilder.create();
1813 mSwitchingDialog.getWindow().setType(
1814 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1815 mSwitchingDialog.show();
1816 }
1817 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001820 synchronized (mMethodMap) {
1821 hideInputMethodMenuLocked();
1822 }
1823 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001824
The Android Open Source Project10592532009-03-18 17:39:46 -07001825 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001826 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827
The Android Open Source Project10592532009-03-18 17:39:46 -07001828 if (mSwitchingDialog != null) {
1829 mSwitchingDialog.dismiss();
1830 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001832
The Android Open Source Project10592532009-03-18 17:39:46 -07001833 mDialogBuilder = null;
1834 mItems = null;
1835 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 public boolean setInputMethodEnabled(String id, boolean enabled) {
1841 synchronized (mMethodMap) {
1842 if (mContext.checkCallingOrSelfPermission(
1843 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1844 != PackageManager.PERMISSION_GRANTED) {
1845 throw new SecurityException(
1846 "Requires permission "
1847 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1848 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 long ident = Binder.clearCallingIdentity();
1851 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001852 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 } finally {
1854 Binder.restoreCallingIdentity(ident);
1855 }
1856 }
1857 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001858
1859 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1860 // Make sure this is a valid input method.
1861 InputMethodInfo imm = mMethodMap.get(id);
1862 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001863 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001864 }
1865
satokd87c2592010-09-29 11:52:06 +09001866 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1867 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001868
satokd87c2592010-09-29 11:52:06 +09001869 if (enabled) {
1870 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1871 if (pair.first.equals(id)) {
1872 // We are enabling this input method, but it is already enabled.
1873 // Nothing to do. The previous state was enabled.
1874 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001875 }
1876 }
satokd87c2592010-09-29 11:52:06 +09001877 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1878 // Previous state was disabled.
1879 return false;
1880 } else {
1881 StringBuilder builder = new StringBuilder();
1882 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1883 builder, enabledInputMethodsList, id)) {
1884 // Disabled input method is currently selected, switch to another one.
1885 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1886 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001887 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
1888 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
1889 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09001890 }
1891 // Previous state was enabled.
1892 return true;
1893 } else {
1894 // We are disabling the input method but it is already disabled.
1895 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001896 return false;
1897 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001898 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001899 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001900
satok723a27e2010-11-11 14:58:11 +09001901 private void saveCurrentInputMethodAndSubtypeToHistory() {
1902 String subtypeId = NOT_A_SUBTYPE_ID_STR;
1903 if (mCurrentSubtype != null) {
1904 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
1905 }
1906 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
satokab751aa2010-09-14 19:17:36 +09001907 }
1908
satok723a27e2010-11-11 14:58:11 +09001909 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
1910 boolean setSubtypeOnly) {
1911 // Update the history of InputMethod and Subtype
1912 saveCurrentInputMethodAndSubtypeToHistory();
1913
1914 // Set Subtype here
1915 if (imi == null || subtypeId < 0) {
1916 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001917 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09001918 } else {
1919 final ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1920 if (subtypeId < subtypes.size()) {
1921 mSettings.putSelectedSubtype(subtypes.get(subtypeId).hashCode());
1922 mCurrentSubtype = subtypes.get(subtypeId);
1923 } else {
1924 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
1925 mCurrentSubtype = null;
1926 }
satokab751aa2010-09-14 19:17:36 +09001927 }
satok723a27e2010-11-11 14:58:11 +09001928
1929 if (!setSubtypeOnly) {
1930 // Set InputMethod here
1931 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
1932 }
1933 }
1934
1935 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
1936 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
1937 int lastSubtypeId = NOT_A_SUBTYPE_ID;
1938 // newDefaultIme is empty when there is no candidate for the selected IME.
1939 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
1940 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
1941 if (subtypeHashCode != null) {
1942 try {
1943 lastSubtypeId = getSubtypeIdFromHashCode(
1944 imi, Integer.valueOf(subtypeHashCode));
1945 } catch (NumberFormatException e) {
1946 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
1947 }
1948 }
1949 }
1950 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09001951 }
1952
1953 private int getSelectedInputMethodSubtypeId(String id) {
1954 InputMethodInfo imi = mMethodMap.get(id);
1955 if (imi == null) {
1956 return NOT_A_SUBTYPE_ID;
1957 }
satokab751aa2010-09-14 19:17:36 +09001958 int subtypeId;
1959 try {
1960 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
1961 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
1962 } catch (SettingNotFoundException e) {
1963 return NOT_A_SUBTYPE_ID;
1964 }
satok723a27e2010-11-11 14:58:11 +09001965 return getSubtypeIdFromHashCode(imi, subtypeId);
1966 }
1967
1968 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09001969 if (imi != null) {
1970 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1971 for (int i = 0; i < subtypes.size(); ++i) {
1972 InputMethodSubtype ims = subtypes.get(i);
1973 if (subtypeHashCode == ims.hashCode()) {
1974 return i;
1975 }
satokab751aa2010-09-14 19:17:36 +09001976 }
1977 }
1978 return NOT_A_SUBTYPE_ID;
1979 }
1980
satokdf31ae62011-01-15 06:19:44 +09001981 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
1982 Resources res, List<InputMethodSubtype> subtypes) {
1983 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09001984 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
1985 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
1986 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09001987 final int N = subtypes.size();
1988 boolean containsKeyboardSubtype = false;
1989 for (int i = 0; i < N; ++i) {
1990 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09001991 final String locale = subtype.getLocale();
1992 final String mode = subtype.getMode();
1993 // When system locale starts with subtype's locale, that subtype will be applicable
1994 // for system locale
1995 // For instance, it's clearly applicable for cases like system locale = en_US and
1996 // subtype = en, but it is not necessarily considered applicable for cases like system
1997 // locale = en and subtype = en_US.
1998 // We just call systemLocale.startsWith(locale) in this function because there is no
1999 // need to find applicable subtypes aggressively unlike
2000 // findLastResortApplicableSubtypeLocked.
2001 if (systemLocale.startsWith(locale)) {
2002 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2003 // If more applicable subtypes are contained, skip.
2004 if (applicableSubtype != null
2005 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2006 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002007 if (!containsKeyboardSubtype
2008 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2009 containsKeyboardSubtype = true;
2010 }
2011 }
2012 }
satok3da92232011-01-11 22:46:30 +09002013 ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2014 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002015 if (!containsKeyboardSubtype) {
2016 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002017 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002018 if (lastResortKeyboardSubtype != null) {
2019 applicableSubtypes.add(lastResortKeyboardSubtype);
2020 }
2021 }
2022 return applicableSubtypes;
2023 }
2024
satok4e4569d2010-11-19 18:45:53 +09002025 /**
2026 * If there are no selected subtypes, tries finding the most applicable one according to the
2027 * given locale.
2028 * @param subtypes this function will search the most applicable subtype in subtypes
2029 * @param mode subtypes will be filtered by mode
2030 * @param locale subtypes will be filtered by locale
satok7599a7fb2010-12-22 13:45:23 +09002031 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2032 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002033 * @return the most applicable subtypeId
2034 */
satokdf31ae62011-01-15 06:19:44 +09002035 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2036 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7fb2010-12-22 13:45:23 +09002037 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002038 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002039 return null;
satok8fbb1e82010-11-02 23:15:58 +09002040 }
satok4e4569d2010-11-19 18:45:53 +09002041 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002042 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002043 }
satok8fbb1e82010-11-02 23:15:58 +09002044 final String language = locale.substring(0, 2);
2045 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002046 InputMethodSubtype applicableSubtype = null;
satok7599a7fb2010-12-22 13:45:23 +09002047 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002048 final int N = subtypes.size();
2049 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002050 InputMethodSubtype subtype = subtypes.get(i);
2051 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002052 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2053 // and all subtypes with all modes can be candidates.
2054 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7fb2010-12-22 13:45:23 +09002055 if (firstMatchedModeSubtype == null) {
2056 firstMatchedModeSubtype = subtype;
2057 }
satok9ef02832010-11-04 21:17:48 +09002058 if (locale.equals(subtypeLocale)) {
2059 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002060 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002061 break;
2062 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2063 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002064 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002065 partialMatchFound = true;
2066 }
satok8fbb1e82010-11-02 23:15:58 +09002067 }
2068 }
2069
satok7599a7fb2010-12-22 13:45:23 +09002070 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2071 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002072 }
2073
satok8fbb1e82010-11-02 23:15:58 +09002074 // The first subtype applicable to the system locale will be defined as the most applicable
2075 // subtype.
2076 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002077 if (applicableSubtype != null) {
2078 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2079 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2080 }
satok8fbb1e82010-11-02 23:15:58 +09002081 }
satokcd7cd292010-11-20 15:46:23 +09002082 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002083 }
2084
satok4e4569d2010-11-19 18:45:53 +09002085 // If there are no selected shortcuts, tries finding the most applicable ones.
2086 private Pair<InputMethodInfo, InputMethodSubtype>
2087 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2088 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2089 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002090 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002091 boolean foundInSystemIME = false;
2092
2093 // Search applicable subtype for each InputMethodInfo
2094 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002095 final String imiId = imi.getId();
2096 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2097 continue;
2098 }
satokcd7cd292010-11-20 15:46:23 +09002099 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002100 final List<InputMethodSubtype> enabledSubtypes =
2101 getEnabledInputMethodSubtypeList(imi, true);
2102 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002103 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002104 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002105 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002106 }
satokdf31ae62011-01-15 06:19:44 +09002107 // 2. Search by the system locale from enabledSubtypes.
2108 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002109 if (subtype == null) {
2110 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002111 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002112 }
satok7599a7fb2010-12-22 13:45:23 +09002113 // 4. Search by the current subtype's locale from all subtypes.
2114 if (subtype == null && mCurrentSubtype != null) {
2115 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002116 mRes, imi.getSubtypes(), mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09002117 }
2118 // 5. Search by the system locale from all subtypes.
2119 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002120 if (subtype == null) {
satok7599a7fb2010-12-22 13:45:23 +09002121 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002122 mRes, imi.getSubtypes(), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002123 }
satokcd7cd292010-11-20 15:46:23 +09002124 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09002125 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002126 // The current input method is the most applicable IME.
2127 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002128 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002129 break;
satok7599a7fb2010-12-22 13:45:23 +09002130 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002131 // The system input method is 2nd applicable IME.
2132 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002133 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09002134 if ((imi.getServiceInfo().applicationInfo.flags
2135 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2136 foundInSystemIME = true;
2137 }
satok4e4569d2010-11-19 18:45:53 +09002138 }
2139 }
2140 }
2141 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002142 if (mostApplicableIMI != null) {
2143 Slog.w(TAG, "Most applicable shortcut input method was:"
2144 + mostApplicableIMI.getId());
2145 if (mostApplicableSubtype != null) {
2146 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2147 + "," + mostApplicableSubtype.getMode() + ","
2148 + mostApplicableSubtype.getLocale());
2149 }
2150 }
satok4e4569d2010-11-19 18:45:53 +09002151 }
satokcd7cd292010-11-20 15:46:23 +09002152 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002153 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002154 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002155 } else {
2156 return null;
2157 }
2158 }
2159
satokab751aa2010-09-14 19:17:36 +09002160 /**
2161 * @return Return the current subtype of this input method.
2162 */
2163 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002164 boolean subtypeIsSelected = false;
2165 try {
2166 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2167 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2168 } catch (SettingNotFoundException e) {
2169 }
satok3ef8b292010-11-23 06:06:29 +09002170 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002171 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002172 String lastInputMethodId = Settings.Secure.getString(
2173 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002174 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2175 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002176 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2177 if (imi != null) {
2178 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002179 // the most applicable subtype from explicitly or implicitly enabled
2180 // subtypes.
2181 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2182 getEnabledInputMethodSubtypeList(imi, true);
2183 // If there is only one explicitly or implicitly enabled subtype,
2184 // just returns it.
2185 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2186 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2187 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2188 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2189 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2190 SUBTYPE_MODE_KEYBOARD, null, true);
2191 if (mCurrentSubtype == null) {
2192 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2193 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2194 true);
2195 }
2196 }
satok4e4569d2010-11-19 18:45:53 +09002197 }
satokcd7cd292010-11-20 15:46:23 +09002198 } else {
satok3ef8b292010-11-23 06:06:29 +09002199 mCurrentSubtype =
2200 mMethodMap.get(lastInputMethodId).getSubtypes().get(subtypeId);
2201 }
satok8fbb1e82010-11-02 23:15:58 +09002202 }
satok3ef8b292010-11-23 06:06:29 +09002203 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002204 }
satokab751aa2010-09-14 19:17:36 +09002205 }
2206
satokf3db1af2010-11-23 13:34:33 +09002207 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2208 InputMethodSubtype subtype) {
2209 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2210 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2211 } else {
2212 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2213 subtypes.add(subtype);
2214 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2215 }
2216 }
2217
satok4e4569d2010-11-19 18:45:53 +09002218 // TODO: We should change the return type from List to List<Parcelable>
2219 public List getShortcutInputMethodsAndSubtypes() {
2220 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002221 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002222 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002223 // If there are no selected shortcut subtypes, the framework will try to find
2224 // the most applicable subtype from all subtypes whose mode is
2225 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002226 Pair<InputMethodInfo, InputMethodSubtype> info =
2227 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2228 SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09002229 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002230 ret.add(info.first);
2231 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09002232 }
satok3da92232011-01-11 22:46:30 +09002233 return ret;
satokf3db1af2010-11-23 13:34:33 +09002234 }
satokf3db1af2010-11-23 13:34:33 +09002235 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2236 ret.add(imi);
2237 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2238 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002239 }
2240 }
satokf3db1af2010-11-23 13:34:33 +09002241 return ret;
satok4e4569d2010-11-19 18:45:53 +09002242 }
2243 }
2244
satokb66d2872010-11-10 01:04:04 +09002245 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2246 synchronized (mMethodMap) {
2247 if (subtype != null && mCurMethodId != null) {
2248 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2249 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2250 if (subtypeId != NOT_A_SUBTYPE_ID) {
2251 setInputMethodLocked(mCurMethodId, subtypeId);
2252 return true;
2253 }
2254 }
2255 return false;
2256 }
2257 }
2258
satokd87c2592010-09-29 11:52:06 +09002259 /**
2260 * Utility class for putting and getting settings for InputMethod
2261 * TODO: Move all putters and getters of settings to this class.
2262 */
2263 private static class InputMethodSettings {
2264 // The string for enabled input method is saved as follows:
2265 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2266 private static final char INPUT_METHOD_SEPARATER = ':';
2267 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002268 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002269 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2270
satok723a27e2010-11-11 14:58:11 +09002271 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002272 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2273
satokdf31ae62011-01-15 06:19:44 +09002274 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002275 private final ContentResolver mResolver;
2276 private final HashMap<String, InputMethodInfo> mMethodMap;
2277 private final ArrayList<InputMethodInfo> mMethodList;
2278
2279 private String mEnabledInputMethodsStrCache;
2280
2281 private static void buildEnabledInputMethodsSettingString(
2282 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2283 String id = pair.first;
2284 ArrayList<String> subtypes = pair.second;
2285 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002286 // Inputmethod and subtypes are saved in the settings as follows:
2287 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2288 for (String subtypeId: subtypes) {
2289 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002290 }
2291 }
2292
2293 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002294 Resources res, ContentResolver resolver,
2295 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2296 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002297 mResolver = resolver;
2298 mMethodMap = methodMap;
2299 mMethodList = methodList;
2300 }
2301
2302 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2303 return createEnabledInputMethodListLocked(
2304 getEnabledInputMethodsAndSubtypeListLocked());
2305 }
2306
satok7f35c8c2010-10-07 21:13:11 +09002307 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002308 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2309 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002310 getEnabledInputMethodsAndSubtypeListLocked());
2311 }
2312
satok67ddf9c2010-11-17 09:45:54 +09002313 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2314 InputMethodInfo imi) {
2315 List<Pair<String, ArrayList<String>>> imsList =
2316 getEnabledInputMethodsAndSubtypeListLocked();
2317 ArrayList<InputMethodSubtype> enabledSubtypes =
2318 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002319 if (imi != null) {
2320 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2321 InputMethodInfo info = mMethodMap.get(imsPair.first);
2322 if (info != null && info.getId().equals(imi.getId())) {
2323 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
2324 for (InputMethodSubtype ims: subtypes) {
2325 for (String s: imsPair.second) {
2326 if (String.valueOf(ims.hashCode()).equals(s)) {
2327 enabledSubtypes.add(ims);
2328 }
satok67ddf9c2010-11-17 09:45:54 +09002329 }
2330 }
satok884ef9a2010-11-18 10:39:46 +09002331 break;
satok67ddf9c2010-11-17 09:45:54 +09002332 }
satok67ddf9c2010-11-17 09:45:54 +09002333 }
2334 }
2335 return enabledSubtypes;
2336 }
2337
satokd87c2592010-09-29 11:52:06 +09002338 // At the initial boot, the settings for input methods are not set,
2339 // so we need to enable IME in that case.
2340 public void enableAllIMEsIfThereIsNoEnabledIME() {
2341 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2342 StringBuilder sb = new StringBuilder();
2343 final int N = mMethodList.size();
2344 for (int i = 0; i < N; i++) {
2345 InputMethodInfo imi = mMethodList.get(i);
2346 Slog.i(TAG, "Adding: " + imi.getId());
2347 if (i > 0) sb.append(':');
2348 sb.append(imi.getId());
2349 }
2350 putEnabledInputMethodsStr(sb.toString());
2351 }
2352 }
2353
2354 public List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
2355 ArrayList<Pair<String, ArrayList<String>>> imsList
2356 = new ArrayList<Pair<String, ArrayList<String>>>();
2357 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2358 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2359 return imsList;
2360 }
satok723a27e2010-11-11 14:58:11 +09002361 mInputMethodSplitter.setString(enabledInputMethodsStr);
2362 while (mInputMethodSplitter.hasNext()) {
2363 String nextImsStr = mInputMethodSplitter.next();
2364 mSubtypeSplitter.setString(nextImsStr);
2365 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002366 ArrayList<String> subtypeHashes = new ArrayList<String>();
2367 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002368 String imeId = mSubtypeSplitter.next();
2369 while (mSubtypeSplitter.hasNext()) {
2370 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002371 }
2372 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2373 }
2374 }
2375 return imsList;
2376 }
2377
2378 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2379 if (reloadInputMethodStr) {
2380 getEnabledInputMethodsStr();
2381 }
2382 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2383 // Add in the newly enabled input method.
2384 putEnabledInputMethodsStr(id);
2385 } else {
2386 putEnabledInputMethodsStr(
2387 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2388 }
2389 }
2390
2391 /**
2392 * Build and put a string of EnabledInputMethods with removing specified Id.
2393 * @return the specified id was removed or not.
2394 */
2395 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2396 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2397 boolean isRemoved = false;
2398 boolean needsAppendSeparator = false;
2399 for (Pair<String, ArrayList<String>> ims: imsList) {
2400 String curId = ims.first;
2401 if (curId.equals(id)) {
2402 // We are disabling this input method, and it is
2403 // currently enabled. Skip it to remove from the
2404 // new list.
2405 isRemoved = true;
2406 } else {
2407 if (needsAppendSeparator) {
2408 builder.append(INPUT_METHOD_SEPARATER);
2409 } else {
2410 needsAppendSeparator = true;
2411 }
2412 buildEnabledInputMethodsSettingString(builder, ims);
2413 }
2414 }
2415 if (isRemoved) {
2416 // Update the setting with the new list of input methods.
2417 putEnabledInputMethodsStr(builder.toString());
2418 }
2419 return isRemoved;
2420 }
2421
2422 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2423 List<Pair<String, ArrayList<String>>> imsList) {
2424 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2425 for (Pair<String, ArrayList<String>> ims: imsList) {
2426 InputMethodInfo info = mMethodMap.get(ims.first);
2427 if (info != null) {
2428 res.add(info);
2429 }
2430 }
2431 return res;
2432 }
2433
satok7f35c8c2010-10-07 21:13:11 +09002434 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002435 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002436 List<Pair<String, ArrayList<String>>> imsList) {
2437 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2438 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2439 for (Pair<String, ArrayList<String>> ims : imsList) {
2440 InputMethodInfo info = mMethodMap.get(ims.first);
2441 if (info != null) {
2442 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2443 }
2444 }
2445 return res;
2446 }
2447
satokd87c2592010-09-29 11:52:06 +09002448 private void putEnabledInputMethodsStr(String str) {
2449 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2450 mEnabledInputMethodsStrCache = str;
2451 }
2452
2453 private String getEnabledInputMethodsStr() {
2454 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2455 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002456 if (DEBUG) {
2457 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2458 }
satokd87c2592010-09-29 11:52:06 +09002459 return mEnabledInputMethodsStrCache;
2460 }
satok723a27e2010-11-11 14:58:11 +09002461
2462 private void saveSubtypeHistory(
2463 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2464 StringBuilder builder = new StringBuilder();
2465 boolean isImeAdded = false;
2466 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2467 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2468 newSubtypeId);
2469 isImeAdded = true;
2470 }
2471 for (Pair<String, String> ime: savedImes) {
2472 String imeId = ime.first;
2473 String subtypeId = ime.second;
2474 if (TextUtils.isEmpty(subtypeId)) {
2475 subtypeId = NOT_A_SUBTYPE_ID_STR;
2476 }
2477 if (isImeAdded) {
2478 builder.append(INPUT_METHOD_SEPARATER);
2479 } else {
2480 isImeAdded = true;
2481 }
2482 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2483 subtypeId);
2484 }
2485 // Remove the last INPUT_METHOD_SEPARATER
2486 putSubtypeHistoryStr(builder.toString());
2487 }
2488
2489 public void addSubtypeToHistory(String imeId, String subtypeId) {
2490 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2491 for (Pair<String, String> ime: subtypeHistory) {
2492 if (ime.first.equals(imeId)) {
2493 if (DEBUG) {
2494 Slog.v(TAG, "Subtype found in the history: " + imeId
2495 + ime.second);
2496 }
2497 // We should break here
2498 subtypeHistory.remove(ime);
2499 break;
2500 }
2501 }
2502 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2503 }
2504
2505 private void putSubtypeHistoryStr(String str) {
2506 if (DEBUG) {
2507 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2508 }
2509 Settings.Secure.putString(
2510 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2511 }
2512
2513 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2514 // Gets the first one from the history
2515 return getLastSubtypeForInputMethodLockedInternal(null);
2516 }
2517
2518 public String getLastSubtypeForInputMethodLocked(String imeId) {
2519 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2520 if (ime != null) {
2521 return ime.second;
2522 } else {
2523 return null;
2524 }
2525 }
2526
2527 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2528 List<Pair<String, ArrayList<String>>> enabledImes =
2529 getEnabledInputMethodsAndSubtypeListLocked();
2530 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2531 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2532 final String imeInTheHistory = imeAndSubtype.first;
2533 // If imeId is empty, returns the first IME and subtype in the history
2534 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2535 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002536 final String subtypeHashCode =
2537 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2538 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002539 if (!TextUtils.isEmpty(subtypeHashCode)) {
2540 if (DEBUG) {
2541 Slog.d(TAG, "Enabled subtype found in the history:" + subtypeHashCode);
2542 }
2543 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2544 }
2545 }
2546 }
2547 if (DEBUG) {
2548 Slog.d(TAG, "No enabled IME found in the history");
2549 }
2550 return null;
2551 }
2552
satokdf31ae62011-01-15 06:19:44 +09002553 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002554 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2555 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2556 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002557 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2558 if (explicitlyEnabledSubtypes.size() == 0) {
2559 // If there are no explicitly enabled subtypes, applicable subtypes are
2560 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002561 InputMethodInfo ime = mMethodMap.get(imeId);
2562 // If IME is enabled and no subtypes are enabled, applicable subtypes
2563 // are enabled implicitly, so needs to treat them to be enabled.
2564 if (ime != null && ime.getSubtypes().size() > 0) {
2565 List<InputMethodSubtype> implicitlySelectedSubtypes =
satokf6cafb62011-01-17 16:29:02 +09002566 getApplicableSubtypesLocked(mRes, ime.getSubtypes());
satokdf31ae62011-01-15 06:19:44 +09002567 if (implicitlySelectedSubtypes != null) {
2568 final int N = implicitlySelectedSubtypes.size();
2569 for (int i = 0; i < N; ++i) {
2570 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2571 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2572 return subtypeHashCode;
2573 }
2574 }
2575 }
2576 }
2577 } else {
satokf6cafb62011-01-17 16:29:02 +09002578 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002579 if (s.equals(subtypeHashCode)) {
2580 // If both imeId and subtypeId are enabled, return subtypeId.
2581 return s;
2582 }
satok723a27e2010-11-11 14:58:11 +09002583 }
2584 }
2585 // If imeId was enabled but subtypeId was disabled.
2586 return NOT_A_SUBTYPE_ID_STR;
2587 }
2588 }
2589 // If both imeId and subtypeId are disabled, return null
2590 return null;
2591 }
2592
2593 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2594 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2595 final String subtypeHistoryStr = getSubtypeHistoryStr();
2596 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2597 return imsList;
2598 }
2599 mInputMethodSplitter.setString(subtypeHistoryStr);
2600 while (mInputMethodSplitter.hasNext()) {
2601 String nextImsStr = mInputMethodSplitter.next();
2602 mSubtypeSplitter.setString(nextImsStr);
2603 if (mSubtypeSplitter.hasNext()) {
2604 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2605 // The first element is ime id.
2606 String imeId = mSubtypeSplitter.next();
2607 while (mSubtypeSplitter.hasNext()) {
2608 subtypeId = mSubtypeSplitter.next();
2609 break;
2610 }
2611 imsList.add(new Pair<String, String>(imeId, subtypeId));
2612 }
2613 }
2614 return imsList;
2615 }
2616
2617 private String getSubtypeHistoryStr() {
2618 if (DEBUG) {
2619 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2620 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2621 }
2622 return Settings.Secure.getString(
2623 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2624 }
2625
2626 public void putSelectedInputMethod(String imeId) {
2627 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2628 }
2629
2630 public void putSelectedSubtype(int subtypeId) {
2631 Settings.Secure.putInt(
2632 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2633 }
satokd87c2592010-09-29 11:52:06 +09002634 }
2635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 @Override
2639 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2640 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2641 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2644 + Binder.getCallingPid()
2645 + ", uid=" + Binder.getCallingUid());
2646 return;
2647 }
2648
2649 IInputMethod method;
2650 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 synchronized (mMethodMap) {
2655 p.println("Current Input Method Manager state:");
2656 int N = mMethodList.size();
2657 p.println(" Input Methods:");
2658 for (int i=0; i<N; i++) {
2659 InputMethodInfo info = mMethodList.get(i);
2660 p.println(" InputMethod #" + i + ":");
2661 info.dump(p, " ");
2662 }
2663 p.println(" Clients:");
2664 for (ClientState ci : mClients.values()) {
2665 p.println(" Client " + ci + ":");
2666 p.println(" client=" + ci.client);
2667 p.println(" inputContext=" + ci.inputContext);
2668 p.println(" sessionRequested=" + ci.sessionRequested);
2669 p.println(" curSession=" + ci.curSession);
2670 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002671 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002673 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2674 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2676 + " mBoundToMethod=" + mBoundToMethod);
2677 p.println(" mCurToken=" + mCurToken);
2678 p.println(" mCurIntent=" + mCurIntent);
2679 method = mCurMethod;
2680 p.println(" mCurMethod=" + mCurMethod);
2681 p.println(" mEnabledSession=" + mEnabledSession);
2682 p.println(" mShowRequested=" + mShowRequested
2683 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2684 + " mShowForced=" + mShowForced
2685 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002686 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002688
Jeff Brownb88102f2010-09-08 11:49:43 -07002689 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 pw.flush();
2692 try {
2693 client.client.asBinder().dump(fd, args);
2694 } catch (RemoteException e) {
2695 p.println("Input method client dead: " + e);
2696 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002697 } else {
2698 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002700
Jeff Brownb88102f2010-09-08 11:49:43 -07002701 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 pw.flush();
2704 try {
2705 method.asBinder().dump(fd, args);
2706 } catch (RemoteException e) {
2707 p.println("Input method service dead: " + e);
2708 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002709 } else {
2710 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 }
2712 }
2713}