blob: 5ae8394e64eef07bf46c15fcc268c644ad0203cb [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);
satok4e4569d2010-11-19 18:45:53 +0900121 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
122 private static final String SUBTYPE_MODE_VOICE = "voice";
123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800125 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900127 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700129 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 final IWindowManager mIWindowManager;
131 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 // All known input methods. mMethodMap also serves as the global
136 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900137 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
138 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 class SessionState {
141 final ClientState client;
142 final IInputMethod method;
143 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 @Override
146 public String toString() {
147 return "SessionState{uid " + client.uid + " pid " + client.pid
148 + " method " + Integer.toHexString(
149 System.identityHashCode(method))
150 + " session " + Integer.toHexString(
151 System.identityHashCode(session))
152 + "}";
153 }
154
155 SessionState(ClientState _client, IInputMethod _method,
156 IInputMethodSession _session) {
157 client = _client;
158 method = _method;
159 session = _session;
160 }
161 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 class ClientState {
164 final IInputMethodClient client;
165 final IInputContext inputContext;
166 final int uid;
167 final int pid;
168 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 boolean sessionRequested;
171 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 @Override
174 public String toString() {
175 return "ClientState{" + Integer.toHexString(
176 System.identityHashCode(this)) + " uid " + uid
177 + " pid " + pid + "}";
178 }
179
180 ClientState(IInputMethodClient _client, IInputContext _inputContext,
181 int _uid, int _pid) {
182 client = _client;
183 inputContext = _inputContext;
184 uid = _uid;
185 pid = _pid;
186 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
187 }
188 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 final HashMap<IBinder, ClientState> mClients
191 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700194 * Set once the system is ready to run third party code.
195 */
196 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800197
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 * Id of the currently selected input method.
200 */
201 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 /**
204 * The current binding sequence number, incremented every time there is
205 * a new bind performed.
206 */
207 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 /**
210 * The client that is currently bound to an input method.
211 */
212 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700215 * The last window token that gained focus.
216 */
217 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800218
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700219 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 * The input context last provided by the current client.
221 */
222 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 /**
225 * The attributes last provided by the current client.
226 */
227 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 /**
230 * The input method ID of the input method service that we are currently
231 * connected to or in the process of connecting to.
232 */
233 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 /**
satokab751aa2010-09-14 19:17:36 +0900236 * The current subtype of the current input method.
237 */
238 private InputMethodSubtype mCurrentSubtype;
239
satok4e4569d2010-11-19 18:45:53 +0900240 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900241 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
242 mShortcutInputMethodsAndSubtypes =
243 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900244
245 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 * Set to true if our ServiceConnection is currently actively bound to
247 * a service (whether or not we have gotten its IBinder back yet).
248 */
249 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 /**
252 * Set if the client has asked for the input method to be shown.
253 */
254 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 /**
257 * Set if we were explicitly told to show the input method.
258 */
259 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 /**
262 * Set if we were forced to be shown.
263 */
264 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 /**
267 * Set if we last told the input method to show itself.
268 */
269 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 /**
272 * The Intent used to connect to the current input method.
273 */
274 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 /**
277 * The token we have made for the currently active input method, to
278 * identify it in the future.
279 */
280 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
283 * If non-null, this is the input method service we are currently connected
284 * to.
285 */
286 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 /**
289 * Time that we last initiated a bind to the input method, to determine
290 * if we should try to disconnect and reconnect to it.
291 */
292 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
295 * Have we called mCurMethod.bindInput()?
296 */
297 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * Currently enabled session. Only touched by service thread, not
301 * protected by a lock.
302 */
303 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 /**
306 * True if the screen is on. The value is true initially.
307 */
308 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 AlertDialog.Builder mDialogBuilder;
311 AlertDialog mSwitchingDialog;
312 InputMethodInfo[] mIms;
313 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900314 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 class SettingsObserver extends ContentObserver {
317 SettingsObserver(Handler handler) {
318 super(handler);
319 ContentResolver resolver = mContext.getContentResolver();
320 resolver.registerContentObserver(Settings.Secure.getUriFor(
321 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900322 resolver.registerContentObserver(Settings.Secure.getUriFor(
323 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 @Override public void onChange(boolean selfChange) {
327 synchronized (mMethodMap) {
328 updateFromSettingsLocked();
329 }
330 }
331 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
334 @Override
335 public void onReceive(Context context, Intent intent) {
336 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
337 mScreenOn = true;
338 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
339 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700340 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
341 hideInputMethodMenu();
342 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800344 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 }
346
347 // Inform the current client of the change in active status
348 try {
349 if (mCurClient != null && mCurClient.client != null) {
350 mCurClient.client.setActive(mScreenOn);
351 }
352 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800353 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 + mCurClient.pid + " uid " + mCurClient.uid);
355 }
356 }
357 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800358
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800359 class MyPackageMonitor extends PackageMonitor {
360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800362 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800364 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
366 final int N = mMethodList.size();
367 if (curInputMethodId != null) {
368 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800369 InputMethodInfo imi = mMethodList.get(i);
370 if (imi.getId().equals(curInputMethodId)) {
371 for (String pkg : packages) {
372 if (imi.getPackageName().equals(pkg)) {
373 if (!doit) {
374 return true;
375 }
satok723a27e2010-11-11 14:58:11 +0900376 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800377 chooseNewDefaultIMELocked();
378 return true;
379 }
380 }
381 }
382 }
383 }
384 }
385 return false;
386 }
387
388 @Override
389 public void onSomePackagesChanged() {
390 synchronized (mMethodMap) {
391 InputMethodInfo curIm = null;
392 String curInputMethodId = Settings.Secure.getString(mContext
393 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
394 final int N = mMethodList.size();
395 if (curInputMethodId != null) {
396 for (int i=0; i<N; i++) {
397 InputMethodInfo imi = mMethodList.get(i);
398 if (imi.getId().equals(curInputMethodId)) {
399 curIm = imi;
400 }
401 int change = isPackageDisappearing(imi.getPackageName());
402 if (change == PACKAGE_TEMPORARY_CHANGE
403 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800404 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800405 + imi.getComponent());
406 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 }
408 }
409 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800410
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800411 buildInputMethodListLocked(mMethodList, mMethodMap);
412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800414
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800415 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800416 int change = isPackageDisappearing(curIm.getPackageName());
417 if (change == PACKAGE_TEMPORARY_CHANGE
418 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800419 ServiceInfo si = null;
420 try {
421 si = mContext.getPackageManager().getServiceInfo(
422 curIm.getComponent(), 0);
423 } catch (PackageManager.NameNotFoundException ex) {
424 }
425 if (si == null) {
426 // Uh oh, current input method is no longer around!
427 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800428 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satokca830212011-01-13 21:15:04 +0900429 mStatusBar.setIMEButtonVisible(mCurToken, false);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800430 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800431 changed = true;
432 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800433 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900434 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800435 }
436 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800437 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800438 }
satokab751aa2010-09-14 19:17:36 +0900439
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800440 if (curIm == null) {
441 // We currently don't have a default input method... is
442 // one now available?
443 changed = chooseNewDefaultIMELocked();
444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800445
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800446 if (changed) {
447 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 }
449 }
450 }
451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 class MethodCallback extends IInputMethodCallback.Stub {
454 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 MethodCallback(IInputMethod method) {
457 mMethod = method;
458 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 public void finishedEvent(int seq, boolean handled) throws RemoteException {
461 }
462
463 public void sessionCreated(IInputMethodSession session) throws RemoteException {
464 onSessionCreated(mMethod, session);
465 }
466 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800467
Joe Onorato089de882010-04-12 08:18:45 -0700468 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800470 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 mHandler = new Handler(this);
472 mIWindowManager = IWindowManager.Stub.asInterface(
473 ServiceManager.getService(Context.WINDOW_SERVICE));
474 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
475 public void executeMessage(Message msg) {
476 handleMessage(msg);
477 }
478 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800479
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800480 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 IntentFilter screenOnOffFilt = new IntentFilter();
483 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
484 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700485 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800487
satok913a8922010-08-26 21:53:41 +0900488 mStatusBar = statusBar;
489 statusBar.setIconVisibility("ime", false);
490
satokd87c2592010-09-29 11:52:06 +0900491 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900492 mSettings = new InputMethodSettings(
493 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900495 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496
satokd87c2592010-09-29 11:52:06 +0900497 if (TextUtils.isEmpty(Settings.Secure.getString(
498 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900500 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
502 try {
satokd87c2592010-09-29 11:52:06 +0900503 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 imi.getPackageName(), 0).getResources();
505 if (res.getBoolean(imi.getIsDefaultResourceId())) {
506 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800507 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 }
509 } catch (PackageManager.NameNotFoundException ex) {
510 } catch (Resources.NotFoundException ex) {
511 }
512 }
513 }
satokd87c2592010-09-29 11:52:06 +0900514 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800516 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900519 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 }
521 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 mSettingsObserver = new SettingsObserver(mHandler);
524 updateFromSettingsLocked();
525 }
526
527 @Override
528 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
529 throws RemoteException {
530 try {
531 return super.onTransact(code, data, reply, flags);
532 } catch (RuntimeException e) {
533 // The input method manager only throws security exceptions, so let's
534 // log all others.
535 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800536 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 }
538 throw e;
539 }
540 }
541
542 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700543 synchronized (mMethodMap) {
544 if (!mSystemReady) {
545 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700546 try {
547 startInputInnerLocked();
548 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800549 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700550 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700551 }
552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 public List<InputMethodInfo> getInputMethodList() {
556 synchronized (mMethodMap) {
557 return new ArrayList<InputMethodInfo>(mMethodList);
558 }
559 }
560
561 public List<InputMethodInfo> getEnabledInputMethodList() {
562 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900563 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
565 }
566
satok16331c82010-12-20 23:48:46 +0900567 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
568 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900569 synchronized (mMethodMap) {
satok884ef9a2010-11-18 10:39:46 +0900570 if (imi == null && mCurMethodId != null) {
571 imi = mMethodMap.get(mCurMethodId);
572 }
satok16331c82010-12-20 23:48:46 +0900573 final List<InputMethodSubtype> enabledSubtypes =
574 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
575 if (!allowsImplicitlySelectedSubtypes || enabledSubtypes.size() > 0) {
576 return enabledSubtypes;
577 } else {
satokdf31ae62011-01-15 06:19:44 +0900578 return getApplicableSubtypesLocked(mRes, imi.getSubtypes());
satok16331c82010-12-20 23:48:46 +0900579 }
satok67ddf9c2010-11-17 09:45:54 +0900580 }
581 }
582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 public void addClient(IInputMethodClient client,
584 IInputContext inputContext, int uid, int pid) {
585 synchronized (mMethodMap) {
586 mClients.put(client.asBinder(), new ClientState(client,
587 inputContext, uid, pid));
588 }
589 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 public void removeClient(IInputMethodClient client) {
592 synchronized (mMethodMap) {
593 mClients.remove(client.asBinder());
594 }
595 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 void executeOrSendMessage(IInterface target, Message msg) {
598 if (target.asBinder() instanceof Binder) {
599 mCaller.sendMessage(msg);
600 } else {
601 handleMessage(msg);
602 msg.recycle();
603 }
604 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800605
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700606 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800608 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 + mCurClient.client.asBinder());
610 if (mBoundToMethod) {
611 mBoundToMethod = false;
612 if (mCurMethod != null) {
613 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
614 MSG_UNBIND_INPUT, mCurMethod));
615 }
616 }
617 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
618 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
619 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 // Call setActive(false) on the old client
622 try {
623 mCurClient.client.setActive(false);
624 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800625 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 + mCurClient.pid + " uid " + mCurClient.uid);
627 }
628 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800629
The Android Open Source Project10592532009-03-18 17:39:46 -0700630 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 }
632 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 private int getImeShowFlags() {
635 int flags = 0;
636 if (mShowForced) {
637 flags |= InputMethod.SHOW_FORCED
638 | InputMethod.SHOW_EXPLICIT;
639 } else if (mShowExplicitlyRequested) {
640 flags |= InputMethod.SHOW_EXPLICIT;
641 }
642 return flags;
643 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 private int getAppShowFlags() {
646 int flags = 0;
647 if (mShowForced) {
648 flags |= InputMethodManager.SHOW_FORCED;
649 } else if (!mShowExplicitlyRequested) {
650 flags |= InputMethodManager.SHOW_IMPLICIT;
651 }
652 return flags;
653 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
656 if (!mBoundToMethod) {
657 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
658 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
659 mBoundToMethod = true;
660 }
661 final SessionState session = mCurClient.curSession;
662 if (initial) {
663 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
664 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
665 } else {
666 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
667 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
668 }
669 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800670 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800671 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 }
673 return needResult
674 ? new InputBindResult(session.session, mCurId, mCurSeq)
675 : null;
676 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 InputBindResult startInputLocked(IInputMethodClient client,
679 IInputContext inputContext, EditorInfo attribute,
680 boolean initial, boolean needResult) {
681 // If no method is currently selected, do nothing.
682 if (mCurMethodId == null) {
683 return mNoBinding;
684 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 ClientState cs = mClients.get(client.asBinder());
687 if (cs == null) {
688 throw new IllegalArgumentException("unknown client "
689 + client.asBinder());
690 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 try {
693 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
694 // Check with the window manager to make sure this client actually
695 // has a window with focus. If not, reject. This is thread safe
696 // because if the focus changes some time before or after, the
697 // next client receiving focus that has any interest in input will
698 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800699 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
701 return null;
702 }
703 } catch (RemoteException e) {
704 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 if (mCurClient != cs) {
707 // If the client is changing, we need to switch over to the new
708 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700709 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800710 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 + cs.client.asBinder());
712
713 // If the screen is on, inform the new client it is active
714 if (mScreenOn) {
715 try {
716 cs.client.setActive(mScreenOn);
717 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800718 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 + cs.pid + " uid " + cs.uid);
720 }
721 }
722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 // Bump up the sequence for this client and attach it.
725 mCurSeq++;
726 if (mCurSeq <= 0) mCurSeq = 1;
727 mCurClient = cs;
728 mCurInputContext = inputContext;
729 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 // Check if the input method is changing.
732 if (mCurId != null && mCurId.equals(mCurMethodId)) {
733 if (cs.curSession != null) {
734 // Fast case: if we are already connected to the input method,
735 // then just return it.
736 return attachNewInputLocked(initial, needResult);
737 }
738 if (mHaveConnection) {
739 if (mCurMethod != null) {
740 if (!cs.sessionRequested) {
741 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800742 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
744 MSG_CREATE_SESSION, mCurMethod,
745 new MethodCallback(mCurMethod)));
746 }
747 // Return to client, and we will get back with it when
748 // we have had a session made for it.
749 return new InputBindResult(null, mCurId, mCurSeq);
750 } else if (SystemClock.uptimeMillis()
751 < (mLastBindTime+TIME_TO_RECONNECT)) {
752 // In this case we have connected to the service, but
753 // don't yet have its interface. If it hasn't been too
754 // long since we did the connection, we'll return to
755 // the client and wait to get the service interface so
756 // we can report back. If it has been too long, we want
757 // to fall through so we can try a disconnect/reconnect
758 // to see if we can get back in touch with the service.
759 return new InputBindResult(null, mCurId, mCurSeq);
760 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800761 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
762 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 }
764 }
765 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800766
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700767 return startInputInnerLocked();
768 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800769
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700770 InputBindResult startInputInnerLocked() {
771 if (mCurMethodId == null) {
772 return mNoBinding;
773 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800774
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700775 if (!mSystemReady) {
776 // If the system is not yet ready, we shouldn't be running third
777 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700778 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700779 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 InputMethodInfo info = mMethodMap.get(mCurMethodId);
782 if (info == null) {
783 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
784 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800785
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700786 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
789 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700790 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
791 com.android.internal.R.string.input_method_binding_label);
792 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
793 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
795 mLastBindTime = SystemClock.uptimeMillis();
796 mHaveConnection = true;
797 mCurId = info.getId();
798 mCurToken = new Binder();
799 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800800 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 mIWindowManager.addWindowToken(mCurToken,
802 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
803 } catch (RemoteException e) {
804 }
805 return new InputBindResult(null, mCurId, mCurSeq);
806 } else {
807 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800808 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 + mCurIntent);
810 }
811 return null;
812 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 public InputBindResult startInput(IInputMethodClient client,
815 IInputContext inputContext, EditorInfo attribute,
816 boolean initial, boolean needResult) {
817 synchronized (mMethodMap) {
818 final long ident = Binder.clearCallingIdentity();
819 try {
820 return startInputLocked(client, inputContext, attribute,
821 initial, needResult);
822 } finally {
823 Binder.restoreCallingIdentity(ident);
824 }
825 }
826 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 public void finishInput(IInputMethodClient client) {
829 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 public void onServiceConnected(ComponentName name, IBinder service) {
832 synchronized (mMethodMap) {
833 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
834 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700835 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800836 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700837 unbindCurrentMethodLocked(false);
838 return;
839 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800840 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700841 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
842 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800844 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700845 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700847 MSG_CREATE_SESSION, mCurMethod,
848 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 }
850 }
851 }
852 }
853
854 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
855 synchronized (mMethodMap) {
856 if (mCurMethod != null && method != null
857 && mCurMethod.asBinder() == method.asBinder()) {
858 if (mCurClient != null) {
859 mCurClient.curSession = new SessionState(mCurClient,
860 method, session);
861 mCurClient.sessionRequested = false;
862 InputBindResult res = attachNewInputLocked(true, true);
863 if (res.method != null) {
864 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
865 MSG_BIND_METHOD, mCurClient.client, res));
866 }
867 }
868 }
869 }
870 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800871
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700872 void unbindCurrentMethodLocked(boolean reportToClient) {
873 if (mHaveConnection) {
874 mContext.unbindService(this);
875 mHaveConnection = false;
876 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800877
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700878 if (mCurToken != null) {
879 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800880 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700881 mIWindowManager.removeWindowToken(mCurToken);
882 } catch (RemoteException e) {
883 }
884 mCurToken = null;
885 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800886
The Android Open Source Project10592532009-03-18 17:39:46 -0700887 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700888 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800889
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700890 if (reportToClient && mCurClient != null) {
891 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
892 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
893 }
894 }
895
Devin Taylor0c33ed22010-02-23 13:26:46 -0600896 private void finishSession(SessionState sessionState) {
897 if (sessionState != null && sessionState.session != null) {
898 try {
899 sessionState.session.finishSession();
900 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700901 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600902 }
903 }
904 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800905
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700906 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 if (mCurMethod != null) {
908 for (ClientState cs : mClients.values()) {
909 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600910 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 cs.curSession = null;
912 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600913
914 finishSession(mEnabledSession);
915 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 mCurMethod = null;
917 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700918 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 public void onServiceDisconnected(ComponentName name) {
922 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800923 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 + " mCurIntent=" + mCurIntent);
925 if (mCurMethod != null && mCurIntent != null
926 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700927 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 // We consider this to be a new bind attempt, since the system
929 // should now try to restart the service for us.
930 mLastBindTime = SystemClock.uptimeMillis();
931 mShowRequested = mInputShown;
932 mInputShown = false;
933 if (mCurClient != null) {
934 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
935 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
936 }
937 }
938 }
939 }
940
941 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700942 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 long ident = Binder.clearCallingIdentity();
944 try {
945 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700946 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 return;
948 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 synchronized (mMethodMap) {
951 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800952 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700953 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800955 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700956 mStatusBar.setIcon("ime", packageName, iconId, 0);
957 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 }
959 }
960 } finally {
961 Binder.restoreCallingIdentity(ident);
962 }
963 }
964
satok06487a52010-10-29 11:37:18 +0900965 public void setIMEButtonVisible(IBinder token, boolean visible) {
966 int uid = Binder.getCallingUid();
967 long ident = Binder.clearCallingIdentity();
968 try {
969 if (token == null || mCurToken != token) {
970 Slog.w(TAG, "Ignoring setIMEButtonVisible of uid " + uid + " token: " + token);
971 return;
972 }
973
974 synchronized (mMethodMap) {
satokcd7cd292010-11-20 15:46:23 +0900975 mStatusBar.setIMEButtonVisible(token, visible);
satok06487a52010-10-29 11:37:18 +0900976 }
977 } finally {
978 Binder.restoreCallingIdentity(ident);
979 }
980 }
981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700983 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
984 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
985 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
986 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +0900988 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +0900989 // There is no input method selected, try to choose new applicable input method.
990 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
991 id = Settings.Secure.getString(mContext.getContentResolver(),
992 Settings.Secure.DEFAULT_INPUT_METHOD);
993 }
994 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 try {
satokab751aa2010-09-14 19:17:36 +0900996 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800998 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -0700999 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001000 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 }
satokf3db1af2010-11-23 13:34:33 +09001002 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001003 } else {
1004 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001005 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001006 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 }
1008 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001009
satokab751aa2010-09-14 19:17:36 +09001010 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 InputMethodInfo info = mMethodMap.get(id);
1012 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001013 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 if (id.equals(mCurMethodId)) {
satokb66d2872010-11-10 01:04:04 +09001017 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
satokcd7cd292010-11-20 15:46:23 +09001018 InputMethodSubtype subtype = null;
satokb66d2872010-11-10 01:04:04 +09001019 if (subtypeId >= 0 && subtypeId < subtypes.size()) {
satokcd7cd292010-11-20 15:46:23 +09001020 subtype = subtypes.get(subtypeId);
1021 }
1022 if (subtype != mCurrentSubtype) {
1023 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001024 if (subtype != null) {
1025 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1026 }
satokcd7cd292010-11-20 15:46:23 +09001027 if (mCurMethod != null) {
1028 try {
satokcd7cd292010-11-20 15:46:23 +09001029 if (mInputShown) {
1030 // If mInputShown is false, there is no IME button on the
1031 // system bar.
1032 // Thus there is no need to make it invisible explicitly.
1033 mStatusBar.setIMEButtonVisible(mCurToken, true);
satokab751aa2010-09-14 19:17:36 +09001034 }
satokcd7cd292010-11-20 15:46:23 +09001035 // If subtype is null, try to find the most applicable one from
1036 // getCurrentInputMethodSubtype.
1037 if (subtype == null) {
1038 subtype = getCurrentInputMethodSubtype();
1039 }
1040 mCurMethod.changeInputMethodSubtype(subtype);
1041 } catch (RemoteException e) {
1042 return;
satokab751aa2010-09-14 19:17:36 +09001043 }
1044 }
1045 }
1046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 return;
1048 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 final long ident = Binder.clearCallingIdentity();
1051 try {
satokab751aa2010-09-14 19:17:36 +09001052 // Set a subtype to this input method.
1053 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001054 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1055 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1056 // because mCurMethodId is stored as a history in
1057 // setSelectedInputMethodAndSubtypeLocked().
1058 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059
1060 if (ActivityManagerNative.isSystemReady()) {
1061 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001062 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 intent.putExtra("input_method_id", id);
1064 mContext.sendBroadcast(intent);
1065 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001066 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 } finally {
1068 Binder.restoreCallingIdentity(ident);
1069 }
1070 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001071
The Android Open Source Project4df24232009-03-05 14:34:35 -08001072 public boolean showSoftInput(IInputMethodClient client, int flags,
1073 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001074 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 long ident = Binder.clearCallingIdentity();
1076 try {
1077 synchronized (mMethodMap) {
1078 if (mCurClient == null || client == null
1079 || mCurClient.client.asBinder() != client.asBinder()) {
1080 try {
1081 // We need to check if this is the current client with
1082 // focus in the window manager, to allow this call to
1083 // be made before input is started in it.
1084 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001085 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001086 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
1088 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001089 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 }
1091 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001092
Joe Onorato8a9b2202010-02-26 18:56:32 -08001093 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001094 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 }
1096 } finally {
1097 Binder.restoreCallingIdentity(ident);
1098 }
1099 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001100
The Android Open Source Project4df24232009-03-05 14:34:35 -08001101 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 mShowRequested = true;
1103 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1104 mShowExplicitlyRequested = true;
1105 }
1106 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1107 mShowExplicitlyRequested = true;
1108 mShowForced = true;
1109 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001110
Dianne Hackborncc278702009-09-02 23:07:23 -07001111 if (!mSystemReady) {
1112 return false;
1113 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001114
The Android Open Source Project4df24232009-03-05 14:34:35 -08001115 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001117 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1118 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1119 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001121 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 } else if (mHaveConnection && SystemClock.uptimeMillis()
1123 < (mLastBindTime+TIME_TO_RECONNECT)) {
1124 // The client has asked to have the input method shown, but
1125 // we have been sitting here too long with a connection to the
1126 // service and no interface received, so let's disconnect/connect
1127 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001128 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 SystemClock.uptimeMillis()-mLastBindTime,1);
1130 mContext.unbindService(this);
1131 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001133
The Android Open Source Project4df24232009-03-05 14:34:35 -08001134 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001136
The Android Open Source Project4df24232009-03-05 14:34:35 -08001137 public boolean hideSoftInput(IInputMethodClient client, int flags,
1138 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001139 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 long ident = Binder.clearCallingIdentity();
1141 try {
1142 synchronized (mMethodMap) {
1143 if (mCurClient == null || client == null
1144 || mCurClient.client.asBinder() != client.asBinder()) {
1145 try {
1146 // We need to check if this is the current client with
1147 // focus in the window manager, to allow this call to
1148 // be made before input is started in it.
1149 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001150 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1151 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001152 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 }
1154 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001155 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 }
1157 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001158
Joe Onorato8a9b2202010-02-26 18:56:32 -08001159 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001160 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 }
1162 } finally {
1163 Binder.restoreCallingIdentity(ident);
1164 }
1165 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001166
The Android Open Source Project4df24232009-03-05 14:34:35 -08001167 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1169 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001170 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001172 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 }
1174 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001175 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001177 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001179 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001181 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1182 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1183 res = true;
1184 } else {
1185 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
1187 mInputShown = false;
1188 mShowRequested = false;
1189 mShowExplicitlyRequested = false;
1190 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001191 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001193
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001194 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1196 boolean first, int windowFlags) {
1197 long ident = Binder.clearCallingIdentity();
1198 try {
1199 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001200 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 + " viewHasFocus=" + viewHasFocus
1202 + " isTextEditor=" + isTextEditor
1203 + " softInputMode=#" + Integer.toHexString(softInputMode)
1204 + " first=" + first + " flags=#"
1205 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 if (mCurClient == null || client == null
1208 || mCurClient.client.asBinder() != client.asBinder()) {
1209 try {
1210 // We need to check if this is the current client with
1211 // focus in the window manager, to allow this call to
1212 // be made before input is started in it.
1213 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001214 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 return;
1216 }
1217 } catch (RemoteException e) {
1218 }
1219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001220
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001221 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001222 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001223 return;
1224 }
1225 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001226
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001227 // Should we auto-show the IME even if the caller has not
1228 // specified what should be done with it?
1229 // We only do this automatically if the window can resize
1230 // to accommodate the IME (so what the user sees will give
1231 // them good context without input information being obscured
1232 // by the IME) or if running on a large screen where there
1233 // is more room for the target window + IME.
1234 final boolean doAutoShow =
1235 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1236 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1237 || mRes.getConfiguration().isLayoutSizeAtLeast(
1238 Configuration.SCREENLAYOUT_SIZE_LARGE);
1239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1241 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001242 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1244 // There is no focus view, and this window will
1245 // be behind any soft input window, so hide the
1246 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001247 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001248 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001250 } else if (isTextEditor && doAutoShow && (softInputMode &
1251 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 // There is a focus view, and we are navigating forward
1253 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001254 // We only do this automatically if the window an resize
1255 // to accomodate the IME (so what the user sees will give
1256 // them good context without input information being obscured
1257 // by the IME) or if running on a large screen where there
1258 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001259 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001260 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 }
1262 break;
1263 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1264 // Do nothing.
1265 break;
1266 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1267 if ((softInputMode &
1268 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001269 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001270 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 }
1272 break;
1273 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001274 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001275 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 break;
1277 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1278 if ((softInputMode &
1279 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001280 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001281 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 }
1283 break;
1284 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001285 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001286 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 break;
1288 }
1289 }
1290 } finally {
1291 Binder.restoreCallingIdentity(ident);
1292 }
1293 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1296 synchronized (mMethodMap) {
1297 if (mCurClient == null || client == null
1298 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001299 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001300 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 }
1302
satok440aab52010-11-25 09:43:11 +09001303 // Always call subtype picker, because subtype picker is a superset of input method
1304 // picker.
satokab751aa2010-09-14 19:17:36 +09001305 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1306 }
1307 }
1308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001310 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1311 }
1312
1313 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1314 synchronized (mMethodMap) {
1315 if (subtype != null) {
1316 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1317 mMethodMap.get(id), subtype.hashCode()));
1318 } else {
1319 setInputMethod(token, id);
1320 }
1321 }
satokab751aa2010-09-14 19:17:36 +09001322 }
1323
satokb416a712010-11-25 20:42:14 +09001324 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001325 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001326 synchronized (mMethodMap) {
1327 if (mCurClient == null || client == null
1328 || mCurClient.client.asBinder() != client.asBinder()) {
1329 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1330 }
satok7fee71f2010-12-17 18:54:26 +09001331 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1332 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001333 }
1334 }
1335
satok735cf382010-11-11 20:40:09 +09001336 public boolean switchToLastInputMethod(IBinder token) {
1337 synchronized (mMethodMap) {
1338 Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1339 if (lastIme != null) {
1340 InputMethodInfo imi = mMethodMap.get(lastIme.first);
1341 if (imi != null) {
satok28203512010-11-24 11:06:49 +09001342 setInputMethodWithSubtypeId(token, lastIme.first, getSubtypeIdFromHashCode(
satok735cf382010-11-11 20:40:09 +09001343 imi, Integer.valueOf(lastIme.second)));
1344 return true;
1345 }
1346 }
1347 return false;
1348 }
1349 }
1350
satok28203512010-11-24 11:06:49 +09001351 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 synchronized (mMethodMap) {
1353 if (token == null) {
1354 if (mContext.checkCallingOrSelfPermission(
1355 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1356 != PackageManager.PERMISSION_GRANTED) {
1357 throw new SecurityException(
1358 "Using null token requires permission "
1359 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1360 }
1361 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001362 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1363 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 return;
1365 }
1366
1367 long ident = Binder.clearCallingIdentity();
1368 try {
satokab751aa2010-09-14 19:17:36 +09001369 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 } finally {
1371 Binder.restoreCallingIdentity(ident);
1372 }
1373 }
1374 }
1375
1376 public void hideMySoftInput(IBinder token, int flags) {
1377 synchronized (mMethodMap) {
1378 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001379 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1380 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 return;
1382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 long ident = Binder.clearCallingIdentity();
1384 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001385 hideCurrentInputLocked(flags, null);
1386 } finally {
1387 Binder.restoreCallingIdentity(ident);
1388 }
1389 }
1390 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001391
The Android Open Source Project4df24232009-03-05 14:34:35 -08001392 public void showMySoftInput(IBinder token, int flags) {
1393 synchronized (mMethodMap) {
1394 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001395 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1396 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001397 return;
1398 }
1399 long ident = Binder.clearCallingIdentity();
1400 try {
1401 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 } finally {
1403 Binder.restoreCallingIdentity(ident);
1404 }
1405 }
1406 }
1407
1408 void setEnabledSessionInMainThread(SessionState session) {
1409 if (mEnabledSession != session) {
1410 if (mEnabledSession != null) {
1411 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001412 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 mEnabledSession.method.setSessionEnabled(
1414 mEnabledSession.session, false);
1415 } catch (RemoteException e) {
1416 }
1417 }
1418 mEnabledSession = session;
1419 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001420 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 session.method.setSessionEnabled(
1422 session.session, true);
1423 } catch (RemoteException e) {
1424 }
1425 }
1426 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 public boolean handleMessage(Message msg) {
1429 HandlerCaller.SomeArgs args;
1430 switch (msg.what) {
1431 case MSG_SHOW_IM_PICKER:
1432 showInputMethodMenu();
1433 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001434
satokab751aa2010-09-14 19:17:36 +09001435 case MSG_SHOW_IM_SUBTYPE_PICKER:
1436 showInputMethodSubtypeMenu();
1437 return true;
1438
satok47a44912010-10-06 16:03:58 +09001439 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001440 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001441 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001442 return true;
1443
1444 case MSG_SHOW_IM_CONFIG:
1445 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001446 return true;
1447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 case MSG_UNBIND_INPUT:
1451 try {
1452 ((IInputMethod)msg.obj).unbindInput();
1453 } catch (RemoteException e) {
1454 // There is nothing interesting about the method dying.
1455 }
1456 return true;
1457 case MSG_BIND_INPUT:
1458 args = (HandlerCaller.SomeArgs)msg.obj;
1459 try {
1460 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1461 } catch (RemoteException e) {
1462 }
1463 return true;
1464 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001465 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001467 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1468 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 } catch (RemoteException e) {
1470 }
1471 return true;
1472 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001473 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001475 ((IInputMethod)args.arg1).hideSoftInput(0,
1476 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 } catch (RemoteException e) {
1478 }
1479 return true;
1480 case MSG_ATTACH_TOKEN:
1481 args = (HandlerCaller.SomeArgs)msg.obj;
1482 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001483 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1485 } catch (RemoteException e) {
1486 }
1487 return true;
1488 case MSG_CREATE_SESSION:
1489 args = (HandlerCaller.SomeArgs)msg.obj;
1490 try {
1491 ((IInputMethod)args.arg1).createSession(
1492 (IInputMethodCallback)args.arg2);
1493 } catch (RemoteException e) {
1494 }
1495 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 case MSG_START_INPUT:
1499 args = (HandlerCaller.SomeArgs)msg.obj;
1500 try {
1501 SessionState session = (SessionState)args.arg1;
1502 setEnabledSessionInMainThread(session);
1503 session.method.startInput((IInputContext)args.arg2,
1504 (EditorInfo)args.arg3);
1505 } catch (RemoteException e) {
1506 }
1507 return true;
1508 case MSG_RESTART_INPUT:
1509 args = (HandlerCaller.SomeArgs)msg.obj;
1510 try {
1511 SessionState session = (SessionState)args.arg1;
1512 setEnabledSessionInMainThread(session);
1513 session.method.restartInput((IInputContext)args.arg2,
1514 (EditorInfo)args.arg3);
1515 } catch (RemoteException e) {
1516 }
1517 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 case MSG_UNBIND_METHOD:
1522 try {
1523 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1524 } catch (RemoteException e) {
1525 // There is nothing interesting about the last client dying.
1526 }
1527 return true;
1528 case MSG_BIND_METHOD:
1529 args = (HandlerCaller.SomeArgs)msg.obj;
1530 try {
1531 ((IInputMethodClient)args.arg1).onBindMethod(
1532 (InputBindResult)args.arg2);
1533 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001534 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 }
1536 return true;
1537 }
1538 return false;
1539 }
1540
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001541 private boolean isSystemIme(InputMethodInfo inputMethod) {
1542 return (inputMethod.getServiceInfo().applicationInfo.flags
1543 & ApplicationInfo.FLAG_SYSTEM) != 0;
1544 }
1545
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001546 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001547 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001548 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001549 // We'd prefer to fall back on a system IME, since that is safer.
1550 int i=enabled.size();
1551 while (i > 0) {
1552 i--;
1553 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1554 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1555 break;
1556 }
1557 }
satokab751aa2010-09-14 19:17:36 +09001558 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001559 if (DEBUG) {
1560 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1561 }
satok723a27e2010-11-11 14:58:11 +09001562 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001563 return true;
1564 }
1565
1566 return false;
1567 }
1568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1570 HashMap<String, InputMethodInfo> map) {
1571 list.clear();
1572 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001575 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001576 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1577 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1578 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1579 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580
1581 List<ResolveInfo> services = pm.queryIntentServices(
1582 new Intent(InputMethod.SERVICE_INTERFACE),
1583 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 for (int i = 0; i < services.size(); ++i) {
1586 ResolveInfo ri = services.get(i);
1587 ServiceInfo si = ri.serviceInfo;
1588 ComponentName compName = new ComponentName(si.packageName, si.name);
1589 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1590 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001591 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 + ": it does not require the permission "
1593 + android.Manifest.permission.BIND_INPUT_METHOD);
1594 continue;
1595 }
1596
Joe Onorato8a9b2202010-02-26 18:56:32 -08001597 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598
1599 try {
1600 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1601 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001602 final String id = p.getId();
1603 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604
Amith Yamasanie861ec12010-03-24 21:39:27 -07001605 // System IMEs are enabled by default, unless there's a hard keyboard
1606 // and the system IME was explicitly disabled
1607 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1608 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001609 }
1610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001612 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001616 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001618 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 }
1620 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001621
1622 String defaultIme = Settings.Secure.getString(mContext
1623 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001624 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001625 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001626 updateFromSettingsLocked();
1627 }
1628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001632
satokab751aa2010-09-14 19:17:36 +09001633 private void showInputMethodMenu() {
1634 showInputMethodMenuInternal(false);
1635 }
1636
1637 private void showInputMethodSubtypeMenu() {
1638 showInputMethodMenuInternal(true);
1639 }
1640
satok217f5482010-12-15 05:19:19 +09001641 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001642 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001643 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001644 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1645 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001646 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001647 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001648 }
satok217f5482010-12-15 05:19:19 +09001649 mContext.startActivity(intent);
1650 }
1651
1652 private void showConfigureInputMethods() {
1653 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1654 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1655 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1656 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001657 mContext.startActivity(intent);
1658 }
1659
satokab751aa2010-09-14 19:17:36 +09001660 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001661 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 String lastInputMethodId = Settings.Secure.getString(context
1668 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001669 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001670 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001671
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001672 synchronized (mMethodMap) {
satok7f35c8c2010-10-07 21:13:11 +09001673 final List<Pair<InputMethodInfo, ArrayList<String>>> immis =
satok67ddf9c2010-11-17 09:45:54 +09001674 mSettings.getEnabledInputMethodAndSubtypeHashCodeListLocked();
satok16331c82010-12-20 23:48:46 +09001675 int N = immis.size();
1676
1677 // Add applicable subtypes if no subtype for each IME is enabled.
1678 for (int i = 0; i < N; ++i) {
1679 InputMethodInfo imi = immis.get(i).first;
1680 ArrayList<String> subtypes = immis.get(i).second;
1681 if (subtypes != null && subtypes.size() == 0) {
1682 ArrayList<InputMethodSubtype> applicableSubtypes =
satokdf31ae62011-01-15 06:19:44 +09001683 getApplicableSubtypesLocked(mRes, imi.getSubtypes());
satok16331c82010-12-20 23:48:46 +09001684 final int numSubtypes = applicableSubtypes.size();
1685 for (int j = 0; j < numSubtypes; ++j) {
1686 subtypes.add(String.valueOf(applicableSubtypes.get(j).hashCode()));
1687 }
1688 }
1689 }
1690
satok7f35c8c2010-10-07 21:13:11 +09001691 ArrayList<Integer> subtypeIds = new ArrayList<Integer>();
1692
1693 if (immis == null || immis.size() == 0) {
1694 return;
1695 }
1696
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001697 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698
satok913a8922010-08-26 21:53:41 +09001699
satokab751aa2010-09-14 19:17:36 +09001700 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1701 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001702
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001703 for (int i = 0; i < N; ++i) {
satok7f35c8c2010-10-07 21:13:11 +09001704 InputMethodInfo property = immis.get(i).first;
1705 final ArrayList<String> enabledSubtypeIds = immis.get(i).second;
1706 HashSet<String> enabledSubtypeSet = new HashSet<String>();
1707 for (String s : enabledSubtypeIds) {
1708 enabledSubtypeSet.add(s);
1709 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001710 if (property == null) {
1711 continue;
1712 }
satokab751aa2010-09-14 19:17:36 +09001713 ArrayList<InputMethodSubtype> subtypes = property.getSubtypes();
1714 CharSequence label = property.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001715 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokab751aa2010-09-14 19:17:36 +09001716 for (int j = 0; j < subtypes.size(); ++j) {
1717 InputMethodSubtype subtype = subtypes.get(j);
satok7f35c8c2010-10-07 21:13:11 +09001718 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1719 CharSequence title;
1720 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001721 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001722 if (nameResId != 0) {
1723 title = pm.getText(property.getPackageName(), nameResId,
1724 property.getServiceInfo().applicationInfo);
1725 } else {
1726 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001727 // TODO: Use more friendly Title and UI
1728 title = label + "," + (mode == null ? "" : mode) + ","
1729 + (language == null ? "" : language);
1730 }
1731 imMap.put(title, new Pair<InputMethodInfo, Integer>(property, j));
satokab751aa2010-09-14 19:17:36 +09001732 }
satokab751aa2010-09-14 19:17:36 +09001733 }
1734 } else {
1735 imMap.put(label,
1736 new Pair<InputMethodInfo, Integer>(property, NOT_A_SUBTYPE_ID));
1737 subtypeIds.add(0);
1738 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001739 }
satok913a8922010-08-26 21:53:41 +09001740
1741 N = imMap.size();
1742 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001743 mIms = new InputMethodInfo[N];
1744 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001745 int checkedItem = 0;
1746 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001747 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1748 mIms[i] = value.first;
1749 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001750 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001751 int subtypeId = mSubtypeIds[i];
1752 if ((subtypeId == NOT_A_SUBTYPE_ID)
1753 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1754 || (subtypeId == lastInputMethodSubtypeId)) {
1755 checkedItem = i;
1756 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 }
satokab751aa2010-09-14 19:17:36 +09001759
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001760 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1761 public void onClick(DialogInterface dialog, int which) {
1762 hideInputMethodMenu();
1763 }
1764 };
satokd87c2592010-09-29 11:52:06 +09001765
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001766 TypedArray a = context.obtainStyledAttributes(null,
1767 com.android.internal.R.styleable.DialogPreference,
1768 com.android.internal.R.attr.alertDialogStyle, 0);
1769 mDialogBuilder = new AlertDialog.Builder(context)
1770 .setTitle(com.android.internal.R.string.select_input_method)
1771 .setOnCancelListener(new OnCancelListener() {
1772 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001774 }
1775 })
1776 .setIcon(a.getDrawable(
1777 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1778 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001779
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001780 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1781 new AlertDialog.OnClickListener() {
1782 public void onClick(DialogInterface dialog, int which) {
1783 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001784 if (mIms == null || mIms.length <= which
1785 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001786 return;
1787 }
1788 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001789 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001790 hideInputMethodMenu();
1791 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001792 if ((subtypeId < 0)
1793 || (subtypeId >= im.getSubtypes().size())) {
1794 subtypeId = NOT_A_SUBTYPE_ID;
1795 }
1796 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001797 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001800 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801
satok7f35c8c2010-10-07 21:13:11 +09001802 if (showSubtypes) {
satok82beadf2010-12-27 19:03:06 +09001803 mDialogBuilder.setPositiveButton(
1804 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09001805 new DialogInterface.OnClickListener() {
1806 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09001807 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09001808 }
1809 });
1810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 mSwitchingDialog = mDialogBuilder.create();
1812 mSwitchingDialog.getWindow().setType(
1813 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1814 mSwitchingDialog.show();
1815 }
1816 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001819 synchronized (mMethodMap) {
1820 hideInputMethodMenuLocked();
1821 }
1822 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001823
The Android Open Source Project10592532009-03-18 17:39:46 -07001824 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001825 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826
The Android Open Source Project10592532009-03-18 17:39:46 -07001827 if (mSwitchingDialog != null) {
1828 mSwitchingDialog.dismiss();
1829 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001831
The Android Open Source Project10592532009-03-18 17:39:46 -07001832 mDialogBuilder = null;
1833 mItems = null;
1834 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 public boolean setInputMethodEnabled(String id, boolean enabled) {
1840 synchronized (mMethodMap) {
1841 if (mContext.checkCallingOrSelfPermission(
1842 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1843 != PackageManager.PERMISSION_GRANTED) {
1844 throw new SecurityException(
1845 "Requires permission "
1846 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1847 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 long ident = Binder.clearCallingIdentity();
1850 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001851 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 } finally {
1853 Binder.restoreCallingIdentity(ident);
1854 }
1855 }
1856 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001857
1858 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1859 // Make sure this is a valid input method.
1860 InputMethodInfo imm = mMethodMap.get(id);
1861 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001862 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001863 }
1864
satokd87c2592010-09-29 11:52:06 +09001865 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1866 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001867
satokd87c2592010-09-29 11:52:06 +09001868 if (enabled) {
1869 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1870 if (pair.first.equals(id)) {
1871 // We are enabling this input method, but it is already enabled.
1872 // Nothing to do. The previous state was enabled.
1873 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001874 }
1875 }
satokd87c2592010-09-29 11:52:06 +09001876 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1877 // Previous state was disabled.
1878 return false;
1879 } else {
1880 StringBuilder builder = new StringBuilder();
1881 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1882 builder, enabledInputMethodsList, id)) {
1883 // Disabled input method is currently selected, switch to another one.
1884 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1885 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001886 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
1887 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
1888 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09001889 }
1890 // Previous state was enabled.
1891 return true;
1892 } else {
1893 // We are disabling the input method but it is already disabled.
1894 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001895 return false;
1896 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001897 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001898 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001899
satok723a27e2010-11-11 14:58:11 +09001900 private void saveCurrentInputMethodAndSubtypeToHistory() {
1901 String subtypeId = NOT_A_SUBTYPE_ID_STR;
1902 if (mCurrentSubtype != null) {
1903 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
1904 }
1905 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
satokab751aa2010-09-14 19:17:36 +09001906 }
1907
satok723a27e2010-11-11 14:58:11 +09001908 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
1909 boolean setSubtypeOnly) {
1910 // Update the history of InputMethod and Subtype
1911 saveCurrentInputMethodAndSubtypeToHistory();
1912
1913 // Set Subtype here
1914 if (imi == null || subtypeId < 0) {
1915 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001916 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09001917 } else {
1918 final ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1919 if (subtypeId < subtypes.size()) {
1920 mSettings.putSelectedSubtype(subtypes.get(subtypeId).hashCode());
1921 mCurrentSubtype = subtypes.get(subtypeId);
1922 } else {
1923 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
1924 mCurrentSubtype = null;
1925 }
satokab751aa2010-09-14 19:17:36 +09001926 }
satok723a27e2010-11-11 14:58:11 +09001927
1928 if (!setSubtypeOnly) {
1929 // Set InputMethod here
1930 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
1931 }
1932 }
1933
1934 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
1935 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
1936 int lastSubtypeId = NOT_A_SUBTYPE_ID;
1937 // newDefaultIme is empty when there is no candidate for the selected IME.
1938 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
1939 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
1940 if (subtypeHashCode != null) {
1941 try {
1942 lastSubtypeId = getSubtypeIdFromHashCode(
1943 imi, Integer.valueOf(subtypeHashCode));
1944 } catch (NumberFormatException e) {
1945 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
1946 }
1947 }
1948 }
1949 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09001950 }
1951
1952 private int getSelectedInputMethodSubtypeId(String id) {
1953 InputMethodInfo imi = mMethodMap.get(id);
1954 if (imi == null) {
1955 return NOT_A_SUBTYPE_ID;
1956 }
satokab751aa2010-09-14 19:17:36 +09001957 int subtypeId;
1958 try {
1959 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
1960 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
1961 } catch (SettingNotFoundException e) {
1962 return NOT_A_SUBTYPE_ID;
1963 }
satok723a27e2010-11-11 14:58:11 +09001964 return getSubtypeIdFromHashCode(imi, subtypeId);
1965 }
1966
1967 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09001968 if (imi != null) {
1969 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1970 for (int i = 0; i < subtypes.size(); ++i) {
1971 InputMethodSubtype ims = subtypes.get(i);
1972 if (subtypeHashCode == ims.hashCode()) {
1973 return i;
1974 }
satokab751aa2010-09-14 19:17:36 +09001975 }
1976 }
1977 return NOT_A_SUBTYPE_ID;
1978 }
1979
satokdf31ae62011-01-15 06:19:44 +09001980 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
1981 Resources res, List<InputMethodSubtype> subtypes) {
1982 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09001983 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
1984 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
1985 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09001986 final int N = subtypes.size();
1987 boolean containsKeyboardSubtype = false;
1988 for (int i = 0; i < N; ++i) {
1989 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09001990 final String locale = subtype.getLocale();
1991 final String mode = subtype.getMode();
1992 // When system locale starts with subtype's locale, that subtype will be applicable
1993 // for system locale
1994 // For instance, it's clearly applicable for cases like system locale = en_US and
1995 // subtype = en, but it is not necessarily considered applicable for cases like system
1996 // locale = en and subtype = en_US.
1997 // We just call systemLocale.startsWith(locale) in this function because there is no
1998 // need to find applicable subtypes aggressively unlike
1999 // findLastResortApplicableSubtypeLocked.
2000 if (systemLocale.startsWith(locale)) {
2001 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2002 // If more applicable subtypes are contained, skip.
2003 if (applicableSubtype != null
2004 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2005 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002006 if (!containsKeyboardSubtype
2007 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2008 containsKeyboardSubtype = true;
2009 }
2010 }
2011 }
satok3da92232011-01-11 22:46:30 +09002012 ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2013 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002014 if (!containsKeyboardSubtype) {
2015 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002016 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002017 if (lastResortKeyboardSubtype != null) {
2018 applicableSubtypes.add(lastResortKeyboardSubtype);
2019 }
2020 }
2021 return applicableSubtypes;
2022 }
2023
satok4e4569d2010-11-19 18:45:53 +09002024 /**
2025 * If there are no selected subtypes, tries finding the most applicable one according to the
2026 * given locale.
2027 * @param subtypes this function will search the most applicable subtype in subtypes
2028 * @param mode subtypes will be filtered by mode
2029 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002030 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2031 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002032 * @return the most applicable subtypeId
2033 */
satokdf31ae62011-01-15 06:19:44 +09002034 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2035 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002036 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002037 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002038 return null;
satok8fbb1e82010-11-02 23:15:58 +09002039 }
satok4e4569d2010-11-19 18:45:53 +09002040 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002041 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002042 }
satok8fbb1e82010-11-02 23:15:58 +09002043 final String language = locale.substring(0, 2);
2044 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002045 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002046 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002047 final int N = subtypes.size();
2048 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002049 InputMethodSubtype subtype = subtypes.get(i);
2050 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002051 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2052 // and all subtypes with all modes can be candidates.
2053 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002054 if (firstMatchedModeSubtype == null) {
2055 firstMatchedModeSubtype = subtype;
2056 }
satok9ef02832010-11-04 21:17:48 +09002057 if (locale.equals(subtypeLocale)) {
2058 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002059 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002060 break;
2061 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2062 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002063 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002064 partialMatchFound = true;
2065 }
satok8fbb1e82010-11-02 23:15:58 +09002066 }
2067 }
2068
satok7599a7f2010-12-22 13:45:23 +09002069 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2070 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002071 }
2072
satok8fbb1e82010-11-02 23:15:58 +09002073 // The first subtype applicable to the system locale will be defined as the most applicable
2074 // subtype.
2075 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002076 if (applicableSubtype != null) {
2077 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2078 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2079 }
satok8fbb1e82010-11-02 23:15:58 +09002080 }
satokcd7cd292010-11-20 15:46:23 +09002081 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002082 }
2083
satok4e4569d2010-11-19 18:45:53 +09002084 // If there are no selected shortcuts, tries finding the most applicable ones.
2085 private Pair<InputMethodInfo, InputMethodSubtype>
2086 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2087 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2088 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002089 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002090 boolean foundInSystemIME = false;
2091
2092 // Search applicable subtype for each InputMethodInfo
2093 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002094 final String imiId = imi.getId();
2095 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2096 continue;
2097 }
satokcd7cd292010-11-20 15:46:23 +09002098 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002099 final List<InputMethodSubtype> enabledSubtypes =
2100 getEnabledInputMethodSubtypeList(imi, true);
2101 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002102 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002103 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002104 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002105 }
satokdf31ae62011-01-15 06:19:44 +09002106 // 2. Search by the system locale from enabledSubtypes.
2107 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002108 if (subtype == null) {
2109 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002110 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002111 }
satok7599a7f2010-12-22 13:45:23 +09002112 // 4. Search by the current subtype's locale from all subtypes.
2113 if (subtype == null && mCurrentSubtype != null) {
2114 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002115 mRes, imi.getSubtypes(), mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002116 }
2117 // 5. Search by the system locale from all subtypes.
2118 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002119 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002120 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002121 mRes, imi.getSubtypes(), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002122 }
satokcd7cd292010-11-20 15:46:23 +09002123 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002124 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002125 // The current input method is the most applicable IME.
2126 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002127 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002128 break;
satok7599a7f2010-12-22 13:45:23 +09002129 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002130 // The system input method is 2nd applicable IME.
2131 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002132 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002133 if ((imi.getServiceInfo().applicationInfo.flags
2134 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2135 foundInSystemIME = true;
2136 }
satok4e4569d2010-11-19 18:45:53 +09002137 }
2138 }
2139 }
2140 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002141 if (mostApplicableIMI != null) {
2142 Slog.w(TAG, "Most applicable shortcut input method was:"
2143 + mostApplicableIMI.getId());
2144 if (mostApplicableSubtype != null) {
2145 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2146 + "," + mostApplicableSubtype.getMode() + ","
2147 + mostApplicableSubtype.getLocale());
2148 }
2149 }
satok4e4569d2010-11-19 18:45:53 +09002150 }
satokcd7cd292010-11-20 15:46:23 +09002151 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002152 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002153 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002154 } else {
2155 return null;
2156 }
2157 }
2158
satokab751aa2010-09-14 19:17:36 +09002159 /**
2160 * @return Return the current subtype of this input method.
2161 */
2162 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002163 boolean subtypeIsSelected = false;
2164 try {
2165 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2166 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2167 } catch (SettingNotFoundException e) {
2168 }
satok3ef8b292010-11-23 06:06:29 +09002169 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002170 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002171 String lastInputMethodId = Settings.Secure.getString(
2172 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002173 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2174 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002175 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2176 if (imi != null) {
2177 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002178 // the most applicable subtype from explicitly or implicitly enabled
2179 // subtypes.
2180 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2181 getEnabledInputMethodSubtypeList(imi, true);
2182 // If there is only one explicitly or implicitly enabled subtype,
2183 // just returns it.
2184 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2185 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2186 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2187 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2188 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2189 SUBTYPE_MODE_KEYBOARD, null, true);
2190 if (mCurrentSubtype == null) {
2191 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2192 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2193 true);
2194 }
2195 }
satok4e4569d2010-11-19 18:45:53 +09002196 }
satokcd7cd292010-11-20 15:46:23 +09002197 } else {
satok3ef8b292010-11-23 06:06:29 +09002198 mCurrentSubtype =
2199 mMethodMap.get(lastInputMethodId).getSubtypes().get(subtypeId);
2200 }
satok8fbb1e82010-11-02 23:15:58 +09002201 }
satok3ef8b292010-11-23 06:06:29 +09002202 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002203 }
satokab751aa2010-09-14 19:17:36 +09002204 }
2205
satokf3db1af2010-11-23 13:34:33 +09002206 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2207 InputMethodSubtype subtype) {
2208 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2209 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2210 } else {
2211 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2212 subtypes.add(subtype);
2213 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2214 }
2215 }
2216
satok4e4569d2010-11-19 18:45:53 +09002217 // TODO: We should change the return type from List to List<Parcelable>
2218 public List getShortcutInputMethodsAndSubtypes() {
2219 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002220 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002221 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002222 // If there are no selected shortcut subtypes, the framework will try to find
2223 // the most applicable subtype from all subtypes whose mode is
2224 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002225 Pair<InputMethodInfo, InputMethodSubtype> info =
2226 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2227 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002228 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002229 ret.add(info.first);
2230 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002231 }
satok3da92232011-01-11 22:46:30 +09002232 return ret;
satokf3db1af2010-11-23 13:34:33 +09002233 }
satokf3db1af2010-11-23 13:34:33 +09002234 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2235 ret.add(imi);
2236 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2237 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002238 }
2239 }
satokf3db1af2010-11-23 13:34:33 +09002240 return ret;
satok4e4569d2010-11-19 18:45:53 +09002241 }
2242 }
2243
satokb66d2872010-11-10 01:04:04 +09002244 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2245 synchronized (mMethodMap) {
2246 if (subtype != null && mCurMethodId != null) {
2247 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2248 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2249 if (subtypeId != NOT_A_SUBTYPE_ID) {
2250 setInputMethodLocked(mCurMethodId, subtypeId);
2251 return true;
2252 }
2253 }
2254 return false;
2255 }
2256 }
2257
satokd87c2592010-09-29 11:52:06 +09002258 /**
2259 * Utility class for putting and getting settings for InputMethod
2260 * TODO: Move all putters and getters of settings to this class.
2261 */
2262 private static class InputMethodSettings {
2263 // The string for enabled input method is saved as follows:
2264 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2265 private static final char INPUT_METHOD_SEPARATER = ':';
2266 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002267 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002268 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2269
satok723a27e2010-11-11 14:58:11 +09002270 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002271 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2272
satokdf31ae62011-01-15 06:19:44 +09002273 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002274 private final ContentResolver mResolver;
2275 private final HashMap<String, InputMethodInfo> mMethodMap;
2276 private final ArrayList<InputMethodInfo> mMethodList;
2277
2278 private String mEnabledInputMethodsStrCache;
2279
2280 private static void buildEnabledInputMethodsSettingString(
2281 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2282 String id = pair.first;
2283 ArrayList<String> subtypes = pair.second;
2284 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002285 // Inputmethod and subtypes are saved in the settings as follows:
2286 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2287 for (String subtypeId: subtypes) {
2288 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002289 }
2290 }
2291
2292 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002293 Resources res, ContentResolver resolver,
2294 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2295 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002296 mResolver = resolver;
2297 mMethodMap = methodMap;
2298 mMethodList = methodList;
2299 }
2300
2301 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2302 return createEnabledInputMethodListLocked(
2303 getEnabledInputMethodsAndSubtypeListLocked());
2304 }
2305
satok7f35c8c2010-10-07 21:13:11 +09002306 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002307 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2308 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002309 getEnabledInputMethodsAndSubtypeListLocked());
2310 }
2311
satok67ddf9c2010-11-17 09:45:54 +09002312 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2313 InputMethodInfo imi) {
2314 List<Pair<String, ArrayList<String>>> imsList =
2315 getEnabledInputMethodsAndSubtypeListLocked();
2316 ArrayList<InputMethodSubtype> enabledSubtypes =
2317 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002318 if (imi != null) {
2319 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2320 InputMethodInfo info = mMethodMap.get(imsPair.first);
2321 if (info != null && info.getId().equals(imi.getId())) {
2322 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
2323 for (InputMethodSubtype ims: subtypes) {
2324 for (String s: imsPair.second) {
2325 if (String.valueOf(ims.hashCode()).equals(s)) {
2326 enabledSubtypes.add(ims);
2327 }
satok67ddf9c2010-11-17 09:45:54 +09002328 }
2329 }
satok884ef9a2010-11-18 10:39:46 +09002330 break;
satok67ddf9c2010-11-17 09:45:54 +09002331 }
satok67ddf9c2010-11-17 09:45:54 +09002332 }
2333 }
2334 return enabledSubtypes;
2335 }
2336
satokd87c2592010-09-29 11:52:06 +09002337 // At the initial boot, the settings for input methods are not set,
2338 // so we need to enable IME in that case.
2339 public void enableAllIMEsIfThereIsNoEnabledIME() {
2340 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2341 StringBuilder sb = new StringBuilder();
2342 final int N = mMethodList.size();
2343 for (int i = 0; i < N; i++) {
2344 InputMethodInfo imi = mMethodList.get(i);
2345 Slog.i(TAG, "Adding: " + imi.getId());
2346 if (i > 0) sb.append(':');
2347 sb.append(imi.getId());
2348 }
2349 putEnabledInputMethodsStr(sb.toString());
2350 }
2351 }
2352
2353 public List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
2354 ArrayList<Pair<String, ArrayList<String>>> imsList
2355 = new ArrayList<Pair<String, ArrayList<String>>>();
2356 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2357 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2358 return imsList;
2359 }
satok723a27e2010-11-11 14:58:11 +09002360 mInputMethodSplitter.setString(enabledInputMethodsStr);
2361 while (mInputMethodSplitter.hasNext()) {
2362 String nextImsStr = mInputMethodSplitter.next();
2363 mSubtypeSplitter.setString(nextImsStr);
2364 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002365 ArrayList<String> subtypeHashes = new ArrayList<String>();
2366 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002367 String imeId = mSubtypeSplitter.next();
2368 while (mSubtypeSplitter.hasNext()) {
2369 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002370 }
2371 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2372 }
2373 }
2374 return imsList;
2375 }
2376
2377 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2378 if (reloadInputMethodStr) {
2379 getEnabledInputMethodsStr();
2380 }
2381 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2382 // Add in the newly enabled input method.
2383 putEnabledInputMethodsStr(id);
2384 } else {
2385 putEnabledInputMethodsStr(
2386 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2387 }
2388 }
2389
2390 /**
2391 * Build and put a string of EnabledInputMethods with removing specified Id.
2392 * @return the specified id was removed or not.
2393 */
2394 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2395 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2396 boolean isRemoved = false;
2397 boolean needsAppendSeparator = false;
2398 for (Pair<String, ArrayList<String>> ims: imsList) {
2399 String curId = ims.first;
2400 if (curId.equals(id)) {
2401 // We are disabling this input method, and it is
2402 // currently enabled. Skip it to remove from the
2403 // new list.
2404 isRemoved = true;
2405 } else {
2406 if (needsAppendSeparator) {
2407 builder.append(INPUT_METHOD_SEPARATER);
2408 } else {
2409 needsAppendSeparator = true;
2410 }
2411 buildEnabledInputMethodsSettingString(builder, ims);
2412 }
2413 }
2414 if (isRemoved) {
2415 // Update the setting with the new list of input methods.
2416 putEnabledInputMethodsStr(builder.toString());
2417 }
2418 return isRemoved;
2419 }
2420
2421 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2422 List<Pair<String, ArrayList<String>>> imsList) {
2423 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2424 for (Pair<String, ArrayList<String>> ims: imsList) {
2425 InputMethodInfo info = mMethodMap.get(ims.first);
2426 if (info != null) {
2427 res.add(info);
2428 }
2429 }
2430 return res;
2431 }
2432
satok7f35c8c2010-10-07 21:13:11 +09002433 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002434 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002435 List<Pair<String, ArrayList<String>>> imsList) {
2436 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2437 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2438 for (Pair<String, ArrayList<String>> ims : imsList) {
2439 InputMethodInfo info = mMethodMap.get(ims.first);
2440 if (info != null) {
2441 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2442 }
2443 }
2444 return res;
2445 }
2446
satokd87c2592010-09-29 11:52:06 +09002447 private void putEnabledInputMethodsStr(String str) {
2448 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2449 mEnabledInputMethodsStrCache = str;
2450 }
2451
2452 private String getEnabledInputMethodsStr() {
2453 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2454 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002455 if (DEBUG) {
2456 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2457 }
satokd87c2592010-09-29 11:52:06 +09002458 return mEnabledInputMethodsStrCache;
2459 }
satok723a27e2010-11-11 14:58:11 +09002460
2461 private void saveSubtypeHistory(
2462 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2463 StringBuilder builder = new StringBuilder();
2464 boolean isImeAdded = false;
2465 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2466 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2467 newSubtypeId);
2468 isImeAdded = true;
2469 }
2470 for (Pair<String, String> ime: savedImes) {
2471 String imeId = ime.first;
2472 String subtypeId = ime.second;
2473 if (TextUtils.isEmpty(subtypeId)) {
2474 subtypeId = NOT_A_SUBTYPE_ID_STR;
2475 }
2476 if (isImeAdded) {
2477 builder.append(INPUT_METHOD_SEPARATER);
2478 } else {
2479 isImeAdded = true;
2480 }
2481 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2482 subtypeId);
2483 }
2484 // Remove the last INPUT_METHOD_SEPARATER
2485 putSubtypeHistoryStr(builder.toString());
2486 }
2487
2488 public void addSubtypeToHistory(String imeId, String subtypeId) {
2489 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2490 for (Pair<String, String> ime: subtypeHistory) {
2491 if (ime.first.equals(imeId)) {
2492 if (DEBUG) {
2493 Slog.v(TAG, "Subtype found in the history: " + imeId
2494 + ime.second);
2495 }
2496 // We should break here
2497 subtypeHistory.remove(ime);
2498 break;
2499 }
2500 }
2501 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2502 }
2503
2504 private void putSubtypeHistoryStr(String str) {
2505 if (DEBUG) {
2506 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2507 }
2508 Settings.Secure.putString(
2509 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2510 }
2511
2512 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2513 // Gets the first one from the history
2514 return getLastSubtypeForInputMethodLockedInternal(null);
2515 }
2516
2517 public String getLastSubtypeForInputMethodLocked(String imeId) {
2518 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2519 if (ime != null) {
2520 return ime.second;
2521 } else {
2522 return null;
2523 }
2524 }
2525
2526 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2527 List<Pair<String, ArrayList<String>>> enabledImes =
2528 getEnabledInputMethodsAndSubtypeListLocked();
2529 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2530 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2531 final String imeInTheHistory = imeAndSubtype.first;
2532 // If imeId is empty, returns the first IME and subtype in the history
2533 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2534 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002535 final String subtypeHashCode =
2536 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2537 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002538 if (!TextUtils.isEmpty(subtypeHashCode)) {
2539 if (DEBUG) {
2540 Slog.d(TAG, "Enabled subtype found in the history:" + subtypeHashCode);
2541 }
2542 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2543 }
2544 }
2545 }
2546 if (DEBUG) {
2547 Slog.d(TAG, "No enabled IME found in the history");
2548 }
2549 return null;
2550 }
2551
satokdf31ae62011-01-15 06:19:44 +09002552 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002553 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2554 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2555 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002556 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2557 if (explicitlyEnabledSubtypes.size() == 0) {
2558 // If there are no explicitly enabled subtypes, applicable subtypes are
2559 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002560 InputMethodInfo ime = mMethodMap.get(imeId);
2561 // If IME is enabled and no subtypes are enabled, applicable subtypes
2562 // are enabled implicitly, so needs to treat them to be enabled.
2563 if (ime != null && ime.getSubtypes().size() > 0) {
2564 List<InputMethodSubtype> implicitlySelectedSubtypes =
satokf6cafb62011-01-17 16:29:02 +09002565 getApplicableSubtypesLocked(mRes, ime.getSubtypes());
satokdf31ae62011-01-15 06:19:44 +09002566 if (implicitlySelectedSubtypes != null) {
2567 final int N = implicitlySelectedSubtypes.size();
2568 for (int i = 0; i < N; ++i) {
2569 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2570 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2571 return subtypeHashCode;
2572 }
2573 }
2574 }
2575 }
2576 } else {
satokf6cafb62011-01-17 16:29:02 +09002577 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002578 if (s.equals(subtypeHashCode)) {
2579 // If both imeId and subtypeId are enabled, return subtypeId.
2580 return s;
2581 }
satok723a27e2010-11-11 14:58:11 +09002582 }
2583 }
2584 // If imeId was enabled but subtypeId was disabled.
2585 return NOT_A_SUBTYPE_ID_STR;
2586 }
2587 }
2588 // If both imeId and subtypeId are disabled, return null
2589 return null;
2590 }
2591
2592 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2593 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2594 final String subtypeHistoryStr = getSubtypeHistoryStr();
2595 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2596 return imsList;
2597 }
2598 mInputMethodSplitter.setString(subtypeHistoryStr);
2599 while (mInputMethodSplitter.hasNext()) {
2600 String nextImsStr = mInputMethodSplitter.next();
2601 mSubtypeSplitter.setString(nextImsStr);
2602 if (mSubtypeSplitter.hasNext()) {
2603 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2604 // The first element is ime id.
2605 String imeId = mSubtypeSplitter.next();
2606 while (mSubtypeSplitter.hasNext()) {
2607 subtypeId = mSubtypeSplitter.next();
2608 break;
2609 }
2610 imsList.add(new Pair<String, String>(imeId, subtypeId));
2611 }
2612 }
2613 return imsList;
2614 }
2615
2616 private String getSubtypeHistoryStr() {
2617 if (DEBUG) {
2618 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2619 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2620 }
2621 return Settings.Secure.getString(
2622 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2623 }
2624
2625 public void putSelectedInputMethod(String imeId) {
2626 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2627 }
2628
2629 public void putSelectedSubtype(int subtypeId) {
2630 Settings.Secure.putInt(
2631 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2632 }
satokd87c2592010-09-29 11:52:06 +09002633 }
2634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637 @Override
2638 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2639 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2640 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2643 + Binder.getCallingPid()
2644 + ", uid=" + Binder.getCallingUid());
2645 return;
2646 }
2647
2648 IInputMethod method;
2649 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 synchronized (mMethodMap) {
2654 p.println("Current Input Method Manager state:");
2655 int N = mMethodList.size();
2656 p.println(" Input Methods:");
2657 for (int i=0; i<N; i++) {
2658 InputMethodInfo info = mMethodList.get(i);
2659 p.println(" InputMethod #" + i + ":");
2660 info.dump(p, " ");
2661 }
2662 p.println(" Clients:");
2663 for (ClientState ci : mClients.values()) {
2664 p.println(" Client " + ci + ":");
2665 p.println(" client=" + ci.client);
2666 p.println(" inputContext=" + ci.inputContext);
2667 p.println(" sessionRequested=" + ci.sessionRequested);
2668 p.println(" curSession=" + ci.curSession);
2669 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002670 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002672 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2673 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2675 + " mBoundToMethod=" + mBoundToMethod);
2676 p.println(" mCurToken=" + mCurToken);
2677 p.println(" mCurIntent=" + mCurIntent);
2678 method = mCurMethod;
2679 p.println(" mCurMethod=" + mCurMethod);
2680 p.println(" mEnabledSession=" + mEnabledSession);
2681 p.println(" mShowRequested=" + mShowRequested
2682 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2683 + " mShowForced=" + mShowForced
2684 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002685 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002687
Jeff Brownb88102f2010-09-08 11:49:43 -07002688 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 pw.flush();
2691 try {
2692 client.client.asBinder().dump(fd, args);
2693 } catch (RemoteException e) {
2694 p.println("Input method client dead: " + e);
2695 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002696 } else {
2697 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002699
Jeff Brownb88102f2010-09-08 11:49:43 -07002700 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 pw.flush();
2703 try {
2704 method.asBinder().dump(fd, args);
2705 } catch (RemoteException e) {
2706 p.println("Input method service dead: " + e);
2707 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002708 } else {
2709 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 }
2711 }
2712}