blob: b5cc5aa6f53c1835e11d2d39a3a7f3e4951b051f [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
satokbb4aa062011-01-19 21:40:27 +0900567 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
568 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
569 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
570 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
571 for (InputMethodInfo imi: getEnabledInputMethodList()) {
572 enabledInputMethodAndSubtypes.put(
573 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
574 }
575 return enabledInputMethodAndSubtypes;
576 }
577
578 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
579 boolean allowsImplicitlySelectedSubtypes) {
580 if (imi == null && mCurMethodId != null) {
581 imi = mMethodMap.get(mCurMethodId);
582 }
583 final List<InputMethodSubtype> enabledSubtypes =
584 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
585 if (!allowsImplicitlySelectedSubtypes || enabledSubtypes.size() > 0) {
586 return enabledSubtypes;
587 } else {
588 return getApplicableSubtypesLocked(mRes, getSubtypes(imi));
589 }
590 }
591
satok16331c82010-12-20 23:48:46 +0900592 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
593 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900594 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900595 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900596 }
597 }
598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 public void addClient(IInputMethodClient client,
600 IInputContext inputContext, int uid, int pid) {
601 synchronized (mMethodMap) {
602 mClients.put(client.asBinder(), new ClientState(client,
603 inputContext, uid, pid));
604 }
605 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 public void removeClient(IInputMethodClient client) {
608 synchronized (mMethodMap) {
609 mClients.remove(client.asBinder());
610 }
611 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 void executeOrSendMessage(IInterface target, Message msg) {
614 if (target.asBinder() instanceof Binder) {
615 mCaller.sendMessage(msg);
616 } else {
617 handleMessage(msg);
618 msg.recycle();
619 }
620 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800621
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700622 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800624 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 + mCurClient.client.asBinder());
626 if (mBoundToMethod) {
627 mBoundToMethod = false;
628 if (mCurMethod != null) {
629 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
630 MSG_UNBIND_INPUT, mCurMethod));
631 }
632 }
633 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
634 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
635 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 // Call setActive(false) on the old client
638 try {
639 mCurClient.client.setActive(false);
640 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800641 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 + mCurClient.pid + " uid " + mCurClient.uid);
643 }
644 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800645
The Android Open Source Project10592532009-03-18 17:39:46 -0700646 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 }
648 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 private int getImeShowFlags() {
651 int flags = 0;
652 if (mShowForced) {
653 flags |= InputMethod.SHOW_FORCED
654 | InputMethod.SHOW_EXPLICIT;
655 } else if (mShowExplicitlyRequested) {
656 flags |= InputMethod.SHOW_EXPLICIT;
657 }
658 return flags;
659 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 private int getAppShowFlags() {
662 int flags = 0;
663 if (mShowForced) {
664 flags |= InputMethodManager.SHOW_FORCED;
665 } else if (!mShowExplicitlyRequested) {
666 flags |= InputMethodManager.SHOW_IMPLICIT;
667 }
668 return flags;
669 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
672 if (!mBoundToMethod) {
673 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
674 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
675 mBoundToMethod = true;
676 }
677 final SessionState session = mCurClient.curSession;
678 if (initial) {
679 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
680 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
681 } else {
682 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
683 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
684 }
685 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800686 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800687 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 }
689 return needResult
690 ? new InputBindResult(session.session, mCurId, mCurSeq)
691 : null;
692 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 InputBindResult startInputLocked(IInputMethodClient client,
695 IInputContext inputContext, EditorInfo attribute,
696 boolean initial, boolean needResult) {
697 // If no method is currently selected, do nothing.
698 if (mCurMethodId == null) {
699 return mNoBinding;
700 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 ClientState cs = mClients.get(client.asBinder());
703 if (cs == null) {
704 throw new IllegalArgumentException("unknown client "
705 + client.asBinder());
706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 try {
709 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
710 // Check with the window manager to make sure this client actually
711 // has a window with focus. If not, reject. This is thread safe
712 // because if the focus changes some time before or after, the
713 // next client receiving focus that has any interest in input will
714 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800715 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
717 return null;
718 }
719 } catch (RemoteException e) {
720 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 if (mCurClient != cs) {
723 // If the client is changing, we need to switch over to the new
724 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700725 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800726 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 + cs.client.asBinder());
728
729 // If the screen is on, inform the new client it is active
730 if (mScreenOn) {
731 try {
732 cs.client.setActive(mScreenOn);
733 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800734 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 + cs.pid + " uid " + cs.uid);
736 }
737 }
738 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 // Bump up the sequence for this client and attach it.
741 mCurSeq++;
742 if (mCurSeq <= 0) mCurSeq = 1;
743 mCurClient = cs;
744 mCurInputContext = inputContext;
745 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 // Check if the input method is changing.
748 if (mCurId != null && mCurId.equals(mCurMethodId)) {
749 if (cs.curSession != null) {
750 // Fast case: if we are already connected to the input method,
751 // then just return it.
752 return attachNewInputLocked(initial, needResult);
753 }
754 if (mHaveConnection) {
755 if (mCurMethod != null) {
756 if (!cs.sessionRequested) {
757 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800758 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
760 MSG_CREATE_SESSION, mCurMethod,
761 new MethodCallback(mCurMethod)));
762 }
763 // Return to client, and we will get back with it when
764 // we have had a session made for it.
765 return new InputBindResult(null, mCurId, mCurSeq);
766 } else if (SystemClock.uptimeMillis()
767 < (mLastBindTime+TIME_TO_RECONNECT)) {
768 // In this case we have connected to the service, but
769 // don't yet have its interface. If it hasn't been too
770 // long since we did the connection, we'll return to
771 // the client and wait to get the service interface so
772 // we can report back. If it has been too long, we want
773 // to fall through so we can try a disconnect/reconnect
774 // to see if we can get back in touch with the service.
775 return new InputBindResult(null, mCurId, mCurSeq);
776 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800777 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
778 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 }
780 }
781 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800782
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700783 return startInputInnerLocked();
784 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800785
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700786 InputBindResult startInputInnerLocked() {
787 if (mCurMethodId == null) {
788 return mNoBinding;
789 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800790
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700791 if (!mSystemReady) {
792 // If the system is not yet ready, we shouldn't be running third
793 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700794 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700795 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 InputMethodInfo info = mMethodMap.get(mCurMethodId);
798 if (info == null) {
799 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
800 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800801
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700802 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
805 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700806 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
807 com.android.internal.R.string.input_method_binding_label);
808 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
809 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
811 mLastBindTime = SystemClock.uptimeMillis();
812 mHaveConnection = true;
813 mCurId = info.getId();
814 mCurToken = new Binder();
815 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800816 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 mIWindowManager.addWindowToken(mCurToken,
818 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
819 } catch (RemoteException e) {
820 }
821 return new InputBindResult(null, mCurId, mCurSeq);
822 } else {
823 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800824 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 + mCurIntent);
826 }
827 return null;
828 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 public InputBindResult startInput(IInputMethodClient client,
831 IInputContext inputContext, EditorInfo attribute,
832 boolean initial, boolean needResult) {
833 synchronized (mMethodMap) {
834 final long ident = Binder.clearCallingIdentity();
835 try {
836 return startInputLocked(client, inputContext, attribute,
837 initial, needResult);
838 } finally {
839 Binder.restoreCallingIdentity(ident);
840 }
841 }
842 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 public void finishInput(IInputMethodClient client) {
845 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 public void onServiceConnected(ComponentName name, IBinder service) {
848 synchronized (mMethodMap) {
849 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
850 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700851 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800852 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700853 unbindCurrentMethodLocked(false);
854 return;
855 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800856 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700857 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
858 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800860 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700861 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700863 MSG_CREATE_SESSION, mCurMethod,
864 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 }
866 }
867 }
868 }
869
870 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
871 synchronized (mMethodMap) {
872 if (mCurMethod != null && method != null
873 && mCurMethod.asBinder() == method.asBinder()) {
874 if (mCurClient != null) {
875 mCurClient.curSession = new SessionState(mCurClient,
876 method, session);
877 mCurClient.sessionRequested = false;
878 InputBindResult res = attachNewInputLocked(true, true);
879 if (res.method != null) {
880 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
881 MSG_BIND_METHOD, mCurClient.client, res));
882 }
883 }
884 }
885 }
886 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800887
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700888 void unbindCurrentMethodLocked(boolean reportToClient) {
889 if (mHaveConnection) {
890 mContext.unbindService(this);
891 mHaveConnection = false;
892 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800893
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700894 if (mCurToken != null) {
895 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800896 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700897 mIWindowManager.removeWindowToken(mCurToken);
898 } catch (RemoteException e) {
899 }
900 mCurToken = null;
901 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800902
The Android Open Source Project10592532009-03-18 17:39:46 -0700903 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700904 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800905
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700906 if (reportToClient && mCurClient != null) {
907 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
908 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
909 }
910 }
911
Devin Taylor0c33ed22010-02-23 13:26:46 -0600912 private void finishSession(SessionState sessionState) {
913 if (sessionState != null && sessionState.session != null) {
914 try {
915 sessionState.session.finishSession();
916 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700917 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600918 }
919 }
920 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800921
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700922 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 if (mCurMethod != null) {
924 for (ClientState cs : mClients.values()) {
925 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600926 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 cs.curSession = null;
928 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600929
930 finishSession(mEnabledSession);
931 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 mCurMethod = null;
933 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700934 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 public void onServiceDisconnected(ComponentName name) {
938 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800939 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 + " mCurIntent=" + mCurIntent);
941 if (mCurMethod != null && mCurIntent != null
942 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700943 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 // We consider this to be a new bind attempt, since the system
945 // should now try to restart the service for us.
946 mLastBindTime = SystemClock.uptimeMillis();
947 mShowRequested = mInputShown;
948 mInputShown = false;
949 if (mCurClient != null) {
950 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
951 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
952 }
953 }
954 }
955 }
956
957 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700958 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 long ident = Binder.clearCallingIdentity();
960 try {
961 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700962 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 return;
964 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 synchronized (mMethodMap) {
967 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800968 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700969 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800971 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700972 mStatusBar.setIcon("ime", packageName, iconId, 0);
973 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 }
975 }
976 } finally {
977 Binder.restoreCallingIdentity(ident);
978 }
979 }
980
satok06487a52010-10-29 11:37:18 +0900981 public void setIMEButtonVisible(IBinder token, boolean visible) {
982 int uid = Binder.getCallingUid();
983 long ident = Binder.clearCallingIdentity();
984 try {
985 if (token == null || mCurToken != token) {
986 Slog.w(TAG, "Ignoring setIMEButtonVisible of uid " + uid + " token: " + token);
987 return;
988 }
989
990 synchronized (mMethodMap) {
satokcd7cd292010-11-20 15:46:23 +0900991 mStatusBar.setIMEButtonVisible(token, visible);
satok06487a52010-10-29 11:37:18 +0900992 }
993 } finally {
994 Binder.restoreCallingIdentity(ident);
995 }
996 }
997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700999 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1000 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1001 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1002 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001004 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001005 // There is no input method selected, try to choose new applicable input method.
1006 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1007 id = Settings.Secure.getString(mContext.getContentResolver(),
1008 Settings.Secure.DEFAULT_INPUT_METHOD);
1009 }
1010 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 try {
satokab751aa2010-09-14 19:17:36 +09001012 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001014 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001015 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001016 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 }
satokf3db1af2010-11-23 13:34:33 +09001018 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001019 } else {
1020 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001021 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001022 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 }
1024 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001025
satokab751aa2010-09-14 19:17:36 +09001026 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 InputMethodInfo info = mMethodMap.get(id);
1028 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001029 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001033 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001034 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1035 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001036 }
1037 if (subtype != mCurrentSubtype) {
1038 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001039 if (subtype != null) {
1040 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1041 }
satokcd7cd292010-11-20 15:46:23 +09001042 if (mCurMethod != null) {
1043 try {
satokcd7cd292010-11-20 15:46:23 +09001044 if (mInputShown) {
1045 // If mInputShown is false, there is no IME button on the
1046 // system bar.
1047 // Thus there is no need to make it invisible explicitly.
1048 mStatusBar.setIMEButtonVisible(mCurToken, true);
satokab751aa2010-09-14 19:17:36 +09001049 }
satokcd7cd292010-11-20 15:46:23 +09001050 // If subtype is null, try to find the most applicable one from
1051 // getCurrentInputMethodSubtype.
1052 if (subtype == null) {
1053 subtype = getCurrentInputMethodSubtype();
1054 }
1055 mCurMethod.changeInputMethodSubtype(subtype);
1056 } catch (RemoteException e) {
1057 return;
satokab751aa2010-09-14 19:17:36 +09001058 }
1059 }
1060 }
1061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 return;
1063 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 final long ident = Binder.clearCallingIdentity();
1066 try {
satokab751aa2010-09-14 19:17:36 +09001067 // Set a subtype to this input method.
1068 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001069 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1070 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1071 // because mCurMethodId is stored as a history in
1072 // setSelectedInputMethodAndSubtypeLocked().
1073 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074
1075 if (ActivityManagerNative.isSystemReady()) {
1076 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001077 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 intent.putExtra("input_method_id", id);
1079 mContext.sendBroadcast(intent);
1080 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001081 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 } finally {
1083 Binder.restoreCallingIdentity(ident);
1084 }
1085 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001086
The Android Open Source Project4df24232009-03-05 14:34:35 -08001087 public boolean showSoftInput(IInputMethodClient client, int flags,
1088 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001089 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 long ident = Binder.clearCallingIdentity();
1091 try {
1092 synchronized (mMethodMap) {
1093 if (mCurClient == null || client == null
1094 || mCurClient.client.asBinder() != client.asBinder()) {
1095 try {
1096 // We need to check if this is the current client with
1097 // focus in the window manager, to allow this call to
1098 // be made before input is started in it.
1099 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001100 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001101 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 }
1103 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001104 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 }
1106 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001107
Joe Onorato8a9b2202010-02-26 18:56:32 -08001108 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001109 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 }
1111 } finally {
1112 Binder.restoreCallingIdentity(ident);
1113 }
1114 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001115
The Android Open Source Project4df24232009-03-05 14:34:35 -08001116 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 mShowRequested = true;
1118 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1119 mShowExplicitlyRequested = true;
1120 }
1121 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1122 mShowExplicitlyRequested = true;
1123 mShowForced = true;
1124 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001125
Dianne Hackborncc278702009-09-02 23:07:23 -07001126 if (!mSystemReady) {
1127 return false;
1128 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001129
The Android Open Source Project4df24232009-03-05 14:34:35 -08001130 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001132 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1133 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1134 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001136 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 } else if (mHaveConnection && SystemClock.uptimeMillis()
1138 < (mLastBindTime+TIME_TO_RECONNECT)) {
1139 // The client has asked to have the input method shown, but
1140 // we have been sitting here too long with a connection to the
1141 // service and no interface received, so let's disconnect/connect
1142 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001143 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 SystemClock.uptimeMillis()-mLastBindTime,1);
1145 mContext.unbindService(this);
1146 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1147 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001148
The Android Open Source Project4df24232009-03-05 14:34:35 -08001149 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001151
The Android Open Source Project4df24232009-03-05 14:34:35 -08001152 public boolean hideSoftInput(IInputMethodClient client, int flags,
1153 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001154 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 long ident = Binder.clearCallingIdentity();
1156 try {
1157 synchronized (mMethodMap) {
1158 if (mCurClient == null || client == null
1159 || mCurClient.client.asBinder() != client.asBinder()) {
1160 try {
1161 // We need to check if this is the current client with
1162 // focus in the window manager, to allow this call to
1163 // be made before input is started in it.
1164 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001165 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1166 + uid + ": " + client);
satok865b9772011-01-21 02:45:06 +09001167 mStatusBar.setIMEButtonVisible(mCurToken, false);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001168 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 }
1170 } catch (RemoteException e) {
satok865b9772011-01-21 02:45:06 +09001171 mStatusBar.setIMEButtonVisible(mCurToken, false);
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 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001175
Joe Onorato8a9b2202010-02-26 18:56:32 -08001176 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001177 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 }
1179 } finally {
1180 Binder.restoreCallingIdentity(ident);
1181 }
1182 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001183
The Android Open Source Project4df24232009-03-05 14:34:35 -08001184 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1186 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001187 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001189 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 }
1191 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001192 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001194 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001196 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001198 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1199 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1200 res = true;
1201 } else {
1202 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 }
1204 mInputShown = false;
1205 mShowRequested = false;
1206 mShowExplicitlyRequested = false;
1207 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001208 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001210
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001211 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1213 boolean first, int windowFlags) {
1214 long ident = Binder.clearCallingIdentity();
1215 try {
1216 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001217 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 + " viewHasFocus=" + viewHasFocus
1219 + " isTextEditor=" + isTextEditor
1220 + " softInputMode=#" + Integer.toHexString(softInputMode)
1221 + " first=" + first + " flags=#"
1222 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 if (mCurClient == null || client == null
1225 || mCurClient.client.asBinder() != client.asBinder()) {
1226 try {
1227 // We need to check if this is the current client with
1228 // focus in the window manager, to allow this call to
1229 // be made before input is started in it.
1230 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001231 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 return;
1233 }
1234 } catch (RemoteException e) {
1235 }
1236 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001237
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001238 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001239 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001240 return;
1241 }
1242 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001243
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001244 // Should we auto-show the IME even if the caller has not
1245 // specified what should be done with it?
1246 // We only do this automatically if the window can resize
1247 // to accommodate the IME (so what the user sees will give
1248 // them good context without input information being obscured
1249 // by the IME) or if running on a large screen where there
1250 // is more room for the target window + IME.
1251 final boolean doAutoShow =
1252 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1253 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1254 || mRes.getConfiguration().isLayoutSizeAtLeast(
1255 Configuration.SCREENLAYOUT_SIZE_LARGE);
1256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1258 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001259 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1261 // There is no focus view, and this window will
1262 // be behind any soft input window, so hide the
1263 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001264 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001265 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001267 } else if (isTextEditor && doAutoShow && (softInputMode &
1268 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 // There is a focus view, and we are navigating forward
1270 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001271 // We only do this automatically if the window an resize
1272 // to accomodate the IME (so what the user sees will give
1273 // them good context without input information being obscured
1274 // by the IME) or if running on a large screen where there
1275 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001276 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001277 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 }
1279 break;
1280 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1281 // Do nothing.
1282 break;
1283 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1284 if ((softInputMode &
1285 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001286 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001287 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 }
1289 break;
1290 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001291 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001292 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 break;
1294 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1295 if ((softInputMode &
1296 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001297 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001298 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 }
1300 break;
1301 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001302 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001303 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 break;
1305 }
1306 }
1307 } finally {
1308 Binder.restoreCallingIdentity(ident);
1309 }
1310 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1313 synchronized (mMethodMap) {
1314 if (mCurClient == null || client == null
1315 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001316 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001317 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 }
1319
satok440aab52010-11-25 09:43:11 +09001320 // Always call subtype picker, because subtype picker is a superset of input method
1321 // picker.
satokab751aa2010-09-14 19:17:36 +09001322 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1323 }
1324 }
1325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001327 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1328 }
1329
1330 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1331 synchronized (mMethodMap) {
1332 if (subtype != null) {
1333 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1334 mMethodMap.get(id), subtype.hashCode()));
1335 } else {
1336 setInputMethod(token, id);
1337 }
1338 }
satokab751aa2010-09-14 19:17:36 +09001339 }
1340
satokb416a712010-11-25 20:42:14 +09001341 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001342 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001343 synchronized (mMethodMap) {
1344 if (mCurClient == null || client == null
1345 || mCurClient.client.asBinder() != client.asBinder()) {
1346 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1347 }
satok7fee71f2010-12-17 18:54:26 +09001348 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1349 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001350 }
1351 }
1352
satok735cf382010-11-11 20:40:09 +09001353 public boolean switchToLastInputMethod(IBinder token) {
1354 synchronized (mMethodMap) {
1355 Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1356 if (lastIme != null) {
1357 InputMethodInfo imi = mMethodMap.get(lastIme.first);
1358 if (imi != null) {
satok28203512010-11-24 11:06:49 +09001359 setInputMethodWithSubtypeId(token, lastIme.first, getSubtypeIdFromHashCode(
satok735cf382010-11-11 20:40:09 +09001360 imi, Integer.valueOf(lastIme.second)));
1361 return true;
1362 }
1363 }
1364 return false;
1365 }
1366 }
1367
satok28203512010-11-24 11:06:49 +09001368 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 synchronized (mMethodMap) {
1370 if (token == null) {
1371 if (mContext.checkCallingOrSelfPermission(
1372 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1373 != PackageManager.PERMISSION_GRANTED) {
1374 throw new SecurityException(
1375 "Using null token requires permission "
1376 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1377 }
1378 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001379 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1380 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 return;
1382 }
1383
1384 long ident = Binder.clearCallingIdentity();
1385 try {
satokab751aa2010-09-14 19:17:36 +09001386 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 } finally {
1388 Binder.restoreCallingIdentity(ident);
1389 }
1390 }
1391 }
1392
1393 public void hideMySoftInput(IBinder token, int flags) {
1394 synchronized (mMethodMap) {
1395 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001396 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1397 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 return;
1399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 long ident = Binder.clearCallingIdentity();
1401 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001402 hideCurrentInputLocked(flags, null);
1403 } finally {
1404 Binder.restoreCallingIdentity(ident);
1405 }
1406 }
1407 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001408
The Android Open Source Project4df24232009-03-05 14:34:35 -08001409 public void showMySoftInput(IBinder token, int flags) {
1410 synchronized (mMethodMap) {
1411 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001412 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1413 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001414 return;
1415 }
1416 long ident = Binder.clearCallingIdentity();
1417 try {
1418 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 } finally {
1420 Binder.restoreCallingIdentity(ident);
1421 }
1422 }
1423 }
1424
1425 void setEnabledSessionInMainThread(SessionState session) {
1426 if (mEnabledSession != session) {
1427 if (mEnabledSession != null) {
1428 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001429 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 mEnabledSession.method.setSessionEnabled(
1431 mEnabledSession.session, false);
1432 } catch (RemoteException e) {
1433 }
1434 }
1435 mEnabledSession = session;
1436 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001437 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 session.method.setSessionEnabled(
1439 session.session, true);
1440 } catch (RemoteException e) {
1441 }
1442 }
1443 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 public boolean handleMessage(Message msg) {
1446 HandlerCaller.SomeArgs args;
1447 switch (msg.what) {
1448 case MSG_SHOW_IM_PICKER:
1449 showInputMethodMenu();
1450 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001451
satokab751aa2010-09-14 19:17:36 +09001452 case MSG_SHOW_IM_SUBTYPE_PICKER:
1453 showInputMethodSubtypeMenu();
1454 return true;
1455
satok47a44912010-10-06 16:03:58 +09001456 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001457 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001458 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001459 return true;
1460
1461 case MSG_SHOW_IM_CONFIG:
1462 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001463 return true;
1464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 case MSG_UNBIND_INPUT:
1468 try {
1469 ((IInputMethod)msg.obj).unbindInput();
1470 } catch (RemoteException e) {
1471 // There is nothing interesting about the method dying.
1472 }
1473 return true;
1474 case MSG_BIND_INPUT:
1475 args = (HandlerCaller.SomeArgs)msg.obj;
1476 try {
1477 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1478 } catch (RemoteException e) {
1479 }
1480 return true;
1481 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001482 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001484 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1485 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 } catch (RemoteException e) {
1487 }
1488 return true;
1489 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001490 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001492 ((IInputMethod)args.arg1).hideSoftInput(0,
1493 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 } catch (RemoteException e) {
1495 }
1496 return true;
1497 case MSG_ATTACH_TOKEN:
1498 args = (HandlerCaller.SomeArgs)msg.obj;
1499 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001500 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1502 } catch (RemoteException e) {
1503 }
1504 return true;
1505 case MSG_CREATE_SESSION:
1506 args = (HandlerCaller.SomeArgs)msg.obj;
1507 try {
1508 ((IInputMethod)args.arg1).createSession(
1509 (IInputMethodCallback)args.arg2);
1510 } catch (RemoteException e) {
1511 }
1512 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 case MSG_START_INPUT:
1516 args = (HandlerCaller.SomeArgs)msg.obj;
1517 try {
1518 SessionState session = (SessionState)args.arg1;
1519 setEnabledSessionInMainThread(session);
1520 session.method.startInput((IInputContext)args.arg2,
1521 (EditorInfo)args.arg3);
1522 } catch (RemoteException e) {
1523 }
1524 return true;
1525 case MSG_RESTART_INPUT:
1526 args = (HandlerCaller.SomeArgs)msg.obj;
1527 try {
1528 SessionState session = (SessionState)args.arg1;
1529 setEnabledSessionInMainThread(session);
1530 session.method.restartInput((IInputContext)args.arg2,
1531 (EditorInfo)args.arg3);
1532 } catch (RemoteException e) {
1533 }
1534 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 case MSG_UNBIND_METHOD:
1539 try {
1540 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1541 } catch (RemoteException e) {
1542 // There is nothing interesting about the last client dying.
1543 }
1544 return true;
1545 case MSG_BIND_METHOD:
1546 args = (HandlerCaller.SomeArgs)msg.obj;
1547 try {
1548 ((IInputMethodClient)args.arg1).onBindMethod(
1549 (InputBindResult)args.arg2);
1550 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001551 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 }
1553 return true;
1554 }
1555 return false;
1556 }
1557
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001558 private boolean isSystemIme(InputMethodInfo inputMethod) {
1559 return (inputMethod.getServiceInfo().applicationInfo.flags
1560 & ApplicationInfo.FLAG_SYSTEM) != 0;
1561 }
1562
Ken Wakasa586f0512011-01-20 22:31:01 +09001563 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1564 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1565 final int subtypeCount = imi.getSubtypeCount();
1566 for (int i = 0; i < subtypeCount; ++i) {
1567 subtypes.add(imi.getSubtypeAt(i));
1568 }
1569 return subtypes;
1570 }
1571
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001572 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001573 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001574 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001575 // We'd prefer to fall back on a system IME, since that is safer.
1576 int i=enabled.size();
1577 while (i > 0) {
1578 i--;
1579 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1580 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1581 break;
1582 }
1583 }
satokab751aa2010-09-14 19:17:36 +09001584 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001585 if (DEBUG) {
1586 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1587 }
satok723a27e2010-11-11 14:58:11 +09001588 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001589 return true;
1590 }
1591
1592 return false;
1593 }
1594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1596 HashMap<String, InputMethodInfo> map) {
1597 list.clear();
1598 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001601 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001602 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1603 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1604 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1605 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606
1607 List<ResolveInfo> services = pm.queryIntentServices(
1608 new Intent(InputMethod.SERVICE_INTERFACE),
1609 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 for (int i = 0; i < services.size(); ++i) {
1612 ResolveInfo ri = services.get(i);
1613 ServiceInfo si = ri.serviceInfo;
1614 ComponentName compName = new ComponentName(si.packageName, si.name);
1615 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1616 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001617 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 + ": it does not require the permission "
1619 + android.Manifest.permission.BIND_INPUT_METHOD);
1620 continue;
1621 }
1622
Joe Onorato8a9b2202010-02-26 18:56:32 -08001623 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624
1625 try {
1626 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1627 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001628 final String id = p.getId();
1629 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630
Amith Yamasanie861ec12010-03-24 21:39:27 -07001631 // System IMEs are enabled by default, unless there's a hard keyboard
1632 // and the system IME was explicitly disabled
1633 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1634 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001635 }
1636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001638 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001642 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001644 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 }
1646 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001647
1648 String defaultIme = Settings.Secure.getString(mContext
1649 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001650 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001651 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001652 updateFromSettingsLocked();
1653 }
1654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001658
satokab751aa2010-09-14 19:17:36 +09001659 private void showInputMethodMenu() {
1660 showInputMethodMenuInternal(false);
1661 }
1662
1663 private void showInputMethodSubtypeMenu() {
1664 showInputMethodMenuInternal(true);
1665 }
1666
satok217f5482010-12-15 05:19:19 +09001667 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001668 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001669 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001670 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1671 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001672 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001673 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001674 }
satok217f5482010-12-15 05:19:19 +09001675 mContext.startActivity(intent);
1676 }
1677
1678 private void showConfigureInputMethods() {
1679 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1680 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1681 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1682 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001683 mContext.startActivity(intent);
1684 }
1685
satokab751aa2010-09-14 19:17:36 +09001686 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001687 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 String lastInputMethodId = Settings.Secure.getString(context
1694 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001695 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001696 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001697
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001698 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09001699 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
1700 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001701 if (immis == null || immis.size() == 0) {
1702 return;
1703 }
1704
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001705 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706
satokab751aa2010-09-14 19:17:36 +09001707 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1708 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001709
satokbb4aa062011-01-19 21:40:27 +09001710 for (InputMethodInfo imi: immis.keySet()) {
1711 if (imi == null) continue;
1712 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09001713 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09001714 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
1715 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09001716 }
satokbb4aa062011-01-19 21:40:27 +09001717 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
1718 CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001719 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09001720 final int subtypeCount = imi.getSubtypeCount();
Ken Wakasa586f0512011-01-20 22:31:01 +09001721 for (int j = 0; j < subtypeCount; ++j) {
satokbb4aa062011-01-19 21:40:27 +09001722 InputMethodSubtype subtype = imi.getSubtypeAt(j);
satok7f35c8c2010-10-07 21:13:11 +09001723 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1724 CharSequence title;
1725 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001726 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001727 if (nameResId != 0) {
satokbb4aa062011-01-19 21:40:27 +09001728 title = pm.getText(imi.getPackageName(), nameResId,
1729 imi.getServiceInfo().applicationInfo);
satok7f35c8c2010-10-07 21:13:11 +09001730 } else {
1731 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001732 // TODO: Use more friendly Title and UI
1733 title = label + "," + (mode == null ? "" : mode) + ","
1734 + (language == null ? "" : language);
1735 }
satokbb4aa062011-01-19 21:40:27 +09001736 imMap.put(title, new Pair<InputMethodInfo, Integer>(imi, j));
satokab751aa2010-09-14 19:17:36 +09001737 }
satokab751aa2010-09-14 19:17:36 +09001738 }
1739 } else {
1740 imMap.put(label,
satokbb4aa062011-01-19 21:40:27 +09001741 new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID));
satokab751aa2010-09-14 19:17:36 +09001742 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001743 }
satok913a8922010-08-26 21:53:41 +09001744
satokbb4aa062011-01-19 21:40:27 +09001745 final int N = imMap.size();
satok913a8922010-08-26 21:53:41 +09001746 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001747 mIms = new InputMethodInfo[N];
1748 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001749 int checkedItem = 0;
1750 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001751 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1752 mIms[i] = value.first;
1753 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001754 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001755 int subtypeId = mSubtypeIds[i];
1756 if ((subtypeId == NOT_A_SUBTYPE_ID)
1757 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1758 || (subtypeId == lastInputMethodSubtypeId)) {
1759 checkedItem = i;
1760 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 }
satokab751aa2010-09-14 19:17:36 +09001763
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001764 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1765 public void onClick(DialogInterface dialog, int which) {
1766 hideInputMethodMenu();
1767 }
1768 };
satokd87c2592010-09-29 11:52:06 +09001769
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001770 TypedArray a = context.obtainStyledAttributes(null,
1771 com.android.internal.R.styleable.DialogPreference,
1772 com.android.internal.R.attr.alertDialogStyle, 0);
1773 mDialogBuilder = new AlertDialog.Builder(context)
1774 .setTitle(com.android.internal.R.string.select_input_method)
1775 .setOnCancelListener(new OnCancelListener() {
1776 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001778 }
1779 })
1780 .setIcon(a.getDrawable(
1781 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1782 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001783
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001784 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1785 new AlertDialog.OnClickListener() {
1786 public void onClick(DialogInterface dialog, int which) {
1787 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001788 if (mIms == null || mIms.length <= which
1789 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001790 return;
1791 }
1792 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001793 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001794 hideInputMethodMenu();
1795 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001796 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09001797 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09001798 subtypeId = NOT_A_SUBTYPE_ID;
1799 }
1800 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001801 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001804 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805
satok7f35c8c2010-10-07 21:13:11 +09001806 if (showSubtypes) {
satok82beadf2010-12-27 19:03:06 +09001807 mDialogBuilder.setPositiveButton(
1808 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09001809 new DialogInterface.OnClickListener() {
1810 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09001811 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09001812 }
1813 });
1814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 mSwitchingDialog = mDialogBuilder.create();
1816 mSwitchingDialog.getWindow().setType(
1817 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1818 mSwitchingDialog.show();
1819 }
1820 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001823 synchronized (mMethodMap) {
1824 hideInputMethodMenuLocked();
1825 }
1826 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001827
The Android Open Source Project10592532009-03-18 17:39:46 -07001828 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001829 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830
The Android Open Source Project10592532009-03-18 17:39:46 -07001831 if (mSwitchingDialog != null) {
1832 mSwitchingDialog.dismiss();
1833 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001835
The Android Open Source Project10592532009-03-18 17:39:46 -07001836 mDialogBuilder = null;
1837 mItems = null;
1838 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 public boolean setInputMethodEnabled(String id, boolean enabled) {
1844 synchronized (mMethodMap) {
1845 if (mContext.checkCallingOrSelfPermission(
1846 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1847 != PackageManager.PERMISSION_GRANTED) {
1848 throw new SecurityException(
1849 "Requires permission "
1850 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1851 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 long ident = Binder.clearCallingIdentity();
1854 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001855 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 } finally {
1857 Binder.restoreCallingIdentity(ident);
1858 }
1859 }
1860 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001861
1862 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1863 // Make sure this is a valid input method.
1864 InputMethodInfo imm = mMethodMap.get(id);
1865 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001866 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001867 }
1868
satokd87c2592010-09-29 11:52:06 +09001869 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1870 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001871
satokd87c2592010-09-29 11:52:06 +09001872 if (enabled) {
1873 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1874 if (pair.first.equals(id)) {
1875 // We are enabling this input method, but it is already enabled.
1876 // Nothing to do. The previous state was enabled.
1877 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001878 }
1879 }
satokd87c2592010-09-29 11:52:06 +09001880 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1881 // Previous state was disabled.
1882 return false;
1883 } else {
1884 StringBuilder builder = new StringBuilder();
1885 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1886 builder, enabledInputMethodsList, id)) {
1887 // Disabled input method is currently selected, switch to another one.
1888 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1889 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001890 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
1891 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
1892 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09001893 }
1894 // Previous state was enabled.
1895 return true;
1896 } else {
1897 // We are disabling the input method but it is already disabled.
1898 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001899 return false;
1900 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001901 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001902 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001903
satok723a27e2010-11-11 14:58:11 +09001904 private void saveCurrentInputMethodAndSubtypeToHistory() {
1905 String subtypeId = NOT_A_SUBTYPE_ID_STR;
1906 if (mCurrentSubtype != null) {
1907 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
1908 }
1909 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
satokab751aa2010-09-14 19:17:36 +09001910 }
1911
satok723a27e2010-11-11 14:58:11 +09001912 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
1913 boolean setSubtypeOnly) {
1914 // Update the history of InputMethod and Subtype
1915 saveCurrentInputMethodAndSubtypeToHistory();
1916
1917 // Set Subtype here
1918 if (imi == null || subtypeId < 0) {
1919 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001920 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09001921 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09001922 if (subtypeId < imi.getSubtypeCount()) {
1923 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
1924 mSettings.putSelectedSubtype(subtype.hashCode());
1925 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09001926 } else {
1927 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
1928 mCurrentSubtype = null;
1929 }
satokab751aa2010-09-14 19:17:36 +09001930 }
satok723a27e2010-11-11 14:58:11 +09001931
1932 if (!setSubtypeOnly) {
1933 // Set InputMethod here
1934 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
1935 }
1936 }
1937
1938 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
1939 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
1940 int lastSubtypeId = NOT_A_SUBTYPE_ID;
1941 // newDefaultIme is empty when there is no candidate for the selected IME.
1942 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
1943 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
1944 if (subtypeHashCode != null) {
1945 try {
1946 lastSubtypeId = getSubtypeIdFromHashCode(
1947 imi, Integer.valueOf(subtypeHashCode));
1948 } catch (NumberFormatException e) {
1949 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
1950 }
1951 }
1952 }
1953 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09001954 }
1955
1956 private int getSelectedInputMethodSubtypeId(String id) {
1957 InputMethodInfo imi = mMethodMap.get(id);
1958 if (imi == null) {
1959 return NOT_A_SUBTYPE_ID;
1960 }
satokab751aa2010-09-14 19:17:36 +09001961 int subtypeId;
1962 try {
1963 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
1964 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
1965 } catch (SettingNotFoundException e) {
1966 return NOT_A_SUBTYPE_ID;
1967 }
satok723a27e2010-11-11 14:58:11 +09001968 return getSubtypeIdFromHashCode(imi, subtypeId);
1969 }
1970
1971 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09001972 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09001973 final int subtypeCount = imi.getSubtypeCount();
1974 for (int i = 0; i < subtypeCount; ++i) {
1975 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09001976 if (subtypeHashCode == ims.hashCode()) {
1977 return i;
1978 }
satokab751aa2010-09-14 19:17:36 +09001979 }
1980 }
1981 return NOT_A_SUBTYPE_ID;
1982 }
1983
satokdf31ae62011-01-15 06:19:44 +09001984 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
1985 Resources res, List<InputMethodSubtype> subtypes) {
1986 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09001987 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
1988 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
1989 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09001990 final int N = subtypes.size();
1991 boolean containsKeyboardSubtype = false;
1992 for (int i = 0; i < N; ++i) {
1993 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09001994 final String locale = subtype.getLocale();
1995 final String mode = subtype.getMode();
1996 // When system locale starts with subtype's locale, that subtype will be applicable
1997 // for system locale
1998 // For instance, it's clearly applicable for cases like system locale = en_US and
1999 // subtype = en, but it is not necessarily considered applicable for cases like system
2000 // locale = en and subtype = en_US.
2001 // We just call systemLocale.startsWith(locale) in this function because there is no
2002 // need to find applicable subtypes aggressively unlike
2003 // findLastResortApplicableSubtypeLocked.
2004 if (systemLocale.startsWith(locale)) {
2005 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2006 // If more applicable subtypes are contained, skip.
2007 if (applicableSubtype != null
2008 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2009 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002010 if (!containsKeyboardSubtype
2011 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2012 containsKeyboardSubtype = true;
2013 }
2014 }
2015 }
satok3da92232011-01-11 22:46:30 +09002016 ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2017 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002018 if (!containsKeyboardSubtype) {
2019 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002020 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002021 if (lastResortKeyboardSubtype != null) {
2022 applicableSubtypes.add(lastResortKeyboardSubtype);
2023 }
2024 }
2025 return applicableSubtypes;
2026 }
2027
satok4e4569d2010-11-19 18:45:53 +09002028 /**
2029 * If there are no selected subtypes, tries finding the most applicable one according to the
2030 * given locale.
2031 * @param subtypes this function will search the most applicable subtype in subtypes
2032 * @param mode subtypes will be filtered by mode
2033 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002034 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2035 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002036 * @return the most applicable subtypeId
2037 */
satokdf31ae62011-01-15 06:19:44 +09002038 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2039 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002040 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002041 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002042 return null;
satok8fbb1e82010-11-02 23:15:58 +09002043 }
satok4e4569d2010-11-19 18:45:53 +09002044 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002045 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002046 }
satok8fbb1e82010-11-02 23:15:58 +09002047 final String language = locale.substring(0, 2);
2048 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002049 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002050 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002051 final int N = subtypes.size();
2052 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002053 InputMethodSubtype subtype = subtypes.get(i);
2054 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002055 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2056 // and all subtypes with all modes can be candidates.
2057 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002058 if (firstMatchedModeSubtype == null) {
2059 firstMatchedModeSubtype = subtype;
2060 }
satok9ef02832010-11-04 21:17:48 +09002061 if (locale.equals(subtypeLocale)) {
2062 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002063 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002064 break;
2065 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2066 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002067 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002068 partialMatchFound = true;
2069 }
satok8fbb1e82010-11-02 23:15:58 +09002070 }
2071 }
2072
satok7599a7f2010-12-22 13:45:23 +09002073 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2074 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002075 }
2076
satok8fbb1e82010-11-02 23:15:58 +09002077 // The first subtype applicable to the system locale will be defined as the most applicable
2078 // subtype.
2079 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002080 if (applicableSubtype != null) {
2081 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2082 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2083 }
satok8fbb1e82010-11-02 23:15:58 +09002084 }
satokcd7cd292010-11-20 15:46:23 +09002085 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002086 }
2087
satok4e4569d2010-11-19 18:45:53 +09002088 // If there are no selected shortcuts, tries finding the most applicable ones.
2089 private Pair<InputMethodInfo, InputMethodSubtype>
2090 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2091 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2092 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002093 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002094 boolean foundInSystemIME = false;
2095
2096 // Search applicable subtype for each InputMethodInfo
2097 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002098 final String imiId = imi.getId();
2099 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2100 continue;
2101 }
satokcd7cd292010-11-20 15:46:23 +09002102 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002103 final List<InputMethodSubtype> enabledSubtypes =
2104 getEnabledInputMethodSubtypeList(imi, true);
2105 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002106 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002107 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002108 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002109 }
satokdf31ae62011-01-15 06:19:44 +09002110 // 2. Search by the system locale from enabledSubtypes.
2111 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002112 if (subtype == null) {
2113 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002114 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002115 }
satok7599a7f2010-12-22 13:45:23 +09002116 // 4. Search by the current subtype's locale from all subtypes.
2117 if (subtype == null && mCurrentSubtype != null) {
2118 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002119 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002120 }
2121 // 5. Search by the system locale from all subtypes.
2122 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002123 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002124 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002125 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002126 }
satokcd7cd292010-11-20 15:46:23 +09002127 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002128 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002129 // The current input method is the most applicable IME.
2130 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002131 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002132 break;
satok7599a7f2010-12-22 13:45:23 +09002133 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002134 // The system input method is 2nd applicable IME.
2135 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002136 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002137 if ((imi.getServiceInfo().applicationInfo.flags
2138 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2139 foundInSystemIME = true;
2140 }
satok4e4569d2010-11-19 18:45:53 +09002141 }
2142 }
2143 }
2144 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002145 if (mostApplicableIMI != null) {
2146 Slog.w(TAG, "Most applicable shortcut input method was:"
2147 + mostApplicableIMI.getId());
2148 if (mostApplicableSubtype != null) {
2149 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2150 + "," + mostApplicableSubtype.getMode() + ","
2151 + mostApplicableSubtype.getLocale());
2152 }
2153 }
satok4e4569d2010-11-19 18:45:53 +09002154 }
satokcd7cd292010-11-20 15:46:23 +09002155 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002156 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002157 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002158 } else {
2159 return null;
2160 }
2161 }
2162
satokab751aa2010-09-14 19:17:36 +09002163 /**
2164 * @return Return the current subtype of this input method.
2165 */
2166 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002167 boolean subtypeIsSelected = false;
2168 try {
2169 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2170 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2171 } catch (SettingNotFoundException e) {
2172 }
satok3ef8b292010-11-23 06:06:29 +09002173 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002174 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002175 String lastInputMethodId = Settings.Secure.getString(
2176 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002177 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2178 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002179 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2180 if (imi != null) {
2181 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002182 // the most applicable subtype from explicitly or implicitly enabled
2183 // subtypes.
2184 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2185 getEnabledInputMethodSubtypeList(imi, true);
2186 // If there is only one explicitly or implicitly enabled subtype,
2187 // just returns it.
2188 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2189 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2190 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2191 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2192 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2193 SUBTYPE_MODE_KEYBOARD, null, true);
2194 if (mCurrentSubtype == null) {
2195 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2196 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2197 true);
2198 }
2199 }
satok4e4569d2010-11-19 18:45:53 +09002200 }
satokcd7cd292010-11-20 15:46:23 +09002201 } else {
satok3ef8b292010-11-23 06:06:29 +09002202 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002203 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002204 }
satok8fbb1e82010-11-02 23:15:58 +09002205 }
satok3ef8b292010-11-23 06:06:29 +09002206 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002207 }
satokab751aa2010-09-14 19:17:36 +09002208 }
2209
satokf3db1af2010-11-23 13:34:33 +09002210 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2211 InputMethodSubtype subtype) {
2212 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2213 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2214 } else {
2215 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2216 subtypes.add(subtype);
2217 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2218 }
2219 }
2220
satok4e4569d2010-11-19 18:45:53 +09002221 // TODO: We should change the return type from List to List<Parcelable>
2222 public List getShortcutInputMethodsAndSubtypes() {
2223 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002224 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002225 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002226 // If there are no selected shortcut subtypes, the framework will try to find
2227 // the most applicable subtype from all subtypes whose mode is
2228 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002229 Pair<InputMethodInfo, InputMethodSubtype> info =
2230 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2231 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002232 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002233 ret.add(info.first);
2234 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002235 }
satok3da92232011-01-11 22:46:30 +09002236 return ret;
satokf3db1af2010-11-23 13:34:33 +09002237 }
satokf3db1af2010-11-23 13:34:33 +09002238 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2239 ret.add(imi);
2240 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2241 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002242 }
2243 }
satokf3db1af2010-11-23 13:34:33 +09002244 return ret;
satok4e4569d2010-11-19 18:45:53 +09002245 }
2246 }
2247
satokb66d2872010-11-10 01:04:04 +09002248 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2249 synchronized (mMethodMap) {
2250 if (subtype != null && mCurMethodId != null) {
2251 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2252 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2253 if (subtypeId != NOT_A_SUBTYPE_ID) {
2254 setInputMethodLocked(mCurMethodId, subtypeId);
2255 return true;
2256 }
2257 }
2258 return false;
2259 }
2260 }
2261
satokd87c2592010-09-29 11:52:06 +09002262 /**
2263 * Utility class for putting and getting settings for InputMethod
2264 * TODO: Move all putters and getters of settings to this class.
2265 */
2266 private static class InputMethodSettings {
2267 // The string for enabled input method is saved as follows:
2268 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2269 private static final char INPUT_METHOD_SEPARATER = ':';
2270 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002271 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002272 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2273
satok723a27e2010-11-11 14:58:11 +09002274 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002275 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2276
satokdf31ae62011-01-15 06:19:44 +09002277 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002278 private final ContentResolver mResolver;
2279 private final HashMap<String, InputMethodInfo> mMethodMap;
2280 private final ArrayList<InputMethodInfo> mMethodList;
2281
2282 private String mEnabledInputMethodsStrCache;
2283
2284 private static void buildEnabledInputMethodsSettingString(
2285 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2286 String id = pair.first;
2287 ArrayList<String> subtypes = pair.second;
2288 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002289 // Inputmethod and subtypes are saved in the settings as follows:
2290 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2291 for (String subtypeId: subtypes) {
2292 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002293 }
2294 }
2295
2296 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002297 Resources res, ContentResolver resolver,
2298 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2299 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002300 mResolver = resolver;
2301 mMethodMap = methodMap;
2302 mMethodList = methodList;
2303 }
2304
2305 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2306 return createEnabledInputMethodListLocked(
2307 getEnabledInputMethodsAndSubtypeListLocked());
2308 }
2309
satok7f35c8c2010-10-07 21:13:11 +09002310 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002311 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2312 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002313 getEnabledInputMethodsAndSubtypeListLocked());
2314 }
2315
satok67ddf9c2010-11-17 09:45:54 +09002316 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2317 InputMethodInfo imi) {
2318 List<Pair<String, ArrayList<String>>> imsList =
2319 getEnabledInputMethodsAndSubtypeListLocked();
2320 ArrayList<InputMethodSubtype> enabledSubtypes =
2321 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002322 if (imi != null) {
2323 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2324 InputMethodInfo info = mMethodMap.get(imsPair.first);
2325 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002326 final int subtypeCount = info.getSubtypeCount();
2327 for (int i = 0; i < subtypeCount; ++i) {
2328 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002329 for (String s: imsPair.second) {
2330 if (String.valueOf(ims.hashCode()).equals(s)) {
2331 enabledSubtypes.add(ims);
2332 }
satok67ddf9c2010-11-17 09:45:54 +09002333 }
2334 }
satok884ef9a2010-11-18 10:39:46 +09002335 break;
satok67ddf9c2010-11-17 09:45:54 +09002336 }
satok67ddf9c2010-11-17 09:45:54 +09002337 }
2338 }
2339 return enabledSubtypes;
2340 }
2341
satokd87c2592010-09-29 11:52:06 +09002342 // At the initial boot, the settings for input methods are not set,
2343 // so we need to enable IME in that case.
2344 public void enableAllIMEsIfThereIsNoEnabledIME() {
2345 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2346 StringBuilder sb = new StringBuilder();
2347 final int N = mMethodList.size();
2348 for (int i = 0; i < N; i++) {
2349 InputMethodInfo imi = mMethodList.get(i);
2350 Slog.i(TAG, "Adding: " + imi.getId());
2351 if (i > 0) sb.append(':');
2352 sb.append(imi.getId());
2353 }
2354 putEnabledInputMethodsStr(sb.toString());
2355 }
2356 }
2357
satokbb4aa062011-01-19 21:40:27 +09002358 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002359 ArrayList<Pair<String, ArrayList<String>>> imsList
2360 = new ArrayList<Pair<String, ArrayList<String>>>();
2361 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2362 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2363 return imsList;
2364 }
satok723a27e2010-11-11 14:58:11 +09002365 mInputMethodSplitter.setString(enabledInputMethodsStr);
2366 while (mInputMethodSplitter.hasNext()) {
2367 String nextImsStr = mInputMethodSplitter.next();
2368 mSubtypeSplitter.setString(nextImsStr);
2369 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002370 ArrayList<String> subtypeHashes = new ArrayList<String>();
2371 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002372 String imeId = mSubtypeSplitter.next();
2373 while (mSubtypeSplitter.hasNext()) {
2374 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002375 }
2376 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2377 }
2378 }
2379 return imsList;
2380 }
2381
2382 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2383 if (reloadInputMethodStr) {
2384 getEnabledInputMethodsStr();
2385 }
2386 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2387 // Add in the newly enabled input method.
2388 putEnabledInputMethodsStr(id);
2389 } else {
2390 putEnabledInputMethodsStr(
2391 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2392 }
2393 }
2394
2395 /**
2396 * Build and put a string of EnabledInputMethods with removing specified Id.
2397 * @return the specified id was removed or not.
2398 */
2399 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2400 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2401 boolean isRemoved = false;
2402 boolean needsAppendSeparator = false;
2403 for (Pair<String, ArrayList<String>> ims: imsList) {
2404 String curId = ims.first;
2405 if (curId.equals(id)) {
2406 // We are disabling this input method, and it is
2407 // currently enabled. Skip it to remove from the
2408 // new list.
2409 isRemoved = true;
2410 } else {
2411 if (needsAppendSeparator) {
2412 builder.append(INPUT_METHOD_SEPARATER);
2413 } else {
2414 needsAppendSeparator = true;
2415 }
2416 buildEnabledInputMethodsSettingString(builder, ims);
2417 }
2418 }
2419 if (isRemoved) {
2420 // Update the setting with the new list of input methods.
2421 putEnabledInputMethodsStr(builder.toString());
2422 }
2423 return isRemoved;
2424 }
2425
2426 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2427 List<Pair<String, ArrayList<String>>> imsList) {
2428 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2429 for (Pair<String, ArrayList<String>> ims: imsList) {
2430 InputMethodInfo info = mMethodMap.get(ims.first);
2431 if (info != null) {
2432 res.add(info);
2433 }
2434 }
2435 return res;
2436 }
2437
satok7f35c8c2010-10-07 21:13:11 +09002438 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002439 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002440 List<Pair<String, ArrayList<String>>> imsList) {
2441 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2442 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2443 for (Pair<String, ArrayList<String>> ims : imsList) {
2444 InputMethodInfo info = mMethodMap.get(ims.first);
2445 if (info != null) {
2446 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2447 }
2448 }
2449 return res;
2450 }
2451
satokd87c2592010-09-29 11:52:06 +09002452 private void putEnabledInputMethodsStr(String str) {
2453 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2454 mEnabledInputMethodsStrCache = str;
2455 }
2456
2457 private String getEnabledInputMethodsStr() {
2458 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2459 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002460 if (DEBUG) {
2461 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2462 }
satokd87c2592010-09-29 11:52:06 +09002463 return mEnabledInputMethodsStrCache;
2464 }
satok723a27e2010-11-11 14:58:11 +09002465
2466 private void saveSubtypeHistory(
2467 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2468 StringBuilder builder = new StringBuilder();
2469 boolean isImeAdded = false;
2470 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2471 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2472 newSubtypeId);
2473 isImeAdded = true;
2474 }
2475 for (Pair<String, String> ime: savedImes) {
2476 String imeId = ime.first;
2477 String subtypeId = ime.second;
2478 if (TextUtils.isEmpty(subtypeId)) {
2479 subtypeId = NOT_A_SUBTYPE_ID_STR;
2480 }
2481 if (isImeAdded) {
2482 builder.append(INPUT_METHOD_SEPARATER);
2483 } else {
2484 isImeAdded = true;
2485 }
2486 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2487 subtypeId);
2488 }
2489 // Remove the last INPUT_METHOD_SEPARATER
2490 putSubtypeHistoryStr(builder.toString());
2491 }
2492
2493 public void addSubtypeToHistory(String imeId, String subtypeId) {
2494 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2495 for (Pair<String, String> ime: subtypeHistory) {
2496 if (ime.first.equals(imeId)) {
2497 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002498 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002499 + ime.second);
2500 }
2501 // We should break here
2502 subtypeHistory.remove(ime);
2503 break;
2504 }
2505 }
satokbb4aa062011-01-19 21:40:27 +09002506 if (DEBUG) {
2507 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2508 }
satok723a27e2010-11-11 14:58:11 +09002509 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2510 }
2511
2512 private void putSubtypeHistoryStr(String str) {
2513 if (DEBUG) {
2514 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2515 }
2516 Settings.Secure.putString(
2517 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2518 }
2519
2520 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2521 // Gets the first one from the history
2522 return getLastSubtypeForInputMethodLockedInternal(null);
2523 }
2524
2525 public String getLastSubtypeForInputMethodLocked(String imeId) {
2526 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2527 if (ime != null) {
2528 return ime.second;
2529 } else {
2530 return null;
2531 }
2532 }
2533
2534 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2535 List<Pair<String, ArrayList<String>>> enabledImes =
2536 getEnabledInputMethodsAndSubtypeListLocked();
2537 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2538 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2539 final String imeInTheHistory = imeAndSubtype.first;
2540 // If imeId is empty, returns the first IME and subtype in the history
2541 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2542 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002543 final String subtypeHashCode =
2544 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2545 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002546 if (!TextUtils.isEmpty(subtypeHashCode)) {
2547 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002548 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002549 }
2550 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2551 }
2552 }
2553 }
2554 if (DEBUG) {
2555 Slog.d(TAG, "No enabled IME found in the history");
2556 }
2557 return null;
2558 }
2559
satokdf31ae62011-01-15 06:19:44 +09002560 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002561 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2562 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2563 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002564 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2565 if (explicitlyEnabledSubtypes.size() == 0) {
2566 // If there are no explicitly enabled subtypes, applicable subtypes are
2567 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002568 InputMethodInfo ime = mMethodMap.get(imeId);
2569 // If IME is enabled and no subtypes are enabled, applicable subtypes
2570 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002571 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002572 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002573 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002574 if (implicitlySelectedSubtypes != null) {
2575 final int N = implicitlySelectedSubtypes.size();
2576 for (int i = 0; i < N; ++i) {
2577 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2578 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2579 return subtypeHashCode;
2580 }
2581 }
2582 }
2583 }
2584 } else {
satokf6cafb62011-01-17 16:29:02 +09002585 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002586 if (s.equals(subtypeHashCode)) {
2587 // If both imeId and subtypeId are enabled, return subtypeId.
2588 return s;
2589 }
satok723a27e2010-11-11 14:58:11 +09002590 }
2591 }
2592 // If imeId was enabled but subtypeId was disabled.
2593 return NOT_A_SUBTYPE_ID_STR;
2594 }
2595 }
2596 // If both imeId and subtypeId are disabled, return null
2597 return null;
2598 }
2599
2600 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2601 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2602 final String subtypeHistoryStr = getSubtypeHistoryStr();
2603 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2604 return imsList;
2605 }
2606 mInputMethodSplitter.setString(subtypeHistoryStr);
2607 while (mInputMethodSplitter.hasNext()) {
2608 String nextImsStr = mInputMethodSplitter.next();
2609 mSubtypeSplitter.setString(nextImsStr);
2610 if (mSubtypeSplitter.hasNext()) {
2611 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2612 // The first element is ime id.
2613 String imeId = mSubtypeSplitter.next();
2614 while (mSubtypeSplitter.hasNext()) {
2615 subtypeId = mSubtypeSplitter.next();
2616 break;
2617 }
2618 imsList.add(new Pair<String, String>(imeId, subtypeId));
2619 }
2620 }
2621 return imsList;
2622 }
2623
2624 private String getSubtypeHistoryStr() {
2625 if (DEBUG) {
2626 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2627 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2628 }
2629 return Settings.Secure.getString(
2630 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2631 }
2632
2633 public void putSelectedInputMethod(String imeId) {
2634 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2635 }
2636
2637 public void putSelectedSubtype(int subtypeId) {
2638 Settings.Secure.putInt(
2639 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2640 }
satokd87c2592010-09-29 11:52:06 +09002641 }
2642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 @Override
2646 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2647 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2648 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2651 + Binder.getCallingPid()
2652 + ", uid=" + Binder.getCallingUid());
2653 return;
2654 }
2655
2656 IInputMethod method;
2657 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 synchronized (mMethodMap) {
2662 p.println("Current Input Method Manager state:");
2663 int N = mMethodList.size();
2664 p.println(" Input Methods:");
2665 for (int i=0; i<N; i++) {
2666 InputMethodInfo info = mMethodList.get(i);
2667 p.println(" InputMethod #" + i + ":");
2668 info.dump(p, " ");
2669 }
2670 p.println(" Clients:");
2671 for (ClientState ci : mClients.values()) {
2672 p.println(" Client " + ci + ":");
2673 p.println(" client=" + ci.client);
2674 p.println(" inputContext=" + ci.inputContext);
2675 p.println(" sessionRequested=" + ci.sessionRequested);
2676 p.println(" curSession=" + ci.curSession);
2677 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002678 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002680 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2681 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2683 + " mBoundToMethod=" + mBoundToMethod);
2684 p.println(" mCurToken=" + mCurToken);
2685 p.println(" mCurIntent=" + mCurIntent);
2686 method = mCurMethod;
2687 p.println(" mCurMethod=" + mCurMethod);
2688 p.println(" mEnabledSession=" + mEnabledSession);
2689 p.println(" mShowRequested=" + mShowRequested
2690 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2691 + " mShowForced=" + mShowForced
2692 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002693 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002695
Jeff Brownb88102f2010-09-08 11:49:43 -07002696 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 pw.flush();
2699 try {
2700 client.client.asBinder().dump(fd, args);
2701 } catch (RemoteException e) {
2702 p.println("Input method client dead: " + e);
2703 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002704 } else {
2705 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002707
Jeff Brownb88102f2010-09-08 11:49:43 -07002708 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 pw.flush();
2711 try {
2712 method.asBinder().dump(fd, args);
2713 } catch (RemoteException e) {
2714 p.println("Input method service dead: " + e);
2715 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002716 } else {
2717 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 }
2719 }
2720}