blob: a2684fb567245a02318d84334c4ba472f91cc125 [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
satokb416a71e2010-11-25 20:42:14 +09001321 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001322 IInputMethodClient client, String inputMethodId) {
satokb416a71e2010-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));
satokb416a71e2010-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) {
satok16331c82010-12-20 23:48:46 +09001984 final String systemLocale = mRes.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09001985 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
1986 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
1987 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09001988 final int N = subtypes.size();
1989 boolean containsKeyboardSubtype = false;
1990 for (int i = 0; i < N; ++i) {
1991 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09001992 final String locale = subtype.getLocale();
1993 final String mode = subtype.getMode();
1994 // When system locale starts with subtype's locale, that subtype will be applicable
1995 // for system locale
1996 // For instance, it's clearly applicable for cases like system locale = en_US and
1997 // subtype = en, but it is not necessarily considered applicable for cases like system
1998 // locale = en and subtype = en_US.
1999 // We just call systemLocale.startsWith(locale) in this function because there is no
2000 // need to find applicable subtypes aggressively unlike
2001 // findLastResortApplicableSubtypeLocked.
2002 if (systemLocale.startsWith(locale)) {
2003 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2004 // If more applicable subtypes are contained, skip.
2005 if (applicableSubtype != null
2006 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2007 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002008 if (!containsKeyboardSubtype
2009 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2010 containsKeyboardSubtype = true;
2011 }
2012 }
2013 }
satok3da92232011-01-11 22:46:30 +09002014 ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2015 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002016 if (!containsKeyboardSubtype) {
2017 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satok7599a7fb2010-12-22 13:45:23 +09002018 subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002019 if (lastResortKeyboardSubtype != null) {
2020 applicableSubtypes.add(lastResortKeyboardSubtype);
2021 }
2022 }
2023 return applicableSubtypes;
2024 }
2025
satok4e4569d2010-11-19 18:45:53 +09002026 /**
2027 * If there are no selected subtypes, tries finding the most applicable one according to the
2028 * given locale.
2029 * @param subtypes this function will search the most applicable subtype in subtypes
2030 * @param mode subtypes will be filtered by mode
2031 * @param locale subtypes will be filtered by locale
satok7599a7fb2010-12-22 13:45:23 +09002032 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2033 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002034 * @return the most applicable subtypeId
2035 */
satokcd7cd292010-11-20 15:46:23 +09002036 private InputMethodSubtype findLastResortApplicableSubtypeLocked(
satok7599a7fb2010-12-22 13:45:23 +09002037 List<InputMethodSubtype> subtypes, String mode, String locale,
2038 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002039 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002040 return null;
satok8fbb1e82010-11-02 23:15:58 +09002041 }
satok4e4569d2010-11-19 18:45:53 +09002042 if (TextUtils.isEmpty(locale)) {
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002043 locale = mRes.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002044 }
satok8fbb1e82010-11-02 23:15:58 +09002045 final String language = locale.substring(0, 2);
2046 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002047 InputMethodSubtype applicableSubtype = null;
satok7599a7fb2010-12-22 13:45:23 +09002048 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002049 final int N = subtypes.size();
2050 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002051 InputMethodSubtype subtype = subtypes.get(i);
2052 final String subtypeLocale = subtype.getLocale();
satok4e4569d2010-11-19 18:45:53 +09002053 // An applicable subtype should match "mode".
2054 if (subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7fb2010-12-22 13:45:23 +09002055 if (firstMatchedModeSubtype == null) {
2056 firstMatchedModeSubtype = subtype;
2057 }
satok9ef02832010-11-04 21:17:48 +09002058 if (locale.equals(subtypeLocale)) {
2059 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002060 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002061 break;
2062 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2063 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002064 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002065 partialMatchFound = true;
2066 }
satok8fbb1e82010-11-02 23:15:58 +09002067 }
2068 }
2069
satok7599a7fb2010-12-22 13:45:23 +09002070 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2071 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002072 }
2073
satok8fbb1e82010-11-02 23:15:58 +09002074 // The first subtype applicable to the system locale will be defined as the most applicable
2075 // subtype.
2076 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002077 if (applicableSubtype != null) {
2078 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2079 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2080 }
satok8fbb1e82010-11-02 23:15:58 +09002081 }
satokcd7cd292010-11-20 15:46:23 +09002082 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002083 }
2084
satok4e4569d2010-11-19 18:45:53 +09002085 // If there are no selected shortcuts, tries finding the most applicable ones.
2086 private Pair<InputMethodInfo, InputMethodSubtype>
2087 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2088 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2089 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002090 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002091 boolean foundInSystemIME = false;
2092
2093 // Search applicable subtype for each InputMethodInfo
2094 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002095 final String imiId = imi.getId();
2096 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2097 continue;
2098 }
satokcd7cd292010-11-20 15:46:23 +09002099 InputMethodSubtype subtype = null;
satok7599a7fb2010-12-22 13:45:23 +09002100 final List<InputMethodSubtype> explicitlyEnabledSubtypes =
2101 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
2102 // 1. Search by the current subtype's locale from explicitlyEnabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002103 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002104 subtype = findLastResortApplicableSubtypeLocked(
satok7599a7fb2010-12-22 13:45:23 +09002105 explicitlyEnabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002106 }
satok7599a7fb2010-12-22 13:45:23 +09002107 // 2. Search by the system locale from explicitlyEnabledSubtypes.
2108 // 3. Search the first enabled subtype matched with mode from explicitlyEnabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002109 if (subtype == null) {
2110 subtype = findLastResortApplicableSubtypeLocked(
satok7599a7fb2010-12-22 13:45:23 +09002111 explicitlyEnabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002112 }
satok7599a7fb2010-12-22 13:45:23 +09002113 // 4. Search by the current subtype's locale from all subtypes.
2114 if (subtype == null && mCurrentSubtype != null) {
2115 subtype = findLastResortApplicableSubtypeLocked(
2116 imi.getSubtypes(), mode, mCurrentSubtype.getLocale(), false);
2117 }
2118 // 5. Search by the system locale from all subtypes.
2119 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002120 if (subtype == null) {
satok7599a7fb2010-12-22 13:45:23 +09002121 subtype = findLastResortApplicableSubtypeLocked(
2122 imi.getSubtypes(), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002123 }
satokcd7cd292010-11-20 15:46:23 +09002124 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09002125 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002126 // The current input method is the most applicable IME.
2127 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002128 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002129 break;
satok7599a7fb2010-12-22 13:45:23 +09002130 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002131 // The system input method is 2nd applicable IME.
2132 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002133 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09002134 if ((imi.getServiceInfo().applicationInfo.flags
2135 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2136 foundInSystemIME = true;
2137 }
satok4e4569d2010-11-19 18:45:53 +09002138 }
2139 }
2140 }
2141 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002142 if (mostApplicableIMI != null) {
2143 Slog.w(TAG, "Most applicable shortcut input method was:"
2144 + mostApplicableIMI.getId());
2145 if (mostApplicableSubtype != null) {
2146 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2147 + "," + mostApplicableSubtype.getMode() + ","
2148 + mostApplicableSubtype.getLocale());
2149 }
2150 }
satok4e4569d2010-11-19 18:45:53 +09002151 }
satokcd7cd292010-11-20 15:46:23 +09002152 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002153 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002154 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002155 } else {
2156 return null;
2157 }
2158 }
2159
satokab751aa2010-09-14 19:17:36 +09002160 /**
2161 * @return Return the current subtype of this input method.
2162 */
2163 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002164 boolean subtypeIsSelected = false;
2165 try {
2166 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2167 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2168 } catch (SettingNotFoundException e) {
2169 }
satok3ef8b292010-11-23 06:06:29 +09002170 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002171 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002172 String lastInputMethodId = Settings.Secure.getString(
2173 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002174 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2175 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002176 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2177 if (imi != null) {
2178 // If there are no selected subtypes, the framework will try to find
2179 // the most applicable subtype from all subtypes whose mode is
2180 // SUBTYPE_MODE_KEYBOARD. This is an exceptional case, so we will hardcode
2181 // the mode.
satokcd7cd292010-11-20 15:46:23 +09002182 mCurrentSubtype = findLastResortApplicableSubtypeLocked(imi.getSubtypes(),
satok7599a7fb2010-12-22 13:45:23 +09002183 SUBTYPE_MODE_KEYBOARD, null, true);
satok4e4569d2010-11-19 18:45:53 +09002184 }
satokcd7cd292010-11-20 15:46:23 +09002185 } else {
satok3ef8b292010-11-23 06:06:29 +09002186 mCurrentSubtype =
2187 mMethodMap.get(lastInputMethodId).getSubtypes().get(subtypeId);
2188 }
satok8fbb1e82010-11-02 23:15:58 +09002189 }
satok3ef8b292010-11-23 06:06:29 +09002190 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002191 }
satokab751aa2010-09-14 19:17:36 +09002192 }
2193
satokf3db1af2010-11-23 13:34:33 +09002194 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2195 InputMethodSubtype subtype) {
2196 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2197 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2198 } else {
2199 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2200 subtypes.add(subtype);
2201 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2202 }
2203 }
2204
satok4e4569d2010-11-19 18:45:53 +09002205 // TODO: We should change the return type from List to List<Parcelable>
2206 public List getShortcutInputMethodsAndSubtypes() {
2207 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002208 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002209 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002210 // If there are no selected shortcut subtypes, the framework will try to find
2211 // the most applicable subtype from all subtypes whose mode is
2212 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002213 Pair<InputMethodInfo, InputMethodSubtype> info =
2214 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2215 SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09002216 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002217 ret.add(info.first);
2218 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09002219 }
satok3da92232011-01-11 22:46:30 +09002220 return ret;
satokf3db1af2010-11-23 13:34:33 +09002221 }
satokf3db1af2010-11-23 13:34:33 +09002222 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2223 ret.add(imi);
2224 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2225 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002226 }
2227 }
satokf3db1af2010-11-23 13:34:33 +09002228 return ret;
satok4e4569d2010-11-19 18:45:53 +09002229 }
2230 }
2231
satokb66d2872010-11-10 01:04:04 +09002232 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2233 synchronized (mMethodMap) {
2234 if (subtype != null && mCurMethodId != null) {
2235 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2236 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2237 if (subtypeId != NOT_A_SUBTYPE_ID) {
2238 setInputMethodLocked(mCurMethodId, subtypeId);
2239 return true;
2240 }
2241 }
2242 return false;
2243 }
2244 }
2245
satokd87c2592010-09-29 11:52:06 +09002246 /**
2247 * Utility class for putting and getting settings for InputMethod
2248 * TODO: Move all putters and getters of settings to this class.
2249 */
2250 private static class InputMethodSettings {
2251 // The string for enabled input method is saved as follows:
2252 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2253 private static final char INPUT_METHOD_SEPARATER = ':';
2254 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002255 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002256 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2257
satok723a27e2010-11-11 14:58:11 +09002258 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002259 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2260
2261 private final ContentResolver mResolver;
2262 private final HashMap<String, InputMethodInfo> mMethodMap;
2263 private final ArrayList<InputMethodInfo> mMethodList;
2264
2265 private String mEnabledInputMethodsStrCache;
2266
2267 private static void buildEnabledInputMethodsSettingString(
2268 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2269 String id = pair.first;
2270 ArrayList<String> subtypes = pair.second;
2271 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002272 // Inputmethod and subtypes are saved in the settings as follows:
2273 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2274 for (String subtypeId: subtypes) {
2275 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002276 }
2277 }
2278
2279 public InputMethodSettings(
2280 ContentResolver resolver, HashMap<String, InputMethodInfo> methodMap,
2281 ArrayList<InputMethodInfo> methodList) {
2282 mResolver = resolver;
2283 mMethodMap = methodMap;
2284 mMethodList = methodList;
2285 }
2286
2287 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2288 return createEnabledInputMethodListLocked(
2289 getEnabledInputMethodsAndSubtypeListLocked());
2290 }
2291
satok7f35c8c2010-10-07 21:13:11 +09002292 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002293 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2294 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002295 getEnabledInputMethodsAndSubtypeListLocked());
2296 }
2297
satok67ddf9c2010-11-17 09:45:54 +09002298 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2299 InputMethodInfo imi) {
2300 List<Pair<String, ArrayList<String>>> imsList =
2301 getEnabledInputMethodsAndSubtypeListLocked();
2302 ArrayList<InputMethodSubtype> enabledSubtypes =
2303 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002304 if (imi != null) {
2305 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2306 InputMethodInfo info = mMethodMap.get(imsPair.first);
2307 if (info != null && info.getId().equals(imi.getId())) {
2308 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
2309 for (InputMethodSubtype ims: subtypes) {
2310 for (String s: imsPair.second) {
2311 if (String.valueOf(ims.hashCode()).equals(s)) {
2312 enabledSubtypes.add(ims);
2313 }
satok67ddf9c2010-11-17 09:45:54 +09002314 }
2315 }
satok884ef9a2010-11-18 10:39:46 +09002316 break;
satok67ddf9c2010-11-17 09:45:54 +09002317 }
satok67ddf9c2010-11-17 09:45:54 +09002318 }
2319 }
2320 return enabledSubtypes;
2321 }
2322
satokd87c2592010-09-29 11:52:06 +09002323 // At the initial boot, the settings for input methods are not set,
2324 // so we need to enable IME in that case.
2325 public void enableAllIMEsIfThereIsNoEnabledIME() {
2326 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2327 StringBuilder sb = new StringBuilder();
2328 final int N = mMethodList.size();
2329 for (int i = 0; i < N; i++) {
2330 InputMethodInfo imi = mMethodList.get(i);
2331 Slog.i(TAG, "Adding: " + imi.getId());
2332 if (i > 0) sb.append(':');
2333 sb.append(imi.getId());
2334 }
2335 putEnabledInputMethodsStr(sb.toString());
2336 }
2337 }
2338
2339 public List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
2340 ArrayList<Pair<String, ArrayList<String>>> imsList
2341 = new ArrayList<Pair<String, ArrayList<String>>>();
2342 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2343 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2344 return imsList;
2345 }
satok723a27e2010-11-11 14:58:11 +09002346 mInputMethodSplitter.setString(enabledInputMethodsStr);
2347 while (mInputMethodSplitter.hasNext()) {
2348 String nextImsStr = mInputMethodSplitter.next();
2349 mSubtypeSplitter.setString(nextImsStr);
2350 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002351 ArrayList<String> subtypeHashes = new ArrayList<String>();
2352 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002353 String imeId = mSubtypeSplitter.next();
2354 while (mSubtypeSplitter.hasNext()) {
2355 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002356 }
2357 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2358 }
2359 }
2360 return imsList;
2361 }
2362
2363 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2364 if (reloadInputMethodStr) {
2365 getEnabledInputMethodsStr();
2366 }
2367 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2368 // Add in the newly enabled input method.
2369 putEnabledInputMethodsStr(id);
2370 } else {
2371 putEnabledInputMethodsStr(
2372 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2373 }
2374 }
2375
2376 /**
2377 * Build and put a string of EnabledInputMethods with removing specified Id.
2378 * @return the specified id was removed or not.
2379 */
2380 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2381 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2382 boolean isRemoved = false;
2383 boolean needsAppendSeparator = false;
2384 for (Pair<String, ArrayList<String>> ims: imsList) {
2385 String curId = ims.first;
2386 if (curId.equals(id)) {
2387 // We are disabling this input method, and it is
2388 // currently enabled. Skip it to remove from the
2389 // new list.
2390 isRemoved = true;
2391 } else {
2392 if (needsAppendSeparator) {
2393 builder.append(INPUT_METHOD_SEPARATER);
2394 } else {
2395 needsAppendSeparator = true;
2396 }
2397 buildEnabledInputMethodsSettingString(builder, ims);
2398 }
2399 }
2400 if (isRemoved) {
2401 // Update the setting with the new list of input methods.
2402 putEnabledInputMethodsStr(builder.toString());
2403 }
2404 return isRemoved;
2405 }
2406
2407 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2408 List<Pair<String, ArrayList<String>>> imsList) {
2409 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2410 for (Pair<String, ArrayList<String>> ims: imsList) {
2411 InputMethodInfo info = mMethodMap.get(ims.first);
2412 if (info != null) {
2413 res.add(info);
2414 }
2415 }
2416 return res;
2417 }
2418
satok7f35c8c2010-10-07 21:13:11 +09002419 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002420 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002421 List<Pair<String, ArrayList<String>>> imsList) {
2422 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2423 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2424 for (Pair<String, ArrayList<String>> ims : imsList) {
2425 InputMethodInfo info = mMethodMap.get(ims.first);
2426 if (info != null) {
2427 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2428 }
2429 }
2430 return res;
2431 }
2432
satokd87c2592010-09-29 11:52:06 +09002433 private void putEnabledInputMethodsStr(String str) {
2434 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2435 mEnabledInputMethodsStrCache = str;
2436 }
2437
2438 private String getEnabledInputMethodsStr() {
2439 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2440 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002441 if (DEBUG) {
2442 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2443 }
satokd87c2592010-09-29 11:52:06 +09002444 return mEnabledInputMethodsStrCache;
2445 }
satok723a27e2010-11-11 14:58:11 +09002446
2447 private void saveSubtypeHistory(
2448 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2449 StringBuilder builder = new StringBuilder();
2450 boolean isImeAdded = false;
2451 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2452 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2453 newSubtypeId);
2454 isImeAdded = true;
2455 }
2456 for (Pair<String, String> ime: savedImes) {
2457 String imeId = ime.first;
2458 String subtypeId = ime.second;
2459 if (TextUtils.isEmpty(subtypeId)) {
2460 subtypeId = NOT_A_SUBTYPE_ID_STR;
2461 }
2462 if (isImeAdded) {
2463 builder.append(INPUT_METHOD_SEPARATER);
2464 } else {
2465 isImeAdded = true;
2466 }
2467 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2468 subtypeId);
2469 }
2470 // Remove the last INPUT_METHOD_SEPARATER
2471 putSubtypeHistoryStr(builder.toString());
2472 }
2473
2474 public void addSubtypeToHistory(String imeId, String subtypeId) {
2475 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2476 for (Pair<String, String> ime: subtypeHistory) {
2477 if (ime.first.equals(imeId)) {
2478 if (DEBUG) {
2479 Slog.v(TAG, "Subtype found in the history: " + imeId
2480 + ime.second);
2481 }
2482 // We should break here
2483 subtypeHistory.remove(ime);
2484 break;
2485 }
2486 }
2487 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2488 }
2489
2490 private void putSubtypeHistoryStr(String str) {
2491 if (DEBUG) {
2492 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2493 }
2494 Settings.Secure.putString(
2495 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2496 }
2497
2498 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2499 // Gets the first one from the history
2500 return getLastSubtypeForInputMethodLockedInternal(null);
2501 }
2502
2503 public String getLastSubtypeForInputMethodLocked(String imeId) {
2504 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2505 if (ime != null) {
2506 return ime.second;
2507 } else {
2508 return null;
2509 }
2510 }
2511
2512 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2513 List<Pair<String, ArrayList<String>>> enabledImes =
2514 getEnabledInputMethodsAndSubtypeListLocked();
2515 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2516 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2517 final String imeInTheHistory = imeAndSubtype.first;
2518 // If imeId is empty, returns the first IME and subtype in the history
2519 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2520 final String subtypeInTheHistory = imeAndSubtype.second;
2521 final String subtypeHashCode = getEnabledSubtypeForInputMethodAndSubtypeLocked(
2522 enabledImes, imeInTheHistory, subtypeInTheHistory);
2523 if (!TextUtils.isEmpty(subtypeHashCode)) {
2524 if (DEBUG) {
2525 Slog.d(TAG, "Enabled subtype found in the history:" + subtypeHashCode);
2526 }
2527 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2528 }
2529 }
2530 }
2531 if (DEBUG) {
2532 Slog.d(TAG, "No enabled IME found in the history");
2533 }
2534 return null;
2535 }
2536
2537 private String getEnabledSubtypeForInputMethodAndSubtypeLocked(List<Pair<String,
2538 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2539 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2540 if (enabledIme.first.equals(imeId)) {
2541 for (String s: enabledIme.second) {
2542 if (s.equals(subtypeHashCode)) {
2543 // If both imeId and subtypeId are enabled, return subtypeId.
2544 return s;
2545 }
2546 }
2547 // If imeId was enabled but subtypeId was disabled.
2548 return NOT_A_SUBTYPE_ID_STR;
2549 }
2550 }
2551 // If both imeId and subtypeId are disabled, return null
2552 return null;
2553 }
2554
2555 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2556 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2557 final String subtypeHistoryStr = getSubtypeHistoryStr();
2558 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2559 return imsList;
2560 }
2561 mInputMethodSplitter.setString(subtypeHistoryStr);
2562 while (mInputMethodSplitter.hasNext()) {
2563 String nextImsStr = mInputMethodSplitter.next();
2564 mSubtypeSplitter.setString(nextImsStr);
2565 if (mSubtypeSplitter.hasNext()) {
2566 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2567 // The first element is ime id.
2568 String imeId = mSubtypeSplitter.next();
2569 while (mSubtypeSplitter.hasNext()) {
2570 subtypeId = mSubtypeSplitter.next();
2571 break;
2572 }
2573 imsList.add(new Pair<String, String>(imeId, subtypeId));
2574 }
2575 }
2576 return imsList;
2577 }
2578
2579 private String getSubtypeHistoryStr() {
2580 if (DEBUG) {
2581 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2582 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2583 }
2584 return Settings.Secure.getString(
2585 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2586 }
2587
2588 public void putSelectedInputMethod(String imeId) {
2589 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2590 }
2591
2592 public void putSelectedSubtype(int subtypeId) {
2593 Settings.Secure.putInt(
2594 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2595 }
satokd87c2592010-09-29 11:52:06 +09002596 }
2597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 @Override
2601 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2602 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2603 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2606 + Binder.getCallingPid()
2607 + ", uid=" + Binder.getCallingUid());
2608 return;
2609 }
2610
2611 IInputMethod method;
2612 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 synchronized (mMethodMap) {
2617 p.println("Current Input Method Manager state:");
2618 int N = mMethodList.size();
2619 p.println(" Input Methods:");
2620 for (int i=0; i<N; i++) {
2621 InputMethodInfo info = mMethodList.get(i);
2622 p.println(" InputMethod #" + i + ":");
2623 info.dump(p, " ");
2624 }
2625 p.println(" Clients:");
2626 for (ClientState ci : mClients.values()) {
2627 p.println(" Client " + ci + ":");
2628 p.println(" client=" + ci.client);
2629 p.println(" inputContext=" + ci.inputContext);
2630 p.println(" sessionRequested=" + ci.sessionRequested);
2631 p.println(" curSession=" + ci.curSession);
2632 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002633 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002635 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2636 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2638 + " mBoundToMethod=" + mBoundToMethod);
2639 p.println(" mCurToken=" + mCurToken);
2640 p.println(" mCurIntent=" + mCurIntent);
2641 method = mCurMethod;
2642 p.println(" mCurMethod=" + mCurMethod);
2643 p.println(" mEnabledSession=" + mEnabledSession);
2644 p.println(" mShowRequested=" + mShowRequested
2645 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2646 + " mShowForced=" + mShowForced
2647 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002648 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002650
Jeff Brownb88102f2010-09-08 11:49:43 -07002651 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 pw.flush();
2654 try {
2655 client.client.asBinder().dump(fd, args);
2656 } catch (RemoteException e) {
2657 p.println("Input method client dead: " + e);
2658 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002659 } else {
2660 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002662
Jeff Brownb88102f2010-09-08 11:49:43 -07002663 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 pw.flush();
2666 try {
2667 method.asBinder().dump(fd, args);
2668 } catch (RemoteException e) {
2669 p.println("Input method service dead: " + e);
2670 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002671 } else {
2672 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 }
2674 }
2675}