blob: 4d7d557ea891cbe116984e4b713163b5874b88ce [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2008 The Android Open Source Project
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.server;
18
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080019import com.android.internal.content.PackageMonitor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import com.android.internal.os.HandlerCaller;
21import com.android.internal.view.IInputContext;
22import com.android.internal.view.IInputMethod;
23import com.android.internal.view.IInputMethodCallback;
24import com.android.internal.view.IInputMethodClient;
25import com.android.internal.view.IInputMethodManager;
26import com.android.internal.view.IInputMethodSession;
27import com.android.internal.view.InputBindResult;
28
Joe Onorato7a0f36b2010-06-07 10:24:36 -070029import com.android.server.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31import org.xmlpull.v1.XmlPullParserException;
32
33import android.app.ActivityManagerNative;
34import android.app.AlertDialog;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070035import android.app.PendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.ComponentName;
37import android.content.ContentResolver;
38import android.content.Context;
39import android.content.DialogInterface;
40import android.content.IntentFilter;
41import android.content.DialogInterface.OnCancelListener;
42import android.content.Intent;
43import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070044import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.pm.PackageManager;
46import android.content.pm.ResolveInfo;
47import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070048import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.res.Resources;
50import android.content.res.TypedArray;
51import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Binder;
53import android.os.Handler;
54import android.os.IBinder;
55import android.os.IInterface;
56import android.os.Message;
57import android.os.Parcel;
58import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080059import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.ServiceManager;
61import android.os.SystemClock;
62import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070063import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090064import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.text.TextUtils;
66import android.util.EventLog;
satokab751aa2010-09-14 19:17:36 +090067import android.util.Pair;
Joe Onorato8a9b2202010-02-26 18:56:32 -080068import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.util.PrintWriterPrinter;
70import android.util.Printer;
71import android.view.IWindowManager;
72import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090073import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.view.inputmethod.InputBinding;
75import android.view.inputmethod.InputMethod;
76import android.view.inputmethod.InputMethodInfo;
77import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090078import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80import java.io.FileDescriptor;
81import java.io.IOException;
82import java.io.PrintWriter;
satok913a8922010-08-26 21:53:41 +090083import java.text.Collator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import java.util.ArrayList;
85import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +090086import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import java.util.List;
satok913a8922010-08-26 21:53:41 +090088import java.util.Map;
89import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91/**
92 * This class provides a system service that manages input methods.
93 */
94public class InputMethodManagerService extends IInputMethodManager.Stub
95 implements ServiceConnection, Handler.Callback {
96 static final boolean DEBUG = false;
97 static final String TAG = "InputManagerService";
98
99 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900100 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900101 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900102 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 static final int MSG_UNBIND_INPUT = 1000;
105 static final int MSG_BIND_INPUT = 1010;
106 static final int MSG_SHOW_SOFT_INPUT = 1020;
107 static final int MSG_HIDE_SOFT_INPUT = 1030;
108 static final int MSG_ATTACH_TOKEN = 1040;
109 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 static final int MSG_START_INPUT = 2000;
112 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 static final int MSG_UNBIND_METHOD = 3000;
115 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800118
satokab751aa2010-09-14 19:17:36 +0900119 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900120 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok217f5482010-12-15 05:19:19 +0900121 private static final String EXTRA_INPUT_METHOD_ID = "input_method_id";
satok4e4569d2010-11-19 18:45:53 +0900122 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
123 private static final String SUBTYPE_MODE_VOICE = "voice";
124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800126 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900128 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700130 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 final IWindowManager mIWindowManager;
132 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 // All known input methods. mMethodMap also serves as the global
137 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900138 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
139 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 class SessionState {
142 final ClientState client;
143 final IInputMethod method;
144 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 @Override
147 public String toString() {
148 return "SessionState{uid " + client.uid + " pid " + client.pid
149 + " method " + Integer.toHexString(
150 System.identityHashCode(method))
151 + " session " + Integer.toHexString(
152 System.identityHashCode(session))
153 + "}";
154 }
155
156 SessionState(ClientState _client, IInputMethod _method,
157 IInputMethodSession _session) {
158 client = _client;
159 method = _method;
160 session = _session;
161 }
162 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 class ClientState {
165 final IInputMethodClient client;
166 final IInputContext inputContext;
167 final int uid;
168 final int pid;
169 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 boolean sessionRequested;
172 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 @Override
175 public String toString() {
176 return "ClientState{" + Integer.toHexString(
177 System.identityHashCode(this)) + " uid " + uid
178 + " pid " + pid + "}";
179 }
180
181 ClientState(IInputMethodClient _client, IInputContext _inputContext,
182 int _uid, int _pid) {
183 client = _client;
184 inputContext = _inputContext;
185 uid = _uid;
186 pid = _pid;
187 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
188 }
189 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 final HashMap<IBinder, ClientState> mClients
192 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700195 * Set once the system is ready to run third party code.
196 */
197 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800198
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700199 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * Id of the currently selected input method.
201 */
202 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 /**
205 * The current binding sequence number, incremented every time there is
206 * a new bind performed.
207 */
208 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 /**
211 * The client that is currently bound to an input method.
212 */
213 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700216 * The last window token that gained focus.
217 */
218 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800219
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700220 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 * The input context last provided by the current client.
222 */
223 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 /**
226 * The attributes last provided by the current client.
227 */
228 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 /**
231 * The input method ID of the input method service that we are currently
232 * connected to or in the process of connecting to.
233 */
234 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 /**
satokab751aa2010-09-14 19:17:36 +0900237 * The current subtype of the current input method.
238 */
239 private InputMethodSubtype mCurrentSubtype;
240
satok4e4569d2010-11-19 18:45:53 +0900241 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900242 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
243 mShortcutInputMethodsAndSubtypes =
244 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900245
246 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * Set to true if our ServiceConnection is currently actively bound to
248 * a service (whether or not we have gotten its IBinder back yet).
249 */
250 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 /**
253 * Set if the client has asked for the input method to be shown.
254 */
255 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 /**
258 * Set if we were explicitly told to show the input method.
259 */
260 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
263 * Set if we were forced to be shown.
264 */
265 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 /**
268 * Set if we last told the input method to show itself.
269 */
270 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 /**
273 * The Intent used to connect to the current input method.
274 */
275 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
278 * The token we have made for the currently active input method, to
279 * identify it in the future.
280 */
281 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 /**
284 * If non-null, this is the input method service we are currently connected
285 * to.
286 */
287 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 /**
290 * Time that we last initiated a bind to the input method, to determine
291 * if we should try to disconnect and reconnect to it.
292 */
293 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 /**
296 * Have we called mCurMethod.bindInput()?
297 */
298 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 /**
301 * Currently enabled session. Only touched by service thread, not
302 * protected by a lock.
303 */
304 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 /**
307 * True if the screen is on. The value is true initially.
308 */
309 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 AlertDialog.Builder mDialogBuilder;
312 AlertDialog mSwitchingDialog;
313 InputMethodInfo[] mIms;
314 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900315 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 class SettingsObserver extends ContentObserver {
318 SettingsObserver(Handler handler) {
319 super(handler);
320 ContentResolver resolver = mContext.getContentResolver();
321 resolver.registerContentObserver(Settings.Secure.getUriFor(
322 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900323 resolver.registerContentObserver(Settings.Secure.getUriFor(
324 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 @Override public void onChange(boolean selfChange) {
328 synchronized (mMethodMap) {
329 updateFromSettingsLocked();
330 }
331 }
332 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
335 @Override
336 public void onReceive(Context context, Intent intent) {
337 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
338 mScreenOn = true;
339 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
340 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700341 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
342 hideInputMethodMenu();
343 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800345 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 }
347
348 // Inform the current client of the change in active status
349 try {
350 if (mCurClient != null && mCurClient.client != null) {
351 mCurClient.client.setActive(mScreenOn);
352 }
353 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800354 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 + mCurClient.pid + " uid " + mCurClient.uid);
356 }
357 }
358 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800359
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800360 class MyPackageMonitor extends PackageMonitor {
361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800363 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800365 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
367 final int N = mMethodList.size();
368 if (curInputMethodId != null) {
369 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800370 InputMethodInfo imi = mMethodList.get(i);
371 if (imi.getId().equals(curInputMethodId)) {
372 for (String pkg : packages) {
373 if (imi.getPackageName().equals(pkg)) {
374 if (!doit) {
375 return true;
376 }
satok723a27e2010-11-11 14:58:11 +0900377 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800378 chooseNewDefaultIMELocked();
379 return true;
380 }
381 }
382 }
383 }
384 }
385 }
386 return false;
387 }
388
389 @Override
390 public void onSomePackagesChanged() {
391 synchronized (mMethodMap) {
392 InputMethodInfo curIm = null;
393 String curInputMethodId = Settings.Secure.getString(mContext
394 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
395 final int N = mMethodList.size();
396 if (curInputMethodId != null) {
397 for (int i=0; i<N; i++) {
398 InputMethodInfo imi = mMethodList.get(i);
399 if (imi.getId().equals(curInputMethodId)) {
400 curIm = imi;
401 }
402 int change = isPackageDisappearing(imi.getPackageName());
403 if (change == PACKAGE_TEMPORARY_CHANGE
404 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800405 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800406 + imi.getComponent());
407 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 }
409 }
410 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800411
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800412 buildInputMethodListLocked(mMethodList, mMethodMap);
413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800415
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800416 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800417 int change = isPackageDisappearing(curIm.getPackageName());
418 if (change == PACKAGE_TEMPORARY_CHANGE
419 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800420 ServiceInfo si = null;
421 try {
422 si = mContext.getPackageManager().getServiceInfo(
423 curIm.getComponent(), 0);
424 } catch (PackageManager.NameNotFoundException ex) {
425 }
426 if (si == null) {
427 // Uh oh, current input method is no longer around!
428 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800429 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satokca830212011-01-13 21:15:04 +0900430 mStatusBar.setIMEButtonVisible(mCurToken, false);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800431 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800432 changed = true;
433 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800434 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900435 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800436 }
437 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800438 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800439 }
satokab751aa2010-09-14 19:17:36 +0900440
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800441 if (curIm == null) {
442 // We currently don't have a default input method... is
443 // one now available?
444 changed = chooseNewDefaultIMELocked();
445 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800446
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800447 if (changed) {
448 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
450 }
451 }
452 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 class MethodCallback extends IInputMethodCallback.Stub {
455 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 MethodCallback(IInputMethod method) {
458 mMethod = method;
459 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 public void finishedEvent(int seq, boolean handled) throws RemoteException {
462 }
463
464 public void sessionCreated(IInputMethodSession session) throws RemoteException {
465 onSessionCreated(mMethod, session);
466 }
467 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800468
Joe Onorato089de882010-04-12 08:18:45 -0700469 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800471 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 mHandler = new Handler(this);
473 mIWindowManager = IWindowManager.Stub.asInterface(
474 ServiceManager.getService(Context.WINDOW_SERVICE));
475 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
476 public void executeMessage(Message msg) {
477 handleMessage(msg);
478 }
479 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800480
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800481 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 IntentFilter screenOnOffFilt = new IntentFilter();
484 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
485 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700486 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800488
satok913a8922010-08-26 21:53:41 +0900489 mStatusBar = statusBar;
490 statusBar.setIconVisibility("ime", false);
491
satokd87c2592010-09-29 11:52:06 +0900492 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900493 mSettings = new InputMethodSettings(
494 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900496 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497
satokd87c2592010-09-29 11:52:06 +0900498 if (TextUtils.isEmpty(Settings.Secure.getString(
499 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900501 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
503 try {
satokd87c2592010-09-29 11:52:06 +0900504 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 imi.getPackageName(), 0).getResources();
506 if (res.getBoolean(imi.getIsDefaultResourceId())) {
507 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800508 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 }
510 } catch (PackageManager.NameNotFoundException ex) {
511 } catch (Resources.NotFoundException ex) {
512 }
513 }
514 }
satokd87c2592010-09-29 11:52:06 +0900515 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800517 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900520 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 }
522 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 mSettingsObserver = new SettingsObserver(mHandler);
525 updateFromSettingsLocked();
526 }
527
528 @Override
529 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
530 throws RemoteException {
531 try {
532 return super.onTransact(code, data, reply, flags);
533 } catch (RuntimeException e) {
534 // The input method manager only throws security exceptions, so let's
535 // log all others.
536 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800537 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 }
539 throw e;
540 }
541 }
542
543 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700544 synchronized (mMethodMap) {
545 if (!mSystemReady) {
546 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700547 try {
548 startInputInnerLocked();
549 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800550 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700551 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700552 }
553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 public List<InputMethodInfo> getInputMethodList() {
557 synchronized (mMethodMap) {
558 return new ArrayList<InputMethodInfo>(mMethodList);
559 }
560 }
561
562 public List<InputMethodInfo> getEnabledInputMethodList() {
563 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900564 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566 }
567
satok16331c82010-12-20 23:48:46 +0900568 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
569 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900570 synchronized (mMethodMap) {
satok884ef9a2010-11-18 10:39:46 +0900571 if (imi == null && mCurMethodId != null) {
572 imi = mMethodMap.get(mCurMethodId);
573 }
satok16331c82010-12-20 23:48:46 +0900574 final List<InputMethodSubtype> enabledSubtypes =
575 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
576 if (!allowsImplicitlySelectedSubtypes || enabledSubtypes.size() > 0) {
577 return enabledSubtypes;
578 } else {
satokdf31ae62011-01-15 06:19:44 +0900579 return getApplicableSubtypesLocked(mRes, imi.getSubtypes());
satok16331c82010-12-20 23:48:46 +0900580 }
satok67ddf9c2010-11-17 09:45:54 +0900581 }
582 }
583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 public void addClient(IInputMethodClient client,
585 IInputContext inputContext, int uid, int pid) {
586 synchronized (mMethodMap) {
587 mClients.put(client.asBinder(), new ClientState(client,
588 inputContext, uid, pid));
589 }
590 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 public void removeClient(IInputMethodClient client) {
593 synchronized (mMethodMap) {
594 mClients.remove(client.asBinder());
595 }
596 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 void executeOrSendMessage(IInterface target, Message msg) {
599 if (target.asBinder() instanceof Binder) {
600 mCaller.sendMessage(msg);
601 } else {
602 handleMessage(msg);
603 msg.recycle();
604 }
605 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800606
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700607 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800609 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 + mCurClient.client.asBinder());
611 if (mBoundToMethod) {
612 mBoundToMethod = false;
613 if (mCurMethod != null) {
614 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
615 MSG_UNBIND_INPUT, mCurMethod));
616 }
617 }
618 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
619 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
620 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 // Call setActive(false) on the old client
623 try {
624 mCurClient.client.setActive(false);
625 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800626 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 + mCurClient.pid + " uid " + mCurClient.uid);
628 }
629 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800630
The Android Open Source Project10592532009-03-18 17:39:46 -0700631 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 }
633 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 private int getImeShowFlags() {
636 int flags = 0;
637 if (mShowForced) {
638 flags |= InputMethod.SHOW_FORCED
639 | InputMethod.SHOW_EXPLICIT;
640 } else if (mShowExplicitlyRequested) {
641 flags |= InputMethod.SHOW_EXPLICIT;
642 }
643 return flags;
644 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 private int getAppShowFlags() {
647 int flags = 0;
648 if (mShowForced) {
649 flags |= InputMethodManager.SHOW_FORCED;
650 } else if (!mShowExplicitlyRequested) {
651 flags |= InputMethodManager.SHOW_IMPLICIT;
652 }
653 return flags;
654 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
657 if (!mBoundToMethod) {
658 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
659 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
660 mBoundToMethod = true;
661 }
662 final SessionState session = mCurClient.curSession;
663 if (initial) {
664 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
665 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
666 } else {
667 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
668 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
669 }
670 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800671 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800672 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 }
674 return needResult
675 ? new InputBindResult(session.session, mCurId, mCurSeq)
676 : null;
677 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 InputBindResult startInputLocked(IInputMethodClient client,
680 IInputContext inputContext, EditorInfo attribute,
681 boolean initial, boolean needResult) {
682 // If no method is currently selected, do nothing.
683 if (mCurMethodId == null) {
684 return mNoBinding;
685 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 ClientState cs = mClients.get(client.asBinder());
688 if (cs == null) {
689 throw new IllegalArgumentException("unknown client "
690 + client.asBinder());
691 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 try {
694 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
695 // Check with the window manager to make sure this client actually
696 // has a window with focus. If not, reject. This is thread safe
697 // because if the focus changes some time before or after, the
698 // next client receiving focus that has any interest in input will
699 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800700 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
702 return null;
703 }
704 } catch (RemoteException e) {
705 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 if (mCurClient != cs) {
708 // If the client is changing, we need to switch over to the new
709 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700710 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800711 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 + cs.client.asBinder());
713
714 // If the screen is on, inform the new client it is active
715 if (mScreenOn) {
716 try {
717 cs.client.setActive(mScreenOn);
718 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800719 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 + cs.pid + " uid " + cs.uid);
721 }
722 }
723 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 // Bump up the sequence for this client and attach it.
726 mCurSeq++;
727 if (mCurSeq <= 0) mCurSeq = 1;
728 mCurClient = cs;
729 mCurInputContext = inputContext;
730 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 // Check if the input method is changing.
733 if (mCurId != null && mCurId.equals(mCurMethodId)) {
734 if (cs.curSession != null) {
735 // Fast case: if we are already connected to the input method,
736 // then just return it.
737 return attachNewInputLocked(initial, needResult);
738 }
739 if (mHaveConnection) {
740 if (mCurMethod != null) {
741 if (!cs.sessionRequested) {
742 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800743 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
745 MSG_CREATE_SESSION, mCurMethod,
746 new MethodCallback(mCurMethod)));
747 }
748 // Return to client, and we will get back with it when
749 // we have had a session made for it.
750 return new InputBindResult(null, mCurId, mCurSeq);
751 } else if (SystemClock.uptimeMillis()
752 < (mLastBindTime+TIME_TO_RECONNECT)) {
753 // In this case we have connected to the service, but
754 // don't yet have its interface. If it hasn't been too
755 // long since we did the connection, we'll return to
756 // the client and wait to get the service interface so
757 // we can report back. If it has been too long, we want
758 // to fall through so we can try a disconnect/reconnect
759 // to see if we can get back in touch with the service.
760 return new InputBindResult(null, mCurId, mCurSeq);
761 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800762 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
763 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 }
765 }
766 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800767
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700768 return startInputInnerLocked();
769 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800770
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700771 InputBindResult startInputInnerLocked() {
772 if (mCurMethodId == null) {
773 return mNoBinding;
774 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800775
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700776 if (!mSystemReady) {
777 // If the system is not yet ready, we shouldn't be running third
778 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700779 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700780 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 InputMethodInfo info = mMethodMap.get(mCurMethodId);
783 if (info == null) {
784 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
785 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800786
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700787 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
790 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700791 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
792 com.android.internal.R.string.input_method_binding_label);
793 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
794 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
796 mLastBindTime = SystemClock.uptimeMillis();
797 mHaveConnection = true;
798 mCurId = info.getId();
799 mCurToken = new Binder();
800 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800801 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 mIWindowManager.addWindowToken(mCurToken,
803 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
804 } catch (RemoteException e) {
805 }
806 return new InputBindResult(null, mCurId, mCurSeq);
807 } else {
808 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800809 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 + mCurIntent);
811 }
812 return null;
813 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 public InputBindResult startInput(IInputMethodClient client,
816 IInputContext inputContext, EditorInfo attribute,
817 boolean initial, boolean needResult) {
818 synchronized (mMethodMap) {
819 final long ident = Binder.clearCallingIdentity();
820 try {
821 return startInputLocked(client, inputContext, attribute,
822 initial, needResult);
823 } finally {
824 Binder.restoreCallingIdentity(ident);
825 }
826 }
827 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 public void finishInput(IInputMethodClient client) {
830 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 public void onServiceConnected(ComponentName name, IBinder service) {
833 synchronized (mMethodMap) {
834 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
835 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700836 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800837 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700838 unbindCurrentMethodLocked(false);
839 return;
840 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800841 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700842 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
843 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800845 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700846 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700848 MSG_CREATE_SESSION, mCurMethod,
849 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 }
851 }
852 }
853 }
854
855 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
856 synchronized (mMethodMap) {
857 if (mCurMethod != null && method != null
858 && mCurMethod.asBinder() == method.asBinder()) {
859 if (mCurClient != null) {
860 mCurClient.curSession = new SessionState(mCurClient,
861 method, session);
862 mCurClient.sessionRequested = false;
863 InputBindResult res = attachNewInputLocked(true, true);
864 if (res.method != null) {
865 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
866 MSG_BIND_METHOD, mCurClient.client, res));
867 }
868 }
869 }
870 }
871 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800872
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700873 void unbindCurrentMethodLocked(boolean reportToClient) {
874 if (mHaveConnection) {
875 mContext.unbindService(this);
876 mHaveConnection = false;
877 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800878
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700879 if (mCurToken != null) {
880 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800881 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700882 mIWindowManager.removeWindowToken(mCurToken);
883 } catch (RemoteException e) {
884 }
885 mCurToken = null;
886 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800887
The Android Open Source Project10592532009-03-18 17:39:46 -0700888 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700889 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800890
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700891 if (reportToClient && mCurClient != null) {
892 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
893 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
894 }
895 }
896
Devin Taylor0c33ed22010-02-23 13:26:46 -0600897 private void finishSession(SessionState sessionState) {
898 if (sessionState != null && sessionState.session != null) {
899 try {
900 sessionState.session.finishSession();
901 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700902 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600903 }
904 }
905 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800906
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700907 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 if (mCurMethod != null) {
909 for (ClientState cs : mClients.values()) {
910 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600911 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 cs.curSession = null;
913 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600914
915 finishSession(mEnabledSession);
916 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 mCurMethod = null;
918 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700919 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 public void onServiceDisconnected(ComponentName name) {
923 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800924 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 + " mCurIntent=" + mCurIntent);
926 if (mCurMethod != null && mCurIntent != null
927 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700928 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 // We consider this to be a new bind attempt, since the system
930 // should now try to restart the service for us.
931 mLastBindTime = SystemClock.uptimeMillis();
932 mShowRequested = mInputShown;
933 mInputShown = false;
934 if (mCurClient != null) {
935 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
936 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
937 }
938 }
939 }
940 }
941
942 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700943 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 long ident = Binder.clearCallingIdentity();
945 try {
946 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700947 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 return;
949 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 synchronized (mMethodMap) {
952 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800953 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700954 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800956 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700957 mStatusBar.setIcon("ime", packageName, iconId, 0);
958 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 }
960 }
961 } finally {
962 Binder.restoreCallingIdentity(ident);
963 }
964 }
965
satok06487a52010-10-29 11:37:18 +0900966 public void setIMEButtonVisible(IBinder token, boolean visible) {
967 int uid = Binder.getCallingUid();
968 long ident = Binder.clearCallingIdentity();
969 try {
970 if (token == null || mCurToken != token) {
971 Slog.w(TAG, "Ignoring setIMEButtonVisible of uid " + uid + " token: " + token);
972 return;
973 }
974
975 synchronized (mMethodMap) {
satokcd7cd292010-11-20 15:46:23 +0900976 mStatusBar.setIMEButtonVisible(token, visible);
satok06487a52010-10-29 11:37:18 +0900977 }
978 } finally {
979 Binder.restoreCallingIdentity(ident);
980 }
981 }
982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700984 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
985 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
986 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
987 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +0900989 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +0900990 // There is no input method selected, try to choose new applicable input method.
991 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
992 id = Settings.Secure.getString(mContext.getContentResolver(),
993 Settings.Secure.DEFAULT_INPUT_METHOD);
994 }
995 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 try {
satokab751aa2010-09-14 19:17:36 +0900997 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800999 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001000 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001001 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
satokf3db1af2010-11-23 13:34:33 +09001003 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001004 } else {
1005 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001006 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001007 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
1009 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001010
satokab751aa2010-09-14 19:17:36 +09001011 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 InputMethodInfo info = mMethodMap.get(id);
1013 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001014 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 if (id.equals(mCurMethodId)) {
satokb66d2872010-11-10 01:04:04 +09001018 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
satokcd7cd292010-11-20 15:46:23 +09001019 InputMethodSubtype subtype = null;
satokb66d2872010-11-10 01:04:04 +09001020 if (subtypeId >= 0 && subtypeId < subtypes.size()) {
satokcd7cd292010-11-20 15:46:23 +09001021 subtype = subtypes.get(subtypeId);
1022 }
1023 if (subtype != mCurrentSubtype) {
1024 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001025 if (subtype != null) {
1026 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1027 }
satokcd7cd292010-11-20 15:46:23 +09001028 if (mCurMethod != null) {
1029 try {
satokcd7cd292010-11-20 15:46:23 +09001030 if (mInputShown) {
1031 // If mInputShown is false, there is no IME button on the
1032 // system bar.
1033 // Thus there is no need to make it invisible explicitly.
1034 mStatusBar.setIMEButtonVisible(mCurToken, true);
satokab751aa2010-09-14 19:17:36 +09001035 }
satokcd7cd292010-11-20 15:46:23 +09001036 // If subtype is null, try to find the most applicable one from
1037 // getCurrentInputMethodSubtype.
1038 if (subtype == null) {
1039 subtype = getCurrentInputMethodSubtype();
1040 }
1041 mCurMethod.changeInputMethodSubtype(subtype);
1042 } catch (RemoteException e) {
1043 return;
satokab751aa2010-09-14 19:17:36 +09001044 }
1045 }
1046 }
1047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 return;
1049 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 final long ident = Binder.clearCallingIdentity();
1052 try {
satokab751aa2010-09-14 19:17:36 +09001053 // Set a subtype to this input method.
1054 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001055 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1056 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1057 // because mCurMethodId is stored as a history in
1058 // setSelectedInputMethodAndSubtypeLocked().
1059 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060
1061 if (ActivityManagerNative.isSystemReady()) {
1062 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001063 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 intent.putExtra("input_method_id", id);
1065 mContext.sendBroadcast(intent);
1066 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001067 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 } finally {
1069 Binder.restoreCallingIdentity(ident);
1070 }
1071 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001072
The Android Open Source Project4df24232009-03-05 14:34:35 -08001073 public boolean showSoftInput(IInputMethodClient client, int flags,
1074 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001075 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 long ident = Binder.clearCallingIdentity();
1077 try {
1078 synchronized (mMethodMap) {
1079 if (mCurClient == null || client == null
1080 || mCurClient.client.asBinder() != client.asBinder()) {
1081 try {
1082 // We need to check if this is the current client with
1083 // focus in the window manager, to allow this call to
1084 // be made before input is started in it.
1085 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001086 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001087 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 }
1089 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001090 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 }
1092 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001093
Joe Onorato8a9b2202010-02-26 18:56:32 -08001094 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001095 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 }
1097 } finally {
1098 Binder.restoreCallingIdentity(ident);
1099 }
1100 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001101
The Android Open Source Project4df24232009-03-05 14:34:35 -08001102 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 mShowRequested = true;
1104 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1105 mShowExplicitlyRequested = true;
1106 }
1107 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1108 mShowExplicitlyRequested = true;
1109 mShowForced = true;
1110 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001111
Dianne Hackborncc278702009-09-02 23:07:23 -07001112 if (!mSystemReady) {
1113 return false;
1114 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001115
The Android Open Source Project4df24232009-03-05 14:34:35 -08001116 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001118 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1119 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1120 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001122 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 } else if (mHaveConnection && SystemClock.uptimeMillis()
1124 < (mLastBindTime+TIME_TO_RECONNECT)) {
1125 // The client has asked to have the input method shown, but
1126 // we have been sitting here too long with a connection to the
1127 // service and no interface received, so let's disconnect/connect
1128 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001129 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 SystemClock.uptimeMillis()-mLastBindTime,1);
1131 mContext.unbindService(this);
1132 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1133 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001134
The Android Open Source Project4df24232009-03-05 14:34:35 -08001135 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001137
The Android Open Source Project4df24232009-03-05 14:34:35 -08001138 public boolean hideSoftInput(IInputMethodClient client, int flags,
1139 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001140 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 long ident = Binder.clearCallingIdentity();
1142 try {
1143 synchronized (mMethodMap) {
1144 if (mCurClient == null || client == null
1145 || mCurClient.client.asBinder() != client.asBinder()) {
1146 try {
1147 // We need to check if this is the current client with
1148 // focus in the window manager, to allow this call to
1149 // be made before input is started in it.
1150 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001151 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1152 + uid + ": " + client);
satok865b9772011-01-21 02:45:06 +09001153 mStatusBar.setIMEButtonVisible(mCurToken, false);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001154 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 }
1156 } catch (RemoteException e) {
satok865b9772011-01-21 02:45:06 +09001157 mStatusBar.setIMEButtonVisible(mCurToken, false);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001158 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 }
1160 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001161
Joe Onorato8a9b2202010-02-26 18:56:32 -08001162 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001163 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 }
1165 } finally {
1166 Binder.restoreCallingIdentity(ident);
1167 }
1168 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001169
The Android Open Source Project4df24232009-03-05 14:34:35 -08001170 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1172 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001173 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001175 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 }
1177 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001178 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001180 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001182 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001184 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1185 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1186 res = true;
1187 } else {
1188 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190 mInputShown = false;
1191 mShowRequested = false;
1192 mShowExplicitlyRequested = false;
1193 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001194 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001196
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001197 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1199 boolean first, int windowFlags) {
1200 long ident = Binder.clearCallingIdentity();
1201 try {
1202 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001203 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 + " viewHasFocus=" + viewHasFocus
1205 + " isTextEditor=" + isTextEditor
1206 + " softInputMode=#" + Integer.toHexString(softInputMode)
1207 + " first=" + first + " flags=#"
1208 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 if (mCurClient == null || client == null
1211 || mCurClient.client.asBinder() != client.asBinder()) {
1212 try {
1213 // We need to check if this is the current client with
1214 // focus in the window manager, to allow this call to
1215 // be made before input is started in it.
1216 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001217 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 return;
1219 }
1220 } catch (RemoteException e) {
1221 }
1222 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001223
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001224 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001225 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001226 return;
1227 }
1228 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001229
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001230 // Should we auto-show the IME even if the caller has not
1231 // specified what should be done with it?
1232 // We only do this automatically if the window can resize
1233 // to accommodate the IME (so what the user sees will give
1234 // them good context without input information being obscured
1235 // by the IME) or if running on a large screen where there
1236 // is more room for the target window + IME.
1237 final boolean doAutoShow =
1238 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1239 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1240 || mRes.getConfiguration().isLayoutSizeAtLeast(
1241 Configuration.SCREENLAYOUT_SIZE_LARGE);
1242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1244 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001245 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1247 // There is no focus view, and this window will
1248 // be behind any soft input window, so hide the
1249 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001250 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001251 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001253 } else if (isTextEditor && doAutoShow && (softInputMode &
1254 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 // There is a focus view, and we are navigating forward
1256 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001257 // We only do this automatically if the window an resize
1258 // to accomodate the IME (so what the user sees will give
1259 // them good context without input information being obscured
1260 // by the IME) or if running on a large screen where there
1261 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001262 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001263 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 }
1265 break;
1266 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1267 // Do nothing.
1268 break;
1269 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1270 if ((softInputMode &
1271 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001272 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001273 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 }
1275 break;
1276 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001277 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001278 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 break;
1280 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1281 if ((softInputMode &
1282 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001283 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001284 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 }
1286 break;
1287 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001288 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001289 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 break;
1291 }
1292 }
1293 } finally {
1294 Binder.restoreCallingIdentity(ident);
1295 }
1296 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1299 synchronized (mMethodMap) {
1300 if (mCurClient == null || client == null
1301 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001302 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001303 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 }
1305
satok440aab52010-11-25 09:43:11 +09001306 // Always call subtype picker, because subtype picker is a superset of input method
1307 // picker.
satokab751aa2010-09-14 19:17:36 +09001308 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1309 }
1310 }
1311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001313 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1314 }
1315
1316 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1317 synchronized (mMethodMap) {
1318 if (subtype != null) {
1319 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1320 mMethodMap.get(id), subtype.hashCode()));
1321 } else {
1322 setInputMethod(token, id);
1323 }
1324 }
satokab751aa2010-09-14 19:17:36 +09001325 }
1326
satokb416a71e2010-11-25 20:42:14 +09001327 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001328 IInputMethodClient client, String inputMethodId) {
satokb416a71e2010-11-25 20:42:14 +09001329 synchronized (mMethodMap) {
1330 if (mCurClient == null || client == null
1331 || mCurClient.client.asBinder() != client.asBinder()) {
1332 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1333 }
satok7fee71f2010-12-17 18:54:26 +09001334 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1335 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09001336 }
1337 }
1338
satok735cf382010-11-11 20:40:09 +09001339 public boolean switchToLastInputMethod(IBinder token) {
1340 synchronized (mMethodMap) {
1341 Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1342 if (lastIme != null) {
1343 InputMethodInfo imi = mMethodMap.get(lastIme.first);
1344 if (imi != null) {
satok28203512010-11-24 11:06:49 +09001345 setInputMethodWithSubtypeId(token, lastIme.first, getSubtypeIdFromHashCode(
satok735cf382010-11-11 20:40:09 +09001346 imi, Integer.valueOf(lastIme.second)));
1347 return true;
1348 }
1349 }
1350 return false;
1351 }
1352 }
1353
satok28203512010-11-24 11:06:49 +09001354 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 synchronized (mMethodMap) {
1356 if (token == null) {
1357 if (mContext.checkCallingOrSelfPermission(
1358 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1359 != PackageManager.PERMISSION_GRANTED) {
1360 throw new SecurityException(
1361 "Using null token requires permission "
1362 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1363 }
1364 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001365 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1366 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 return;
1368 }
1369
1370 long ident = Binder.clearCallingIdentity();
1371 try {
satokab751aa2010-09-14 19:17:36 +09001372 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 } finally {
1374 Binder.restoreCallingIdentity(ident);
1375 }
1376 }
1377 }
1378
1379 public void hideMySoftInput(IBinder token, int flags) {
1380 synchronized (mMethodMap) {
1381 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001382 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1383 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 return;
1385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 long ident = Binder.clearCallingIdentity();
1387 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001388 hideCurrentInputLocked(flags, null);
1389 } finally {
1390 Binder.restoreCallingIdentity(ident);
1391 }
1392 }
1393 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001394
The Android Open Source Project4df24232009-03-05 14:34:35 -08001395 public void showMySoftInput(IBinder token, int flags) {
1396 synchronized (mMethodMap) {
1397 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001398 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1399 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001400 return;
1401 }
1402 long ident = Binder.clearCallingIdentity();
1403 try {
1404 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 } finally {
1406 Binder.restoreCallingIdentity(ident);
1407 }
1408 }
1409 }
1410
1411 void setEnabledSessionInMainThread(SessionState session) {
1412 if (mEnabledSession != session) {
1413 if (mEnabledSession != null) {
1414 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001415 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 mEnabledSession.method.setSessionEnabled(
1417 mEnabledSession.session, false);
1418 } catch (RemoteException e) {
1419 }
1420 }
1421 mEnabledSession = session;
1422 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001423 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 session.method.setSessionEnabled(
1425 session.session, true);
1426 } catch (RemoteException e) {
1427 }
1428 }
1429 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 public boolean handleMessage(Message msg) {
1432 HandlerCaller.SomeArgs args;
1433 switch (msg.what) {
1434 case MSG_SHOW_IM_PICKER:
1435 showInputMethodMenu();
1436 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001437
satokab751aa2010-09-14 19:17:36 +09001438 case MSG_SHOW_IM_SUBTYPE_PICKER:
1439 showInputMethodSubtypeMenu();
1440 return true;
1441
satok47a44912010-10-06 16:03:58 +09001442 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001443 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001444 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001445 return true;
1446
1447 case MSG_SHOW_IM_CONFIG:
1448 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001449 return true;
1450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 case MSG_UNBIND_INPUT:
1454 try {
1455 ((IInputMethod)msg.obj).unbindInput();
1456 } catch (RemoteException e) {
1457 // There is nothing interesting about the method dying.
1458 }
1459 return true;
1460 case MSG_BIND_INPUT:
1461 args = (HandlerCaller.SomeArgs)msg.obj;
1462 try {
1463 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1464 } catch (RemoteException e) {
1465 }
1466 return true;
1467 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001468 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001470 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1471 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 } catch (RemoteException e) {
1473 }
1474 return true;
1475 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001476 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001478 ((IInputMethod)args.arg1).hideSoftInput(0,
1479 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 } catch (RemoteException e) {
1481 }
1482 return true;
1483 case MSG_ATTACH_TOKEN:
1484 args = (HandlerCaller.SomeArgs)msg.obj;
1485 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001486 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1488 } catch (RemoteException e) {
1489 }
1490 return true;
1491 case MSG_CREATE_SESSION:
1492 args = (HandlerCaller.SomeArgs)msg.obj;
1493 try {
1494 ((IInputMethod)args.arg1).createSession(
1495 (IInputMethodCallback)args.arg2);
1496 } catch (RemoteException e) {
1497 }
1498 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 case MSG_START_INPUT:
1502 args = (HandlerCaller.SomeArgs)msg.obj;
1503 try {
1504 SessionState session = (SessionState)args.arg1;
1505 setEnabledSessionInMainThread(session);
1506 session.method.startInput((IInputContext)args.arg2,
1507 (EditorInfo)args.arg3);
1508 } catch (RemoteException e) {
1509 }
1510 return true;
1511 case MSG_RESTART_INPUT:
1512 args = (HandlerCaller.SomeArgs)msg.obj;
1513 try {
1514 SessionState session = (SessionState)args.arg1;
1515 setEnabledSessionInMainThread(session);
1516 session.method.restartInput((IInputContext)args.arg2,
1517 (EditorInfo)args.arg3);
1518 } catch (RemoteException e) {
1519 }
1520 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 case MSG_UNBIND_METHOD:
1525 try {
1526 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1527 } catch (RemoteException e) {
1528 // There is nothing interesting about the last client dying.
1529 }
1530 return true;
1531 case MSG_BIND_METHOD:
1532 args = (HandlerCaller.SomeArgs)msg.obj;
1533 try {
1534 ((IInputMethodClient)args.arg1).onBindMethod(
1535 (InputBindResult)args.arg2);
1536 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001537 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 }
1539 return true;
1540 }
1541 return false;
1542 }
1543
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001544 private boolean isSystemIme(InputMethodInfo inputMethod) {
1545 return (inputMethod.getServiceInfo().applicationInfo.flags
1546 & ApplicationInfo.FLAG_SYSTEM) != 0;
1547 }
1548
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001549 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001550 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001551 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001552 // We'd prefer to fall back on a system IME, since that is safer.
1553 int i=enabled.size();
1554 while (i > 0) {
1555 i--;
1556 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1557 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1558 break;
1559 }
1560 }
satokab751aa2010-09-14 19:17:36 +09001561 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001562 if (DEBUG) {
1563 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1564 }
satok723a27e2010-11-11 14:58:11 +09001565 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001566 return true;
1567 }
1568
1569 return false;
1570 }
1571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1573 HashMap<String, InputMethodInfo> map) {
1574 list.clear();
1575 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001578 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001579 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1580 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1581 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1582 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583
1584 List<ResolveInfo> services = pm.queryIntentServices(
1585 new Intent(InputMethod.SERVICE_INTERFACE),
1586 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 for (int i = 0; i < services.size(); ++i) {
1589 ResolveInfo ri = services.get(i);
1590 ServiceInfo si = ri.serviceInfo;
1591 ComponentName compName = new ComponentName(si.packageName, si.name);
1592 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1593 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001594 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 + ": it does not require the permission "
1596 + android.Manifest.permission.BIND_INPUT_METHOD);
1597 continue;
1598 }
1599
Joe Onorato8a9b2202010-02-26 18:56:32 -08001600 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601
1602 try {
1603 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1604 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001605 final String id = p.getId();
1606 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607
Amith Yamasanie861ec12010-03-24 21:39:27 -07001608 // System IMEs are enabled by default, unless there's a hard keyboard
1609 // and the system IME was explicitly disabled
1610 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1611 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001612 }
1613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001615 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001619 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001621 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 }
1623 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001624
1625 String defaultIme = Settings.Secure.getString(mContext
1626 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001627 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001628 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001629 updateFromSettingsLocked();
1630 }
1631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001635
satokab751aa2010-09-14 19:17:36 +09001636 private void showInputMethodMenu() {
1637 showInputMethodMenuInternal(false);
1638 }
1639
1640 private void showInputMethodSubtypeMenu() {
1641 showInputMethodMenuInternal(true);
1642 }
1643
satok217f5482010-12-15 05:19:19 +09001644 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
satok86417ea2010-10-27 14:11:03 +09001645 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_AND_SUBTYPE_ENABLER);
satok47a44912010-10-06 16:03:58 +09001646 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001647 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1648 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001649 if (!TextUtils.isEmpty(inputMethodId)) {
1650 intent.putExtra(EXTRA_INPUT_METHOD_ID, inputMethodId);
1651 }
satok217f5482010-12-15 05:19:19 +09001652 mContext.startActivity(intent);
1653 }
1654
1655 private void showConfigureInputMethods() {
1656 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1657 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1658 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1659 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001660 mContext.startActivity(intent);
1661 }
1662
satokab751aa2010-09-14 19:17:36 +09001663 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001664 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 String lastInputMethodId = Settings.Secure.getString(context
1671 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001672 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001673 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001674
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001675 synchronized (mMethodMap) {
satok7f35c8c2010-10-07 21:13:11 +09001676 final List<Pair<InputMethodInfo, ArrayList<String>>> immis =
satok67ddf9c2010-11-17 09:45:54 +09001677 mSettings.getEnabledInputMethodAndSubtypeHashCodeListLocked();
satok16331c82010-12-20 23:48:46 +09001678 int N = immis.size();
1679
1680 // Add applicable subtypes if no subtype for each IME is enabled.
1681 for (int i = 0; i < N; ++i) {
1682 InputMethodInfo imi = immis.get(i).first;
1683 ArrayList<String> subtypes = immis.get(i).second;
1684 if (subtypes != null && subtypes.size() == 0) {
1685 ArrayList<InputMethodSubtype> applicableSubtypes =
satokdf31ae62011-01-15 06:19:44 +09001686 getApplicableSubtypesLocked(mRes, imi.getSubtypes());
satok16331c82010-12-20 23:48:46 +09001687 final int numSubtypes = applicableSubtypes.size();
1688 for (int j = 0; j < numSubtypes; ++j) {
1689 subtypes.add(String.valueOf(applicableSubtypes.get(j).hashCode()));
1690 }
1691 }
1692 }
1693
satok7f35c8c2010-10-07 21:13:11 +09001694 ArrayList<Integer> subtypeIds = new ArrayList<Integer>();
1695
1696 if (immis == null || immis.size() == 0) {
1697 return;
1698 }
1699
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001700 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701
satok913a8922010-08-26 21:53:41 +09001702
satokab751aa2010-09-14 19:17:36 +09001703 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1704 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001705
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001706 for (int i = 0; i < N; ++i) {
satok7f35c8c2010-10-07 21:13:11 +09001707 InputMethodInfo property = immis.get(i).first;
1708 final ArrayList<String> enabledSubtypeIds = immis.get(i).second;
1709 HashSet<String> enabledSubtypeSet = new HashSet<String>();
1710 for (String s : enabledSubtypeIds) {
1711 enabledSubtypeSet.add(s);
1712 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001713 if (property == null) {
1714 continue;
1715 }
satokab751aa2010-09-14 19:17:36 +09001716 ArrayList<InputMethodSubtype> subtypes = property.getSubtypes();
1717 CharSequence label = property.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001718 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokab751aa2010-09-14 19:17:36 +09001719 for (int j = 0; j < subtypes.size(); ++j) {
1720 InputMethodSubtype subtype = subtypes.get(j);
satok7f35c8c2010-10-07 21:13:11 +09001721 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1722 CharSequence title;
1723 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001724 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001725 if (nameResId != 0) {
1726 title = pm.getText(property.getPackageName(), nameResId,
1727 property.getServiceInfo().applicationInfo);
1728 } else {
1729 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001730 // TODO: Use more friendly Title and UI
1731 title = label + "," + (mode == null ? "" : mode) + ","
1732 + (language == null ? "" : language);
1733 }
1734 imMap.put(title, new Pair<InputMethodInfo, Integer>(property, j));
satokab751aa2010-09-14 19:17:36 +09001735 }
satokab751aa2010-09-14 19:17:36 +09001736 }
1737 } else {
1738 imMap.put(label,
1739 new Pair<InputMethodInfo, Integer>(property, NOT_A_SUBTYPE_ID));
1740 subtypeIds.add(0);
1741 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001742 }
satok913a8922010-08-26 21:53:41 +09001743
1744 N = imMap.size();
1745 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001746 mIms = new InputMethodInfo[N];
1747 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001748 int checkedItem = 0;
1749 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001750 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1751 mIms[i] = value.first;
1752 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001753 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001754 int subtypeId = mSubtypeIds[i];
1755 if ((subtypeId == NOT_A_SUBTYPE_ID)
1756 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1757 || (subtypeId == lastInputMethodSubtypeId)) {
1758 checkedItem = i;
1759 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001760 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 }
satokab751aa2010-09-14 19:17:36 +09001762
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001763 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1764 public void onClick(DialogInterface dialog, int which) {
1765 hideInputMethodMenu();
1766 }
1767 };
satokd87c2592010-09-29 11:52:06 +09001768
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001769 TypedArray a = context.obtainStyledAttributes(null,
1770 com.android.internal.R.styleable.DialogPreference,
1771 com.android.internal.R.attr.alertDialogStyle, 0);
1772 mDialogBuilder = new AlertDialog.Builder(context)
1773 .setTitle(com.android.internal.R.string.select_input_method)
1774 .setOnCancelListener(new OnCancelListener() {
1775 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001777 }
1778 })
1779 .setIcon(a.getDrawable(
1780 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1781 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001782
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001783 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1784 new AlertDialog.OnClickListener() {
1785 public void onClick(DialogInterface dialog, int which) {
1786 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001787 if (mIms == null || mIms.length <= which
1788 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001789 return;
1790 }
1791 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001792 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001793 hideInputMethodMenu();
1794 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001795 if ((subtypeId < 0)
1796 || (subtypeId >= im.getSubtypes().size())) {
1797 subtypeId = NOT_A_SUBTYPE_ID;
1798 }
1799 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001800 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001803 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804
satok7f35c8c2010-10-07 21:13:11 +09001805 if (showSubtypes) {
satok82beadf2010-12-27 19:03:06 +09001806 mDialogBuilder.setPositiveButton(
1807 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09001808 new DialogInterface.OnClickListener() {
1809 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09001810 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09001811 }
1812 });
1813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 mSwitchingDialog = mDialogBuilder.create();
1815 mSwitchingDialog.getWindow().setType(
1816 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1817 mSwitchingDialog.show();
1818 }
1819 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001822 synchronized (mMethodMap) {
1823 hideInputMethodMenuLocked();
1824 }
1825 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001826
The Android Open Source Project10592532009-03-18 17:39:46 -07001827 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001828 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829
The Android Open Source Project10592532009-03-18 17:39:46 -07001830 if (mSwitchingDialog != null) {
1831 mSwitchingDialog.dismiss();
1832 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001834
The Android Open Source Project10592532009-03-18 17:39:46 -07001835 mDialogBuilder = null;
1836 mItems = null;
1837 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 public boolean setInputMethodEnabled(String id, boolean enabled) {
1843 synchronized (mMethodMap) {
1844 if (mContext.checkCallingOrSelfPermission(
1845 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1846 != PackageManager.PERMISSION_GRANTED) {
1847 throw new SecurityException(
1848 "Requires permission "
1849 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1850 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 long ident = Binder.clearCallingIdentity();
1853 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001854 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 } finally {
1856 Binder.restoreCallingIdentity(ident);
1857 }
1858 }
1859 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001860
1861 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1862 // Make sure this is a valid input method.
1863 InputMethodInfo imm = mMethodMap.get(id);
1864 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001865 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001866 }
1867
satokd87c2592010-09-29 11:52:06 +09001868 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1869 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001870
satokd87c2592010-09-29 11:52:06 +09001871 if (enabled) {
1872 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1873 if (pair.first.equals(id)) {
1874 // We are enabling this input method, but it is already enabled.
1875 // Nothing to do. The previous state was enabled.
1876 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001877 }
1878 }
satokd87c2592010-09-29 11:52:06 +09001879 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1880 // Previous state was disabled.
1881 return false;
1882 } else {
1883 StringBuilder builder = new StringBuilder();
1884 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1885 builder, enabledInputMethodsList, id)) {
1886 // Disabled input method is currently selected, switch to another one.
1887 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1888 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001889 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
1890 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
1891 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09001892 }
1893 // Previous state was enabled.
1894 return true;
1895 } else {
1896 // We are disabling the input method but it is already disabled.
1897 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001898 return false;
1899 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001900 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001901 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001902
satok723a27e2010-11-11 14:58:11 +09001903 private void saveCurrentInputMethodAndSubtypeToHistory() {
1904 String subtypeId = NOT_A_SUBTYPE_ID_STR;
1905 if (mCurrentSubtype != null) {
1906 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
1907 }
1908 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
satokab751aa2010-09-14 19:17:36 +09001909 }
1910
satok723a27e2010-11-11 14:58:11 +09001911 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
1912 boolean setSubtypeOnly) {
1913 // Update the history of InputMethod and Subtype
1914 saveCurrentInputMethodAndSubtypeToHistory();
1915
1916 // Set Subtype here
1917 if (imi == null || subtypeId < 0) {
1918 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001919 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09001920 } else {
1921 final ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1922 if (subtypeId < subtypes.size()) {
1923 mSettings.putSelectedSubtype(subtypes.get(subtypeId).hashCode());
1924 mCurrentSubtype = subtypes.get(subtypeId);
1925 } else {
1926 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
1927 mCurrentSubtype = null;
1928 }
satokab751aa2010-09-14 19:17:36 +09001929 }
satok723a27e2010-11-11 14:58:11 +09001930
1931 if (!setSubtypeOnly) {
1932 // Set InputMethod here
1933 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
1934 }
1935 }
1936
1937 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
1938 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
1939 int lastSubtypeId = NOT_A_SUBTYPE_ID;
1940 // newDefaultIme is empty when there is no candidate for the selected IME.
1941 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
1942 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
1943 if (subtypeHashCode != null) {
1944 try {
1945 lastSubtypeId = getSubtypeIdFromHashCode(
1946 imi, Integer.valueOf(subtypeHashCode));
1947 } catch (NumberFormatException e) {
1948 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
1949 }
1950 }
1951 }
1952 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09001953 }
1954
1955 private int getSelectedInputMethodSubtypeId(String id) {
1956 InputMethodInfo imi = mMethodMap.get(id);
1957 if (imi == null) {
1958 return NOT_A_SUBTYPE_ID;
1959 }
satokab751aa2010-09-14 19:17:36 +09001960 int subtypeId;
1961 try {
1962 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
1963 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
1964 } catch (SettingNotFoundException e) {
1965 return NOT_A_SUBTYPE_ID;
1966 }
satok723a27e2010-11-11 14:58:11 +09001967 return getSubtypeIdFromHashCode(imi, subtypeId);
1968 }
1969
1970 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09001971 if (imi != null) {
1972 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1973 for (int i = 0; i < subtypes.size(); ++i) {
1974 InputMethodSubtype ims = subtypes.get(i);
1975 if (subtypeHashCode == ims.hashCode()) {
1976 return i;
1977 }
satokab751aa2010-09-14 19:17:36 +09001978 }
1979 }
1980 return NOT_A_SUBTYPE_ID;
1981 }
1982
satokdf31ae62011-01-15 06:19:44 +09001983 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
1984 Resources res, List<InputMethodSubtype> subtypes) {
1985 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09001986 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
1987 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
1988 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09001989 final int N = subtypes.size();
1990 boolean containsKeyboardSubtype = false;
1991 for (int i = 0; i < N; ++i) {
1992 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09001993 final String locale = subtype.getLocale();
1994 final String mode = subtype.getMode();
1995 // When system locale starts with subtype's locale, that subtype will be applicable
1996 // for system locale
1997 // For instance, it's clearly applicable for cases like system locale = en_US and
1998 // subtype = en, but it is not necessarily considered applicable for cases like system
1999 // locale = en and subtype = en_US.
2000 // We just call systemLocale.startsWith(locale) in this function because there is no
2001 // need to find applicable subtypes aggressively unlike
2002 // findLastResortApplicableSubtypeLocked.
2003 if (systemLocale.startsWith(locale)) {
2004 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2005 // If more applicable subtypes are contained, skip.
2006 if (applicableSubtype != null
2007 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2008 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002009 if (!containsKeyboardSubtype
2010 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2011 containsKeyboardSubtype = true;
2012 }
2013 }
2014 }
satok3da92232011-01-11 22:46:30 +09002015 ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2016 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002017 if (!containsKeyboardSubtype) {
2018 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002019 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002020 if (lastResortKeyboardSubtype != null) {
2021 applicableSubtypes.add(lastResortKeyboardSubtype);
2022 }
2023 }
2024 return applicableSubtypes;
2025 }
2026
satok4e4569d2010-11-19 18:45:53 +09002027 /**
2028 * If there are no selected subtypes, tries finding the most applicable one according to the
2029 * given locale.
2030 * @param subtypes this function will search the most applicable subtype in subtypes
2031 * @param mode subtypes will be filtered by mode
2032 * @param locale subtypes will be filtered by locale
satok7599a7fb2010-12-22 13:45:23 +09002033 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2034 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002035 * @return the most applicable subtypeId
2036 */
satokdf31ae62011-01-15 06:19:44 +09002037 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2038 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7fb2010-12-22 13:45:23 +09002039 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002040 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002041 return null;
satok8fbb1e82010-11-02 23:15:58 +09002042 }
satok4e4569d2010-11-19 18:45:53 +09002043 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002044 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002045 }
satok8fbb1e82010-11-02 23:15:58 +09002046 final String language = locale.substring(0, 2);
2047 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002048 InputMethodSubtype applicableSubtype = null;
satok7599a7fb2010-12-22 13:45:23 +09002049 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002050 final int N = subtypes.size();
2051 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002052 InputMethodSubtype subtype = subtypes.get(i);
2053 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002054 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2055 // and all subtypes with all modes can be candidates.
2056 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7fb2010-12-22 13:45:23 +09002057 if (firstMatchedModeSubtype == null) {
2058 firstMatchedModeSubtype = subtype;
2059 }
satok9ef02832010-11-04 21:17:48 +09002060 if (locale.equals(subtypeLocale)) {
2061 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002062 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002063 break;
2064 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2065 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002066 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002067 partialMatchFound = true;
2068 }
satok8fbb1e82010-11-02 23:15:58 +09002069 }
2070 }
2071
satok7599a7fb2010-12-22 13:45:23 +09002072 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2073 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002074 }
2075
satok8fbb1e82010-11-02 23:15:58 +09002076 // The first subtype applicable to the system locale will be defined as the most applicable
2077 // subtype.
2078 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002079 if (applicableSubtype != null) {
2080 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2081 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2082 }
satok8fbb1e82010-11-02 23:15:58 +09002083 }
satokcd7cd292010-11-20 15:46:23 +09002084 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002085 }
2086
satok4e4569d2010-11-19 18:45:53 +09002087 // If there are no selected shortcuts, tries finding the most applicable ones.
2088 private Pair<InputMethodInfo, InputMethodSubtype>
2089 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2090 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2091 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002092 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002093 boolean foundInSystemIME = false;
2094
2095 // Search applicable subtype for each InputMethodInfo
2096 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002097 final String imiId = imi.getId();
2098 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2099 continue;
2100 }
satokcd7cd292010-11-20 15:46:23 +09002101 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002102 final List<InputMethodSubtype> enabledSubtypes =
2103 getEnabledInputMethodSubtypeList(imi, true);
2104 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002105 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002106 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002107 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002108 }
satokdf31ae62011-01-15 06:19:44 +09002109 // 2. Search by the system locale from enabledSubtypes.
2110 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002111 if (subtype == null) {
2112 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002113 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002114 }
satok7599a7fb2010-12-22 13:45:23 +09002115 // 4. Search by the current subtype's locale from all subtypes.
2116 if (subtype == null && mCurrentSubtype != null) {
2117 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002118 mRes, imi.getSubtypes(), mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09002119 }
2120 // 5. Search by the system locale from all subtypes.
2121 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002122 if (subtype == null) {
satok7599a7fb2010-12-22 13:45:23 +09002123 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002124 mRes, imi.getSubtypes(), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002125 }
satokcd7cd292010-11-20 15:46:23 +09002126 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09002127 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002128 // The current input method is the most applicable IME.
2129 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002130 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002131 break;
satok7599a7fb2010-12-22 13:45:23 +09002132 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002133 // The system input method is 2nd applicable IME.
2134 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002135 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09002136 if ((imi.getServiceInfo().applicationInfo.flags
2137 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2138 foundInSystemIME = true;
2139 }
satok4e4569d2010-11-19 18:45:53 +09002140 }
2141 }
2142 }
2143 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002144 if (mostApplicableIMI != null) {
2145 Slog.w(TAG, "Most applicable shortcut input method was:"
2146 + mostApplicableIMI.getId());
2147 if (mostApplicableSubtype != null) {
2148 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2149 + "," + mostApplicableSubtype.getMode() + ","
2150 + mostApplicableSubtype.getLocale());
2151 }
2152 }
satok4e4569d2010-11-19 18:45:53 +09002153 }
satokcd7cd292010-11-20 15:46:23 +09002154 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002155 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002156 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002157 } else {
2158 return null;
2159 }
2160 }
2161
satokab751aa2010-09-14 19:17:36 +09002162 /**
2163 * @return Return the current subtype of this input method.
2164 */
2165 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002166 boolean subtypeIsSelected = false;
2167 try {
2168 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2169 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2170 } catch (SettingNotFoundException e) {
2171 }
satok3ef8b292010-11-23 06:06:29 +09002172 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002173 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002174 String lastInputMethodId = Settings.Secure.getString(
2175 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002176 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2177 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002178 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2179 if (imi != null) {
2180 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002181 // the most applicable subtype from explicitly or implicitly enabled
2182 // subtypes.
2183 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2184 getEnabledInputMethodSubtypeList(imi, true);
2185 // If there is only one explicitly or implicitly enabled subtype,
2186 // just returns it.
2187 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2188 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2189 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2190 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2191 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2192 SUBTYPE_MODE_KEYBOARD, null, true);
2193 if (mCurrentSubtype == null) {
2194 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2195 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2196 true);
2197 }
2198 }
satok4e4569d2010-11-19 18:45:53 +09002199 }
satokcd7cd292010-11-20 15:46:23 +09002200 } else {
satok3ef8b292010-11-23 06:06:29 +09002201 mCurrentSubtype =
2202 mMethodMap.get(lastInputMethodId).getSubtypes().get(subtypeId);
2203 }
satok8fbb1e82010-11-02 23:15:58 +09002204 }
satok3ef8b292010-11-23 06:06:29 +09002205 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002206 }
satokab751aa2010-09-14 19:17:36 +09002207 }
2208
satokf3db1af2010-11-23 13:34:33 +09002209 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2210 InputMethodSubtype subtype) {
2211 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2212 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2213 } else {
2214 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2215 subtypes.add(subtype);
2216 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2217 }
2218 }
2219
satok4e4569d2010-11-19 18:45:53 +09002220 // TODO: We should change the return type from List to List<Parcelable>
2221 public List getShortcutInputMethodsAndSubtypes() {
2222 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002223 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002224 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002225 // If there are no selected shortcut subtypes, the framework will try to find
2226 // the most applicable subtype from all subtypes whose mode is
2227 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002228 Pair<InputMethodInfo, InputMethodSubtype> info =
2229 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2230 SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09002231 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002232 ret.add(info.first);
2233 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09002234 }
satok3da92232011-01-11 22:46:30 +09002235 return ret;
satokf3db1af2010-11-23 13:34:33 +09002236 }
satokf3db1af2010-11-23 13:34:33 +09002237 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2238 ret.add(imi);
2239 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2240 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002241 }
2242 }
satokf3db1af2010-11-23 13:34:33 +09002243 return ret;
satok4e4569d2010-11-19 18:45:53 +09002244 }
2245 }
2246
satokb66d2872010-11-10 01:04:04 +09002247 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2248 synchronized (mMethodMap) {
2249 if (subtype != null && mCurMethodId != null) {
2250 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2251 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2252 if (subtypeId != NOT_A_SUBTYPE_ID) {
2253 setInputMethodLocked(mCurMethodId, subtypeId);
2254 return true;
2255 }
2256 }
2257 return false;
2258 }
2259 }
2260
satokd87c2592010-09-29 11:52:06 +09002261 /**
2262 * Utility class for putting and getting settings for InputMethod
2263 * TODO: Move all putters and getters of settings to this class.
2264 */
2265 private static class InputMethodSettings {
2266 // The string for enabled input method is saved as follows:
2267 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2268 private static final char INPUT_METHOD_SEPARATER = ':';
2269 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002270 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002271 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2272
satok723a27e2010-11-11 14:58:11 +09002273 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002274 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2275
satokdf31ae62011-01-15 06:19:44 +09002276 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002277 private final ContentResolver mResolver;
2278 private final HashMap<String, InputMethodInfo> mMethodMap;
2279 private final ArrayList<InputMethodInfo> mMethodList;
2280
2281 private String mEnabledInputMethodsStrCache;
2282
2283 private static void buildEnabledInputMethodsSettingString(
2284 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2285 String id = pair.first;
2286 ArrayList<String> subtypes = pair.second;
2287 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002288 // Inputmethod and subtypes are saved in the settings as follows:
2289 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2290 for (String subtypeId: subtypes) {
2291 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002292 }
2293 }
2294
2295 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002296 Resources res, ContentResolver resolver,
2297 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2298 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002299 mResolver = resolver;
2300 mMethodMap = methodMap;
2301 mMethodList = methodList;
2302 }
2303
2304 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2305 return createEnabledInputMethodListLocked(
2306 getEnabledInputMethodsAndSubtypeListLocked());
2307 }
2308
satok7f35c8c2010-10-07 21:13:11 +09002309 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002310 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2311 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002312 getEnabledInputMethodsAndSubtypeListLocked());
2313 }
2314
satok67ddf9c2010-11-17 09:45:54 +09002315 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2316 InputMethodInfo imi) {
2317 List<Pair<String, ArrayList<String>>> imsList =
2318 getEnabledInputMethodsAndSubtypeListLocked();
2319 ArrayList<InputMethodSubtype> enabledSubtypes =
2320 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002321 if (imi != null) {
2322 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2323 InputMethodInfo info = mMethodMap.get(imsPair.first);
2324 if (info != null && info.getId().equals(imi.getId())) {
2325 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
2326 for (InputMethodSubtype ims: subtypes) {
2327 for (String s: imsPair.second) {
2328 if (String.valueOf(ims.hashCode()).equals(s)) {
2329 enabledSubtypes.add(ims);
2330 }
satok67ddf9c2010-11-17 09:45:54 +09002331 }
2332 }
satok884ef9a2010-11-18 10:39:46 +09002333 break;
satok67ddf9c2010-11-17 09:45:54 +09002334 }
satok67ddf9c2010-11-17 09:45:54 +09002335 }
2336 }
2337 return enabledSubtypes;
2338 }
2339
satokd87c2592010-09-29 11:52:06 +09002340 // At the initial boot, the settings for input methods are not set,
2341 // so we need to enable IME in that case.
2342 public void enableAllIMEsIfThereIsNoEnabledIME() {
2343 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2344 StringBuilder sb = new StringBuilder();
2345 final int N = mMethodList.size();
2346 for (int i = 0; i < N; i++) {
2347 InputMethodInfo imi = mMethodList.get(i);
2348 Slog.i(TAG, "Adding: " + imi.getId());
2349 if (i > 0) sb.append(':');
2350 sb.append(imi.getId());
2351 }
2352 putEnabledInputMethodsStr(sb.toString());
2353 }
2354 }
2355
2356 public List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
2357 ArrayList<Pair<String, ArrayList<String>>> imsList
2358 = new ArrayList<Pair<String, ArrayList<String>>>();
2359 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2360 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2361 return imsList;
2362 }
satok723a27e2010-11-11 14:58:11 +09002363 mInputMethodSplitter.setString(enabledInputMethodsStr);
2364 while (mInputMethodSplitter.hasNext()) {
2365 String nextImsStr = mInputMethodSplitter.next();
2366 mSubtypeSplitter.setString(nextImsStr);
2367 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002368 ArrayList<String> subtypeHashes = new ArrayList<String>();
2369 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002370 String imeId = mSubtypeSplitter.next();
2371 while (mSubtypeSplitter.hasNext()) {
2372 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002373 }
2374 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2375 }
2376 }
2377 return imsList;
2378 }
2379
2380 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2381 if (reloadInputMethodStr) {
2382 getEnabledInputMethodsStr();
2383 }
2384 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2385 // Add in the newly enabled input method.
2386 putEnabledInputMethodsStr(id);
2387 } else {
2388 putEnabledInputMethodsStr(
2389 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2390 }
2391 }
2392
2393 /**
2394 * Build and put a string of EnabledInputMethods with removing specified Id.
2395 * @return the specified id was removed or not.
2396 */
2397 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2398 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2399 boolean isRemoved = false;
2400 boolean needsAppendSeparator = false;
2401 for (Pair<String, ArrayList<String>> ims: imsList) {
2402 String curId = ims.first;
2403 if (curId.equals(id)) {
2404 // We are disabling this input method, and it is
2405 // currently enabled. Skip it to remove from the
2406 // new list.
2407 isRemoved = true;
2408 } else {
2409 if (needsAppendSeparator) {
2410 builder.append(INPUT_METHOD_SEPARATER);
2411 } else {
2412 needsAppendSeparator = true;
2413 }
2414 buildEnabledInputMethodsSettingString(builder, ims);
2415 }
2416 }
2417 if (isRemoved) {
2418 // Update the setting with the new list of input methods.
2419 putEnabledInputMethodsStr(builder.toString());
2420 }
2421 return isRemoved;
2422 }
2423
2424 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2425 List<Pair<String, ArrayList<String>>> imsList) {
2426 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2427 for (Pair<String, ArrayList<String>> ims: imsList) {
2428 InputMethodInfo info = mMethodMap.get(ims.first);
2429 if (info != null) {
2430 res.add(info);
2431 }
2432 }
2433 return res;
2434 }
2435
satok7f35c8c2010-10-07 21:13:11 +09002436 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002437 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002438 List<Pair<String, ArrayList<String>>> imsList) {
2439 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2440 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2441 for (Pair<String, ArrayList<String>> ims : imsList) {
2442 InputMethodInfo info = mMethodMap.get(ims.first);
2443 if (info != null) {
2444 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2445 }
2446 }
2447 return res;
2448 }
2449
satokd87c2592010-09-29 11:52:06 +09002450 private void putEnabledInputMethodsStr(String str) {
2451 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2452 mEnabledInputMethodsStrCache = str;
2453 }
2454
2455 private String getEnabledInputMethodsStr() {
2456 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2457 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002458 if (DEBUG) {
2459 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2460 }
satokd87c2592010-09-29 11:52:06 +09002461 return mEnabledInputMethodsStrCache;
2462 }
satok723a27e2010-11-11 14:58:11 +09002463
2464 private void saveSubtypeHistory(
2465 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2466 StringBuilder builder = new StringBuilder();
2467 boolean isImeAdded = false;
2468 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2469 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2470 newSubtypeId);
2471 isImeAdded = true;
2472 }
2473 for (Pair<String, String> ime: savedImes) {
2474 String imeId = ime.first;
2475 String subtypeId = ime.second;
2476 if (TextUtils.isEmpty(subtypeId)) {
2477 subtypeId = NOT_A_SUBTYPE_ID_STR;
2478 }
2479 if (isImeAdded) {
2480 builder.append(INPUT_METHOD_SEPARATER);
2481 } else {
2482 isImeAdded = true;
2483 }
2484 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2485 subtypeId);
2486 }
2487 // Remove the last INPUT_METHOD_SEPARATER
2488 putSubtypeHistoryStr(builder.toString());
2489 }
2490
2491 public void addSubtypeToHistory(String imeId, String subtypeId) {
2492 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2493 for (Pair<String, String> ime: subtypeHistory) {
2494 if (ime.first.equals(imeId)) {
2495 if (DEBUG) {
2496 Slog.v(TAG, "Subtype found in the history: " + imeId
2497 + ime.second);
2498 }
2499 // We should break here
2500 subtypeHistory.remove(ime);
2501 break;
2502 }
2503 }
2504 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2505 }
2506
2507 private void putSubtypeHistoryStr(String str) {
2508 if (DEBUG) {
2509 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2510 }
2511 Settings.Secure.putString(
2512 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2513 }
2514
2515 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2516 // Gets the first one from the history
2517 return getLastSubtypeForInputMethodLockedInternal(null);
2518 }
2519
2520 public String getLastSubtypeForInputMethodLocked(String imeId) {
2521 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2522 if (ime != null) {
2523 return ime.second;
2524 } else {
2525 return null;
2526 }
2527 }
2528
2529 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2530 List<Pair<String, ArrayList<String>>> enabledImes =
2531 getEnabledInputMethodsAndSubtypeListLocked();
2532 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2533 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2534 final String imeInTheHistory = imeAndSubtype.first;
2535 // If imeId is empty, returns the first IME and subtype in the history
2536 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2537 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002538 final String subtypeHashCode =
2539 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2540 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002541 if (!TextUtils.isEmpty(subtypeHashCode)) {
2542 if (DEBUG) {
2543 Slog.d(TAG, "Enabled subtype found in the history:" + subtypeHashCode);
2544 }
2545 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2546 }
2547 }
2548 }
2549 if (DEBUG) {
2550 Slog.d(TAG, "No enabled IME found in the history");
2551 }
2552 return null;
2553 }
2554
satokdf31ae62011-01-15 06:19:44 +09002555 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002556 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2557 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2558 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002559 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2560 if (explicitlyEnabledSubtypes.size() == 0) {
2561 // If there are no explicitly enabled subtypes, applicable subtypes are
2562 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002563 InputMethodInfo ime = mMethodMap.get(imeId);
2564 // If IME is enabled and no subtypes are enabled, applicable subtypes
2565 // are enabled implicitly, so needs to treat them to be enabled.
2566 if (ime != null && ime.getSubtypes().size() > 0) {
2567 List<InputMethodSubtype> implicitlySelectedSubtypes =
satokf6cafb62011-01-17 16:29:02 +09002568 getApplicableSubtypesLocked(mRes, ime.getSubtypes());
satokdf31ae62011-01-15 06:19:44 +09002569 if (implicitlySelectedSubtypes != null) {
2570 final int N = implicitlySelectedSubtypes.size();
2571 for (int i = 0; i < N; ++i) {
2572 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2573 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2574 return subtypeHashCode;
2575 }
2576 }
2577 }
2578 }
2579 } else {
satokf6cafb62011-01-17 16:29:02 +09002580 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002581 if (s.equals(subtypeHashCode)) {
2582 // If both imeId and subtypeId are enabled, return subtypeId.
2583 return s;
2584 }
satok723a27e2010-11-11 14:58:11 +09002585 }
2586 }
2587 // If imeId was enabled but subtypeId was disabled.
2588 return NOT_A_SUBTYPE_ID_STR;
2589 }
2590 }
2591 // If both imeId and subtypeId are disabled, return null
2592 return null;
2593 }
2594
2595 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2596 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2597 final String subtypeHistoryStr = getSubtypeHistoryStr();
2598 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2599 return imsList;
2600 }
2601 mInputMethodSplitter.setString(subtypeHistoryStr);
2602 while (mInputMethodSplitter.hasNext()) {
2603 String nextImsStr = mInputMethodSplitter.next();
2604 mSubtypeSplitter.setString(nextImsStr);
2605 if (mSubtypeSplitter.hasNext()) {
2606 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2607 // The first element is ime id.
2608 String imeId = mSubtypeSplitter.next();
2609 while (mSubtypeSplitter.hasNext()) {
2610 subtypeId = mSubtypeSplitter.next();
2611 break;
2612 }
2613 imsList.add(new Pair<String, String>(imeId, subtypeId));
2614 }
2615 }
2616 return imsList;
2617 }
2618
2619 private String getSubtypeHistoryStr() {
2620 if (DEBUG) {
2621 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2622 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2623 }
2624 return Settings.Secure.getString(
2625 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2626 }
2627
2628 public void putSelectedInputMethod(String imeId) {
2629 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2630 }
2631
2632 public void putSelectedSubtype(int subtypeId) {
2633 Settings.Secure.putInt(
2634 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2635 }
satokd87c2592010-09-29 11:52:06 +09002636 }
2637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 @Override
2641 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2642 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2643 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2646 + Binder.getCallingPid()
2647 + ", uid=" + Binder.getCallingUid());
2648 return;
2649 }
2650
2651 IInputMethod method;
2652 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 synchronized (mMethodMap) {
2657 p.println("Current Input Method Manager state:");
2658 int N = mMethodList.size();
2659 p.println(" Input Methods:");
2660 for (int i=0; i<N; i++) {
2661 InputMethodInfo info = mMethodList.get(i);
2662 p.println(" InputMethod #" + i + ":");
2663 info.dump(p, " ");
2664 }
2665 p.println(" Clients:");
2666 for (ClientState ci : mClients.values()) {
2667 p.println(" Client " + ci + ":");
2668 p.println(" client=" + ci.client);
2669 p.println(" inputContext=" + ci.inputContext);
2670 p.println(" sessionRequested=" + ci.sessionRequested);
2671 p.println(" curSession=" + ci.curSession);
2672 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002673 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002675 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2676 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2678 + " mBoundToMethod=" + mBoundToMethod);
2679 p.println(" mCurToken=" + mCurToken);
2680 p.println(" mCurIntent=" + mCurIntent);
2681 method = mCurMethod;
2682 p.println(" mCurMethod=" + mCurMethod);
2683 p.println(" mEnabledSession=" + mEnabledSession);
2684 p.println(" mShowRequested=" + mShowRequested
2685 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2686 + " mShowForced=" + mShowForced
2687 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002688 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002690
Jeff Brownb88102f2010-09-08 11:49:43 -07002691 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 pw.flush();
2694 try {
2695 client.client.asBinder().dump(fd, args);
2696 } catch (RemoteException e) {
2697 p.println("Input method client dead: " + e);
2698 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002699 } else {
2700 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002702
Jeff Brownb88102f2010-09-08 11:49:43 -07002703 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 pw.flush();
2706 try {
2707 method.asBinder().dump(fd, args);
2708 } catch (RemoteException e) {
2709 p.println("Input method service dead: " + e);
2710 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002711 } else {
2712 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 }
2714 }
2715}