blob: 95200fa9d0be385abd1af2c1614c44ce7401ebe2 [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;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 static final int MSG_UNBIND_INPUT = 1000;
104 static final int MSG_BIND_INPUT = 1010;
105 static final int MSG_SHOW_SOFT_INPUT = 1020;
106 static final int MSG_HIDE_SOFT_INPUT = 1030;
107 static final int MSG_ATTACH_TOKEN = 1040;
108 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 static final int MSG_START_INPUT = 2000;
111 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 static final int MSG_UNBIND_METHOD = 3000;
114 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800117
satokab751aa2010-09-14 19:17:36 +0900118 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900119 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok8fbb1e82010-11-02 23:15:58 +0900120 // If IME doesn't support the system locale, the default subtype will be the first defined one.
121 private static final int DEFAULT_SUBTYPE_ID = 0;
satokab751aa2010-09-14 19:17:36 +0900122
satok4e4569d2010-11-19 18:45:53 +0900123 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
124 private static final String SUBTYPE_MODE_VOICE = "voice";
125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800127 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900129 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700131 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 final IWindowManager mIWindowManager;
133 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 // All known input methods. mMethodMap also serves as the global
138 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900139 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
140 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 class SessionState {
143 final ClientState client;
144 final IInputMethod method;
145 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 @Override
148 public String toString() {
149 return "SessionState{uid " + client.uid + " pid " + client.pid
150 + " method " + Integer.toHexString(
151 System.identityHashCode(method))
152 + " session " + Integer.toHexString(
153 System.identityHashCode(session))
154 + "}";
155 }
156
157 SessionState(ClientState _client, IInputMethod _method,
158 IInputMethodSession _session) {
159 client = _client;
160 method = _method;
161 session = _session;
162 }
163 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 class ClientState {
166 final IInputMethodClient client;
167 final IInputContext inputContext;
168 final int uid;
169 final int pid;
170 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 boolean sessionRequested;
173 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 @Override
176 public String toString() {
177 return "ClientState{" + Integer.toHexString(
178 System.identityHashCode(this)) + " uid " + uid
179 + " pid " + pid + "}";
180 }
181
182 ClientState(IInputMethodClient _client, IInputContext _inputContext,
183 int _uid, int _pid) {
184 client = _client;
185 inputContext = _inputContext;
186 uid = _uid;
187 pid = _pid;
188 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
189 }
190 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 final HashMap<IBinder, ClientState> mClients
193 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700196 * Set once the system is ready to run third party code.
197 */
198 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800199
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700200 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * Id of the currently selected input method.
202 */
203 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 /**
206 * The current binding sequence number, incremented every time there is
207 * a new bind performed.
208 */
209 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 /**
212 * The client that is currently bound to an input method.
213 */
214 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700217 * The last window token that gained focus.
218 */
219 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800220
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700221 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * The input context last provided by the current client.
223 */
224 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 /**
227 * The attributes last provided by the current client.
228 */
229 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 /**
232 * The input method ID of the input method service that we are currently
233 * connected to or in the process of connecting to.
234 */
235 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 /**
satokab751aa2010-09-14 19:17:36 +0900238 * The current subtype of the current input method.
239 */
240 private InputMethodSubtype mCurrentSubtype;
241
satok4e4569d2010-11-19 18:45:53 +0900242 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900243 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
244 mShortcutInputMethodsAndSubtypes =
245 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900246
247 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * Set to true if our ServiceConnection is currently actively bound to
249 * a service (whether or not we have gotten its IBinder back yet).
250 */
251 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 /**
254 * Set if the client has asked for the input method to be shown.
255 */
256 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 /**
259 * Set if we were explicitly told to show the input method.
260 */
261 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 /**
264 * Set if we were forced to be shown.
265 */
266 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 /**
269 * Set if we last told the input method to show itself.
270 */
271 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 /**
274 * The Intent used to connect to the current input method.
275 */
276 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
279 * The token we have made for the currently active input method, to
280 * identify it in the future.
281 */
282 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 /**
285 * If non-null, this is the input method service we are currently connected
286 * to.
287 */
288 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 /**
291 * Time that we last initiated a bind to the input method, to determine
292 * if we should try to disconnect and reconnect to it.
293 */
294 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 /**
297 * Have we called mCurMethod.bindInput()?
298 */
299 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 /**
302 * Currently enabled session. Only touched by service thread, not
303 * protected by a lock.
304 */
305 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 /**
308 * True if the screen is on. The value is true initially.
309 */
310 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 AlertDialog.Builder mDialogBuilder;
313 AlertDialog mSwitchingDialog;
314 InputMethodInfo[] mIms;
315 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900316 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 class SettingsObserver extends ContentObserver {
319 SettingsObserver(Handler handler) {
320 super(handler);
321 ContentResolver resolver = mContext.getContentResolver();
322 resolver.registerContentObserver(Settings.Secure.getUriFor(
323 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900324 resolver.registerContentObserver(Settings.Secure.getUriFor(
325 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 @Override public void onChange(boolean selfChange) {
329 synchronized (mMethodMap) {
330 updateFromSettingsLocked();
331 }
332 }
333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
336 @Override
337 public void onReceive(Context context, Intent intent) {
338 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
339 mScreenOn = true;
340 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
341 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700342 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
343 hideInputMethodMenu();
344 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800346 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 }
348
349 // Inform the current client of the change in active status
350 try {
351 if (mCurClient != null && mCurClient.client != null) {
352 mCurClient.client.setActive(mScreenOn);
353 }
354 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800355 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 + mCurClient.pid + " uid " + mCurClient.uid);
357 }
358 }
359 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800360
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800361 class MyPackageMonitor extends PackageMonitor {
362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800364 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800366 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
368 final int N = mMethodList.size();
369 if (curInputMethodId != null) {
370 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800371 InputMethodInfo imi = mMethodList.get(i);
372 if (imi.getId().equals(curInputMethodId)) {
373 for (String pkg : packages) {
374 if (imi.getPackageName().equals(pkg)) {
375 if (!doit) {
376 return true;
377 }
satok723a27e2010-11-11 14:58:11 +0900378 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800379 chooseNewDefaultIMELocked();
380 return true;
381 }
382 }
383 }
384 }
385 }
386 }
387 return false;
388 }
389
390 @Override
391 public void onSomePackagesChanged() {
392 synchronized (mMethodMap) {
393 InputMethodInfo curIm = null;
394 String curInputMethodId = Settings.Secure.getString(mContext
395 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
396 final int N = mMethodList.size();
397 if (curInputMethodId != null) {
398 for (int i=0; i<N; i++) {
399 InputMethodInfo imi = mMethodList.get(i);
400 if (imi.getId().equals(curInputMethodId)) {
401 curIm = imi;
402 }
403 int change = isPackageDisappearing(imi.getPackageName());
404 if (change == PACKAGE_TEMPORARY_CHANGE
405 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800406 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800407 + imi.getComponent());
408 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 }
410 }
411 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800412
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800413 buildInputMethodListLocked(mMethodList, mMethodMap);
414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800416
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800417 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800418 int change = isPackageDisappearing(curIm.getPackageName());
419 if (change == PACKAGE_TEMPORARY_CHANGE
420 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800421 ServiceInfo si = null;
422 try {
423 si = mContext.getPackageManager().getServiceInfo(
424 curIm.getComponent(), 0);
425 } catch (PackageManager.NameNotFoundException ex) {
426 }
427 if (si == null) {
428 // Uh oh, current input method is no longer around!
429 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800430 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800431 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800432 changed = true;
433 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800434 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900435 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800436 }
437 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800438 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800439 }
satokab751aa2010-09-14 19:17:36 +0900440
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800441 if (curIm == null) {
442 // We currently don't have a default input method... is
443 // one now available?
444 changed = chooseNewDefaultIMELocked();
445 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800446
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800447 if (changed) {
448 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
450 }
451 }
452 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 class MethodCallback extends IInputMethodCallback.Stub {
455 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 MethodCallback(IInputMethod method) {
458 mMethod = method;
459 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 public void finishedEvent(int seq, boolean handled) throws RemoteException {
462 }
463
464 public void sessionCreated(IInputMethodSession session) throws RemoteException {
465 onSessionCreated(mMethod, session);
466 }
467 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800468
Joe Onorato089de882010-04-12 08:18:45 -0700469 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800471 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 mHandler = new Handler(this);
473 mIWindowManager = IWindowManager.Stub.asInterface(
474 ServiceManager.getService(Context.WINDOW_SERVICE));
475 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
476 public void executeMessage(Message msg) {
477 handleMessage(msg);
478 }
479 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800480
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800481 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 IntentFilter screenOnOffFilt = new IntentFilter();
484 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
485 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700486 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800488
satok913a8922010-08-26 21:53:41 +0900489 mStatusBar = statusBar;
490 statusBar.setIconVisibility("ime", false);
491
satokd87c2592010-09-29 11:52:06 +0900492 // mSettings should be created before buildInputMethodListLocked
493 mSettings = new InputMethodSettings(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
satok67ddf9c2010-11-17 09:45:54 +0900567 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi) {
568 synchronized (mMethodMap) {
satok884ef9a2010-11-18 10:39:46 +0900569 if (imi == null && mCurMethodId != null) {
570 imi = mMethodMap.get(mCurMethodId);
571 }
satok67ddf9c2010-11-17 09:45:54 +0900572 return mSettings.getEnabledInputMethodSubtypeListLocked(imi);
573 }
574 }
575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 public void addClient(IInputMethodClient client,
577 IInputContext inputContext, int uid, int pid) {
578 synchronized (mMethodMap) {
579 mClients.put(client.asBinder(), new ClientState(client,
580 inputContext, uid, pid));
581 }
582 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 public void removeClient(IInputMethodClient client) {
585 synchronized (mMethodMap) {
586 mClients.remove(client.asBinder());
587 }
588 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 void executeOrSendMessage(IInterface target, Message msg) {
591 if (target.asBinder() instanceof Binder) {
592 mCaller.sendMessage(msg);
593 } else {
594 handleMessage(msg);
595 msg.recycle();
596 }
597 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800598
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700599 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800601 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 + mCurClient.client.asBinder());
603 if (mBoundToMethod) {
604 mBoundToMethod = false;
605 if (mCurMethod != null) {
606 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
607 MSG_UNBIND_INPUT, mCurMethod));
608 }
609 }
610 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
611 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
612 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 // Call setActive(false) on the old client
615 try {
616 mCurClient.client.setActive(false);
617 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800618 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 + mCurClient.pid + " uid " + mCurClient.uid);
620 }
621 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800622
The Android Open Source Project10592532009-03-18 17:39:46 -0700623 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 }
625 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 private int getImeShowFlags() {
628 int flags = 0;
629 if (mShowForced) {
630 flags |= InputMethod.SHOW_FORCED
631 | InputMethod.SHOW_EXPLICIT;
632 } else if (mShowExplicitlyRequested) {
633 flags |= InputMethod.SHOW_EXPLICIT;
634 }
635 return flags;
636 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 private int getAppShowFlags() {
639 int flags = 0;
640 if (mShowForced) {
641 flags |= InputMethodManager.SHOW_FORCED;
642 } else if (!mShowExplicitlyRequested) {
643 flags |= InputMethodManager.SHOW_IMPLICIT;
644 }
645 return flags;
646 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
649 if (!mBoundToMethod) {
650 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
651 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
652 mBoundToMethod = true;
653 }
654 final SessionState session = mCurClient.curSession;
655 if (initial) {
656 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
657 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
658 } else {
659 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
660 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
661 }
662 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800663 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800664 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 }
666 return needResult
667 ? new InputBindResult(session.session, mCurId, mCurSeq)
668 : null;
669 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 InputBindResult startInputLocked(IInputMethodClient client,
672 IInputContext inputContext, EditorInfo attribute,
673 boolean initial, boolean needResult) {
674 // If no method is currently selected, do nothing.
675 if (mCurMethodId == null) {
676 return mNoBinding;
677 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 ClientState cs = mClients.get(client.asBinder());
680 if (cs == null) {
681 throw new IllegalArgumentException("unknown client "
682 + client.asBinder());
683 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 try {
686 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
687 // Check with the window manager to make sure this client actually
688 // has a window with focus. If not, reject. This is thread safe
689 // because if the focus changes some time before or after, the
690 // next client receiving focus that has any interest in input will
691 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800692 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
694 return null;
695 }
696 } catch (RemoteException e) {
697 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 if (mCurClient != cs) {
700 // If the client is changing, we need to switch over to the new
701 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700702 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800703 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 + cs.client.asBinder());
705
706 // If the screen is on, inform the new client it is active
707 if (mScreenOn) {
708 try {
709 cs.client.setActive(mScreenOn);
710 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800711 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 + cs.pid + " uid " + cs.uid);
713 }
714 }
715 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 // Bump up the sequence for this client and attach it.
718 mCurSeq++;
719 if (mCurSeq <= 0) mCurSeq = 1;
720 mCurClient = cs;
721 mCurInputContext = inputContext;
722 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 // Check if the input method is changing.
725 if (mCurId != null && mCurId.equals(mCurMethodId)) {
726 if (cs.curSession != null) {
727 // Fast case: if we are already connected to the input method,
728 // then just return it.
729 return attachNewInputLocked(initial, needResult);
730 }
731 if (mHaveConnection) {
732 if (mCurMethod != null) {
733 if (!cs.sessionRequested) {
734 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800735 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
737 MSG_CREATE_SESSION, mCurMethod,
738 new MethodCallback(mCurMethod)));
739 }
740 // Return to client, and we will get back with it when
741 // we have had a session made for it.
742 return new InputBindResult(null, mCurId, mCurSeq);
743 } else if (SystemClock.uptimeMillis()
744 < (mLastBindTime+TIME_TO_RECONNECT)) {
745 // In this case we have connected to the service, but
746 // don't yet have its interface. If it hasn't been too
747 // long since we did the connection, we'll return to
748 // the client and wait to get the service interface so
749 // we can report back. If it has been too long, we want
750 // to fall through so we can try a disconnect/reconnect
751 // to see if we can get back in touch with the service.
752 return new InputBindResult(null, mCurId, mCurSeq);
753 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800754 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
755 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 }
757 }
758 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800759
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700760 return startInputInnerLocked();
761 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800762
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700763 InputBindResult startInputInnerLocked() {
764 if (mCurMethodId == null) {
765 return mNoBinding;
766 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800767
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700768 if (!mSystemReady) {
769 // If the system is not yet ready, we shouldn't be running third
770 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700771 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700772 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 InputMethodInfo info = mMethodMap.get(mCurMethodId);
775 if (info == null) {
776 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
777 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800778
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700779 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
782 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700783 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
784 com.android.internal.R.string.input_method_binding_label);
785 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
786 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
788 mLastBindTime = SystemClock.uptimeMillis();
789 mHaveConnection = true;
790 mCurId = info.getId();
791 mCurToken = new Binder();
792 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800793 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 mIWindowManager.addWindowToken(mCurToken,
795 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
796 } catch (RemoteException e) {
797 }
798 return new InputBindResult(null, mCurId, mCurSeq);
799 } else {
800 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800801 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 + mCurIntent);
803 }
804 return null;
805 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 public InputBindResult startInput(IInputMethodClient client,
808 IInputContext inputContext, EditorInfo attribute,
809 boolean initial, boolean needResult) {
810 synchronized (mMethodMap) {
811 final long ident = Binder.clearCallingIdentity();
812 try {
813 return startInputLocked(client, inputContext, attribute,
814 initial, needResult);
815 } finally {
816 Binder.restoreCallingIdentity(ident);
817 }
818 }
819 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 public void finishInput(IInputMethodClient client) {
822 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 public void onServiceConnected(ComponentName name, IBinder service) {
825 synchronized (mMethodMap) {
826 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
827 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700828 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800829 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700830 unbindCurrentMethodLocked(false);
831 return;
832 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800833 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700834 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
835 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800837 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700838 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700840 MSG_CREATE_SESSION, mCurMethod,
841 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 }
843 }
844 }
845 }
846
847 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
848 synchronized (mMethodMap) {
849 if (mCurMethod != null && method != null
850 && mCurMethod.asBinder() == method.asBinder()) {
851 if (mCurClient != null) {
852 mCurClient.curSession = new SessionState(mCurClient,
853 method, session);
854 mCurClient.sessionRequested = false;
855 InputBindResult res = attachNewInputLocked(true, true);
856 if (res.method != null) {
857 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
858 MSG_BIND_METHOD, mCurClient.client, res));
859 }
860 }
861 }
862 }
863 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800864
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700865 void unbindCurrentMethodLocked(boolean reportToClient) {
866 if (mHaveConnection) {
867 mContext.unbindService(this);
868 mHaveConnection = false;
869 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800870
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700871 if (mCurToken != null) {
872 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800873 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700874 mIWindowManager.removeWindowToken(mCurToken);
875 } catch (RemoteException e) {
876 }
877 mCurToken = null;
878 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800879
The Android Open Source Project10592532009-03-18 17:39:46 -0700880 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700881 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800882
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700883 if (reportToClient && mCurClient != null) {
884 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
885 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
886 }
887 }
888
Devin Taylor0c33ed22010-02-23 13:26:46 -0600889 private void finishSession(SessionState sessionState) {
890 if (sessionState != null && sessionState.session != null) {
891 try {
892 sessionState.session.finishSession();
893 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700894 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600895 }
896 }
897 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800898
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700899 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 if (mCurMethod != null) {
901 for (ClientState cs : mClients.values()) {
902 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600903 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 cs.curSession = null;
905 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600906
907 finishSession(mEnabledSession);
908 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 mCurMethod = null;
910 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700911 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 public void onServiceDisconnected(ComponentName name) {
915 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800916 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 + " mCurIntent=" + mCurIntent);
918 if (mCurMethod != null && mCurIntent != null
919 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700920 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 // We consider this to be a new bind attempt, since the system
922 // should now try to restart the service for us.
923 mLastBindTime = SystemClock.uptimeMillis();
924 mShowRequested = mInputShown;
925 mInputShown = false;
926 if (mCurClient != null) {
927 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
928 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
929 }
930 }
931 }
932 }
933
934 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700935 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 long ident = Binder.clearCallingIdentity();
937 try {
938 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700939 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 return;
941 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 synchronized (mMethodMap) {
944 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800945 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700946 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800948 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700949 mStatusBar.setIcon("ime", packageName, iconId, 0);
950 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 }
952 }
953 } finally {
954 Binder.restoreCallingIdentity(ident);
955 }
956 }
957
satok06487a52010-10-29 11:37:18 +0900958 public void setIMEButtonVisible(IBinder token, boolean visible) {
959 int uid = Binder.getCallingUid();
960 long ident = Binder.clearCallingIdentity();
961 try {
962 if (token == null || mCurToken != token) {
963 Slog.w(TAG, "Ignoring setIMEButtonVisible of uid " + uid + " token: " + token);
964 return;
965 }
966
967 synchronized (mMethodMap) {
satokcd7cd292010-11-20 15:46:23 +0900968 mStatusBar.setIMEButtonVisible(token, visible);
satok06487a52010-10-29 11:37:18 +0900969 }
970 } finally {
971 Binder.restoreCallingIdentity(ident);
972 }
973 }
974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700976 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
977 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
978 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
979 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +0900981 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +0900982 // There is no input method selected, try to choose new applicable input method.
983 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
984 id = Settings.Secure.getString(mContext.getContentResolver(),
985 Settings.Secure.DEFAULT_INPUT_METHOD);
986 }
987 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 try {
satokab751aa2010-09-14 19:17:36 +0900989 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800991 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -0700992 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700993 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 }
satokf3db1af2010-11-23 13:34:33 +0900995 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700996 } else {
997 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -0700998 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700999 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 }
1001 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001002
satokab751aa2010-09-14 19:17:36 +09001003 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 InputMethodInfo info = mMethodMap.get(id);
1005 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001006 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 if (id.equals(mCurMethodId)) {
satokb66d2872010-11-10 01:04:04 +09001010 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
satokcd7cd292010-11-20 15:46:23 +09001011 InputMethodSubtype subtype = null;
satokb66d2872010-11-10 01:04:04 +09001012 if (subtypeId >= 0 && subtypeId < subtypes.size()) {
satokcd7cd292010-11-20 15:46:23 +09001013 subtype = subtypes.get(subtypeId);
1014 }
1015 if (subtype != mCurrentSubtype) {
1016 synchronized (mMethodMap) {
1017 if (mCurMethod != null) {
1018 try {
1019 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1020 if (mInputShown) {
1021 // If mInputShown is false, there is no IME button on the
1022 // system bar.
1023 // Thus there is no need to make it invisible explicitly.
1024 mStatusBar.setIMEButtonVisible(mCurToken, true);
satokab751aa2010-09-14 19:17:36 +09001025 }
satokcd7cd292010-11-20 15:46:23 +09001026 // If subtype is null, try to find the most applicable one from
1027 // getCurrentInputMethodSubtype.
1028 if (subtype == null) {
1029 subtype = getCurrentInputMethodSubtype();
1030 }
1031 mCurMethod.changeInputMethodSubtype(subtype);
1032 } catch (RemoteException e) {
1033 return;
satokab751aa2010-09-14 19:17:36 +09001034 }
1035 }
1036 }
1037 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 return;
1039 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 final long ident = Binder.clearCallingIdentity();
1042 try {
satokab751aa2010-09-14 19:17:36 +09001043 // Set a subtype to this input method.
1044 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001045 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1046 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1047 // because mCurMethodId is stored as a history in
1048 // setSelectedInputMethodAndSubtypeLocked().
1049 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050
1051 if (ActivityManagerNative.isSystemReady()) {
1052 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001053 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 intent.putExtra("input_method_id", id);
1055 mContext.sendBroadcast(intent);
1056 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001057 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 } finally {
1059 Binder.restoreCallingIdentity(ident);
1060 }
1061 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001062
The Android Open Source Project4df24232009-03-05 14:34:35 -08001063 public boolean showSoftInput(IInputMethodClient client, int flags,
1064 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001065 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 long ident = Binder.clearCallingIdentity();
1067 try {
1068 synchronized (mMethodMap) {
1069 if (mCurClient == null || client == null
1070 || mCurClient.client.asBinder() != client.asBinder()) {
1071 try {
1072 // We need to check if this is the current client with
1073 // focus in the window manager, to allow this call to
1074 // be made before input is started in it.
1075 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001076 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001077 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 }
1079 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001080 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 }
1082 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001083
Joe Onorato8a9b2202010-02-26 18:56:32 -08001084 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001085 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 }
1087 } finally {
1088 Binder.restoreCallingIdentity(ident);
1089 }
1090 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001091
The Android Open Source Project4df24232009-03-05 14:34:35 -08001092 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 mShowRequested = true;
1094 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1095 mShowExplicitlyRequested = true;
1096 }
1097 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1098 mShowExplicitlyRequested = true;
1099 mShowForced = true;
1100 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001101
Dianne Hackborncc278702009-09-02 23:07:23 -07001102 if (!mSystemReady) {
1103 return false;
1104 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001105
The Android Open Source Project4df24232009-03-05 14:34:35 -08001106 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001108 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1109 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1110 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001112 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 } else if (mHaveConnection && SystemClock.uptimeMillis()
1114 < (mLastBindTime+TIME_TO_RECONNECT)) {
1115 // The client has asked to have the input method shown, but
1116 // we have been sitting here too long with a connection to the
1117 // service and no interface received, so let's disconnect/connect
1118 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001119 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 SystemClock.uptimeMillis()-mLastBindTime,1);
1121 mContext.unbindService(this);
1122 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1123 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001124
The Android Open Source Project4df24232009-03-05 14:34:35 -08001125 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001127
The Android Open Source Project4df24232009-03-05 14:34:35 -08001128 public boolean hideSoftInput(IInputMethodClient client, int flags,
1129 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001130 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 long ident = Binder.clearCallingIdentity();
1132 try {
1133 synchronized (mMethodMap) {
1134 if (mCurClient == null || client == null
1135 || mCurClient.client.asBinder() != client.asBinder()) {
1136 try {
1137 // We need to check if this is the current client with
1138 // focus in the window manager, to allow this call to
1139 // be made before input is started in it.
1140 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001141 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1142 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001143 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
1145 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001146 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 }
1148 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001149
Joe Onorato8a9b2202010-02-26 18:56:32 -08001150 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001151 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 }
1153 } finally {
1154 Binder.restoreCallingIdentity(ident);
1155 }
1156 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001157
The Android Open Source Project4df24232009-03-05 14:34:35 -08001158 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1160 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001161 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001163 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 }
1165 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001166 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001168 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001170 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001172 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1173 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1174 res = true;
1175 } else {
1176 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 }
1178 mInputShown = false;
1179 mShowRequested = false;
1180 mShowExplicitlyRequested = false;
1181 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001182 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001184
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001185 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1187 boolean first, int windowFlags) {
1188 long ident = Binder.clearCallingIdentity();
1189 try {
1190 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001191 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 + " viewHasFocus=" + viewHasFocus
1193 + " isTextEditor=" + isTextEditor
1194 + " softInputMode=#" + Integer.toHexString(softInputMode)
1195 + " first=" + first + " flags=#"
1196 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 if (mCurClient == null || client == null
1199 || mCurClient.client.asBinder() != client.asBinder()) {
1200 try {
1201 // We need to check if this is the current client with
1202 // focus in the window manager, to allow this call to
1203 // be made before input is started in it.
1204 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001205 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 return;
1207 }
1208 } catch (RemoteException e) {
1209 }
1210 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001211
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001212 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001213 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001214 return;
1215 }
1216 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001217
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001218 // Should we auto-show the IME even if the caller has not
1219 // specified what should be done with it?
1220 // We only do this automatically if the window can resize
1221 // to accommodate the IME (so what the user sees will give
1222 // them good context without input information being obscured
1223 // by the IME) or if running on a large screen where there
1224 // is more room for the target window + IME.
1225 final boolean doAutoShow =
1226 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1227 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1228 || mRes.getConfiguration().isLayoutSizeAtLeast(
1229 Configuration.SCREENLAYOUT_SIZE_LARGE);
1230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1232 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001233 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1235 // There is no focus view, and this window will
1236 // be behind any soft input window, so hide the
1237 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001238 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001239 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001241 } else if (isTextEditor && doAutoShow && (softInputMode &
1242 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 // There is a focus view, and we are navigating forward
1244 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001245 // We only do this automatically if the window an resize
1246 // to accomodate the IME (so what the user sees will give
1247 // them good context without input information being obscured
1248 // by the IME) or if running on a large screen where there
1249 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001250 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001251 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 }
1253 break;
1254 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1255 // Do nothing.
1256 break;
1257 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1258 if ((softInputMode &
1259 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001260 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001261 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 }
1263 break;
1264 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001265 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001266 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 break;
1268 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1269 if ((softInputMode &
1270 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001271 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001272 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 }
1274 break;
1275 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001276 if (DEBUG) Slog.v(TAG, "Window asks to always 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 break;
1279 }
1280 }
1281 } finally {
1282 Binder.restoreCallingIdentity(ident);
1283 }
1284 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1287 synchronized (mMethodMap) {
1288 if (mCurClient == null || client == null
1289 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001290 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001291 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 }
1293
satok440aab52010-11-25 09:43:11 +09001294 // Always call subtype picker, because subtype picker is a superset of input method
1295 // picker.
satokab751aa2010-09-14 19:17:36 +09001296 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1297 }
1298 }
1299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001301 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1302 }
1303
1304 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1305 synchronized (mMethodMap) {
1306 if (subtype != null) {
1307 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1308 mMethodMap.get(id), subtype.hashCode()));
1309 } else {
1310 setInputMethod(token, id);
1311 }
1312 }
satokab751aa2010-09-14 19:17:36 +09001313 }
1314
satokb416a712010-11-25 20:42:14 +09001315 public void showInputMethodAndSubtypeEnablerFromClient(
1316 IInputMethodClient client, String topId) {
1317 // TODO: Handle topId for setting the top position of the list ActivityManagerNative
1318 synchronized (mMethodMap) {
1319 if (mCurClient == null || client == null
1320 || mCurClient.client.asBinder() != client.asBinder()) {
1321 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1322 }
1323 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_ENABLER);
1324 }
1325 }
1326
satok735cf382010-11-11 20:40:09 +09001327 public boolean switchToLastInputMethod(IBinder token) {
1328 synchronized (mMethodMap) {
1329 Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1330 if (lastIme != null) {
1331 InputMethodInfo imi = mMethodMap.get(lastIme.first);
1332 if (imi != null) {
satok28203512010-11-24 11:06:49 +09001333 setInputMethodWithSubtypeId(token, lastIme.first, getSubtypeIdFromHashCode(
satok735cf382010-11-11 20:40:09 +09001334 imi, Integer.valueOf(lastIme.second)));
1335 return true;
1336 }
1337 }
1338 return false;
1339 }
1340 }
1341
satok28203512010-11-24 11:06:49 +09001342 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 synchronized (mMethodMap) {
1344 if (token == null) {
1345 if (mContext.checkCallingOrSelfPermission(
1346 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1347 != PackageManager.PERMISSION_GRANTED) {
1348 throw new SecurityException(
1349 "Using null token requires permission "
1350 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1351 }
1352 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001353 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1354 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 return;
1356 }
1357
1358 long ident = Binder.clearCallingIdentity();
1359 try {
satokab751aa2010-09-14 19:17:36 +09001360 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 } finally {
1362 Binder.restoreCallingIdentity(ident);
1363 }
1364 }
1365 }
1366
1367 public void hideMySoftInput(IBinder token, int flags) {
1368 synchronized (mMethodMap) {
1369 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001370 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1371 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 return;
1373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 long ident = Binder.clearCallingIdentity();
1375 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001376 hideCurrentInputLocked(flags, null);
1377 } finally {
1378 Binder.restoreCallingIdentity(ident);
1379 }
1380 }
1381 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001382
The Android Open Source Project4df24232009-03-05 14:34:35 -08001383 public void showMySoftInput(IBinder token, int flags) {
1384 synchronized (mMethodMap) {
1385 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001386 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1387 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001388 return;
1389 }
1390 long ident = Binder.clearCallingIdentity();
1391 try {
1392 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 } finally {
1394 Binder.restoreCallingIdentity(ident);
1395 }
1396 }
1397 }
1398
1399 void setEnabledSessionInMainThread(SessionState session) {
1400 if (mEnabledSession != session) {
1401 if (mEnabledSession != null) {
1402 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001403 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 mEnabledSession.method.setSessionEnabled(
1405 mEnabledSession.session, false);
1406 } catch (RemoteException e) {
1407 }
1408 }
1409 mEnabledSession = session;
1410 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001411 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 session.method.setSessionEnabled(
1413 session.session, true);
1414 } catch (RemoteException e) {
1415 }
1416 }
1417 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 public boolean handleMessage(Message msg) {
1420 HandlerCaller.SomeArgs args;
1421 switch (msg.what) {
1422 case MSG_SHOW_IM_PICKER:
1423 showInputMethodMenu();
1424 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001425
satokab751aa2010-09-14 19:17:36 +09001426 case MSG_SHOW_IM_SUBTYPE_PICKER:
1427 showInputMethodSubtypeMenu();
1428 return true;
1429
satok47a44912010-10-06 16:03:58 +09001430 case MSG_SHOW_IM_SUBTYPE_ENABLER:
1431 showInputMethodAndSubtypeEnabler();
1432 return true;
1433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 case MSG_UNBIND_INPUT:
1437 try {
1438 ((IInputMethod)msg.obj).unbindInput();
1439 } catch (RemoteException e) {
1440 // There is nothing interesting about the method dying.
1441 }
1442 return true;
1443 case MSG_BIND_INPUT:
1444 args = (HandlerCaller.SomeArgs)msg.obj;
1445 try {
1446 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1447 } catch (RemoteException e) {
1448 }
1449 return true;
1450 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001451 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001453 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1454 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 } catch (RemoteException e) {
1456 }
1457 return true;
1458 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001459 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001461 ((IInputMethod)args.arg1).hideSoftInput(0,
1462 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 } catch (RemoteException e) {
1464 }
1465 return true;
1466 case MSG_ATTACH_TOKEN:
1467 args = (HandlerCaller.SomeArgs)msg.obj;
1468 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001469 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1471 } catch (RemoteException e) {
1472 }
1473 return true;
1474 case MSG_CREATE_SESSION:
1475 args = (HandlerCaller.SomeArgs)msg.obj;
1476 try {
1477 ((IInputMethod)args.arg1).createSession(
1478 (IInputMethodCallback)args.arg2);
1479 } catch (RemoteException e) {
1480 }
1481 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 case MSG_START_INPUT:
1485 args = (HandlerCaller.SomeArgs)msg.obj;
1486 try {
1487 SessionState session = (SessionState)args.arg1;
1488 setEnabledSessionInMainThread(session);
1489 session.method.startInput((IInputContext)args.arg2,
1490 (EditorInfo)args.arg3);
1491 } catch (RemoteException e) {
1492 }
1493 return true;
1494 case MSG_RESTART_INPUT:
1495 args = (HandlerCaller.SomeArgs)msg.obj;
1496 try {
1497 SessionState session = (SessionState)args.arg1;
1498 setEnabledSessionInMainThread(session);
1499 session.method.restartInput((IInputContext)args.arg2,
1500 (EditorInfo)args.arg3);
1501 } catch (RemoteException e) {
1502 }
1503 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 case MSG_UNBIND_METHOD:
1508 try {
1509 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1510 } catch (RemoteException e) {
1511 // There is nothing interesting about the last client dying.
1512 }
1513 return true;
1514 case MSG_BIND_METHOD:
1515 args = (HandlerCaller.SomeArgs)msg.obj;
1516 try {
1517 ((IInputMethodClient)args.arg1).onBindMethod(
1518 (InputBindResult)args.arg2);
1519 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001520 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 }
1522 return true;
1523 }
1524 return false;
1525 }
1526
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001527 private boolean isSystemIme(InputMethodInfo inputMethod) {
1528 return (inputMethod.getServiceInfo().applicationInfo.flags
1529 & ApplicationInfo.FLAG_SYSTEM) != 0;
1530 }
1531
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001532 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001533 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001534 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001535 // We'd prefer to fall back on a system IME, since that is safer.
1536 int i=enabled.size();
1537 while (i > 0) {
1538 i--;
1539 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1540 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1541 break;
1542 }
1543 }
satokab751aa2010-09-14 19:17:36 +09001544 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001545 if (DEBUG) {
1546 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1547 }
satok723a27e2010-11-11 14:58:11 +09001548 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001549 return true;
1550 }
1551
1552 return false;
1553 }
1554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1556 HashMap<String, InputMethodInfo> map) {
1557 list.clear();
1558 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001561 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001562 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1563 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1564 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1565 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566
1567 List<ResolveInfo> services = pm.queryIntentServices(
1568 new Intent(InputMethod.SERVICE_INTERFACE),
1569 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 for (int i = 0; i < services.size(); ++i) {
1572 ResolveInfo ri = services.get(i);
1573 ServiceInfo si = ri.serviceInfo;
1574 ComponentName compName = new ComponentName(si.packageName, si.name);
1575 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1576 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001577 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 + ": it does not require the permission "
1579 + android.Manifest.permission.BIND_INPUT_METHOD);
1580 continue;
1581 }
1582
Joe Onorato8a9b2202010-02-26 18:56:32 -08001583 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584
1585 try {
1586 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1587 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001588 final String id = p.getId();
1589 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590
Amith Yamasanie861ec12010-03-24 21:39:27 -07001591 // System IMEs are enabled by default, unless there's a hard keyboard
1592 // and the system IME was explicitly disabled
1593 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1594 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001595 }
1596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001598 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001602 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001604 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 }
1606 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001607
1608 String defaultIme = Settings.Secure.getString(mContext
1609 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001610 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001611 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001612 updateFromSettingsLocked();
1613 }
1614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001618
satokab751aa2010-09-14 19:17:36 +09001619 private void showInputMethodMenu() {
1620 showInputMethodMenuInternal(false);
1621 }
1622
1623 private void showInputMethodSubtypeMenu() {
1624 showInputMethodMenuInternal(true);
1625 }
1626
satok47a44912010-10-06 16:03:58 +09001627 private void showInputMethodAndSubtypeEnabler() {
satok86417ea2010-10-27 14:11:03 +09001628 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_AND_SUBTYPE_ENABLER);
satok47a44912010-10-06 16:03:58 +09001629 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001630 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1631 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001632 mContext.startActivity(intent);
1633 }
1634
satokab751aa2010-09-14 19:17:36 +09001635 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001636 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 String lastInputMethodId = Settings.Secure.getString(context
1643 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001644 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001645 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001646
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001647 synchronized (mMethodMap) {
satok7f35c8c2010-10-07 21:13:11 +09001648 final List<Pair<InputMethodInfo, ArrayList<String>>> immis =
satok67ddf9c2010-11-17 09:45:54 +09001649 mSettings.getEnabledInputMethodAndSubtypeHashCodeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001650 ArrayList<Integer> subtypeIds = new ArrayList<Integer>();
1651
1652 if (immis == null || immis.size() == 0) {
1653 return;
1654 }
1655
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001656 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001658 int N = immis.size();
satok913a8922010-08-26 21:53:41 +09001659
satokab751aa2010-09-14 19:17:36 +09001660 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1661 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001662
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001663 for (int i = 0; i < N; ++i) {
satok7f35c8c2010-10-07 21:13:11 +09001664 InputMethodInfo property = immis.get(i).first;
1665 final ArrayList<String> enabledSubtypeIds = immis.get(i).second;
1666 HashSet<String> enabledSubtypeSet = new HashSet<String>();
1667 for (String s : enabledSubtypeIds) {
1668 enabledSubtypeSet.add(s);
1669 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001670 if (property == null) {
1671 continue;
1672 }
satokab751aa2010-09-14 19:17:36 +09001673 ArrayList<InputMethodSubtype> subtypes = property.getSubtypes();
1674 CharSequence label = property.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001675 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokab751aa2010-09-14 19:17:36 +09001676 for (int j = 0; j < subtypes.size(); ++j) {
1677 InputMethodSubtype subtype = subtypes.get(j);
satok7f35c8c2010-10-07 21:13:11 +09001678 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1679 CharSequence title;
1680 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001681 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001682 if (nameResId != 0) {
1683 title = pm.getText(property.getPackageName(), nameResId,
1684 property.getServiceInfo().applicationInfo);
1685 } else {
1686 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001687 // TODO: Use more friendly Title and UI
1688 title = label + "," + (mode == null ? "" : mode) + ","
1689 + (language == null ? "" : language);
1690 }
1691 imMap.put(title, new Pair<InputMethodInfo, Integer>(property, j));
satokab751aa2010-09-14 19:17:36 +09001692 }
satokab751aa2010-09-14 19:17:36 +09001693 }
1694 } else {
1695 imMap.put(label,
1696 new Pair<InputMethodInfo, Integer>(property, NOT_A_SUBTYPE_ID));
1697 subtypeIds.add(0);
1698 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001699 }
satok913a8922010-08-26 21:53:41 +09001700
1701 N = imMap.size();
1702 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001703 mIms = new InputMethodInfo[N];
1704 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001705 int checkedItem = 0;
1706 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001707 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1708 mIms[i] = value.first;
1709 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001710 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001711 int subtypeId = mSubtypeIds[i];
1712 if ((subtypeId == NOT_A_SUBTYPE_ID)
1713 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1714 || (subtypeId == lastInputMethodSubtypeId)) {
1715 checkedItem = i;
1716 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 }
satokab751aa2010-09-14 19:17:36 +09001719
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001720 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1721 public void onClick(DialogInterface dialog, int which) {
1722 hideInputMethodMenu();
1723 }
1724 };
satokd87c2592010-09-29 11:52:06 +09001725
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001726 TypedArray a = context.obtainStyledAttributes(null,
1727 com.android.internal.R.styleable.DialogPreference,
1728 com.android.internal.R.attr.alertDialogStyle, 0);
1729 mDialogBuilder = new AlertDialog.Builder(context)
1730 .setTitle(com.android.internal.R.string.select_input_method)
1731 .setOnCancelListener(new OnCancelListener() {
1732 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001734 }
1735 })
1736 .setIcon(a.getDrawable(
1737 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1738 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001739
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001740 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1741 new AlertDialog.OnClickListener() {
1742 public void onClick(DialogInterface dialog, int which) {
1743 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001744 if (mIms == null || mIms.length <= which
1745 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001746 return;
1747 }
1748 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001749 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001750 hideInputMethodMenu();
1751 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001752 if ((subtypeId < 0)
1753 || (subtypeId >= im.getSubtypes().size())) {
1754 subtypeId = NOT_A_SUBTYPE_ID;
1755 }
1756 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001757 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001760 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761
satok7f35c8c2010-10-07 21:13:11 +09001762 if (showSubtypes) {
1763 mDialogBuilder.setPositiveButton(com.android.internal.R.string.more_item_label,
1764 new DialogInterface.OnClickListener() {
1765 public void onClick(DialogInterface dialog, int whichButton) {
1766 showInputMethodAndSubtypeEnabler();
1767 }
1768 });
1769 }
satok0ff647b2010-10-08 13:49:28 +09001770 mDialogBuilder.setNegativeButton(com.android.internal.R.string.cancel,
1771 new DialogInterface.OnClickListener() {
1772 public void onClick(DialogInterface dialog, int whichButton) {
1773 hideInputMethodMenu();
1774 }
1775 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 mSwitchingDialog = mDialogBuilder.create();
1777 mSwitchingDialog.getWindow().setType(
1778 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1779 mSwitchingDialog.show();
1780 }
1781 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001784 synchronized (mMethodMap) {
1785 hideInputMethodMenuLocked();
1786 }
1787 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001788
The Android Open Source Project10592532009-03-18 17:39:46 -07001789 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001790 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791
The Android Open Source Project10592532009-03-18 17:39:46 -07001792 if (mSwitchingDialog != null) {
1793 mSwitchingDialog.dismiss();
1794 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001796
The Android Open Source Project10592532009-03-18 17:39:46 -07001797 mDialogBuilder = null;
1798 mItems = null;
1799 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 public boolean setInputMethodEnabled(String id, boolean enabled) {
1805 synchronized (mMethodMap) {
1806 if (mContext.checkCallingOrSelfPermission(
1807 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1808 != PackageManager.PERMISSION_GRANTED) {
1809 throw new SecurityException(
1810 "Requires permission "
1811 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1812 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 long ident = Binder.clearCallingIdentity();
1815 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001816 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 } finally {
1818 Binder.restoreCallingIdentity(ident);
1819 }
1820 }
1821 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001822
1823 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1824 // Make sure this is a valid input method.
1825 InputMethodInfo imm = mMethodMap.get(id);
1826 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001827 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001828 }
1829
satokd87c2592010-09-29 11:52:06 +09001830 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1831 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001832
satokd87c2592010-09-29 11:52:06 +09001833 if (enabled) {
1834 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1835 if (pair.first.equals(id)) {
1836 // We are enabling this input method, but it is already enabled.
1837 // Nothing to do. The previous state was enabled.
1838 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001839 }
1840 }
satokd87c2592010-09-29 11:52:06 +09001841 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1842 // Previous state was disabled.
1843 return false;
1844 } else {
1845 StringBuilder builder = new StringBuilder();
1846 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1847 builder, enabledInputMethodsList, id)) {
1848 // Disabled input method is currently selected, switch to another one.
1849 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1850 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001851 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
1852 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
1853 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09001854 }
1855 // Previous state was enabled.
1856 return true;
1857 } else {
1858 // We are disabling the input method but it is already disabled.
1859 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001860 return false;
1861 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001862 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001863 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001864
satok723a27e2010-11-11 14:58:11 +09001865 private void saveCurrentInputMethodAndSubtypeToHistory() {
1866 String subtypeId = NOT_A_SUBTYPE_ID_STR;
1867 if (mCurrentSubtype != null) {
1868 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
1869 }
1870 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
satokab751aa2010-09-14 19:17:36 +09001871 }
1872
satok723a27e2010-11-11 14:58:11 +09001873 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
1874 boolean setSubtypeOnly) {
1875 // Update the history of InputMethod and Subtype
1876 saveCurrentInputMethodAndSubtypeToHistory();
1877
1878 // Set Subtype here
1879 if (imi == null || subtypeId < 0) {
1880 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001881 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09001882 } else {
1883 final ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1884 if (subtypeId < subtypes.size()) {
1885 mSettings.putSelectedSubtype(subtypes.get(subtypeId).hashCode());
1886 mCurrentSubtype = subtypes.get(subtypeId);
1887 } else {
1888 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
1889 mCurrentSubtype = null;
1890 }
satokab751aa2010-09-14 19:17:36 +09001891 }
satok723a27e2010-11-11 14:58:11 +09001892
1893 if (!setSubtypeOnly) {
1894 // Set InputMethod here
1895 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
1896 }
1897 }
1898
1899 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
1900 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
1901 int lastSubtypeId = NOT_A_SUBTYPE_ID;
1902 // newDefaultIme is empty when there is no candidate for the selected IME.
1903 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
1904 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
1905 if (subtypeHashCode != null) {
1906 try {
1907 lastSubtypeId = getSubtypeIdFromHashCode(
1908 imi, Integer.valueOf(subtypeHashCode));
1909 } catch (NumberFormatException e) {
1910 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
1911 }
1912 }
1913 }
1914 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09001915 }
1916
1917 private int getSelectedInputMethodSubtypeId(String id) {
1918 InputMethodInfo imi = mMethodMap.get(id);
1919 if (imi == null) {
1920 return NOT_A_SUBTYPE_ID;
1921 }
satokab751aa2010-09-14 19:17:36 +09001922 int subtypeId;
1923 try {
1924 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
1925 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
1926 } catch (SettingNotFoundException e) {
1927 return NOT_A_SUBTYPE_ID;
1928 }
satok723a27e2010-11-11 14:58:11 +09001929 return getSubtypeIdFromHashCode(imi, subtypeId);
1930 }
1931
1932 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09001933 if (imi != null) {
1934 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1935 for (int i = 0; i < subtypes.size(); ++i) {
1936 InputMethodSubtype ims = subtypes.get(i);
1937 if (subtypeHashCode == ims.hashCode()) {
1938 return i;
1939 }
satokab751aa2010-09-14 19:17:36 +09001940 }
1941 }
1942 return NOT_A_SUBTYPE_ID;
1943 }
1944
satok4e4569d2010-11-19 18:45:53 +09001945 /**
1946 * If there are no selected subtypes, tries finding the most applicable one according to the
1947 * given locale.
1948 * @param subtypes this function will search the most applicable subtype in subtypes
1949 * @param mode subtypes will be filtered by mode
1950 * @param locale subtypes will be filtered by locale
1951 * @param defaultSubtypeId if this function can't find the most applicable subtype, it will
1952 * return defaultSubtypeId
1953 * @return the most applicable subtypeId
1954 */
satokcd7cd292010-11-20 15:46:23 +09001955 private InputMethodSubtype findLastResortApplicableSubtypeLocked(
satok4e4569d2010-11-19 18:45:53 +09001956 List<InputMethodSubtype> subtypes, String mode, String locale, int defaultSubtypeId) {
satok8fbb1e82010-11-02 23:15:58 +09001957 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09001958 return null;
satok8fbb1e82010-11-02 23:15:58 +09001959 }
satok4e4569d2010-11-19 18:45:53 +09001960 if (TextUtils.isEmpty(locale)) {
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001961 locale = mRes.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09001962 }
satok8fbb1e82010-11-02 23:15:58 +09001963 final String language = locale.substring(0, 2);
1964 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09001965 InputMethodSubtype applicableSubtype = null;
satok8fbb1e82010-11-02 23:15:58 +09001966 for (int i = 0; i < subtypes.size(); ++i) {
satokcd7cd292010-11-20 15:46:23 +09001967 InputMethodSubtype subtype = subtypes.get(i);
1968 final String subtypeLocale = subtype.getLocale();
satok4e4569d2010-11-19 18:45:53 +09001969 // An applicable subtype should match "mode".
1970 if (subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok9ef02832010-11-04 21:17:48 +09001971 if (locale.equals(subtypeLocale)) {
1972 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09001973 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09001974 break;
1975 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
1976 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09001977 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09001978 partialMatchFound = true;
1979 }
satok8fbb1e82010-11-02 23:15:58 +09001980 }
1981 }
1982
1983 // The first subtype applicable to the system locale will be defined as the most applicable
1984 // subtype.
1985 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09001986 Slog.d(TAG, "Applicable InputMethodSubtype was found: " + applicableSubtype.getMode()
1987 + "," + applicableSubtype.getLocale());
satok8fbb1e82010-11-02 23:15:58 +09001988 }
satokcd7cd292010-11-20 15:46:23 +09001989 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09001990 }
1991
satok4e4569d2010-11-19 18:45:53 +09001992 // If there are no selected shortcuts, tries finding the most applicable ones.
1993 private Pair<InputMethodInfo, InputMethodSubtype>
1994 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
1995 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1996 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09001997 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09001998 boolean foundInSystemIME = false;
1999
2000 // Search applicable subtype for each InputMethodInfo
2001 for (InputMethodInfo imi: imis) {
satokcd7cd292010-11-20 15:46:23 +09002002 InputMethodSubtype subtype = null;
satok4e4569d2010-11-19 18:45:53 +09002003 if (mCurrentSubtype != null) {
2004 // 1. Search with the current subtype's locale and the enabled subtypes
satokcd7cd292010-11-20 15:46:23 +09002005 subtype = findLastResortApplicableSubtypeLocked(
satok4e4569d2010-11-19 18:45:53 +09002006 mSettings.getEnabledInputMethodSubtypeListLocked(
2007 imi), mode, mCurrentSubtype.getLocale(), NOT_A_SUBTYPE_ID);
satokcd7cd292010-11-20 15:46:23 +09002008 if (subtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002009 // 2. Search with the current subtype's locale and all subtypes
satokcd7cd292010-11-20 15:46:23 +09002010 subtype = findLastResortApplicableSubtypeLocked(imi.getSubtypes(),
satok4e4569d2010-11-19 18:45:53 +09002011 mode, mCurrentSubtype.getLocale(), NOT_A_SUBTYPE_ID);
2012 }
2013 }
2014 // 3. Search with the system locale and the enabled subtypes
satokcd7cd292010-11-20 15:46:23 +09002015 if (subtype == null) {
2016 subtype = findLastResortApplicableSubtypeLocked(
satok4e4569d2010-11-19 18:45:53 +09002017 mSettings.getEnabledInputMethodSubtypeListLocked(
2018 imi), mode, null, NOT_A_SUBTYPE_ID);
2019 }
satokcd7cd292010-11-20 15:46:23 +09002020 if (subtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002021 // 4. Search with the system locale and all subtypes
satokcd7cd292010-11-20 15:46:23 +09002022 subtype = findLastResortApplicableSubtypeLocked(imi.getSubtypes(),
satok4e4569d2010-11-19 18:45:53 +09002023 mode, null, NOT_A_SUBTYPE_ID);
2024 }
satokcd7cd292010-11-20 15:46:23 +09002025 if (subtype != null) {
satok4e4569d2010-11-19 18:45:53 +09002026 if (imi.getId().equals(mCurMethodId)) {
2027 // The current input method is the most applicable IME.
2028 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002029 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002030 break;
2031 } else if ((imi.getServiceInfo().applicationInfo.flags
2032 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2033 // The system input method is 2nd applicable IME.
2034 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002035 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002036 foundInSystemIME = true;
2037 } else if (!foundInSystemIME) {
2038 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002039 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002040 }
2041 }
2042 }
2043 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002044 if (mostApplicableIMI != null) {
2045 Slog.w(TAG, "Most applicable shortcut input method was:"
2046 + mostApplicableIMI.getId());
2047 if (mostApplicableSubtype != null) {
2048 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2049 + "," + mostApplicableSubtype.getMode() + ","
2050 + mostApplicableSubtype.getLocale());
2051 }
2052 }
satok4e4569d2010-11-19 18:45:53 +09002053 }
satokcd7cd292010-11-20 15:46:23 +09002054 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002055 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002056 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002057 } else {
2058 return null;
2059 }
2060 }
2061
satokab751aa2010-09-14 19:17:36 +09002062 /**
2063 * @return Return the current subtype of this input method.
2064 */
2065 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002066 boolean subtypeIsSelected = false;
2067 try {
2068 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2069 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2070 } catch (SettingNotFoundException e) {
2071 }
satok3ef8b292010-11-23 06:06:29 +09002072 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002073 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002074 String lastInputMethodId = Settings.Secure.getString(
2075 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002076 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2077 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002078 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2079 if (imi != null) {
2080 // If there are no selected subtypes, the framework will try to find
2081 // the most applicable subtype from all subtypes whose mode is
2082 // SUBTYPE_MODE_KEYBOARD. This is an exceptional case, so we will hardcode
2083 // the mode.
satokcd7cd292010-11-20 15:46:23 +09002084 mCurrentSubtype = findLastResortApplicableSubtypeLocked(imi.getSubtypes(),
satok4e4569d2010-11-19 18:45:53 +09002085 SUBTYPE_MODE_KEYBOARD, null, DEFAULT_SUBTYPE_ID);
2086 }
satokcd7cd292010-11-20 15:46:23 +09002087 } else {
satok3ef8b292010-11-23 06:06:29 +09002088 mCurrentSubtype =
2089 mMethodMap.get(lastInputMethodId).getSubtypes().get(subtypeId);
2090 }
satok8fbb1e82010-11-02 23:15:58 +09002091 }
satok3ef8b292010-11-23 06:06:29 +09002092 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002093 }
satokab751aa2010-09-14 19:17:36 +09002094 }
2095
satokf3db1af2010-11-23 13:34:33 +09002096 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2097 InputMethodSubtype subtype) {
2098 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2099 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2100 } else {
2101 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2102 subtypes.add(subtype);
2103 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2104 }
2105 }
2106
satok4e4569d2010-11-19 18:45:53 +09002107 // TODO: We should change the return type from List to List<Parcelable>
2108 public List getShortcutInputMethodsAndSubtypes() {
2109 synchronized (mMethodMap) {
satokf3db1af2010-11-23 13:34:33 +09002110 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002111 // If there are no selected shortcut subtypes, the framework will try to find
2112 // the most applicable subtype from all subtypes whose mode is
2113 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002114 Pair<InputMethodInfo, InputMethodSubtype> info =
2115 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2116 SUBTYPE_MODE_VOICE);
2117 addShortcutInputMethodAndSubtypes(info.first, info.second);
2118 }
satokcd7cd292010-11-20 15:46:23 +09002119 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002120 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2121 ret.add(imi);
2122 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2123 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002124 }
2125 }
satokf3db1af2010-11-23 13:34:33 +09002126 return ret;
satok4e4569d2010-11-19 18:45:53 +09002127 }
2128 }
2129
satokb66d2872010-11-10 01:04:04 +09002130 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2131 synchronized (mMethodMap) {
2132 if (subtype != null && mCurMethodId != null) {
2133 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2134 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2135 if (subtypeId != NOT_A_SUBTYPE_ID) {
2136 setInputMethodLocked(mCurMethodId, subtypeId);
2137 return true;
2138 }
2139 }
2140 return false;
2141 }
2142 }
2143
satokd87c2592010-09-29 11:52:06 +09002144 /**
2145 * Utility class for putting and getting settings for InputMethod
2146 * TODO: Move all putters and getters of settings to this class.
2147 */
2148 private static class InputMethodSettings {
2149 // The string for enabled input method is saved as follows:
2150 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2151 private static final char INPUT_METHOD_SEPARATER = ':';
2152 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002153 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002154 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2155
satok723a27e2010-11-11 14:58:11 +09002156 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002157 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2158
2159 private final ContentResolver mResolver;
2160 private final HashMap<String, InputMethodInfo> mMethodMap;
2161 private final ArrayList<InputMethodInfo> mMethodList;
2162
2163 private String mEnabledInputMethodsStrCache;
2164
2165 private static void buildEnabledInputMethodsSettingString(
2166 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2167 String id = pair.first;
2168 ArrayList<String> subtypes = pair.second;
2169 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002170 // Inputmethod and subtypes are saved in the settings as follows:
2171 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2172 for (String subtypeId: subtypes) {
2173 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002174 }
2175 }
2176
2177 public InputMethodSettings(
2178 ContentResolver resolver, HashMap<String, InputMethodInfo> methodMap,
2179 ArrayList<InputMethodInfo> methodList) {
2180 mResolver = resolver;
2181 mMethodMap = methodMap;
2182 mMethodList = methodList;
2183 }
2184
2185 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2186 return createEnabledInputMethodListLocked(
2187 getEnabledInputMethodsAndSubtypeListLocked());
2188 }
2189
satok7f35c8c2010-10-07 21:13:11 +09002190 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002191 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2192 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002193 getEnabledInputMethodsAndSubtypeListLocked());
2194 }
2195
satok67ddf9c2010-11-17 09:45:54 +09002196 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2197 InputMethodInfo imi) {
2198 List<Pair<String, ArrayList<String>>> imsList =
2199 getEnabledInputMethodsAndSubtypeListLocked();
2200 ArrayList<InputMethodSubtype> enabledSubtypes =
2201 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002202 if (imi != null) {
2203 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2204 InputMethodInfo info = mMethodMap.get(imsPair.first);
2205 if (info != null && info.getId().equals(imi.getId())) {
2206 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
2207 for (InputMethodSubtype ims: subtypes) {
2208 for (String s: imsPair.second) {
2209 if (String.valueOf(ims.hashCode()).equals(s)) {
2210 enabledSubtypes.add(ims);
2211 }
satok67ddf9c2010-11-17 09:45:54 +09002212 }
2213 }
satok884ef9a2010-11-18 10:39:46 +09002214 break;
satok67ddf9c2010-11-17 09:45:54 +09002215 }
satok67ddf9c2010-11-17 09:45:54 +09002216 }
2217 }
2218 return enabledSubtypes;
2219 }
2220
satokd87c2592010-09-29 11:52:06 +09002221 // At the initial boot, the settings for input methods are not set,
2222 // so we need to enable IME in that case.
2223 public void enableAllIMEsIfThereIsNoEnabledIME() {
2224 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2225 StringBuilder sb = new StringBuilder();
2226 final int N = mMethodList.size();
2227 for (int i = 0; i < N; i++) {
2228 InputMethodInfo imi = mMethodList.get(i);
2229 Slog.i(TAG, "Adding: " + imi.getId());
2230 if (i > 0) sb.append(':');
2231 sb.append(imi.getId());
2232 }
2233 putEnabledInputMethodsStr(sb.toString());
2234 }
2235 }
2236
2237 public List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
2238 ArrayList<Pair<String, ArrayList<String>>> imsList
2239 = new ArrayList<Pair<String, ArrayList<String>>>();
2240 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2241 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2242 return imsList;
2243 }
satok723a27e2010-11-11 14:58:11 +09002244 mInputMethodSplitter.setString(enabledInputMethodsStr);
2245 while (mInputMethodSplitter.hasNext()) {
2246 String nextImsStr = mInputMethodSplitter.next();
2247 mSubtypeSplitter.setString(nextImsStr);
2248 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002249 ArrayList<String> subtypeHashes = new ArrayList<String>();
2250 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002251 String imeId = mSubtypeSplitter.next();
2252 while (mSubtypeSplitter.hasNext()) {
2253 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002254 }
2255 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2256 }
2257 }
2258 return imsList;
2259 }
2260
2261 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2262 if (reloadInputMethodStr) {
2263 getEnabledInputMethodsStr();
2264 }
2265 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2266 // Add in the newly enabled input method.
2267 putEnabledInputMethodsStr(id);
2268 } else {
2269 putEnabledInputMethodsStr(
2270 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2271 }
2272 }
2273
2274 /**
2275 * Build and put a string of EnabledInputMethods with removing specified Id.
2276 * @return the specified id was removed or not.
2277 */
2278 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2279 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2280 boolean isRemoved = false;
2281 boolean needsAppendSeparator = false;
2282 for (Pair<String, ArrayList<String>> ims: imsList) {
2283 String curId = ims.first;
2284 if (curId.equals(id)) {
2285 // We are disabling this input method, and it is
2286 // currently enabled. Skip it to remove from the
2287 // new list.
2288 isRemoved = true;
2289 } else {
2290 if (needsAppendSeparator) {
2291 builder.append(INPUT_METHOD_SEPARATER);
2292 } else {
2293 needsAppendSeparator = true;
2294 }
2295 buildEnabledInputMethodsSettingString(builder, ims);
2296 }
2297 }
2298 if (isRemoved) {
2299 // Update the setting with the new list of input methods.
2300 putEnabledInputMethodsStr(builder.toString());
2301 }
2302 return isRemoved;
2303 }
2304
2305 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2306 List<Pair<String, ArrayList<String>>> imsList) {
2307 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2308 for (Pair<String, ArrayList<String>> ims: imsList) {
2309 InputMethodInfo info = mMethodMap.get(ims.first);
2310 if (info != null) {
2311 res.add(info);
2312 }
2313 }
2314 return res;
2315 }
2316
satok7f35c8c2010-10-07 21:13:11 +09002317 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002318 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002319 List<Pair<String, ArrayList<String>>> imsList) {
2320 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2321 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2322 for (Pair<String, ArrayList<String>> ims : imsList) {
2323 InputMethodInfo info = mMethodMap.get(ims.first);
2324 if (info != null) {
2325 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2326 }
2327 }
2328 return res;
2329 }
2330
satokd87c2592010-09-29 11:52:06 +09002331 private void putEnabledInputMethodsStr(String str) {
2332 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2333 mEnabledInputMethodsStrCache = str;
2334 }
2335
2336 private String getEnabledInputMethodsStr() {
2337 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2338 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002339 if (DEBUG) {
2340 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2341 }
satokd87c2592010-09-29 11:52:06 +09002342 return mEnabledInputMethodsStrCache;
2343 }
satok723a27e2010-11-11 14:58:11 +09002344
2345 private void saveSubtypeHistory(
2346 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2347 StringBuilder builder = new StringBuilder();
2348 boolean isImeAdded = false;
2349 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2350 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2351 newSubtypeId);
2352 isImeAdded = true;
2353 }
2354 for (Pair<String, String> ime: savedImes) {
2355 String imeId = ime.first;
2356 String subtypeId = ime.second;
2357 if (TextUtils.isEmpty(subtypeId)) {
2358 subtypeId = NOT_A_SUBTYPE_ID_STR;
2359 }
2360 if (isImeAdded) {
2361 builder.append(INPUT_METHOD_SEPARATER);
2362 } else {
2363 isImeAdded = true;
2364 }
2365 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2366 subtypeId);
2367 }
2368 // Remove the last INPUT_METHOD_SEPARATER
2369 putSubtypeHistoryStr(builder.toString());
2370 }
2371
2372 public void addSubtypeToHistory(String imeId, String subtypeId) {
2373 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2374 for (Pair<String, String> ime: subtypeHistory) {
2375 if (ime.first.equals(imeId)) {
2376 if (DEBUG) {
2377 Slog.v(TAG, "Subtype found in the history: " + imeId
2378 + ime.second);
2379 }
2380 // We should break here
2381 subtypeHistory.remove(ime);
2382 break;
2383 }
2384 }
2385 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2386 }
2387
2388 private void putSubtypeHistoryStr(String str) {
2389 if (DEBUG) {
2390 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2391 }
2392 Settings.Secure.putString(
2393 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2394 }
2395
2396 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2397 // Gets the first one from the history
2398 return getLastSubtypeForInputMethodLockedInternal(null);
2399 }
2400
2401 public String getLastSubtypeForInputMethodLocked(String imeId) {
2402 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2403 if (ime != null) {
2404 return ime.second;
2405 } else {
2406 return null;
2407 }
2408 }
2409
2410 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2411 List<Pair<String, ArrayList<String>>> enabledImes =
2412 getEnabledInputMethodsAndSubtypeListLocked();
2413 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2414 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2415 final String imeInTheHistory = imeAndSubtype.first;
2416 // If imeId is empty, returns the first IME and subtype in the history
2417 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2418 final String subtypeInTheHistory = imeAndSubtype.second;
2419 final String subtypeHashCode = getEnabledSubtypeForInputMethodAndSubtypeLocked(
2420 enabledImes, imeInTheHistory, subtypeInTheHistory);
2421 if (!TextUtils.isEmpty(subtypeHashCode)) {
2422 if (DEBUG) {
2423 Slog.d(TAG, "Enabled subtype found in the history:" + subtypeHashCode);
2424 }
2425 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2426 }
2427 }
2428 }
2429 if (DEBUG) {
2430 Slog.d(TAG, "No enabled IME found in the history");
2431 }
2432 return null;
2433 }
2434
2435 private String getEnabledSubtypeForInputMethodAndSubtypeLocked(List<Pair<String,
2436 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2437 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2438 if (enabledIme.first.equals(imeId)) {
2439 for (String s: enabledIme.second) {
2440 if (s.equals(subtypeHashCode)) {
2441 // If both imeId and subtypeId are enabled, return subtypeId.
2442 return s;
2443 }
2444 }
2445 // If imeId was enabled but subtypeId was disabled.
2446 return NOT_A_SUBTYPE_ID_STR;
2447 }
2448 }
2449 // If both imeId and subtypeId are disabled, return null
2450 return null;
2451 }
2452
2453 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2454 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2455 final String subtypeHistoryStr = getSubtypeHistoryStr();
2456 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2457 return imsList;
2458 }
2459 mInputMethodSplitter.setString(subtypeHistoryStr);
2460 while (mInputMethodSplitter.hasNext()) {
2461 String nextImsStr = mInputMethodSplitter.next();
2462 mSubtypeSplitter.setString(nextImsStr);
2463 if (mSubtypeSplitter.hasNext()) {
2464 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2465 // The first element is ime id.
2466 String imeId = mSubtypeSplitter.next();
2467 while (mSubtypeSplitter.hasNext()) {
2468 subtypeId = mSubtypeSplitter.next();
2469 break;
2470 }
2471 imsList.add(new Pair<String, String>(imeId, subtypeId));
2472 }
2473 }
2474 return imsList;
2475 }
2476
2477 private String getSubtypeHistoryStr() {
2478 if (DEBUG) {
2479 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2480 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2481 }
2482 return Settings.Secure.getString(
2483 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2484 }
2485
2486 public void putSelectedInputMethod(String imeId) {
2487 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2488 }
2489
2490 public void putSelectedSubtype(int subtypeId) {
2491 Settings.Secure.putInt(
2492 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2493 }
satokd87c2592010-09-29 11:52:06 +09002494 }
2495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 @Override
2499 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2500 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2501 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2504 + Binder.getCallingPid()
2505 + ", uid=" + Binder.getCallingUid());
2506 return;
2507 }
2508
2509 IInputMethod method;
2510 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 synchronized (mMethodMap) {
2515 p.println("Current Input Method Manager state:");
2516 int N = mMethodList.size();
2517 p.println(" Input Methods:");
2518 for (int i=0; i<N; i++) {
2519 InputMethodInfo info = mMethodList.get(i);
2520 p.println(" InputMethod #" + i + ":");
2521 info.dump(p, " ");
2522 }
2523 p.println(" Clients:");
2524 for (ClientState ci : mClients.values()) {
2525 p.println(" Client " + ci + ":");
2526 p.println(" client=" + ci.client);
2527 p.println(" inputContext=" + ci.inputContext);
2528 p.println(" sessionRequested=" + ci.sessionRequested);
2529 p.println(" curSession=" + ci.curSession);
2530 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002531 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002533 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2534 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2536 + " mBoundToMethod=" + mBoundToMethod);
2537 p.println(" mCurToken=" + mCurToken);
2538 p.println(" mCurIntent=" + mCurIntent);
2539 method = mCurMethod;
2540 p.println(" mCurMethod=" + mCurMethod);
2541 p.println(" mEnabledSession=" + mEnabledSession);
2542 p.println(" mShowRequested=" + mShowRequested
2543 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2544 + " mShowForced=" + mShowForced
2545 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002546 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002548
Jeff Brownb88102f2010-09-08 11:49:43 -07002549 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 pw.flush();
2552 try {
2553 client.client.asBinder().dump(fd, args);
2554 } catch (RemoteException e) {
2555 p.println("Input method client dead: " + e);
2556 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002557 } else {
2558 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002560
Jeff Brownb88102f2010-09-08 11:49:43 -07002561 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563 pw.flush();
2564 try {
2565 method.asBinder().dump(fd, args);
2566 } catch (RemoteException e) {
2567 p.println("Input method service dead: " + e);
2568 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002569 } else {
2570 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 }
2572 }
2573}