blob: 7ed91876e43959dc6f9b1f8f598123cabbc29a37 [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);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800430 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800431 changed = true;
432 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800433 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900434 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800435 }
436 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800437 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800438 }
satokab751aa2010-09-14 19:17:36 +0900439
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800440 if (curIm == null) {
441 // We currently don't have a default input method... is
442 // one now available?
443 changed = chooseNewDefaultIMELocked();
444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800445
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800446 if (changed) {
447 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 }
449 }
450 }
451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 class MethodCallback extends IInputMethodCallback.Stub {
454 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 MethodCallback(IInputMethod method) {
457 mMethod = method;
458 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 public void finishedEvent(int seq, boolean handled) throws RemoteException {
461 }
462
463 public void sessionCreated(IInputMethodSession session) throws RemoteException {
464 onSessionCreated(mMethod, session);
465 }
466 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800467
Joe Onorato089de882010-04-12 08:18:45 -0700468 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800470 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 mHandler = new Handler(this);
472 mIWindowManager = IWindowManager.Stub.asInterface(
473 ServiceManager.getService(Context.WINDOW_SERVICE));
474 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
475 public void executeMessage(Message msg) {
476 handleMessage(msg);
477 }
478 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800479
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800480 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 IntentFilter screenOnOffFilt = new IntentFilter();
483 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
484 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700485 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800487
satok913a8922010-08-26 21:53:41 +0900488 mStatusBar = statusBar;
489 statusBar.setIconVisibility("ime", false);
490
satokd87c2592010-09-29 11:52:06 +0900491 // mSettings should be created before buildInputMethodListLocked
492 mSettings = new InputMethodSettings(context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900494 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495
satokd87c2592010-09-29 11:52:06 +0900496 if (TextUtils.isEmpty(Settings.Secure.getString(
497 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900499 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
501 try {
satokd87c2592010-09-29 11:52:06 +0900502 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 imi.getPackageName(), 0).getResources();
504 if (res.getBoolean(imi.getIsDefaultResourceId())) {
505 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800506 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 }
508 } catch (PackageManager.NameNotFoundException ex) {
509 } catch (Resources.NotFoundException ex) {
510 }
511 }
512 }
satokd87c2592010-09-29 11:52:06 +0900513 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800515 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900518 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 }
520 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 mSettingsObserver = new SettingsObserver(mHandler);
523 updateFromSettingsLocked();
524 }
525
526 @Override
527 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
528 throws RemoteException {
529 try {
530 return super.onTransact(code, data, reply, flags);
531 } catch (RuntimeException e) {
532 // The input method manager only throws security exceptions, so let's
533 // log all others.
534 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800535 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 }
537 throw e;
538 }
539 }
540
541 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700542 synchronized (mMethodMap) {
543 if (!mSystemReady) {
544 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700545 try {
546 startInputInnerLocked();
547 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800548 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700549 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700550 }
551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 public List<InputMethodInfo> getInputMethodList() {
555 synchronized (mMethodMap) {
556 return new ArrayList<InputMethodInfo>(mMethodList);
557 }
558 }
559
560 public List<InputMethodInfo> getEnabledInputMethodList() {
561 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900562 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 }
564 }
565
satok16331c82010-12-20 23:48:46 +0900566 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
567 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900568 synchronized (mMethodMap) {
satok884ef9a2010-11-18 10:39:46 +0900569 if (imi == null && mCurMethodId != null) {
570 imi = mMethodMap.get(mCurMethodId);
571 }
satok16331c82010-12-20 23:48:46 +0900572 final List<InputMethodSubtype> enabledSubtypes =
573 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
574 if (!allowsImplicitlySelectedSubtypes || enabledSubtypes.size() > 0) {
575 return enabledSubtypes;
576 } else {
577 return getApplicableSubtypesLocked(imi.getSubtypes());
578 }
satok67ddf9c2010-11-17 09:45:54 +0900579 }
580 }
581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 public void addClient(IInputMethodClient client,
583 IInputContext inputContext, int uid, int pid) {
584 synchronized (mMethodMap) {
585 mClients.put(client.asBinder(), new ClientState(client,
586 inputContext, uid, pid));
587 }
588 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 public void removeClient(IInputMethodClient client) {
591 synchronized (mMethodMap) {
592 mClients.remove(client.asBinder());
593 }
594 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 void executeOrSendMessage(IInterface target, Message msg) {
597 if (target.asBinder() instanceof Binder) {
598 mCaller.sendMessage(msg);
599 } else {
600 handleMessage(msg);
601 msg.recycle();
602 }
603 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800604
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700605 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800607 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 + mCurClient.client.asBinder());
609 if (mBoundToMethod) {
610 mBoundToMethod = false;
611 if (mCurMethod != null) {
612 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
613 MSG_UNBIND_INPUT, mCurMethod));
614 }
615 }
616 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
617 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
618 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 // Call setActive(false) on the old client
621 try {
622 mCurClient.client.setActive(false);
623 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800624 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 + mCurClient.pid + " uid " + mCurClient.uid);
626 }
627 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800628
The Android Open Source Project10592532009-03-18 17:39:46 -0700629 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 }
631 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 private int getImeShowFlags() {
634 int flags = 0;
635 if (mShowForced) {
636 flags |= InputMethod.SHOW_FORCED
637 | InputMethod.SHOW_EXPLICIT;
638 } else if (mShowExplicitlyRequested) {
639 flags |= InputMethod.SHOW_EXPLICIT;
640 }
641 return flags;
642 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 private int getAppShowFlags() {
645 int flags = 0;
646 if (mShowForced) {
647 flags |= InputMethodManager.SHOW_FORCED;
648 } else if (!mShowExplicitlyRequested) {
649 flags |= InputMethodManager.SHOW_IMPLICIT;
650 }
651 return flags;
652 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
655 if (!mBoundToMethod) {
656 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
657 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
658 mBoundToMethod = true;
659 }
660 final SessionState session = mCurClient.curSession;
661 if (initial) {
662 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
663 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
664 } else {
665 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
666 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
667 }
668 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800669 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800670 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 }
672 return needResult
673 ? new InputBindResult(session.session, mCurId, mCurSeq)
674 : null;
675 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 InputBindResult startInputLocked(IInputMethodClient client,
678 IInputContext inputContext, EditorInfo attribute,
679 boolean initial, boolean needResult) {
680 // If no method is currently selected, do nothing.
681 if (mCurMethodId == null) {
682 return mNoBinding;
683 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 ClientState cs = mClients.get(client.asBinder());
686 if (cs == null) {
687 throw new IllegalArgumentException("unknown client "
688 + client.asBinder());
689 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 try {
692 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
693 // Check with the window manager to make sure this client actually
694 // has a window with focus. If not, reject. This is thread safe
695 // because if the focus changes some time before or after, the
696 // next client receiving focus that has any interest in input will
697 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800698 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
700 return null;
701 }
702 } catch (RemoteException e) {
703 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 if (mCurClient != cs) {
706 // If the client is changing, we need to switch over to the new
707 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700708 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800709 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 + cs.client.asBinder());
711
712 // If the screen is on, inform the new client it is active
713 if (mScreenOn) {
714 try {
715 cs.client.setActive(mScreenOn);
716 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800717 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 + cs.pid + " uid " + cs.uid);
719 }
720 }
721 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 // Bump up the sequence for this client and attach it.
724 mCurSeq++;
725 if (mCurSeq <= 0) mCurSeq = 1;
726 mCurClient = cs;
727 mCurInputContext = inputContext;
728 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 // Check if the input method is changing.
731 if (mCurId != null && mCurId.equals(mCurMethodId)) {
732 if (cs.curSession != null) {
733 // Fast case: if we are already connected to the input method,
734 // then just return it.
735 return attachNewInputLocked(initial, needResult);
736 }
737 if (mHaveConnection) {
738 if (mCurMethod != null) {
739 if (!cs.sessionRequested) {
740 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800741 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
743 MSG_CREATE_SESSION, mCurMethod,
744 new MethodCallback(mCurMethod)));
745 }
746 // Return to client, and we will get back with it when
747 // we have had a session made for it.
748 return new InputBindResult(null, mCurId, mCurSeq);
749 } else if (SystemClock.uptimeMillis()
750 < (mLastBindTime+TIME_TO_RECONNECT)) {
751 // In this case we have connected to the service, but
752 // don't yet have its interface. If it hasn't been too
753 // long since we did the connection, we'll return to
754 // the client and wait to get the service interface so
755 // we can report back. If it has been too long, we want
756 // to fall through so we can try a disconnect/reconnect
757 // to see if we can get back in touch with the service.
758 return new InputBindResult(null, mCurId, mCurSeq);
759 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800760 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
761 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 }
763 }
764 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800765
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700766 return startInputInnerLocked();
767 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800768
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700769 InputBindResult startInputInnerLocked() {
770 if (mCurMethodId == null) {
771 return mNoBinding;
772 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800773
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700774 if (!mSystemReady) {
775 // If the system is not yet ready, we shouldn't be running third
776 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700777 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700778 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 InputMethodInfo info = mMethodMap.get(mCurMethodId);
781 if (info == null) {
782 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
783 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800784
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700785 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
788 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700789 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
790 com.android.internal.R.string.input_method_binding_label);
791 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
792 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
794 mLastBindTime = SystemClock.uptimeMillis();
795 mHaveConnection = true;
796 mCurId = info.getId();
797 mCurToken = new Binder();
798 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800799 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 mIWindowManager.addWindowToken(mCurToken,
801 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
802 } catch (RemoteException e) {
803 }
804 return new InputBindResult(null, mCurId, mCurSeq);
805 } else {
806 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800807 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 + mCurIntent);
809 }
810 return null;
811 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 public InputBindResult startInput(IInputMethodClient client,
814 IInputContext inputContext, EditorInfo attribute,
815 boolean initial, boolean needResult) {
816 synchronized (mMethodMap) {
817 final long ident = Binder.clearCallingIdentity();
818 try {
819 return startInputLocked(client, inputContext, attribute,
820 initial, needResult);
821 } finally {
822 Binder.restoreCallingIdentity(ident);
823 }
824 }
825 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 public void finishInput(IInputMethodClient client) {
828 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 public void onServiceConnected(ComponentName name, IBinder service) {
831 synchronized (mMethodMap) {
832 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
833 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700834 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800835 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700836 unbindCurrentMethodLocked(false);
837 return;
838 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800839 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700840 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
841 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800843 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700844 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700846 MSG_CREATE_SESSION, mCurMethod,
847 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 }
849 }
850 }
851 }
852
853 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
854 synchronized (mMethodMap) {
855 if (mCurMethod != null && method != null
856 && mCurMethod.asBinder() == method.asBinder()) {
857 if (mCurClient != null) {
858 mCurClient.curSession = new SessionState(mCurClient,
859 method, session);
860 mCurClient.sessionRequested = false;
861 InputBindResult res = attachNewInputLocked(true, true);
862 if (res.method != null) {
863 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
864 MSG_BIND_METHOD, mCurClient.client, res));
865 }
866 }
867 }
868 }
869 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800870
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700871 void unbindCurrentMethodLocked(boolean reportToClient) {
872 if (mHaveConnection) {
873 mContext.unbindService(this);
874 mHaveConnection = false;
875 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800876
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700877 if (mCurToken != null) {
878 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800879 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700880 mIWindowManager.removeWindowToken(mCurToken);
881 } catch (RemoteException e) {
882 }
883 mCurToken = null;
884 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800885
The Android Open Source Project10592532009-03-18 17:39:46 -0700886 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700887 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800888
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700889 if (reportToClient && mCurClient != null) {
890 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
891 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
892 }
893 }
894
Devin Taylor0c33ed22010-02-23 13:26:46 -0600895 private void finishSession(SessionState sessionState) {
896 if (sessionState != null && sessionState.session != null) {
897 try {
898 sessionState.session.finishSession();
899 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700900 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600901 }
902 }
903 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800904
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700905 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 if (mCurMethod != null) {
907 for (ClientState cs : mClients.values()) {
908 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600909 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 cs.curSession = null;
911 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600912
913 finishSession(mEnabledSession);
914 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 mCurMethod = null;
916 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700917 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 public void onServiceDisconnected(ComponentName name) {
921 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800922 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 + " mCurIntent=" + mCurIntent);
924 if (mCurMethod != null && mCurIntent != null
925 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700926 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 // We consider this to be a new bind attempt, since the system
928 // should now try to restart the service for us.
929 mLastBindTime = SystemClock.uptimeMillis();
930 mShowRequested = mInputShown;
931 mInputShown = false;
932 if (mCurClient != null) {
933 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
934 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
935 }
936 }
937 }
938 }
939
940 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700941 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 long ident = Binder.clearCallingIdentity();
943 try {
944 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700945 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 return;
947 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 synchronized (mMethodMap) {
950 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800951 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700952 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800954 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700955 mStatusBar.setIcon("ime", packageName, iconId, 0);
956 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 }
958 }
959 } finally {
960 Binder.restoreCallingIdentity(ident);
961 }
962 }
963
satok06487a52010-10-29 11:37:18 +0900964 public void setIMEButtonVisible(IBinder token, boolean visible) {
965 int uid = Binder.getCallingUid();
966 long ident = Binder.clearCallingIdentity();
967 try {
968 if (token == null || mCurToken != token) {
969 Slog.w(TAG, "Ignoring setIMEButtonVisible of uid " + uid + " token: " + token);
970 return;
971 }
972
973 synchronized (mMethodMap) {
satokcd7cd292010-11-20 15:46:23 +0900974 mStatusBar.setIMEButtonVisible(token, visible);
satok06487a52010-10-29 11:37:18 +0900975 }
976 } finally {
977 Binder.restoreCallingIdentity(ident);
978 }
979 }
980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700982 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
983 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
984 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
985 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +0900987 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +0900988 // There is no input method selected, try to choose new applicable input method.
989 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
990 id = Settings.Secure.getString(mContext.getContentResolver(),
991 Settings.Secure.DEFAULT_INPUT_METHOD);
992 }
993 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 try {
satokab751aa2010-09-14 19:17:36 +0900995 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800997 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -0700998 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700999 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 }
satokf3db1af2010-11-23 13:34:33 +09001001 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001002 } else {
1003 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001004 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001005 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 }
1007 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001008
satokab751aa2010-09-14 19:17:36 +09001009 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 InputMethodInfo info = mMethodMap.get(id);
1011 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001012 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 if (id.equals(mCurMethodId)) {
satokb66d2872010-11-10 01:04:04 +09001016 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
satokcd7cd292010-11-20 15:46:23 +09001017 InputMethodSubtype subtype = null;
satokb66d2872010-11-10 01:04:04 +09001018 if (subtypeId >= 0 && subtypeId < subtypes.size()) {
satokcd7cd292010-11-20 15:46:23 +09001019 subtype = subtypes.get(subtypeId);
1020 }
1021 if (subtype != mCurrentSubtype) {
1022 synchronized (mMethodMap) {
1023 if (mCurMethod != null) {
1024 try {
1025 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1026 if (mInputShown) {
1027 // If mInputShown is false, there is no IME button on the
1028 // system bar.
1029 // Thus there is no need to make it invisible explicitly.
1030 mStatusBar.setIMEButtonVisible(mCurToken, true);
satokab751aa2010-09-14 19:17:36 +09001031 }
satokcd7cd292010-11-20 15:46:23 +09001032 // If subtype is null, try to find the most applicable one from
1033 // getCurrentInputMethodSubtype.
1034 if (subtype == null) {
1035 subtype = getCurrentInputMethodSubtype();
1036 }
1037 mCurMethod.changeInputMethodSubtype(subtype);
1038 } catch (RemoteException e) {
1039 return;
satokab751aa2010-09-14 19:17:36 +09001040 }
1041 }
1042 }
1043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 return;
1045 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 final long ident = Binder.clearCallingIdentity();
1048 try {
satokab751aa2010-09-14 19:17:36 +09001049 // Set a subtype to this input method.
1050 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001051 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1052 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1053 // because mCurMethodId is stored as a history in
1054 // setSelectedInputMethodAndSubtypeLocked().
1055 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056
1057 if (ActivityManagerNative.isSystemReady()) {
1058 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001059 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 intent.putExtra("input_method_id", id);
1061 mContext.sendBroadcast(intent);
1062 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001063 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 } finally {
1065 Binder.restoreCallingIdentity(ident);
1066 }
1067 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001068
The Android Open Source Project4df24232009-03-05 14:34:35 -08001069 public boolean showSoftInput(IInputMethodClient client, int flags,
1070 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001071 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 long ident = Binder.clearCallingIdentity();
1073 try {
1074 synchronized (mMethodMap) {
1075 if (mCurClient == null || client == null
1076 || mCurClient.client.asBinder() != client.asBinder()) {
1077 try {
1078 // We need to check if this is the current client with
1079 // focus in the window manager, to allow this call to
1080 // be made before input is started in it.
1081 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001082 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001083 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 }
1085 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001086 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
1088 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001089
Joe Onorato8a9b2202010-02-26 18:56:32 -08001090 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001091 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 }
1093 } finally {
1094 Binder.restoreCallingIdentity(ident);
1095 }
1096 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001097
The Android Open Source Project4df24232009-03-05 14:34:35 -08001098 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 mShowRequested = true;
1100 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1101 mShowExplicitlyRequested = true;
1102 }
1103 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1104 mShowExplicitlyRequested = true;
1105 mShowForced = true;
1106 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001107
Dianne Hackborncc278702009-09-02 23:07:23 -07001108 if (!mSystemReady) {
1109 return false;
1110 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001111
The Android Open Source Project4df24232009-03-05 14:34:35 -08001112 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001114 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1115 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1116 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001118 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 } else if (mHaveConnection && SystemClock.uptimeMillis()
1120 < (mLastBindTime+TIME_TO_RECONNECT)) {
1121 // The client has asked to have the input method shown, but
1122 // we have been sitting here too long with a connection to the
1123 // service and no interface received, so let's disconnect/connect
1124 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001125 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 SystemClock.uptimeMillis()-mLastBindTime,1);
1127 mContext.unbindService(this);
1128 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1129 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001130
The Android Open Source Project4df24232009-03-05 14:34:35 -08001131 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001133
The Android Open Source Project4df24232009-03-05 14:34:35 -08001134 public boolean hideSoftInput(IInputMethodClient client, int flags,
1135 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001136 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 long ident = Binder.clearCallingIdentity();
1138 try {
1139 synchronized (mMethodMap) {
1140 if (mCurClient == null || client == null
1141 || mCurClient.client.asBinder() != client.asBinder()) {
1142 try {
1143 // We need to check if this is the current client with
1144 // focus in the window manager, to allow this call to
1145 // be made before input is started in it.
1146 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001147 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1148 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001149 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 }
1151 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001152 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 }
1154 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001155
Joe Onorato8a9b2202010-02-26 18:56:32 -08001156 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001157 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 }
1159 } finally {
1160 Binder.restoreCallingIdentity(ident);
1161 }
1162 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001163
The Android Open Source Project4df24232009-03-05 14:34:35 -08001164 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1166 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001167 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001169 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 }
1171 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001172 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001174 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001176 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001178 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1179 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1180 res = true;
1181 } else {
1182 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 }
1184 mInputShown = false;
1185 mShowRequested = false;
1186 mShowExplicitlyRequested = false;
1187 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001188 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001190
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001191 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1193 boolean first, int windowFlags) {
1194 long ident = Binder.clearCallingIdentity();
1195 try {
1196 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001197 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 + " viewHasFocus=" + viewHasFocus
1199 + " isTextEditor=" + isTextEditor
1200 + " softInputMode=#" + Integer.toHexString(softInputMode)
1201 + " first=" + first + " flags=#"
1202 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 if (mCurClient == null || client == null
1205 || mCurClient.client.asBinder() != client.asBinder()) {
1206 try {
1207 // We need to check if this is the current client with
1208 // focus in the window manager, to allow this call to
1209 // be made before input is started in it.
1210 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001211 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 return;
1213 }
1214 } catch (RemoteException e) {
1215 }
1216 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001217
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001218 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001219 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001220 return;
1221 }
1222 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001223
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001224 // Should we auto-show the IME even if the caller has not
1225 // specified what should be done with it?
1226 // We only do this automatically if the window can resize
1227 // to accommodate the IME (so what the user sees will give
1228 // them good context without input information being obscured
1229 // by the IME) or if running on a large screen where there
1230 // is more room for the target window + IME.
1231 final boolean doAutoShow =
1232 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1233 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1234 || mRes.getConfiguration().isLayoutSizeAtLeast(
1235 Configuration.SCREENLAYOUT_SIZE_LARGE);
1236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1238 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001239 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1241 // There is no focus view, and this window will
1242 // be behind any soft input window, so hide the
1243 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001244 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001245 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001247 } else if (isTextEditor && doAutoShow && (softInputMode &
1248 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 // There is a focus view, and we are navigating forward
1250 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001251 // We only do this automatically if the window an resize
1252 // to accomodate the IME (so what the user sees will give
1253 // them good context without input information being obscured
1254 // by the IME) or if running on a large screen where there
1255 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001256 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001257 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 }
1259 break;
1260 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1261 // Do nothing.
1262 break;
1263 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1264 if ((softInputMode &
1265 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001266 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001267 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 }
1269 break;
1270 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001271 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001272 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 break;
1274 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1275 if ((softInputMode &
1276 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001277 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001278 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 }
1280 break;
1281 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001282 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001283 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 break;
1285 }
1286 }
1287 } finally {
1288 Binder.restoreCallingIdentity(ident);
1289 }
1290 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1293 synchronized (mMethodMap) {
1294 if (mCurClient == null || client == null
1295 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001296 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001297 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 }
1299
satok440aab52010-11-25 09:43:11 +09001300 // Always call subtype picker, because subtype picker is a superset of input method
1301 // picker.
satokab751aa2010-09-14 19:17:36 +09001302 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1303 }
1304 }
1305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001307 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1308 }
1309
1310 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1311 synchronized (mMethodMap) {
1312 if (subtype != null) {
1313 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1314 mMethodMap.get(id), subtype.hashCode()));
1315 } else {
1316 setInputMethod(token, id);
1317 }
1318 }
satokab751aa2010-09-14 19:17:36 +09001319 }
1320
satokb416a712010-11-25 20:42:14 +09001321 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001322 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001323 synchronized (mMethodMap) {
1324 if (mCurClient == null || client == null
1325 || mCurClient.client.asBinder() != client.asBinder()) {
1326 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1327 }
satok7fee71f2010-12-17 18:54:26 +09001328 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1329 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001330 }
1331 }
1332
satok735cf382010-11-11 20:40:09 +09001333 public boolean switchToLastInputMethod(IBinder token) {
1334 synchronized (mMethodMap) {
1335 Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1336 if (lastIme != null) {
1337 InputMethodInfo imi = mMethodMap.get(lastIme.first);
1338 if (imi != null) {
satok28203512010-11-24 11:06:49 +09001339 setInputMethodWithSubtypeId(token, lastIme.first, getSubtypeIdFromHashCode(
satok735cf382010-11-11 20:40:09 +09001340 imi, Integer.valueOf(lastIme.second)));
1341 return true;
1342 }
1343 }
1344 return false;
1345 }
1346 }
1347
satok28203512010-11-24 11:06:49 +09001348 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 synchronized (mMethodMap) {
1350 if (token == null) {
1351 if (mContext.checkCallingOrSelfPermission(
1352 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1353 != PackageManager.PERMISSION_GRANTED) {
1354 throw new SecurityException(
1355 "Using null token requires permission "
1356 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1357 }
1358 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001359 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1360 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 return;
1362 }
1363
1364 long ident = Binder.clearCallingIdentity();
1365 try {
satokab751aa2010-09-14 19:17:36 +09001366 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 } finally {
1368 Binder.restoreCallingIdentity(ident);
1369 }
1370 }
1371 }
1372
1373 public void hideMySoftInput(IBinder token, int flags) {
1374 synchronized (mMethodMap) {
1375 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001376 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1377 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 return;
1379 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 long ident = Binder.clearCallingIdentity();
1381 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001382 hideCurrentInputLocked(flags, null);
1383 } finally {
1384 Binder.restoreCallingIdentity(ident);
1385 }
1386 }
1387 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001388
The Android Open Source Project4df24232009-03-05 14:34:35 -08001389 public void showMySoftInput(IBinder token, int flags) {
1390 synchronized (mMethodMap) {
1391 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001392 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1393 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001394 return;
1395 }
1396 long ident = Binder.clearCallingIdentity();
1397 try {
1398 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 } finally {
1400 Binder.restoreCallingIdentity(ident);
1401 }
1402 }
1403 }
1404
1405 void setEnabledSessionInMainThread(SessionState session) {
1406 if (mEnabledSession != session) {
1407 if (mEnabledSession != null) {
1408 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001409 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 mEnabledSession.method.setSessionEnabled(
1411 mEnabledSession.session, false);
1412 } catch (RemoteException e) {
1413 }
1414 }
1415 mEnabledSession = session;
1416 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001417 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 session.method.setSessionEnabled(
1419 session.session, true);
1420 } catch (RemoteException e) {
1421 }
1422 }
1423 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 public boolean handleMessage(Message msg) {
1426 HandlerCaller.SomeArgs args;
1427 switch (msg.what) {
1428 case MSG_SHOW_IM_PICKER:
1429 showInputMethodMenu();
1430 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001431
satokab751aa2010-09-14 19:17:36 +09001432 case MSG_SHOW_IM_SUBTYPE_PICKER:
1433 showInputMethodSubtypeMenu();
1434 return true;
1435
satok47a44912010-10-06 16:03:58 +09001436 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001437 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001438 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001439 return true;
1440
1441 case MSG_SHOW_IM_CONFIG:
1442 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001443 return true;
1444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 case MSG_UNBIND_INPUT:
1448 try {
1449 ((IInputMethod)msg.obj).unbindInput();
1450 } catch (RemoteException e) {
1451 // There is nothing interesting about the method dying.
1452 }
1453 return true;
1454 case MSG_BIND_INPUT:
1455 args = (HandlerCaller.SomeArgs)msg.obj;
1456 try {
1457 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1458 } catch (RemoteException e) {
1459 }
1460 return true;
1461 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001462 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001464 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1465 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 } catch (RemoteException e) {
1467 }
1468 return true;
1469 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001470 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001472 ((IInputMethod)args.arg1).hideSoftInput(0,
1473 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 } catch (RemoteException e) {
1475 }
1476 return true;
1477 case MSG_ATTACH_TOKEN:
1478 args = (HandlerCaller.SomeArgs)msg.obj;
1479 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001480 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1482 } catch (RemoteException e) {
1483 }
1484 return true;
1485 case MSG_CREATE_SESSION:
1486 args = (HandlerCaller.SomeArgs)msg.obj;
1487 try {
1488 ((IInputMethod)args.arg1).createSession(
1489 (IInputMethodCallback)args.arg2);
1490 } catch (RemoteException e) {
1491 }
1492 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 case MSG_START_INPUT:
1496 args = (HandlerCaller.SomeArgs)msg.obj;
1497 try {
1498 SessionState session = (SessionState)args.arg1;
1499 setEnabledSessionInMainThread(session);
1500 session.method.startInput((IInputContext)args.arg2,
1501 (EditorInfo)args.arg3);
1502 } catch (RemoteException e) {
1503 }
1504 return true;
1505 case MSG_RESTART_INPUT:
1506 args = (HandlerCaller.SomeArgs)msg.obj;
1507 try {
1508 SessionState session = (SessionState)args.arg1;
1509 setEnabledSessionInMainThread(session);
1510 session.method.restartInput((IInputContext)args.arg2,
1511 (EditorInfo)args.arg3);
1512 } catch (RemoteException e) {
1513 }
1514 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 case MSG_UNBIND_METHOD:
1519 try {
1520 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1521 } catch (RemoteException e) {
1522 // There is nothing interesting about the last client dying.
1523 }
1524 return true;
1525 case MSG_BIND_METHOD:
1526 args = (HandlerCaller.SomeArgs)msg.obj;
1527 try {
1528 ((IInputMethodClient)args.arg1).onBindMethod(
1529 (InputBindResult)args.arg2);
1530 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001531 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 }
1533 return true;
1534 }
1535 return false;
1536 }
1537
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001538 private boolean isSystemIme(InputMethodInfo inputMethod) {
1539 return (inputMethod.getServiceInfo().applicationInfo.flags
1540 & ApplicationInfo.FLAG_SYSTEM) != 0;
1541 }
1542
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001543 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001544 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001545 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001546 // We'd prefer to fall back on a system IME, since that is safer.
1547 int i=enabled.size();
1548 while (i > 0) {
1549 i--;
1550 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1551 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1552 break;
1553 }
1554 }
satokab751aa2010-09-14 19:17:36 +09001555 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001556 if (DEBUG) {
1557 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1558 }
satok723a27e2010-11-11 14:58:11 +09001559 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001560 return true;
1561 }
1562
1563 return false;
1564 }
1565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1567 HashMap<String, InputMethodInfo> map) {
1568 list.clear();
1569 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001572 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001573 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1574 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1575 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1576 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577
1578 List<ResolveInfo> services = pm.queryIntentServices(
1579 new Intent(InputMethod.SERVICE_INTERFACE),
1580 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 for (int i = 0; i < services.size(); ++i) {
1583 ResolveInfo ri = services.get(i);
1584 ServiceInfo si = ri.serviceInfo;
1585 ComponentName compName = new ComponentName(si.packageName, si.name);
1586 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1587 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001588 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 + ": it does not require the permission "
1590 + android.Manifest.permission.BIND_INPUT_METHOD);
1591 continue;
1592 }
1593
Joe Onorato8a9b2202010-02-26 18:56:32 -08001594 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595
1596 try {
1597 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1598 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001599 final String id = p.getId();
1600 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601
Amith Yamasanie861ec12010-03-24 21:39:27 -07001602 // System IMEs are enabled by default, unless there's a hard keyboard
1603 // and the system IME was explicitly disabled
1604 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1605 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001606 }
1607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001609 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001613 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001615 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 }
1617 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001618
1619 String defaultIme = Settings.Secure.getString(mContext
1620 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001621 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001622 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001623 updateFromSettingsLocked();
1624 }
1625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001629
satokab751aa2010-09-14 19:17:36 +09001630 private void showInputMethodMenu() {
1631 showInputMethodMenuInternal(false);
1632 }
1633
1634 private void showInputMethodSubtypeMenu() {
1635 showInputMethodMenuInternal(true);
1636 }
1637
satok217f5482010-12-15 05:19:19 +09001638 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
satok86417ea2010-10-27 14:11:03 +09001639 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_AND_SUBTYPE_ENABLER);
satok47a44912010-10-06 16:03:58 +09001640 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001641 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1642 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001643 if (!TextUtils.isEmpty(inputMethodId)) {
1644 intent.putExtra(EXTRA_INPUT_METHOD_ID, inputMethodId);
1645 }
satok217f5482010-12-15 05:19:19 +09001646 mContext.startActivity(intent);
1647 }
1648
1649 private void showConfigureInputMethods() {
1650 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1651 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1652 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1653 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001654 mContext.startActivity(intent);
1655 }
1656
satokab751aa2010-09-14 19:17:36 +09001657 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001658 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 String lastInputMethodId = Settings.Secure.getString(context
1665 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001666 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001667 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001668
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001669 synchronized (mMethodMap) {
satok7f35c8c2010-10-07 21:13:11 +09001670 final List<Pair<InputMethodInfo, ArrayList<String>>> immis =
satok67ddf9c2010-11-17 09:45:54 +09001671 mSettings.getEnabledInputMethodAndSubtypeHashCodeListLocked();
satok16331c82010-12-20 23:48:46 +09001672 int N = immis.size();
1673
1674 // Add applicable subtypes if no subtype for each IME is enabled.
1675 for (int i = 0; i < N; ++i) {
1676 InputMethodInfo imi = immis.get(i).first;
1677 ArrayList<String> subtypes = immis.get(i).second;
1678 if (subtypes != null && subtypes.size() == 0) {
1679 ArrayList<InputMethodSubtype> applicableSubtypes =
1680 getApplicableSubtypesLocked(imi.getSubtypes());
1681 final int numSubtypes = applicableSubtypes.size();
1682 for (int j = 0; j < numSubtypes; ++j) {
1683 subtypes.add(String.valueOf(applicableSubtypes.get(j).hashCode()));
1684 }
1685 }
1686 }
1687
satok7f35c8c2010-10-07 21:13:11 +09001688 ArrayList<Integer> subtypeIds = new ArrayList<Integer>();
1689
1690 if (immis == null || immis.size() == 0) {
1691 return;
1692 }
1693
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001694 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695
satok913a8922010-08-26 21:53:41 +09001696
satokab751aa2010-09-14 19:17:36 +09001697 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1698 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001699
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001700 for (int i = 0; i < N; ++i) {
satok7f35c8c2010-10-07 21:13:11 +09001701 InputMethodInfo property = immis.get(i).first;
1702 final ArrayList<String> enabledSubtypeIds = immis.get(i).second;
1703 HashSet<String> enabledSubtypeSet = new HashSet<String>();
1704 for (String s : enabledSubtypeIds) {
1705 enabledSubtypeSet.add(s);
1706 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001707 if (property == null) {
1708 continue;
1709 }
satokab751aa2010-09-14 19:17:36 +09001710 ArrayList<InputMethodSubtype> subtypes = property.getSubtypes();
1711 CharSequence label = property.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001712 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokab751aa2010-09-14 19:17:36 +09001713 for (int j = 0; j < subtypes.size(); ++j) {
1714 InputMethodSubtype subtype = subtypes.get(j);
satok7f35c8c2010-10-07 21:13:11 +09001715 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1716 CharSequence title;
1717 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001718 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001719 if (nameResId != 0) {
1720 title = pm.getText(property.getPackageName(), nameResId,
1721 property.getServiceInfo().applicationInfo);
1722 } else {
1723 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001724 // TODO: Use more friendly Title and UI
1725 title = label + "," + (mode == null ? "" : mode) + ","
1726 + (language == null ? "" : language);
1727 }
1728 imMap.put(title, new Pair<InputMethodInfo, Integer>(property, j));
satokab751aa2010-09-14 19:17:36 +09001729 }
satokab751aa2010-09-14 19:17:36 +09001730 }
1731 } else {
1732 imMap.put(label,
1733 new Pair<InputMethodInfo, Integer>(property, NOT_A_SUBTYPE_ID));
1734 subtypeIds.add(0);
1735 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001736 }
satok913a8922010-08-26 21:53:41 +09001737
1738 N = imMap.size();
1739 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001740 mIms = new InputMethodInfo[N];
1741 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001742 int checkedItem = 0;
1743 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001744 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1745 mIms[i] = value.first;
1746 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001747 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001748 int subtypeId = mSubtypeIds[i];
1749 if ((subtypeId == NOT_A_SUBTYPE_ID)
1750 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1751 || (subtypeId == lastInputMethodSubtypeId)) {
1752 checkedItem = i;
1753 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 }
satokab751aa2010-09-14 19:17:36 +09001756
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001757 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1758 public void onClick(DialogInterface dialog, int which) {
1759 hideInputMethodMenu();
1760 }
1761 };
satokd87c2592010-09-29 11:52:06 +09001762
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001763 TypedArray a = context.obtainStyledAttributes(null,
1764 com.android.internal.R.styleable.DialogPreference,
1765 com.android.internal.R.attr.alertDialogStyle, 0);
1766 mDialogBuilder = new AlertDialog.Builder(context)
1767 .setTitle(com.android.internal.R.string.select_input_method)
1768 .setOnCancelListener(new OnCancelListener() {
1769 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001771 }
1772 })
1773 .setIcon(a.getDrawable(
1774 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1775 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001776
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001777 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1778 new AlertDialog.OnClickListener() {
1779 public void onClick(DialogInterface dialog, int which) {
1780 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001781 if (mIms == null || mIms.length <= which
1782 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001783 return;
1784 }
1785 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001786 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001787 hideInputMethodMenu();
1788 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001789 if ((subtypeId < 0)
1790 || (subtypeId >= im.getSubtypes().size())) {
1791 subtypeId = NOT_A_SUBTYPE_ID;
1792 }
1793 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001794 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001797 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798
satok7f35c8c2010-10-07 21:13:11 +09001799 if (showSubtypes) {
1800 mDialogBuilder.setPositiveButton(com.android.internal.R.string.more_item_label,
1801 new DialogInterface.OnClickListener() {
1802 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09001803 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09001804 }
1805 });
1806 }
satok0ff647b2010-10-08 13:49:28 +09001807 mDialogBuilder.setNegativeButton(com.android.internal.R.string.cancel,
1808 new DialogInterface.OnClickListener() {
1809 public void onClick(DialogInterface dialog, int whichButton) {
1810 hideInputMethodMenu();
1811 }
1812 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 mSwitchingDialog = mDialogBuilder.create();
1814 mSwitchingDialog.getWindow().setType(
1815 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1816 mSwitchingDialog.show();
1817 }
1818 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001821 synchronized (mMethodMap) {
1822 hideInputMethodMenuLocked();
1823 }
1824 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001825
The Android Open Source Project10592532009-03-18 17:39:46 -07001826 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001827 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828
The Android Open Source Project10592532009-03-18 17:39:46 -07001829 if (mSwitchingDialog != null) {
1830 mSwitchingDialog.dismiss();
1831 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001833
The Android Open Source Project10592532009-03-18 17:39:46 -07001834 mDialogBuilder = null;
1835 mItems = null;
1836 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 public boolean setInputMethodEnabled(String id, boolean enabled) {
1842 synchronized (mMethodMap) {
1843 if (mContext.checkCallingOrSelfPermission(
1844 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1845 != PackageManager.PERMISSION_GRANTED) {
1846 throw new SecurityException(
1847 "Requires permission "
1848 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1849 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 long ident = Binder.clearCallingIdentity();
1852 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001853 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 } finally {
1855 Binder.restoreCallingIdentity(ident);
1856 }
1857 }
1858 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001859
1860 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1861 // Make sure this is a valid input method.
1862 InputMethodInfo imm = mMethodMap.get(id);
1863 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001864 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001865 }
1866
satokd87c2592010-09-29 11:52:06 +09001867 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1868 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001869
satokd87c2592010-09-29 11:52:06 +09001870 if (enabled) {
1871 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1872 if (pair.first.equals(id)) {
1873 // We are enabling this input method, but it is already enabled.
1874 // Nothing to do. The previous state was enabled.
1875 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001876 }
1877 }
satokd87c2592010-09-29 11:52:06 +09001878 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1879 // Previous state was disabled.
1880 return false;
1881 } else {
1882 StringBuilder builder = new StringBuilder();
1883 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1884 builder, enabledInputMethodsList, id)) {
1885 // Disabled input method is currently selected, switch to another one.
1886 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1887 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001888 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
1889 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
1890 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09001891 }
1892 // Previous state was enabled.
1893 return true;
1894 } else {
1895 // We are disabling the input method but it is already disabled.
1896 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001897 return false;
1898 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001899 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001900 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001901
satok723a27e2010-11-11 14:58:11 +09001902 private void saveCurrentInputMethodAndSubtypeToHistory() {
1903 String subtypeId = NOT_A_SUBTYPE_ID_STR;
1904 if (mCurrentSubtype != null) {
1905 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
1906 }
1907 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
satokab751aa2010-09-14 19:17:36 +09001908 }
1909
satok723a27e2010-11-11 14:58:11 +09001910 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
1911 boolean setSubtypeOnly) {
1912 // Update the history of InputMethod and Subtype
1913 saveCurrentInputMethodAndSubtypeToHistory();
1914
1915 // Set Subtype here
1916 if (imi == null || subtypeId < 0) {
1917 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001918 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09001919 } else {
1920 final ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1921 if (subtypeId < subtypes.size()) {
1922 mSettings.putSelectedSubtype(subtypes.get(subtypeId).hashCode());
1923 mCurrentSubtype = subtypes.get(subtypeId);
1924 } else {
1925 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
1926 mCurrentSubtype = null;
1927 }
satokab751aa2010-09-14 19:17:36 +09001928 }
satok723a27e2010-11-11 14:58:11 +09001929
1930 if (!setSubtypeOnly) {
1931 // Set InputMethod here
1932 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
1933 }
1934 }
1935
1936 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
1937 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
1938 int lastSubtypeId = NOT_A_SUBTYPE_ID;
1939 // newDefaultIme is empty when there is no candidate for the selected IME.
1940 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
1941 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
1942 if (subtypeHashCode != null) {
1943 try {
1944 lastSubtypeId = getSubtypeIdFromHashCode(
1945 imi, Integer.valueOf(subtypeHashCode));
1946 } catch (NumberFormatException e) {
1947 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
1948 }
1949 }
1950 }
1951 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09001952 }
1953
1954 private int getSelectedInputMethodSubtypeId(String id) {
1955 InputMethodInfo imi = mMethodMap.get(id);
1956 if (imi == null) {
1957 return NOT_A_SUBTYPE_ID;
1958 }
satokab751aa2010-09-14 19:17:36 +09001959 int subtypeId;
1960 try {
1961 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
1962 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
1963 } catch (SettingNotFoundException e) {
1964 return NOT_A_SUBTYPE_ID;
1965 }
satok723a27e2010-11-11 14:58:11 +09001966 return getSubtypeIdFromHashCode(imi, subtypeId);
1967 }
1968
1969 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09001970 if (imi != null) {
1971 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1972 for (int i = 0; i < subtypes.size(); ++i) {
1973 InputMethodSubtype ims = subtypes.get(i);
1974 if (subtypeHashCode == ims.hashCode()) {
1975 return i;
1976 }
satokab751aa2010-09-14 19:17:36 +09001977 }
1978 }
1979 return NOT_A_SUBTYPE_ID;
1980 }
1981
satok16331c82010-12-20 23:48:46 +09001982 private ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
1983 List<InputMethodSubtype> subtypes) {
1984 ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>();
1985 final String systemLocale = mRes.getConfiguration().locale.toString();
1986 if (TextUtils.isEmpty(systemLocale)) return applicableSubtypes;
1987 final int N = subtypes.size();
1988 boolean containsKeyboardSubtype = false;
1989 for (int i = 0; i < N; ++i) {
1990 InputMethodSubtype subtype = subtypes.get(i);
1991 if (subtype.getLocale().equals(systemLocale)) {
1992 applicableSubtypes.add(subtype);
1993 if (!containsKeyboardSubtype
1994 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
1995 containsKeyboardSubtype = true;
1996 }
1997 }
1998 }
1999 if (!containsKeyboardSubtype) {
2000 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satok7599a7f2010-12-22 13:45:23 +09002001 subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002002 if (lastResortKeyboardSubtype != null) {
2003 applicableSubtypes.add(lastResortKeyboardSubtype);
2004 }
2005 }
2006 return applicableSubtypes;
2007 }
2008
satok4e4569d2010-11-19 18:45:53 +09002009 /**
2010 * If there are no selected subtypes, tries finding the most applicable one according to the
2011 * given locale.
2012 * @param subtypes this function will search the most applicable subtype in subtypes
2013 * @param mode subtypes will be filtered by mode
2014 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002015 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2016 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002017 * @return the most applicable subtypeId
2018 */
satokcd7cd292010-11-20 15:46:23 +09002019 private InputMethodSubtype findLastResortApplicableSubtypeLocked(
satok7599a7f2010-12-22 13:45:23 +09002020 List<InputMethodSubtype> subtypes, String mode, String locale,
2021 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002022 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002023 return null;
satok8fbb1e82010-11-02 23:15:58 +09002024 }
satok4e4569d2010-11-19 18:45:53 +09002025 if (TextUtils.isEmpty(locale)) {
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002026 locale = mRes.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002027 }
satok8fbb1e82010-11-02 23:15:58 +09002028 final String language = locale.substring(0, 2);
2029 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002030 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002031 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002032 final int N = subtypes.size();
2033 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002034 InputMethodSubtype subtype = subtypes.get(i);
2035 final String subtypeLocale = subtype.getLocale();
satok4e4569d2010-11-19 18:45:53 +09002036 // An applicable subtype should match "mode".
2037 if (subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002038 if (firstMatchedModeSubtype == null) {
2039 firstMatchedModeSubtype = subtype;
2040 }
satok9ef02832010-11-04 21:17:48 +09002041 if (locale.equals(subtypeLocale)) {
2042 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002043 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002044 break;
2045 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2046 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002047 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002048 partialMatchFound = true;
2049 }
satok8fbb1e82010-11-02 23:15:58 +09002050 }
2051 }
2052
satok7599a7f2010-12-22 13:45:23 +09002053 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2054 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002055 }
2056
satok8fbb1e82010-11-02 23:15:58 +09002057 // The first subtype applicable to the system locale will be defined as the most applicable
2058 // subtype.
2059 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002060 if (applicableSubtype != null) {
2061 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2062 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2063 }
satok8fbb1e82010-11-02 23:15:58 +09002064 }
satokcd7cd292010-11-20 15:46:23 +09002065 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002066 }
2067
satok4e4569d2010-11-19 18:45:53 +09002068 // If there are no selected shortcuts, tries finding the most applicable ones.
2069 private Pair<InputMethodInfo, InputMethodSubtype>
2070 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2071 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2072 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002073 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002074 boolean foundInSystemIME = false;
2075
2076 // Search applicable subtype for each InputMethodInfo
2077 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002078 final String imiId = imi.getId();
2079 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2080 continue;
2081 }
satokcd7cd292010-11-20 15:46:23 +09002082 InputMethodSubtype subtype = null;
satok7599a7f2010-12-22 13:45:23 +09002083 final List<InputMethodSubtype> explicitlyEnabledSubtypes =
2084 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
2085 // 1. Search by the current subtype's locale from explicitlyEnabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002086 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002087 subtype = findLastResortApplicableSubtypeLocked(
satok7599a7f2010-12-22 13:45:23 +09002088 explicitlyEnabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002089 }
satok7599a7f2010-12-22 13:45:23 +09002090 // 2. Search by the system locale from explicitlyEnabledSubtypes.
2091 // 3. Search the first enabled subtype matched with mode from explicitlyEnabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002092 if (subtype == null) {
2093 subtype = findLastResortApplicableSubtypeLocked(
satok7599a7f2010-12-22 13:45:23 +09002094 explicitlyEnabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002095 }
satok7599a7f2010-12-22 13:45:23 +09002096 // 4. Search by the current subtype's locale from all subtypes.
2097 if (subtype == null && mCurrentSubtype != null) {
2098 subtype = findLastResortApplicableSubtypeLocked(
2099 imi.getSubtypes(), mode, mCurrentSubtype.getLocale(), false);
2100 }
2101 // 5. Search by the system locale from all subtypes.
2102 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002103 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002104 subtype = findLastResortApplicableSubtypeLocked(
2105 imi.getSubtypes(), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002106 }
satokcd7cd292010-11-20 15:46:23 +09002107 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002108 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002109 // The current input method is the most applicable IME.
2110 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002111 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002112 break;
satok7599a7f2010-12-22 13:45:23 +09002113 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002114 // The system input method is 2nd applicable IME.
2115 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002116 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002117 if ((imi.getServiceInfo().applicationInfo.flags
2118 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2119 foundInSystemIME = true;
2120 }
satok4e4569d2010-11-19 18:45:53 +09002121 }
2122 }
2123 }
2124 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002125 if (mostApplicableIMI != null) {
2126 Slog.w(TAG, "Most applicable shortcut input method was:"
2127 + mostApplicableIMI.getId());
2128 if (mostApplicableSubtype != null) {
2129 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2130 + "," + mostApplicableSubtype.getMode() + ","
2131 + mostApplicableSubtype.getLocale());
2132 }
2133 }
satok4e4569d2010-11-19 18:45:53 +09002134 }
satokcd7cd292010-11-20 15:46:23 +09002135 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002136 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002137 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002138 } else {
2139 return null;
2140 }
2141 }
2142
satokab751aa2010-09-14 19:17:36 +09002143 /**
2144 * @return Return the current subtype of this input method.
2145 */
2146 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002147 boolean subtypeIsSelected = false;
2148 try {
2149 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2150 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2151 } catch (SettingNotFoundException e) {
2152 }
satok3ef8b292010-11-23 06:06:29 +09002153 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002154 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002155 String lastInputMethodId = Settings.Secure.getString(
2156 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002157 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2158 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002159 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2160 if (imi != null) {
2161 // If there are no selected subtypes, the framework will try to find
2162 // the most applicable subtype from all subtypes whose mode is
2163 // SUBTYPE_MODE_KEYBOARD. This is an exceptional case, so we will hardcode
2164 // the mode.
satokcd7cd292010-11-20 15:46:23 +09002165 mCurrentSubtype = findLastResortApplicableSubtypeLocked(imi.getSubtypes(),
satok7599a7f2010-12-22 13:45:23 +09002166 SUBTYPE_MODE_KEYBOARD, null, true);
satok4e4569d2010-11-19 18:45:53 +09002167 }
satokcd7cd292010-11-20 15:46:23 +09002168 } else {
satok3ef8b292010-11-23 06:06:29 +09002169 mCurrentSubtype =
2170 mMethodMap.get(lastInputMethodId).getSubtypes().get(subtypeId);
2171 }
satok8fbb1e82010-11-02 23:15:58 +09002172 }
satok3ef8b292010-11-23 06:06:29 +09002173 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002174 }
satokab751aa2010-09-14 19:17:36 +09002175 }
2176
satokf3db1af2010-11-23 13:34:33 +09002177 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2178 InputMethodSubtype subtype) {
2179 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2180 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2181 } else {
2182 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2183 subtypes.add(subtype);
2184 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2185 }
2186 }
2187
satok4e4569d2010-11-19 18:45:53 +09002188 // TODO: We should change the return type from List to List<Parcelable>
2189 public List getShortcutInputMethodsAndSubtypes() {
2190 synchronized (mMethodMap) {
satokf3db1af2010-11-23 13:34:33 +09002191 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002192 // If there are no selected shortcut subtypes, the framework will try to find
2193 // the most applicable subtype from all subtypes whose mode is
2194 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002195 Pair<InputMethodInfo, InputMethodSubtype> info =
2196 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2197 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002198 if (info != null) {
2199 addShortcutInputMethodAndSubtypes(info.first, info.second);
2200 }
satokf3db1af2010-11-23 13:34:33 +09002201 }
satokcd7cd292010-11-20 15:46:23 +09002202 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002203 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2204 ret.add(imi);
2205 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2206 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002207 }
2208 }
satokf3db1af2010-11-23 13:34:33 +09002209 return ret;
satok4e4569d2010-11-19 18:45:53 +09002210 }
2211 }
2212
satokb66d2872010-11-10 01:04:04 +09002213 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2214 synchronized (mMethodMap) {
2215 if (subtype != null && mCurMethodId != null) {
2216 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2217 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2218 if (subtypeId != NOT_A_SUBTYPE_ID) {
2219 setInputMethodLocked(mCurMethodId, subtypeId);
2220 return true;
2221 }
2222 }
2223 return false;
2224 }
2225 }
2226
satokd87c2592010-09-29 11:52:06 +09002227 /**
2228 * Utility class for putting and getting settings for InputMethod
2229 * TODO: Move all putters and getters of settings to this class.
2230 */
2231 private static class InputMethodSettings {
2232 // The string for enabled input method is saved as follows:
2233 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2234 private static final char INPUT_METHOD_SEPARATER = ':';
2235 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002236 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002237 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2238
satok723a27e2010-11-11 14:58:11 +09002239 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002240 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2241
2242 private final ContentResolver mResolver;
2243 private final HashMap<String, InputMethodInfo> mMethodMap;
2244 private final ArrayList<InputMethodInfo> mMethodList;
2245
2246 private String mEnabledInputMethodsStrCache;
2247
2248 private static void buildEnabledInputMethodsSettingString(
2249 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2250 String id = pair.first;
2251 ArrayList<String> subtypes = pair.second;
2252 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002253 // Inputmethod and subtypes are saved in the settings as follows:
2254 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2255 for (String subtypeId: subtypes) {
2256 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002257 }
2258 }
2259
2260 public InputMethodSettings(
2261 ContentResolver resolver, HashMap<String, InputMethodInfo> methodMap,
2262 ArrayList<InputMethodInfo> methodList) {
2263 mResolver = resolver;
2264 mMethodMap = methodMap;
2265 mMethodList = methodList;
2266 }
2267
2268 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2269 return createEnabledInputMethodListLocked(
2270 getEnabledInputMethodsAndSubtypeListLocked());
2271 }
2272
satok7f35c8c2010-10-07 21:13:11 +09002273 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002274 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2275 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002276 getEnabledInputMethodsAndSubtypeListLocked());
2277 }
2278
satok67ddf9c2010-11-17 09:45:54 +09002279 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2280 InputMethodInfo imi) {
2281 List<Pair<String, ArrayList<String>>> imsList =
2282 getEnabledInputMethodsAndSubtypeListLocked();
2283 ArrayList<InputMethodSubtype> enabledSubtypes =
2284 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002285 if (imi != null) {
2286 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2287 InputMethodInfo info = mMethodMap.get(imsPair.first);
2288 if (info != null && info.getId().equals(imi.getId())) {
2289 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
2290 for (InputMethodSubtype ims: subtypes) {
2291 for (String s: imsPair.second) {
2292 if (String.valueOf(ims.hashCode()).equals(s)) {
2293 enabledSubtypes.add(ims);
2294 }
satok67ddf9c2010-11-17 09:45:54 +09002295 }
2296 }
satok884ef9a2010-11-18 10:39:46 +09002297 break;
satok67ddf9c2010-11-17 09:45:54 +09002298 }
satok67ddf9c2010-11-17 09:45:54 +09002299 }
2300 }
2301 return enabledSubtypes;
2302 }
2303
satokd87c2592010-09-29 11:52:06 +09002304 // At the initial boot, the settings for input methods are not set,
2305 // so we need to enable IME in that case.
2306 public void enableAllIMEsIfThereIsNoEnabledIME() {
2307 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2308 StringBuilder sb = new StringBuilder();
2309 final int N = mMethodList.size();
2310 for (int i = 0; i < N; i++) {
2311 InputMethodInfo imi = mMethodList.get(i);
2312 Slog.i(TAG, "Adding: " + imi.getId());
2313 if (i > 0) sb.append(':');
2314 sb.append(imi.getId());
2315 }
2316 putEnabledInputMethodsStr(sb.toString());
2317 }
2318 }
2319
2320 public List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
2321 ArrayList<Pair<String, ArrayList<String>>> imsList
2322 = new ArrayList<Pair<String, ArrayList<String>>>();
2323 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2324 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2325 return imsList;
2326 }
satok723a27e2010-11-11 14:58:11 +09002327 mInputMethodSplitter.setString(enabledInputMethodsStr);
2328 while (mInputMethodSplitter.hasNext()) {
2329 String nextImsStr = mInputMethodSplitter.next();
2330 mSubtypeSplitter.setString(nextImsStr);
2331 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002332 ArrayList<String> subtypeHashes = new ArrayList<String>();
2333 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002334 String imeId = mSubtypeSplitter.next();
2335 while (mSubtypeSplitter.hasNext()) {
2336 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002337 }
2338 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2339 }
2340 }
2341 return imsList;
2342 }
2343
2344 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2345 if (reloadInputMethodStr) {
2346 getEnabledInputMethodsStr();
2347 }
2348 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2349 // Add in the newly enabled input method.
2350 putEnabledInputMethodsStr(id);
2351 } else {
2352 putEnabledInputMethodsStr(
2353 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2354 }
2355 }
2356
2357 /**
2358 * Build and put a string of EnabledInputMethods with removing specified Id.
2359 * @return the specified id was removed or not.
2360 */
2361 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2362 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2363 boolean isRemoved = false;
2364 boolean needsAppendSeparator = false;
2365 for (Pair<String, ArrayList<String>> ims: imsList) {
2366 String curId = ims.first;
2367 if (curId.equals(id)) {
2368 // We are disabling this input method, and it is
2369 // currently enabled. Skip it to remove from the
2370 // new list.
2371 isRemoved = true;
2372 } else {
2373 if (needsAppendSeparator) {
2374 builder.append(INPUT_METHOD_SEPARATER);
2375 } else {
2376 needsAppendSeparator = true;
2377 }
2378 buildEnabledInputMethodsSettingString(builder, ims);
2379 }
2380 }
2381 if (isRemoved) {
2382 // Update the setting with the new list of input methods.
2383 putEnabledInputMethodsStr(builder.toString());
2384 }
2385 return isRemoved;
2386 }
2387
2388 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2389 List<Pair<String, ArrayList<String>>> imsList) {
2390 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2391 for (Pair<String, ArrayList<String>> ims: imsList) {
2392 InputMethodInfo info = mMethodMap.get(ims.first);
2393 if (info != null) {
2394 res.add(info);
2395 }
2396 }
2397 return res;
2398 }
2399
satok7f35c8c2010-10-07 21:13:11 +09002400 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002401 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002402 List<Pair<String, ArrayList<String>>> imsList) {
2403 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2404 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2405 for (Pair<String, ArrayList<String>> ims : imsList) {
2406 InputMethodInfo info = mMethodMap.get(ims.first);
2407 if (info != null) {
2408 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2409 }
2410 }
2411 return res;
2412 }
2413
satokd87c2592010-09-29 11:52:06 +09002414 private void putEnabledInputMethodsStr(String str) {
2415 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2416 mEnabledInputMethodsStrCache = str;
2417 }
2418
2419 private String getEnabledInputMethodsStr() {
2420 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2421 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002422 if (DEBUG) {
2423 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2424 }
satokd87c2592010-09-29 11:52:06 +09002425 return mEnabledInputMethodsStrCache;
2426 }
satok723a27e2010-11-11 14:58:11 +09002427
2428 private void saveSubtypeHistory(
2429 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2430 StringBuilder builder = new StringBuilder();
2431 boolean isImeAdded = false;
2432 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2433 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2434 newSubtypeId);
2435 isImeAdded = true;
2436 }
2437 for (Pair<String, String> ime: savedImes) {
2438 String imeId = ime.first;
2439 String subtypeId = ime.second;
2440 if (TextUtils.isEmpty(subtypeId)) {
2441 subtypeId = NOT_A_SUBTYPE_ID_STR;
2442 }
2443 if (isImeAdded) {
2444 builder.append(INPUT_METHOD_SEPARATER);
2445 } else {
2446 isImeAdded = true;
2447 }
2448 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2449 subtypeId);
2450 }
2451 // Remove the last INPUT_METHOD_SEPARATER
2452 putSubtypeHistoryStr(builder.toString());
2453 }
2454
2455 public void addSubtypeToHistory(String imeId, String subtypeId) {
2456 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2457 for (Pair<String, String> ime: subtypeHistory) {
2458 if (ime.first.equals(imeId)) {
2459 if (DEBUG) {
2460 Slog.v(TAG, "Subtype found in the history: " + imeId
2461 + ime.second);
2462 }
2463 // We should break here
2464 subtypeHistory.remove(ime);
2465 break;
2466 }
2467 }
2468 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2469 }
2470
2471 private void putSubtypeHistoryStr(String str) {
2472 if (DEBUG) {
2473 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2474 }
2475 Settings.Secure.putString(
2476 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2477 }
2478
2479 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2480 // Gets the first one from the history
2481 return getLastSubtypeForInputMethodLockedInternal(null);
2482 }
2483
2484 public String getLastSubtypeForInputMethodLocked(String imeId) {
2485 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2486 if (ime != null) {
2487 return ime.second;
2488 } else {
2489 return null;
2490 }
2491 }
2492
2493 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2494 List<Pair<String, ArrayList<String>>> enabledImes =
2495 getEnabledInputMethodsAndSubtypeListLocked();
2496 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2497 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2498 final String imeInTheHistory = imeAndSubtype.first;
2499 // If imeId is empty, returns the first IME and subtype in the history
2500 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2501 final String subtypeInTheHistory = imeAndSubtype.second;
2502 final String subtypeHashCode = getEnabledSubtypeForInputMethodAndSubtypeLocked(
2503 enabledImes, imeInTheHistory, subtypeInTheHistory);
2504 if (!TextUtils.isEmpty(subtypeHashCode)) {
2505 if (DEBUG) {
2506 Slog.d(TAG, "Enabled subtype found in the history:" + subtypeHashCode);
2507 }
2508 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2509 }
2510 }
2511 }
2512 if (DEBUG) {
2513 Slog.d(TAG, "No enabled IME found in the history");
2514 }
2515 return null;
2516 }
2517
2518 private String getEnabledSubtypeForInputMethodAndSubtypeLocked(List<Pair<String,
2519 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2520 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2521 if (enabledIme.first.equals(imeId)) {
2522 for (String s: enabledIme.second) {
2523 if (s.equals(subtypeHashCode)) {
2524 // If both imeId and subtypeId are enabled, return subtypeId.
2525 return s;
2526 }
2527 }
2528 // If imeId was enabled but subtypeId was disabled.
2529 return NOT_A_SUBTYPE_ID_STR;
2530 }
2531 }
2532 // If both imeId and subtypeId are disabled, return null
2533 return null;
2534 }
2535
2536 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2537 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2538 final String subtypeHistoryStr = getSubtypeHistoryStr();
2539 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2540 return imsList;
2541 }
2542 mInputMethodSplitter.setString(subtypeHistoryStr);
2543 while (mInputMethodSplitter.hasNext()) {
2544 String nextImsStr = mInputMethodSplitter.next();
2545 mSubtypeSplitter.setString(nextImsStr);
2546 if (mSubtypeSplitter.hasNext()) {
2547 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2548 // The first element is ime id.
2549 String imeId = mSubtypeSplitter.next();
2550 while (mSubtypeSplitter.hasNext()) {
2551 subtypeId = mSubtypeSplitter.next();
2552 break;
2553 }
2554 imsList.add(new Pair<String, String>(imeId, subtypeId));
2555 }
2556 }
2557 return imsList;
2558 }
2559
2560 private String getSubtypeHistoryStr() {
2561 if (DEBUG) {
2562 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2563 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2564 }
2565 return Settings.Secure.getString(
2566 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2567 }
2568
2569 public void putSelectedInputMethod(String imeId) {
2570 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2571 }
2572
2573 public void putSelectedSubtype(int subtypeId) {
2574 Settings.Secure.putInt(
2575 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2576 }
satokd87c2592010-09-29 11:52:06 +09002577 }
2578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 @Override
2582 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2583 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2584 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2587 + Binder.getCallingPid()
2588 + ", uid=" + Binder.getCallingUid());
2589 return;
2590 }
2591
2592 IInputMethod method;
2593 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 synchronized (mMethodMap) {
2598 p.println("Current Input Method Manager state:");
2599 int N = mMethodList.size();
2600 p.println(" Input Methods:");
2601 for (int i=0; i<N; i++) {
2602 InputMethodInfo info = mMethodList.get(i);
2603 p.println(" InputMethod #" + i + ":");
2604 info.dump(p, " ");
2605 }
2606 p.println(" Clients:");
2607 for (ClientState ci : mClients.values()) {
2608 p.println(" Client " + ci + ":");
2609 p.println(" client=" + ci.client);
2610 p.println(" inputContext=" + ci.inputContext);
2611 p.println(" sessionRequested=" + ci.sessionRequested);
2612 p.println(" curSession=" + ci.curSession);
2613 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002614 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002616 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2617 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2619 + " mBoundToMethod=" + mBoundToMethod);
2620 p.println(" mCurToken=" + mCurToken);
2621 p.println(" mCurIntent=" + mCurIntent);
2622 method = mCurMethod;
2623 p.println(" mCurMethod=" + mCurMethod);
2624 p.println(" mEnabledSession=" + mEnabledSession);
2625 p.println(" mShowRequested=" + mShowRequested
2626 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2627 + " mShowForced=" + mShowForced
2628 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002629 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002631
Jeff Brownb88102f2010-09-08 11:49:43 -07002632 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 pw.flush();
2635 try {
2636 client.client.asBinder().dump(fd, args);
2637 } catch (RemoteException e) {
2638 p.println("Input method client dead: " + e);
2639 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002640 } else {
2641 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002643
Jeff Brownb88102f2010-09-08 11:49:43 -07002644 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 pw.flush();
2647 try {
2648 method.asBinder().dump(fd, args);
2649 } catch (RemoteException e) {
2650 p.println("Input method service dead: " + e);
2651 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002652 } else {
2653 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 }
2655 }
2656}