blob: 723432d14844e92f25ecc212e21ce46244b4f6fb [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;
satok4e4569d2010-11-19 18:45:53 +090058import android.os.Parcelable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080060import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.ServiceManager;
62import android.os.SystemClock;
63import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070064import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090065import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.text.TextUtils;
67import android.util.EventLog;
satokab751aa2010-09-14 19:17:36 +090068import android.util.Pair;
Joe Onorato8a9b2202010-02-26 18:56:32 -080069import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.util.PrintWriterPrinter;
71import android.util.Printer;
72import android.view.IWindowManager;
73import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputBinding;
76import android.view.inputmethod.InputMethod;
77import android.view.inputmethod.InputMethodInfo;
78import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090079import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
81import java.io.FileDescriptor;
82import java.io.IOException;
83import java.io.PrintWriter;
satok913a8922010-08-26 21:53:41 +090084import java.text.Collator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import java.util.ArrayList;
86import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +090087import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import java.util.List;
satok913a8922010-08-26 21:53:41 +090089import java.util.Map;
90import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92/**
93 * This class provides a system service that manages input methods.
94 */
95public class InputMethodManagerService extends IInputMethodManager.Stub
96 implements ServiceConnection, Handler.Callback {
97 static final boolean DEBUG = false;
98 static final String TAG = "InputManagerService";
99
100 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900101 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900102 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 static final int MSG_UNBIND_INPUT = 1000;
105 static final int MSG_BIND_INPUT = 1010;
106 static final int MSG_SHOW_SOFT_INPUT = 1020;
107 static final int MSG_HIDE_SOFT_INPUT = 1030;
108 static final int MSG_ATTACH_TOKEN = 1040;
109 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 static final int MSG_START_INPUT = 2000;
112 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 static final int MSG_UNBIND_METHOD = 3000;
115 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800118
satokab751aa2010-09-14 19:17:36 +0900119 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900120 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok8fbb1e82010-11-02 23:15:58 +0900121 // If IME doesn't support the system locale, the default subtype will be the first defined one.
122 private static final int DEFAULT_SUBTYPE_ID = 0;
satokab751aa2010-09-14 19:17:36 +0900123
satok4e4569d2010-11-19 18:45:53 +0900124 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
125 private static final String SUBTYPE_MODE_VOICE = "voice";
126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 final Context mContext;
128 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;
471 mHandler = new Handler(this);
472 mIWindowManager = IWindowManager.Stub.asInterface(
473 ServiceManager.getService(Context.WINDOW_SERVICE));
474 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
475 public void executeMessage(Message msg) {
476 handleMessage(msg);
477 }
478 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800479
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800480 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 IntentFilter screenOnOffFilt = new IntentFilter();
483 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
484 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700485 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800487
satok913a8922010-08-26 21:53:41 +0900488 mStatusBar = statusBar;
489 statusBar.setIconVisibility("ime", false);
490
satokd87c2592010-09-29 11:52:06 +0900491 // mSettings should be created before buildInputMethodListLocked
492 mSettings = new InputMethodSettings(context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900494 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495
satokd87c2592010-09-29 11:52:06 +0900496 if (TextUtils.isEmpty(Settings.Secure.getString(
497 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900499 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
501 try {
satokd87c2592010-09-29 11:52:06 +0900502 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 imi.getPackageName(), 0).getResources();
504 if (res.getBoolean(imi.getIsDefaultResourceId())) {
505 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800506 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 }
508 } catch (PackageManager.NameNotFoundException ex) {
509 } catch (Resources.NotFoundException ex) {
510 }
511 }
512 }
satokd87c2592010-09-29 11:52:06 +0900513 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800515 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900518 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 }
520 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 mSettingsObserver = new SettingsObserver(mHandler);
523 updateFromSettingsLocked();
524 }
525
526 @Override
527 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
528 throws RemoteException {
529 try {
530 return super.onTransact(code, data, reply, flags);
531 } catch (RuntimeException e) {
532 // The input method manager only throws security exceptions, so let's
533 // log all others.
534 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800535 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 }
537 throw e;
538 }
539 }
540
541 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700542 synchronized (mMethodMap) {
543 if (!mSystemReady) {
544 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700545 try {
546 startInputInnerLocked();
547 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800548 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700549 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700550 }
551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 public List<InputMethodInfo> getInputMethodList() {
555 synchronized (mMethodMap) {
556 return new ArrayList<InputMethodInfo>(mMethodList);
557 }
558 }
559
560 public List<InputMethodInfo> getEnabledInputMethodList() {
561 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900562 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 }
564 }
565
satok67ddf9c2010-11-17 09:45:54 +0900566 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi) {
567 synchronized (mMethodMap) {
satok884ef9a2010-11-18 10:39:46 +0900568 if (imi == null && mCurMethodId != null) {
569 imi = mMethodMap.get(mCurMethodId);
570 }
satok67ddf9c2010-11-17 09:45:54 +0900571 return mSettings.getEnabledInputMethodSubtypeListLocked(imi);
572 }
573 }
574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 public void addClient(IInputMethodClient client,
576 IInputContext inputContext, int uid, int pid) {
577 synchronized (mMethodMap) {
578 mClients.put(client.asBinder(), new ClientState(client,
579 inputContext, uid, pid));
580 }
581 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 public void removeClient(IInputMethodClient client) {
584 synchronized (mMethodMap) {
585 mClients.remove(client.asBinder());
586 }
587 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 void executeOrSendMessage(IInterface target, Message msg) {
590 if (target.asBinder() instanceof Binder) {
591 mCaller.sendMessage(msg);
592 } else {
593 handleMessage(msg);
594 msg.recycle();
595 }
596 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800597
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700598 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800600 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 + mCurClient.client.asBinder());
602 if (mBoundToMethod) {
603 mBoundToMethod = false;
604 if (mCurMethod != null) {
605 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
606 MSG_UNBIND_INPUT, mCurMethod));
607 }
608 }
609 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
610 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
611 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 // Call setActive(false) on the old client
614 try {
615 mCurClient.client.setActive(false);
616 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800617 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 + mCurClient.pid + " uid " + mCurClient.uid);
619 }
620 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800621
The Android Open Source Project10592532009-03-18 17:39:46 -0700622 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 }
624 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 private int getImeShowFlags() {
627 int flags = 0;
628 if (mShowForced) {
629 flags |= InputMethod.SHOW_FORCED
630 | InputMethod.SHOW_EXPLICIT;
631 } else if (mShowExplicitlyRequested) {
632 flags |= InputMethod.SHOW_EXPLICIT;
633 }
634 return flags;
635 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 private int getAppShowFlags() {
638 int flags = 0;
639 if (mShowForced) {
640 flags |= InputMethodManager.SHOW_FORCED;
641 } else if (!mShowExplicitlyRequested) {
642 flags |= InputMethodManager.SHOW_IMPLICIT;
643 }
644 return flags;
645 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
648 if (!mBoundToMethod) {
649 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
650 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
651 mBoundToMethod = true;
652 }
653 final SessionState session = mCurClient.curSession;
654 if (initial) {
655 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
656 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
657 } else {
658 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
659 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
660 }
661 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800662 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800663 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 }
665 return needResult
666 ? new InputBindResult(session.session, mCurId, mCurSeq)
667 : null;
668 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 InputBindResult startInputLocked(IInputMethodClient client,
671 IInputContext inputContext, EditorInfo attribute,
672 boolean initial, boolean needResult) {
673 // If no method is currently selected, do nothing.
674 if (mCurMethodId == null) {
675 return mNoBinding;
676 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 ClientState cs = mClients.get(client.asBinder());
679 if (cs == null) {
680 throw new IllegalArgumentException("unknown client "
681 + client.asBinder());
682 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 try {
685 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
686 // Check with the window manager to make sure this client actually
687 // has a window with focus. If not, reject. This is thread safe
688 // because if the focus changes some time before or after, the
689 // next client receiving focus that has any interest in input will
690 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800691 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
693 return null;
694 }
695 } catch (RemoteException e) {
696 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 if (mCurClient != cs) {
699 // If the client is changing, we need to switch over to the new
700 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700701 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800702 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 + cs.client.asBinder());
704
705 // If the screen is on, inform the new client it is active
706 if (mScreenOn) {
707 try {
708 cs.client.setActive(mScreenOn);
709 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800710 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 + cs.pid + " uid " + cs.uid);
712 }
713 }
714 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 // Bump up the sequence for this client and attach it.
717 mCurSeq++;
718 if (mCurSeq <= 0) mCurSeq = 1;
719 mCurClient = cs;
720 mCurInputContext = inputContext;
721 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 // Check if the input method is changing.
724 if (mCurId != null && mCurId.equals(mCurMethodId)) {
725 if (cs.curSession != null) {
726 // Fast case: if we are already connected to the input method,
727 // then just return it.
728 return attachNewInputLocked(initial, needResult);
729 }
730 if (mHaveConnection) {
731 if (mCurMethod != null) {
732 if (!cs.sessionRequested) {
733 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800734 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
736 MSG_CREATE_SESSION, mCurMethod,
737 new MethodCallback(mCurMethod)));
738 }
739 // Return to client, and we will get back with it when
740 // we have had a session made for it.
741 return new InputBindResult(null, mCurId, mCurSeq);
742 } else if (SystemClock.uptimeMillis()
743 < (mLastBindTime+TIME_TO_RECONNECT)) {
744 // In this case we have connected to the service, but
745 // don't yet have its interface. If it hasn't been too
746 // long since we did the connection, we'll return to
747 // the client and wait to get the service interface so
748 // we can report back. If it has been too long, we want
749 // to fall through so we can try a disconnect/reconnect
750 // to see if we can get back in touch with the service.
751 return new InputBindResult(null, mCurId, mCurSeq);
752 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800753 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
754 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
756 }
757 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800758
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700759 return startInputInnerLocked();
760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800761
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700762 InputBindResult startInputInnerLocked() {
763 if (mCurMethodId == null) {
764 return mNoBinding;
765 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800766
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700767 if (!mSystemReady) {
768 // If the system is not yet ready, we shouldn't be running third
769 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700770 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700771 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 InputMethodInfo info = mMethodMap.get(mCurMethodId);
774 if (info == null) {
775 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
776 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800777
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700778 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
781 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700782 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
783 com.android.internal.R.string.input_method_binding_label);
784 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
785 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
787 mLastBindTime = SystemClock.uptimeMillis();
788 mHaveConnection = true;
789 mCurId = info.getId();
790 mCurToken = new Binder();
791 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800792 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 mIWindowManager.addWindowToken(mCurToken,
794 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
795 } catch (RemoteException e) {
796 }
797 return new InputBindResult(null, mCurId, mCurSeq);
798 } else {
799 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800800 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 + mCurIntent);
802 }
803 return null;
804 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 public InputBindResult startInput(IInputMethodClient client,
807 IInputContext inputContext, EditorInfo attribute,
808 boolean initial, boolean needResult) {
809 synchronized (mMethodMap) {
810 final long ident = Binder.clearCallingIdentity();
811 try {
812 return startInputLocked(client, inputContext, attribute,
813 initial, needResult);
814 } finally {
815 Binder.restoreCallingIdentity(ident);
816 }
817 }
818 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 public void finishInput(IInputMethodClient client) {
821 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 public void onServiceConnected(ComponentName name, IBinder service) {
824 synchronized (mMethodMap) {
825 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
826 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700827 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800828 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700829 unbindCurrentMethodLocked(false);
830 return;
831 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800832 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700833 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
834 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800836 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700837 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700839 MSG_CREATE_SESSION, mCurMethod,
840 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 }
842 }
843 }
844 }
845
846 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
847 synchronized (mMethodMap) {
848 if (mCurMethod != null && method != null
849 && mCurMethod.asBinder() == method.asBinder()) {
850 if (mCurClient != null) {
851 mCurClient.curSession = new SessionState(mCurClient,
852 method, session);
853 mCurClient.sessionRequested = false;
854 InputBindResult res = attachNewInputLocked(true, true);
855 if (res.method != null) {
856 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
857 MSG_BIND_METHOD, mCurClient.client, res));
858 }
859 }
860 }
861 }
862 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800863
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700864 void unbindCurrentMethodLocked(boolean reportToClient) {
865 if (mHaveConnection) {
866 mContext.unbindService(this);
867 mHaveConnection = false;
868 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800869
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700870 if (mCurToken != null) {
871 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800872 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700873 mIWindowManager.removeWindowToken(mCurToken);
874 } catch (RemoteException e) {
875 }
876 mCurToken = null;
877 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800878
The Android Open Source Project10592532009-03-18 17:39:46 -0700879 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700880 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800881
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700882 if (reportToClient && mCurClient != null) {
883 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
884 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
885 }
886 }
887
Devin Taylor0c33ed22010-02-23 13:26:46 -0600888 private void finishSession(SessionState sessionState) {
889 if (sessionState != null && sessionState.session != null) {
890 try {
891 sessionState.session.finishSession();
892 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700893 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600894 }
895 }
896 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800897
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700898 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 if (mCurMethod != null) {
900 for (ClientState cs : mClients.values()) {
901 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600902 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 cs.curSession = null;
904 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600905
906 finishSession(mEnabledSession);
907 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 mCurMethod = null;
909 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700910 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 public void onServiceDisconnected(ComponentName name) {
914 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800915 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 + " mCurIntent=" + mCurIntent);
917 if (mCurMethod != null && mCurIntent != null
918 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700919 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 // We consider this to be a new bind attempt, since the system
921 // should now try to restart the service for us.
922 mLastBindTime = SystemClock.uptimeMillis();
923 mShowRequested = mInputShown;
924 mInputShown = false;
925 if (mCurClient != null) {
926 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
927 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
928 }
929 }
930 }
931 }
932
933 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700934 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 long ident = Binder.clearCallingIdentity();
936 try {
937 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700938 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 return;
940 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 synchronized (mMethodMap) {
943 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800944 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700945 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800947 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700948 mStatusBar.setIcon("ime", packageName, iconId, 0);
949 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 }
951 }
952 } finally {
953 Binder.restoreCallingIdentity(ident);
954 }
955 }
956
satok06487a52010-10-29 11:37:18 +0900957 public void setIMEButtonVisible(IBinder token, boolean visible) {
958 int uid = Binder.getCallingUid();
959 long ident = Binder.clearCallingIdentity();
960 try {
961 if (token == null || mCurToken != token) {
962 Slog.w(TAG, "Ignoring setIMEButtonVisible of uid " + uid + " token: " + token);
963 return;
964 }
965
966 synchronized (mMethodMap) {
967 mStatusBar.setIMEButtonVisible(visible);
968 }
969 } finally {
970 Binder.restoreCallingIdentity(ident);
971 }
972 }
973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700975 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
976 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
977 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
978 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +0900980 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +0900981 // There is no input method selected, try to choose new applicable input method.
982 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
983 id = Settings.Secure.getString(mContext.getContentResolver(),
984 Settings.Secure.DEFAULT_INPUT_METHOD);
985 }
986 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 try {
satokab751aa2010-09-14 19:17:36 +0900988 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800990 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -0700991 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700992 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 }
satokf3db1af2010-11-23 13:34:33 +0900994 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700995 } else {
996 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -0700997 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700998 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 }
1000 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001001
satokab751aa2010-09-14 19:17:36 +09001002 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 InputMethodInfo info = mMethodMap.get(id);
1004 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001005 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 if (id.equals(mCurMethodId)) {
satokb66d2872010-11-10 01:04:04 +09001009 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
1010 if (subtypeId >= 0 && subtypeId < subtypes.size()) {
1011 InputMethodSubtype subtype = subtypes.get(subtypeId);
satokab751aa2010-09-14 19:17:36 +09001012 if (subtype != mCurrentSubtype) {
1013 synchronized (mMethodMap) {
1014 if (mCurMethod != null) {
1015 try {
satok723a27e2010-11-11 14:58:11 +09001016 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
satok06e07442010-11-02 19:46:55 +09001017 if (mInputShown) {
1018 // If mInputShown is false, there is no IME button on the
1019 // system bar.
1020 // Thus there is no need to make it invisible explicitly.
1021 mStatusBar.setIMEButtonVisible(true);
1022 }
satokab751aa2010-09-14 19:17:36 +09001023 mCurMethod.changeInputMethodSubtype(subtype);
1024 } catch (RemoteException e) {
1025 return;
1026 }
1027 }
1028 }
1029 }
1030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 return;
1032 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 final long ident = Binder.clearCallingIdentity();
1035 try {
satokab751aa2010-09-14 19:17:36 +09001036 // Set a subtype to this input method.
1037 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001038 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1039 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1040 // because mCurMethodId is stored as a history in
1041 // setSelectedInputMethodAndSubtypeLocked().
1042 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043
1044 if (ActivityManagerNative.isSystemReady()) {
1045 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001046 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 intent.putExtra("input_method_id", id);
1048 mContext.sendBroadcast(intent);
1049 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001050 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 } finally {
1052 Binder.restoreCallingIdentity(ident);
1053 }
1054 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001055
The Android Open Source Project4df24232009-03-05 14:34:35 -08001056 public boolean showSoftInput(IInputMethodClient client, int flags,
1057 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001058 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 long ident = Binder.clearCallingIdentity();
1060 try {
1061 synchronized (mMethodMap) {
1062 if (mCurClient == null || client == null
1063 || mCurClient.client.asBinder() != client.asBinder()) {
1064 try {
1065 // We need to check if this is the current client with
1066 // focus in the window manager, to allow this call to
1067 // be made before input is started in it.
1068 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001069 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001070 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 }
1072 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001073 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 }
1075 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001076
Joe Onorato8a9b2202010-02-26 18:56:32 -08001077 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001078 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 }
1080 } finally {
1081 Binder.restoreCallingIdentity(ident);
1082 }
1083 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001084
The Android Open Source Project4df24232009-03-05 14:34:35 -08001085 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 mShowRequested = true;
1087 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1088 mShowExplicitlyRequested = true;
1089 }
1090 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1091 mShowExplicitlyRequested = true;
1092 mShowForced = true;
1093 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001094
Dianne Hackborncc278702009-09-02 23:07:23 -07001095 if (!mSystemReady) {
1096 return false;
1097 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001098
The Android Open Source Project4df24232009-03-05 14:34:35 -08001099 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001101 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1102 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1103 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001105 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 } else if (mHaveConnection && SystemClock.uptimeMillis()
1107 < (mLastBindTime+TIME_TO_RECONNECT)) {
1108 // The client has asked to have the input method shown, but
1109 // we have been sitting here too long with a connection to the
1110 // service and no interface received, so let's disconnect/connect
1111 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001112 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 SystemClock.uptimeMillis()-mLastBindTime,1);
1114 mContext.unbindService(this);
1115 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1116 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001117
The Android Open Source Project4df24232009-03-05 14:34:35 -08001118 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001120
The Android Open Source Project4df24232009-03-05 14:34:35 -08001121 public boolean hideSoftInput(IInputMethodClient client, int flags,
1122 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001123 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 long ident = Binder.clearCallingIdentity();
1125 try {
1126 synchronized (mMethodMap) {
1127 if (mCurClient == null || client == null
1128 || mCurClient.client.asBinder() != client.asBinder()) {
1129 try {
1130 // We need to check if this is the current client with
1131 // focus in the window manager, to allow this call to
1132 // be made before input is started in it.
1133 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001134 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1135 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001136 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 }
1138 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001139 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 }
1141 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001142
Joe Onorato8a9b2202010-02-26 18:56:32 -08001143 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001144 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 }
1146 } finally {
1147 Binder.restoreCallingIdentity(ident);
1148 }
1149 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001150
The Android Open Source Project4df24232009-03-05 14:34:35 -08001151 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1153 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001154 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001156 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 }
1158 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001159 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001161 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001163 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001165 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1166 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1167 res = true;
1168 } else {
1169 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 }
1171 mInputShown = false;
1172 mShowRequested = false;
1173 mShowExplicitlyRequested = false;
1174 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001175 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001177
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001178 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1180 boolean first, int windowFlags) {
1181 long ident = Binder.clearCallingIdentity();
1182 try {
1183 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001184 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 + " viewHasFocus=" + viewHasFocus
1186 + " isTextEditor=" + isTextEditor
1187 + " softInputMode=#" + Integer.toHexString(softInputMode)
1188 + " first=" + first + " flags=#"
1189 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 if (mCurClient == null || client == null
1192 || mCurClient.client.asBinder() != client.asBinder()) {
1193 try {
1194 // We need to check if this is the current client with
1195 // focus in the window manager, to allow this call to
1196 // be made before input is started in it.
1197 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001198 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 return;
1200 }
1201 } catch (RemoteException e) {
1202 }
1203 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001204
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001205 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001206 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001207 return;
1208 }
1209 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1212 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
1213 if (!isTextEditor || (softInputMode &
1214 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1215 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) {
1216 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1217 // There is no focus view, and this window will
1218 // be behind any soft input window, so hide the
1219 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001220 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001221 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 }
1223 } else if (isTextEditor && (softInputMode &
1224 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1225 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1226 && (softInputMode &
1227 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1228 // There is a focus view, and we are navigating forward
1229 // into the window, so show the input window for the user.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001230 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001231 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 }
1233 break;
1234 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1235 // Do nothing.
1236 break;
1237 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1238 if ((softInputMode &
1239 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001240 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001241 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 }
1243 break;
1244 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001245 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001246 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 break;
1248 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1249 if ((softInputMode &
1250 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001251 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001252 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 }
1254 break;
1255 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001256 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001257 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 break;
1259 }
1260 }
1261 } finally {
1262 Binder.restoreCallingIdentity(ident);
1263 }
1264 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1267 synchronized (mMethodMap) {
1268 if (mCurClient == null || client == null
1269 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001270 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001271 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 }
1273
1274 mHandler.sendEmptyMessage(MSG_SHOW_IM_PICKER);
1275 }
1276 }
1277
satokab751aa2010-09-14 19:17:36 +09001278 public void showInputMethodSubtypePickerFromClient(IInputMethodClient client) {
1279 synchronized (mMethodMap) {
1280 if (mCurClient == null || client == null
1281 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001282 Slog.w(TAG, "Ignoring showInputMethodSubtypePickerFromClient of: " + client);
satokab751aa2010-09-14 19:17:36 +09001283 }
1284
1285 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1286 }
1287 }
1288
satok47a44912010-10-06 16:03:58 +09001289 public void showInputMethodAndSubtypeEnablerFromClient(
1290 IInputMethodClient client, String topId) {
1291 // TODO: Handle topId for setting the top position of the list activity
1292 synchronized (mMethodMap) {
1293 if (mCurClient == null || client == null
1294 || mCurClient.client.asBinder() != client.asBinder()) {
1295 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1296 }
1297
1298 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_ENABLER);
1299 }
1300 }
1301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001303 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1304 }
1305
1306 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1307 synchronized (mMethodMap) {
1308 if (subtype != null) {
1309 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1310 mMethodMap.get(id), subtype.hashCode()));
1311 } else {
1312 setInputMethod(token, id);
1313 }
1314 }
satokab751aa2010-09-14 19:17:36 +09001315 }
1316
satok735cf382010-11-11 20:40:09 +09001317 public boolean switchToLastInputMethod(IBinder token) {
1318 synchronized (mMethodMap) {
1319 Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1320 if (lastIme != null) {
1321 InputMethodInfo imi = mMethodMap.get(lastIme.first);
1322 if (imi != null) {
satok28203512010-11-24 11:06:49 +09001323 setInputMethodWithSubtypeId(token, lastIme.first, getSubtypeIdFromHashCode(
satok735cf382010-11-11 20:40:09 +09001324 imi, Integer.valueOf(lastIme.second)));
1325 return true;
1326 }
1327 }
1328 return false;
1329 }
1330 }
1331
satok28203512010-11-24 11:06:49 +09001332 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 synchronized (mMethodMap) {
1334 if (token == null) {
1335 if (mContext.checkCallingOrSelfPermission(
1336 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1337 != PackageManager.PERMISSION_GRANTED) {
1338 throw new SecurityException(
1339 "Using null token requires permission "
1340 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1341 }
1342 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001343 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1344 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 return;
1346 }
1347
1348 long ident = Binder.clearCallingIdentity();
1349 try {
satokab751aa2010-09-14 19:17:36 +09001350 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 } finally {
1352 Binder.restoreCallingIdentity(ident);
1353 }
1354 }
1355 }
1356
1357 public void hideMySoftInput(IBinder token, int flags) {
1358 synchronized (mMethodMap) {
1359 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001360 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1361 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 return;
1363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 long ident = Binder.clearCallingIdentity();
1365 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001366 hideCurrentInputLocked(flags, null);
1367 } finally {
1368 Binder.restoreCallingIdentity(ident);
1369 }
1370 }
1371 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001372
The Android Open Source Project4df24232009-03-05 14:34:35 -08001373 public void showMySoftInput(IBinder token, int flags) {
1374 synchronized (mMethodMap) {
1375 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001376 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1377 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001378 return;
1379 }
1380 long ident = Binder.clearCallingIdentity();
1381 try {
1382 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 } finally {
1384 Binder.restoreCallingIdentity(ident);
1385 }
1386 }
1387 }
1388
1389 void setEnabledSessionInMainThread(SessionState session) {
1390 if (mEnabledSession != session) {
1391 if (mEnabledSession != null) {
1392 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001393 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 mEnabledSession.method.setSessionEnabled(
1395 mEnabledSession.session, false);
1396 } catch (RemoteException e) {
1397 }
1398 }
1399 mEnabledSession = session;
1400 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001401 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 session.method.setSessionEnabled(
1403 session.session, true);
1404 } catch (RemoteException e) {
1405 }
1406 }
1407 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 public boolean handleMessage(Message msg) {
1410 HandlerCaller.SomeArgs args;
1411 switch (msg.what) {
1412 case MSG_SHOW_IM_PICKER:
1413 showInputMethodMenu();
1414 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001415
satokab751aa2010-09-14 19:17:36 +09001416 case MSG_SHOW_IM_SUBTYPE_PICKER:
1417 showInputMethodSubtypeMenu();
1418 return true;
1419
satok47a44912010-10-06 16:03:58 +09001420 case MSG_SHOW_IM_SUBTYPE_ENABLER:
1421 showInputMethodAndSubtypeEnabler();
1422 return true;
1423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 case MSG_UNBIND_INPUT:
1427 try {
1428 ((IInputMethod)msg.obj).unbindInput();
1429 } catch (RemoteException e) {
1430 // There is nothing interesting about the method dying.
1431 }
1432 return true;
1433 case MSG_BIND_INPUT:
1434 args = (HandlerCaller.SomeArgs)msg.obj;
1435 try {
1436 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1437 } catch (RemoteException e) {
1438 }
1439 return true;
1440 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001441 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001443 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1444 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 } catch (RemoteException e) {
1446 }
1447 return true;
1448 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001449 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001451 ((IInputMethod)args.arg1).hideSoftInput(0,
1452 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 } catch (RemoteException e) {
1454 }
1455 return true;
1456 case MSG_ATTACH_TOKEN:
1457 args = (HandlerCaller.SomeArgs)msg.obj;
1458 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001459 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1461 } catch (RemoteException e) {
1462 }
1463 return true;
1464 case MSG_CREATE_SESSION:
1465 args = (HandlerCaller.SomeArgs)msg.obj;
1466 try {
1467 ((IInputMethod)args.arg1).createSession(
1468 (IInputMethodCallback)args.arg2);
1469 } catch (RemoteException e) {
1470 }
1471 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 case MSG_START_INPUT:
1475 args = (HandlerCaller.SomeArgs)msg.obj;
1476 try {
1477 SessionState session = (SessionState)args.arg1;
1478 setEnabledSessionInMainThread(session);
1479 session.method.startInput((IInputContext)args.arg2,
1480 (EditorInfo)args.arg3);
1481 } catch (RemoteException e) {
1482 }
1483 return true;
1484 case MSG_RESTART_INPUT:
1485 args = (HandlerCaller.SomeArgs)msg.obj;
1486 try {
1487 SessionState session = (SessionState)args.arg1;
1488 setEnabledSessionInMainThread(session);
1489 session.method.restartInput((IInputContext)args.arg2,
1490 (EditorInfo)args.arg3);
1491 } catch (RemoteException e) {
1492 }
1493 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 case MSG_UNBIND_METHOD:
1498 try {
1499 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1500 } catch (RemoteException e) {
1501 // There is nothing interesting about the last client dying.
1502 }
1503 return true;
1504 case MSG_BIND_METHOD:
1505 args = (HandlerCaller.SomeArgs)msg.obj;
1506 try {
1507 ((IInputMethodClient)args.arg1).onBindMethod(
1508 (InputBindResult)args.arg2);
1509 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001510 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 }
1512 return true;
1513 }
1514 return false;
1515 }
1516
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001517 private boolean isSystemIme(InputMethodInfo inputMethod) {
1518 return (inputMethod.getServiceInfo().applicationInfo.flags
1519 & ApplicationInfo.FLAG_SYSTEM) != 0;
1520 }
1521
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001522 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001523 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001524 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001525 // We'd prefer to fall back on a system IME, since that is safer.
1526 int i=enabled.size();
1527 while (i > 0) {
1528 i--;
1529 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1530 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1531 break;
1532 }
1533 }
satokab751aa2010-09-14 19:17:36 +09001534 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001535 if (DEBUG) {
1536 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1537 }
satok723a27e2010-11-11 14:58:11 +09001538 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001539 return true;
1540 }
1541
1542 return false;
1543 }
1544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1546 HashMap<String, InputMethodInfo> map) {
1547 list.clear();
1548 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 PackageManager pm = mContext.getPackageManager();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001551 final Configuration config = mContext.getResources().getConfiguration();
1552 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1553 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1554 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1555 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556
1557 List<ResolveInfo> services = pm.queryIntentServices(
1558 new Intent(InputMethod.SERVICE_INTERFACE),
1559 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 for (int i = 0; i < services.size(); ++i) {
1562 ResolveInfo ri = services.get(i);
1563 ServiceInfo si = ri.serviceInfo;
1564 ComponentName compName = new ComponentName(si.packageName, si.name);
1565 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1566 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001567 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 + ": it does not require the permission "
1569 + android.Manifest.permission.BIND_INPUT_METHOD);
1570 continue;
1571 }
1572
Joe Onorato8a9b2202010-02-26 18:56:32 -08001573 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574
1575 try {
1576 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1577 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001578 final String id = p.getId();
1579 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580
Amith Yamasanie861ec12010-03-24 21:39:27 -07001581 // System IMEs are enabled by default, unless there's a hard keyboard
1582 // and the system IME was explicitly disabled
1583 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1584 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001585 }
1586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001588 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001592 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001594 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 }
1596 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001597
1598 String defaultIme = Settings.Secure.getString(mContext
1599 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001600 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001601 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001602 updateFromSettingsLocked();
1603 }
1604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001608
satokab751aa2010-09-14 19:17:36 +09001609 private void showInputMethodMenu() {
1610 showInputMethodMenuInternal(false);
1611 }
1612
1613 private void showInputMethodSubtypeMenu() {
1614 showInputMethodMenuInternal(true);
1615 }
1616
satok47a44912010-10-06 16:03:58 +09001617 private void showInputMethodAndSubtypeEnabler() {
satok86417ea2010-10-27 14:11:03 +09001618 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_AND_SUBTYPE_ENABLER);
satok47a44912010-10-06 16:03:58 +09001619 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001620 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1621 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001622 mContext.startActivity(intent);
1623 }
1624
satokab751aa2010-09-14 19:17:36 +09001625 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001626 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 String lastInputMethodId = Settings.Secure.getString(context
1633 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001634 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001635 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001636
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001637 synchronized (mMethodMap) {
satok7f35c8c2010-10-07 21:13:11 +09001638 final List<Pair<InputMethodInfo, ArrayList<String>>> immis =
satok67ddf9c2010-11-17 09:45:54 +09001639 mSettings.getEnabledInputMethodAndSubtypeHashCodeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001640 ArrayList<Integer> subtypeIds = new ArrayList<Integer>();
1641
1642 if (immis == null || immis.size() == 0) {
1643 return;
1644 }
1645
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001646 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001648 int N = immis.size();
satok913a8922010-08-26 21:53:41 +09001649
satokab751aa2010-09-14 19:17:36 +09001650 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1651 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001652
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001653 for (int i = 0; i < N; ++i) {
satok7f35c8c2010-10-07 21:13:11 +09001654 InputMethodInfo property = immis.get(i).first;
1655 final ArrayList<String> enabledSubtypeIds = immis.get(i).second;
1656 HashSet<String> enabledSubtypeSet = new HashSet<String>();
1657 for (String s : enabledSubtypeIds) {
1658 enabledSubtypeSet.add(s);
1659 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001660 if (property == null) {
1661 continue;
1662 }
satokab751aa2010-09-14 19:17:36 +09001663 ArrayList<InputMethodSubtype> subtypes = property.getSubtypes();
1664 CharSequence label = property.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001665 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokab751aa2010-09-14 19:17:36 +09001666 for (int j = 0; j < subtypes.size(); ++j) {
1667 InputMethodSubtype subtype = subtypes.get(j);
satok7f35c8c2010-10-07 21:13:11 +09001668 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1669 CharSequence title;
1670 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001671 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001672 if (nameResId != 0) {
1673 title = pm.getText(property.getPackageName(), nameResId,
1674 property.getServiceInfo().applicationInfo);
1675 } else {
1676 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001677 // TODO: Use more friendly Title and UI
1678 title = label + "," + (mode == null ? "" : mode) + ","
1679 + (language == null ? "" : language);
1680 }
1681 imMap.put(title, new Pair<InputMethodInfo, Integer>(property, j));
satokab751aa2010-09-14 19:17:36 +09001682 }
satokab751aa2010-09-14 19:17:36 +09001683 }
1684 } else {
1685 imMap.put(label,
1686 new Pair<InputMethodInfo, Integer>(property, NOT_A_SUBTYPE_ID));
1687 subtypeIds.add(0);
1688 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001689 }
satok913a8922010-08-26 21:53:41 +09001690
1691 N = imMap.size();
1692 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001693 mIms = new InputMethodInfo[N];
1694 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001695 int checkedItem = 0;
1696 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001697 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1698 mIms[i] = value.first;
1699 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001700 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001701 int subtypeId = mSubtypeIds[i];
1702 if ((subtypeId == NOT_A_SUBTYPE_ID)
1703 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1704 || (subtypeId == lastInputMethodSubtypeId)) {
1705 checkedItem = i;
1706 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001707 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 }
satokab751aa2010-09-14 19:17:36 +09001709
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001710 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1711 public void onClick(DialogInterface dialog, int which) {
1712 hideInputMethodMenu();
1713 }
1714 };
satokd87c2592010-09-29 11:52:06 +09001715
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001716 TypedArray a = context.obtainStyledAttributes(null,
1717 com.android.internal.R.styleable.DialogPreference,
1718 com.android.internal.R.attr.alertDialogStyle, 0);
1719 mDialogBuilder = new AlertDialog.Builder(context)
1720 .setTitle(com.android.internal.R.string.select_input_method)
1721 .setOnCancelListener(new OnCancelListener() {
1722 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001724 }
1725 })
1726 .setIcon(a.getDrawable(
1727 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1728 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001729
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001730 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1731 new AlertDialog.OnClickListener() {
1732 public void onClick(DialogInterface dialog, int which) {
1733 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001734 if (mIms == null || mIms.length <= which
1735 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001736 return;
1737 }
1738 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001739 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001740 hideInputMethodMenu();
1741 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001742 if ((subtypeId < 0)
1743 || (subtypeId >= im.getSubtypes().size())) {
1744 subtypeId = NOT_A_SUBTYPE_ID;
1745 }
1746 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001747 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001750 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751
satok7f35c8c2010-10-07 21:13:11 +09001752 if (showSubtypes) {
1753 mDialogBuilder.setPositiveButton(com.android.internal.R.string.more_item_label,
1754 new DialogInterface.OnClickListener() {
1755 public void onClick(DialogInterface dialog, int whichButton) {
1756 showInputMethodAndSubtypeEnabler();
1757 }
1758 });
1759 }
satok0ff647b2010-10-08 13:49:28 +09001760 mDialogBuilder.setNegativeButton(com.android.internal.R.string.cancel,
1761 new DialogInterface.OnClickListener() {
1762 public void onClick(DialogInterface dialog, int whichButton) {
1763 hideInputMethodMenu();
1764 }
1765 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 mSwitchingDialog = mDialogBuilder.create();
1767 mSwitchingDialog.getWindow().setType(
1768 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1769 mSwitchingDialog.show();
1770 }
1771 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001774 synchronized (mMethodMap) {
1775 hideInputMethodMenuLocked();
1776 }
1777 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001778
The Android Open Source Project10592532009-03-18 17:39:46 -07001779 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001780 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781
The Android Open Source Project10592532009-03-18 17:39:46 -07001782 if (mSwitchingDialog != null) {
1783 mSwitchingDialog.dismiss();
1784 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001786
The Android Open Source Project10592532009-03-18 17:39:46 -07001787 mDialogBuilder = null;
1788 mItems = null;
1789 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 public boolean setInputMethodEnabled(String id, boolean enabled) {
1795 synchronized (mMethodMap) {
1796 if (mContext.checkCallingOrSelfPermission(
1797 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1798 != PackageManager.PERMISSION_GRANTED) {
1799 throw new SecurityException(
1800 "Requires permission "
1801 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1802 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 long ident = Binder.clearCallingIdentity();
1805 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001806 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 } finally {
1808 Binder.restoreCallingIdentity(ident);
1809 }
1810 }
1811 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001812
1813 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1814 // Make sure this is a valid input method.
1815 InputMethodInfo imm = mMethodMap.get(id);
1816 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001817 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001818 }
1819
satokd87c2592010-09-29 11:52:06 +09001820 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1821 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001822
satokd87c2592010-09-29 11:52:06 +09001823 if (enabled) {
1824 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1825 if (pair.first.equals(id)) {
1826 // We are enabling this input method, but it is already enabled.
1827 // Nothing to do. The previous state was enabled.
1828 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001829 }
1830 }
satokd87c2592010-09-29 11:52:06 +09001831 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1832 // Previous state was disabled.
1833 return false;
1834 } else {
1835 StringBuilder builder = new StringBuilder();
1836 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1837 builder, enabledInputMethodsList, id)) {
1838 // Disabled input method is currently selected, switch to another one.
1839 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1840 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001841 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
1842 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
1843 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09001844 }
1845 // Previous state was enabled.
1846 return true;
1847 } else {
1848 // We are disabling the input method but it is already disabled.
1849 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001850 return false;
1851 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001852 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001853 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001854
satok723a27e2010-11-11 14:58:11 +09001855 private void saveCurrentInputMethodAndSubtypeToHistory() {
1856 String subtypeId = NOT_A_SUBTYPE_ID_STR;
1857 if (mCurrentSubtype != null) {
1858 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
1859 }
1860 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
satokab751aa2010-09-14 19:17:36 +09001861 }
1862
satok723a27e2010-11-11 14:58:11 +09001863 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
1864 boolean setSubtypeOnly) {
1865 // Update the history of InputMethod and Subtype
1866 saveCurrentInputMethodAndSubtypeToHistory();
1867
1868 // Set Subtype here
1869 if (imi == null || subtypeId < 0) {
1870 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001871 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09001872 } else {
1873 final ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1874 if (subtypeId < subtypes.size()) {
1875 mSettings.putSelectedSubtype(subtypes.get(subtypeId).hashCode());
1876 mCurrentSubtype = subtypes.get(subtypeId);
1877 } else {
1878 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
1879 mCurrentSubtype = null;
1880 }
satokab751aa2010-09-14 19:17:36 +09001881 }
satok723a27e2010-11-11 14:58:11 +09001882
1883 if (!setSubtypeOnly) {
1884 // Set InputMethod here
1885 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
1886 }
1887 }
1888
1889 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
1890 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
1891 int lastSubtypeId = NOT_A_SUBTYPE_ID;
1892 // newDefaultIme is empty when there is no candidate for the selected IME.
1893 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
1894 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
1895 if (subtypeHashCode != null) {
1896 try {
1897 lastSubtypeId = getSubtypeIdFromHashCode(
1898 imi, Integer.valueOf(subtypeHashCode));
1899 } catch (NumberFormatException e) {
1900 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
1901 }
1902 }
1903 }
1904 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09001905 }
1906
1907 private int getSelectedInputMethodSubtypeId(String id) {
1908 InputMethodInfo imi = mMethodMap.get(id);
1909 if (imi == null) {
1910 return NOT_A_SUBTYPE_ID;
1911 }
satokab751aa2010-09-14 19:17:36 +09001912 int subtypeId;
1913 try {
1914 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
1915 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
1916 } catch (SettingNotFoundException e) {
1917 return NOT_A_SUBTYPE_ID;
1918 }
satok723a27e2010-11-11 14:58:11 +09001919 return getSubtypeIdFromHashCode(imi, subtypeId);
1920 }
1921
1922 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09001923 if (imi != null) {
1924 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1925 for (int i = 0; i < subtypes.size(); ++i) {
1926 InputMethodSubtype ims = subtypes.get(i);
1927 if (subtypeHashCode == ims.hashCode()) {
1928 return i;
1929 }
satokab751aa2010-09-14 19:17:36 +09001930 }
1931 }
1932 return NOT_A_SUBTYPE_ID;
1933 }
1934
satok4e4569d2010-11-19 18:45:53 +09001935 /**
1936 * If there are no selected subtypes, tries finding the most applicable one according to the
1937 * given locale.
1938 * @param subtypes this function will search the most applicable subtype in subtypes
1939 * @param mode subtypes will be filtered by mode
1940 * @param locale subtypes will be filtered by locale
1941 * @param defaultSubtypeId if this function can't find the most applicable subtype, it will
1942 * return defaultSubtypeId
1943 * @return the most applicable subtypeId
1944 */
1945 private int findLastResortApplicableSubtypeLocked(
1946 List<InputMethodSubtype> subtypes, String mode, String locale, int defaultSubtypeId) {
satok8fbb1e82010-11-02 23:15:58 +09001947 if (subtypes == null || subtypes.size() == 0) {
1948 return NOT_A_SUBTYPE_ID;
1949 }
satok4e4569d2010-11-19 18:45:53 +09001950 if (TextUtils.isEmpty(locale)) {
1951 locale = mContext.getResources().getConfiguration().locale.toString();
1952 }
satok8fbb1e82010-11-02 23:15:58 +09001953 final String language = locale.substring(0, 2);
1954 boolean partialMatchFound = false;
satok4e4569d2010-11-19 18:45:53 +09001955 int applicableSubtypeId = defaultSubtypeId;
satok8fbb1e82010-11-02 23:15:58 +09001956 for (int i = 0; i < subtypes.size(); ++i) {
1957 final String subtypeLocale = subtypes.get(i).getLocale();
satok4e4569d2010-11-19 18:45:53 +09001958 // An applicable subtype should match "mode".
1959 if (subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok9ef02832010-11-04 21:17:48 +09001960 if (locale.equals(subtypeLocale)) {
1961 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
1962 applicableSubtypeId = i;
1963 break;
1964 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
1965 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
1966 applicableSubtypeId = i;
1967 partialMatchFound = true;
1968 }
satok8fbb1e82010-11-02 23:15:58 +09001969 }
1970 }
1971
1972 // The first subtype applicable to the system locale will be defined as the most applicable
1973 // subtype.
1974 if (DEBUG) {
satok03eb319a2010-11-11 18:17:42 +09001975 Slog.d(TAG, "Applicable InputMethodSubtype was found: " + applicableSubtypeId + ","
satok8fbb1e82010-11-02 23:15:58 +09001976 + subtypes.get(applicableSubtypeId).getLocale());
1977 }
1978 return applicableSubtypeId;
1979 }
1980
satok4e4569d2010-11-19 18:45:53 +09001981 // If there are no selected shortcuts, tries finding the most applicable ones.
1982 private Pair<InputMethodInfo, InputMethodSubtype>
1983 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
1984 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1985 InputMethodInfo mostApplicableIMI = null;
1986 int mostApplicableSubtypeId = NOT_A_SUBTYPE_ID;
1987 boolean foundInSystemIME = false;
1988
1989 // Search applicable subtype for each InputMethodInfo
1990 for (InputMethodInfo imi: imis) {
1991 int subtypeId = NOT_A_SUBTYPE_ID;
1992 if (mCurrentSubtype != null) {
1993 // 1. Search with the current subtype's locale and the enabled subtypes
1994 subtypeId = findLastResortApplicableSubtypeLocked(
1995 mSettings.getEnabledInputMethodSubtypeListLocked(
1996 imi), mode, mCurrentSubtype.getLocale(), NOT_A_SUBTYPE_ID);
1997 if (subtypeId == NOT_A_SUBTYPE_ID) {
1998 // 2. Search with the current subtype's locale and all subtypes
1999 subtypeId = findLastResortApplicableSubtypeLocked(imi.getSubtypes(),
2000 mode, mCurrentSubtype.getLocale(), NOT_A_SUBTYPE_ID);
2001 }
2002 }
2003 // 3. Search with the system locale and the enabled subtypes
2004 if (subtypeId == NOT_A_SUBTYPE_ID) {
2005 subtypeId = findLastResortApplicableSubtypeLocked(
2006 mSettings.getEnabledInputMethodSubtypeListLocked(
2007 imi), mode, null, NOT_A_SUBTYPE_ID);
2008 }
2009 if (subtypeId == NOT_A_SUBTYPE_ID) {
2010 // 4. Search with the system locale and all subtypes
2011 subtypeId = findLastResortApplicableSubtypeLocked(imi.getSubtypes(),
2012 mode, null, NOT_A_SUBTYPE_ID);
2013 }
2014 if (subtypeId != NOT_A_SUBTYPE_ID) {
2015 if (imi.getId().equals(mCurMethodId)) {
2016 // The current input method is the most applicable IME.
2017 mostApplicableIMI = imi;
2018 mostApplicableSubtypeId = subtypeId;
2019 break;
2020 } else if ((imi.getServiceInfo().applicationInfo.flags
2021 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2022 // The system input method is 2nd applicable IME.
2023 mostApplicableIMI = imi;
2024 mostApplicableSubtypeId = subtypeId;
2025 foundInSystemIME = true;
2026 } else if (!foundInSystemIME) {
2027 mostApplicableIMI = imi;
2028 mostApplicableSubtypeId = subtypeId;
2029 }
2030 }
2031 }
2032 if (DEBUG) {
2033 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2034 + mostApplicableIMI.getId() + "," + mostApplicableSubtypeId);
2035 }
2036 if (mostApplicableIMI != null && mostApplicableSubtypeId != NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002037 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
2038 mostApplicableIMI.getSubtypes().get(mostApplicableSubtypeId));
2039 } else {
2040 return null;
2041 }
2042 }
2043
satokab751aa2010-09-14 19:17:36 +09002044 /**
2045 * @return Return the current subtype of this input method.
2046 */
2047 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002048 boolean subtypeIsSelected = false;
2049 try {
2050 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2051 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2052 } catch (SettingNotFoundException e) {
2053 }
satok3ef8b292010-11-23 06:06:29 +09002054 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002055 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002056 String lastInputMethodId = Settings.Secure.getString(
2057 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002058 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2059 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002060 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2061 if (imi != null) {
2062 // If there are no selected subtypes, the framework will try to find
2063 // the most applicable subtype from all subtypes whose mode is
2064 // SUBTYPE_MODE_KEYBOARD. This is an exceptional case, so we will hardcode
2065 // the mode.
2066 subtypeId = findLastResortApplicableSubtypeLocked(imi.getSubtypes(),
2067 SUBTYPE_MODE_KEYBOARD, null, DEFAULT_SUBTYPE_ID);
2068 }
satok3ef8b292010-11-23 06:06:29 +09002069 }
2070 if (subtypeId != NOT_A_SUBTYPE_ID) {
2071 mCurrentSubtype =
2072 mMethodMap.get(lastInputMethodId).getSubtypes().get(subtypeId);
satok4e4569d2010-11-19 18:45:53 +09002073 } else {
2074 mCurrentSubtype = null;
satok3ef8b292010-11-23 06:06:29 +09002075 }
satok8fbb1e82010-11-02 23:15:58 +09002076 }
satok3ef8b292010-11-23 06:06:29 +09002077 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002078 }
satokab751aa2010-09-14 19:17:36 +09002079 }
2080
satokf3db1af2010-11-23 13:34:33 +09002081 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2082 InputMethodSubtype subtype) {
2083 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2084 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2085 } else {
2086 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2087 subtypes.add(subtype);
2088 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2089 }
2090 }
2091
satok4e4569d2010-11-19 18:45:53 +09002092 // TODO: We should change the return type from List to List<Parcelable>
2093 public List getShortcutInputMethodsAndSubtypes() {
2094 synchronized (mMethodMap) {
satokf3db1af2010-11-23 13:34:33 +09002095 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002096 // If there are no selected shortcut subtypes, the framework will try to find
2097 // the most applicable subtype from all subtypes whose mode is
2098 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002099 Pair<InputMethodInfo, InputMethodSubtype> info =
2100 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2101 SUBTYPE_MODE_VOICE);
2102 addShortcutInputMethodAndSubtypes(info.first, info.second);
2103 }
2104 ArrayList ret = new ArrayList<Object>();
2105 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2106 ret.add(imi);
2107 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2108 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002109 }
2110 }
satokf3db1af2010-11-23 13:34:33 +09002111 return ret;
satok4e4569d2010-11-19 18:45:53 +09002112 }
2113 }
2114
satokb66d2872010-11-10 01:04:04 +09002115 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2116 synchronized (mMethodMap) {
2117 if (subtype != null && mCurMethodId != null) {
2118 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2119 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2120 if (subtypeId != NOT_A_SUBTYPE_ID) {
2121 setInputMethodLocked(mCurMethodId, subtypeId);
2122 return true;
2123 }
2124 }
2125 return false;
2126 }
2127 }
2128
satokd87c2592010-09-29 11:52:06 +09002129 /**
2130 * Utility class for putting and getting settings for InputMethod
2131 * TODO: Move all putters and getters of settings to this class.
2132 */
2133 private static class InputMethodSettings {
2134 // The string for enabled input method is saved as follows:
2135 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2136 private static final char INPUT_METHOD_SEPARATER = ':';
2137 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002138 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002139 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2140
satok723a27e2010-11-11 14:58:11 +09002141 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002142 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2143
2144 private final ContentResolver mResolver;
2145 private final HashMap<String, InputMethodInfo> mMethodMap;
2146 private final ArrayList<InputMethodInfo> mMethodList;
2147
2148 private String mEnabledInputMethodsStrCache;
2149
2150 private static void buildEnabledInputMethodsSettingString(
2151 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2152 String id = pair.first;
2153 ArrayList<String> subtypes = pair.second;
2154 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002155 // Inputmethod and subtypes are saved in the settings as follows:
2156 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2157 for (String subtypeId: subtypes) {
2158 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002159 }
2160 }
2161
2162 public InputMethodSettings(
2163 ContentResolver resolver, HashMap<String, InputMethodInfo> methodMap,
2164 ArrayList<InputMethodInfo> methodList) {
2165 mResolver = resolver;
2166 mMethodMap = methodMap;
2167 mMethodList = methodList;
2168 }
2169
2170 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2171 return createEnabledInputMethodListLocked(
2172 getEnabledInputMethodsAndSubtypeListLocked());
2173 }
2174
satok7f35c8c2010-10-07 21:13:11 +09002175 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002176 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2177 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002178 getEnabledInputMethodsAndSubtypeListLocked());
2179 }
2180
satok67ddf9c2010-11-17 09:45:54 +09002181 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2182 InputMethodInfo imi) {
2183 List<Pair<String, ArrayList<String>>> imsList =
2184 getEnabledInputMethodsAndSubtypeListLocked();
2185 ArrayList<InputMethodSubtype> enabledSubtypes =
2186 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002187 if (imi != null) {
2188 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2189 InputMethodInfo info = mMethodMap.get(imsPair.first);
2190 if (info != null && info.getId().equals(imi.getId())) {
2191 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
2192 for (InputMethodSubtype ims: subtypes) {
2193 for (String s: imsPair.second) {
2194 if (String.valueOf(ims.hashCode()).equals(s)) {
2195 enabledSubtypes.add(ims);
2196 }
satok67ddf9c2010-11-17 09:45:54 +09002197 }
2198 }
satok884ef9a2010-11-18 10:39:46 +09002199 break;
satok67ddf9c2010-11-17 09:45:54 +09002200 }
satok67ddf9c2010-11-17 09:45:54 +09002201 }
2202 }
2203 return enabledSubtypes;
2204 }
2205
satokd87c2592010-09-29 11:52:06 +09002206 // At the initial boot, the settings for input methods are not set,
2207 // so we need to enable IME in that case.
2208 public void enableAllIMEsIfThereIsNoEnabledIME() {
2209 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2210 StringBuilder sb = new StringBuilder();
2211 final int N = mMethodList.size();
2212 for (int i = 0; i < N; i++) {
2213 InputMethodInfo imi = mMethodList.get(i);
2214 Slog.i(TAG, "Adding: " + imi.getId());
2215 if (i > 0) sb.append(':');
2216 sb.append(imi.getId());
2217 }
2218 putEnabledInputMethodsStr(sb.toString());
2219 }
2220 }
2221
2222 public List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
2223 ArrayList<Pair<String, ArrayList<String>>> imsList
2224 = new ArrayList<Pair<String, ArrayList<String>>>();
2225 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2226 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2227 return imsList;
2228 }
satok723a27e2010-11-11 14:58:11 +09002229 mInputMethodSplitter.setString(enabledInputMethodsStr);
2230 while (mInputMethodSplitter.hasNext()) {
2231 String nextImsStr = mInputMethodSplitter.next();
2232 mSubtypeSplitter.setString(nextImsStr);
2233 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002234 ArrayList<String> subtypeHashes = new ArrayList<String>();
2235 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002236 String imeId = mSubtypeSplitter.next();
2237 while (mSubtypeSplitter.hasNext()) {
2238 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002239 }
2240 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2241 }
2242 }
2243 return imsList;
2244 }
2245
2246 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2247 if (reloadInputMethodStr) {
2248 getEnabledInputMethodsStr();
2249 }
2250 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2251 // Add in the newly enabled input method.
2252 putEnabledInputMethodsStr(id);
2253 } else {
2254 putEnabledInputMethodsStr(
2255 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2256 }
2257 }
2258
2259 /**
2260 * Build and put a string of EnabledInputMethods with removing specified Id.
2261 * @return the specified id was removed or not.
2262 */
2263 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2264 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2265 boolean isRemoved = false;
2266 boolean needsAppendSeparator = false;
2267 for (Pair<String, ArrayList<String>> ims: imsList) {
2268 String curId = ims.first;
2269 if (curId.equals(id)) {
2270 // We are disabling this input method, and it is
2271 // currently enabled. Skip it to remove from the
2272 // new list.
2273 isRemoved = true;
2274 } else {
2275 if (needsAppendSeparator) {
2276 builder.append(INPUT_METHOD_SEPARATER);
2277 } else {
2278 needsAppendSeparator = true;
2279 }
2280 buildEnabledInputMethodsSettingString(builder, ims);
2281 }
2282 }
2283 if (isRemoved) {
2284 // Update the setting with the new list of input methods.
2285 putEnabledInputMethodsStr(builder.toString());
2286 }
2287 return isRemoved;
2288 }
2289
2290 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2291 List<Pair<String, ArrayList<String>>> imsList) {
2292 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2293 for (Pair<String, ArrayList<String>> ims: imsList) {
2294 InputMethodInfo info = mMethodMap.get(ims.first);
2295 if (info != null) {
2296 res.add(info);
2297 }
2298 }
2299 return res;
2300 }
2301
satok7f35c8c2010-10-07 21:13:11 +09002302 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002303 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002304 List<Pair<String, ArrayList<String>>> imsList) {
2305 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2306 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2307 for (Pair<String, ArrayList<String>> ims : imsList) {
2308 InputMethodInfo info = mMethodMap.get(ims.first);
2309 if (info != null) {
2310 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2311 }
2312 }
2313 return res;
2314 }
2315
satokd87c2592010-09-29 11:52:06 +09002316 private void putEnabledInputMethodsStr(String str) {
2317 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2318 mEnabledInputMethodsStrCache = str;
2319 }
2320
2321 private String getEnabledInputMethodsStr() {
2322 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2323 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002324 if (DEBUG) {
2325 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2326 }
satokd87c2592010-09-29 11:52:06 +09002327 return mEnabledInputMethodsStrCache;
2328 }
satok723a27e2010-11-11 14:58:11 +09002329
2330 private void saveSubtypeHistory(
2331 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2332 StringBuilder builder = new StringBuilder();
2333 boolean isImeAdded = false;
2334 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2335 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2336 newSubtypeId);
2337 isImeAdded = true;
2338 }
2339 for (Pair<String, String> ime: savedImes) {
2340 String imeId = ime.first;
2341 String subtypeId = ime.second;
2342 if (TextUtils.isEmpty(subtypeId)) {
2343 subtypeId = NOT_A_SUBTYPE_ID_STR;
2344 }
2345 if (isImeAdded) {
2346 builder.append(INPUT_METHOD_SEPARATER);
2347 } else {
2348 isImeAdded = true;
2349 }
2350 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2351 subtypeId);
2352 }
2353 // Remove the last INPUT_METHOD_SEPARATER
2354 putSubtypeHistoryStr(builder.toString());
2355 }
2356
2357 public void addSubtypeToHistory(String imeId, String subtypeId) {
2358 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2359 for (Pair<String, String> ime: subtypeHistory) {
2360 if (ime.first.equals(imeId)) {
2361 if (DEBUG) {
2362 Slog.v(TAG, "Subtype found in the history: " + imeId
2363 + ime.second);
2364 }
2365 // We should break here
2366 subtypeHistory.remove(ime);
2367 break;
2368 }
2369 }
2370 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2371 }
2372
2373 private void putSubtypeHistoryStr(String str) {
2374 if (DEBUG) {
2375 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2376 }
2377 Settings.Secure.putString(
2378 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2379 }
2380
2381 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2382 // Gets the first one from the history
2383 return getLastSubtypeForInputMethodLockedInternal(null);
2384 }
2385
2386 public String getLastSubtypeForInputMethodLocked(String imeId) {
2387 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2388 if (ime != null) {
2389 return ime.second;
2390 } else {
2391 return null;
2392 }
2393 }
2394
2395 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2396 List<Pair<String, ArrayList<String>>> enabledImes =
2397 getEnabledInputMethodsAndSubtypeListLocked();
2398 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2399 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2400 final String imeInTheHistory = imeAndSubtype.first;
2401 // If imeId is empty, returns the first IME and subtype in the history
2402 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2403 final String subtypeInTheHistory = imeAndSubtype.second;
2404 final String subtypeHashCode = getEnabledSubtypeForInputMethodAndSubtypeLocked(
2405 enabledImes, imeInTheHistory, subtypeInTheHistory);
2406 if (!TextUtils.isEmpty(subtypeHashCode)) {
2407 if (DEBUG) {
2408 Slog.d(TAG, "Enabled subtype found in the history:" + subtypeHashCode);
2409 }
2410 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2411 }
2412 }
2413 }
2414 if (DEBUG) {
2415 Slog.d(TAG, "No enabled IME found in the history");
2416 }
2417 return null;
2418 }
2419
2420 private String getEnabledSubtypeForInputMethodAndSubtypeLocked(List<Pair<String,
2421 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2422 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2423 if (enabledIme.first.equals(imeId)) {
2424 for (String s: enabledIme.second) {
2425 if (s.equals(subtypeHashCode)) {
2426 // If both imeId and subtypeId are enabled, return subtypeId.
2427 return s;
2428 }
2429 }
2430 // If imeId was enabled but subtypeId was disabled.
2431 return NOT_A_SUBTYPE_ID_STR;
2432 }
2433 }
2434 // If both imeId and subtypeId are disabled, return null
2435 return null;
2436 }
2437
2438 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2439 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2440 final String subtypeHistoryStr = getSubtypeHistoryStr();
2441 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2442 return imsList;
2443 }
2444 mInputMethodSplitter.setString(subtypeHistoryStr);
2445 while (mInputMethodSplitter.hasNext()) {
2446 String nextImsStr = mInputMethodSplitter.next();
2447 mSubtypeSplitter.setString(nextImsStr);
2448 if (mSubtypeSplitter.hasNext()) {
2449 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2450 // The first element is ime id.
2451 String imeId = mSubtypeSplitter.next();
2452 while (mSubtypeSplitter.hasNext()) {
2453 subtypeId = mSubtypeSplitter.next();
2454 break;
2455 }
2456 imsList.add(new Pair<String, String>(imeId, subtypeId));
2457 }
2458 }
2459 return imsList;
2460 }
2461
2462 private String getSubtypeHistoryStr() {
2463 if (DEBUG) {
2464 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2465 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2466 }
2467 return Settings.Secure.getString(
2468 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2469 }
2470
2471 public void putSelectedInputMethod(String imeId) {
2472 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2473 }
2474
2475 public void putSelectedSubtype(int subtypeId) {
2476 Settings.Secure.putInt(
2477 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2478 }
satokd87c2592010-09-29 11:52:06 +09002479 }
2480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 @Override
2484 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2485 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2486 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2489 + Binder.getCallingPid()
2490 + ", uid=" + Binder.getCallingUid());
2491 return;
2492 }
2493
2494 IInputMethod method;
2495 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 synchronized (mMethodMap) {
2500 p.println("Current Input Method Manager state:");
2501 int N = mMethodList.size();
2502 p.println(" Input Methods:");
2503 for (int i=0; i<N; i++) {
2504 InputMethodInfo info = mMethodList.get(i);
2505 p.println(" InputMethod #" + i + ":");
2506 info.dump(p, " ");
2507 }
2508 p.println(" Clients:");
2509 for (ClientState ci : mClients.values()) {
2510 p.println(" Client " + ci + ":");
2511 p.println(" client=" + ci.client);
2512 p.println(" inputContext=" + ci.inputContext);
2513 p.println(" sessionRequested=" + ci.sessionRequested);
2514 p.println(" curSession=" + ci.curSession);
2515 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002516 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002518 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2519 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2521 + " mBoundToMethod=" + mBoundToMethod);
2522 p.println(" mCurToken=" + mCurToken);
2523 p.println(" mCurIntent=" + mCurIntent);
2524 method = mCurMethod;
2525 p.println(" mCurMethod=" + mCurMethod);
2526 p.println(" mEnabledSession=" + mEnabledSession);
2527 p.println(" mShowRequested=" + mShowRequested
2528 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2529 + " mShowForced=" + mShowForced
2530 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002531 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002533
Jeff Brownb88102f2010-09-08 11:49:43 -07002534 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 pw.flush();
2537 try {
2538 client.client.asBinder().dump(fd, args);
2539 } catch (RemoteException e) {
2540 p.println("Input method client dead: " + e);
2541 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002542 } else {
2543 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002545
Jeff Brownb88102f2010-09-08 11:49:43 -07002546 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 pw.flush();
2549 try {
2550 method.asBinder().dump(fd, args);
2551 } catch (RemoteException e) {
2552 p.println("Input method service dead: " + e);
2553 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002554 } else {
2555 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 }
2557 }
2558}