blob: 9c56a2a4c3627cc854b74859bd5aabaa0ec5c646 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2008 The Android Open Source Project
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.server;
18
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080019import com.android.internal.content.PackageMonitor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import com.android.internal.os.HandlerCaller;
21import com.android.internal.view.IInputContext;
22import com.android.internal.view.IInputMethod;
23import com.android.internal.view.IInputMethodCallback;
24import com.android.internal.view.IInputMethodClient;
25import com.android.internal.view.IInputMethodManager;
26import com.android.internal.view.IInputMethodSession;
27import com.android.internal.view.InputBindResult;
28
Joe Onorato7a0f36b2010-06-07 10:24:36 -070029import com.android.server.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31import org.xmlpull.v1.XmlPullParserException;
32
33import android.app.ActivityManagerNative;
34import android.app.AlertDialog;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070035import android.app.PendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.ComponentName;
37import android.content.ContentResolver;
38import android.content.Context;
39import android.content.DialogInterface;
40import android.content.IntentFilter;
41import android.content.DialogInterface.OnCancelListener;
42import android.content.Intent;
43import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070044import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.pm.PackageManager;
46import android.content.pm.ResolveInfo;
47import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070048import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.res.Resources;
50import android.content.res.TypedArray;
51import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Binder;
53import android.os.Handler;
54import android.os.IBinder;
55import android.os.IInterface;
56import android.os.Message;
57import android.os.Parcel;
58import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080059import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.ServiceManager;
61import android.os.SystemClock;
62import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070063import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090064import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.text.TextUtils;
66import android.util.EventLog;
satokab751aa2010-09-14 19:17:36 +090067import android.util.Pair;
Joe Onorato8a9b2202010-02-26 18:56:32 -080068import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.util.PrintWriterPrinter;
70import android.util.Printer;
71import android.view.IWindowManager;
72import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090073import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.view.inputmethod.InputBinding;
75import android.view.inputmethod.InputMethod;
76import android.view.inputmethod.InputMethodInfo;
77import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090078import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80import java.io.FileDescriptor;
81import java.io.IOException;
82import java.io.PrintWriter;
satok913a8922010-08-26 21:53:41 +090083import java.text.Collator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import java.util.ArrayList;
85import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +090086import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import java.util.List;
satok913a8922010-08-26 21:53:41 +090088import java.util.Map;
89import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91/**
92 * This class provides a system service that manages input methods.
93 */
94public class InputMethodManagerService extends IInputMethodManager.Stub
95 implements ServiceConnection, Handler.Callback {
96 static final boolean DEBUG = false;
97 static final String TAG = "InputManagerService";
98
99 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900100 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900101 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900102 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 static final int MSG_UNBIND_INPUT = 1000;
105 static final int MSG_BIND_INPUT = 1010;
106 static final int MSG_SHOW_SOFT_INPUT = 1020;
107 static final int MSG_HIDE_SOFT_INPUT = 1030;
108 static final int MSG_ATTACH_TOKEN = 1040;
109 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 static final int MSG_START_INPUT = 2000;
112 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 static final int MSG_UNBIND_METHOD = 3000;
115 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800118
satokab751aa2010-09-14 19:17:36 +0900119 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900120 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
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
satok217f5482010-12-15 05:19:19 +0900124 private static final String EXTRA_INPUT_METHOD_ID = "input_method_id";
satok4e4569d2010-11-19 18:45:53 +0900125 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
126 private static final String SUBTYPE_MODE_VOICE = "voice";
127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800129 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900131 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700133 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 final IWindowManager mIWindowManager;
135 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 // All known input methods. mMethodMap also serves as the global
140 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900141 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
142 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 class SessionState {
145 final ClientState client;
146 final IInputMethod method;
147 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 @Override
150 public String toString() {
151 return "SessionState{uid " + client.uid + " pid " + client.pid
152 + " method " + Integer.toHexString(
153 System.identityHashCode(method))
154 + " session " + Integer.toHexString(
155 System.identityHashCode(session))
156 + "}";
157 }
158
159 SessionState(ClientState _client, IInputMethod _method,
160 IInputMethodSession _session) {
161 client = _client;
162 method = _method;
163 session = _session;
164 }
165 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 class ClientState {
168 final IInputMethodClient client;
169 final IInputContext inputContext;
170 final int uid;
171 final int pid;
172 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 boolean sessionRequested;
175 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 @Override
178 public String toString() {
179 return "ClientState{" + Integer.toHexString(
180 System.identityHashCode(this)) + " uid " + uid
181 + " pid " + pid + "}";
182 }
183
184 ClientState(IInputMethodClient _client, IInputContext _inputContext,
185 int _uid, int _pid) {
186 client = _client;
187 inputContext = _inputContext;
188 uid = _uid;
189 pid = _pid;
190 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
191 }
192 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 final HashMap<IBinder, ClientState> mClients
195 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700198 * Set once the system is ready to run third party code.
199 */
200 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800201
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700202 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 * Id of the currently selected input method.
204 */
205 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 /**
208 * The current binding sequence number, incremented every time there is
209 * a new bind performed.
210 */
211 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 /**
214 * The client that is currently bound to an input method.
215 */
216 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700219 * The last window token that gained focus.
220 */
221 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800222
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700223 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 * The input context last provided by the current client.
225 */
226 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 /**
229 * The attributes last provided by the current client.
230 */
231 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 /**
234 * The input method ID of the input method service that we are currently
235 * connected to or in the process of connecting to.
236 */
237 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 /**
satokab751aa2010-09-14 19:17:36 +0900240 * The current subtype of the current input method.
241 */
242 private InputMethodSubtype mCurrentSubtype;
243
satok4e4569d2010-11-19 18:45:53 +0900244 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900245 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
246 mShortcutInputMethodsAndSubtypes =
247 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900248
249 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 * Set to true if our ServiceConnection is currently actively bound to
251 * a service (whether or not we have gotten its IBinder back yet).
252 */
253 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 /**
256 * Set if the client has asked for the input method to be shown.
257 */
258 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 /**
261 * Set if we were explicitly told to show the input method.
262 */
263 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 /**
266 * Set if we were forced to be shown.
267 */
268 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 /**
271 * Set if we last told the input method to show itself.
272 */
273 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 /**
276 * The Intent used to connect to the current input method.
277 */
278 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 /**
281 * The token we have made for the currently active input method, to
282 * identify it in the future.
283 */
284 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 /**
287 * If non-null, this is the input method service we are currently connected
288 * to.
289 */
290 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 /**
293 * Time that we last initiated a bind to the input method, to determine
294 * if we should try to disconnect and reconnect to it.
295 */
296 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 /**
299 * Have we called mCurMethod.bindInput()?
300 */
301 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 /**
304 * Currently enabled session. Only touched by service thread, not
305 * protected by a lock.
306 */
307 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 /**
310 * True if the screen is on. The value is true initially.
311 */
312 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 AlertDialog.Builder mDialogBuilder;
315 AlertDialog mSwitchingDialog;
316 InputMethodInfo[] mIms;
317 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900318 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 class SettingsObserver extends ContentObserver {
321 SettingsObserver(Handler handler) {
322 super(handler);
323 ContentResolver resolver = mContext.getContentResolver();
324 resolver.registerContentObserver(Settings.Secure.getUriFor(
325 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900326 resolver.registerContentObserver(Settings.Secure.getUriFor(
327 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 @Override public void onChange(boolean selfChange) {
331 synchronized (mMethodMap) {
332 updateFromSettingsLocked();
333 }
334 }
335 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
338 @Override
339 public void onReceive(Context context, Intent intent) {
340 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
341 mScreenOn = true;
342 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
343 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700344 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
345 hideInputMethodMenu();
346 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800348 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 }
350
351 // Inform the current client of the change in active status
352 try {
353 if (mCurClient != null && mCurClient.client != null) {
354 mCurClient.client.setActive(mScreenOn);
355 }
356 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800357 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 + mCurClient.pid + " uid " + mCurClient.uid);
359 }
360 }
361 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800362
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800363 class MyPackageMonitor extends PackageMonitor {
364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800366 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800368 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
370 final int N = mMethodList.size();
371 if (curInputMethodId != null) {
372 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800373 InputMethodInfo imi = mMethodList.get(i);
374 if (imi.getId().equals(curInputMethodId)) {
375 for (String pkg : packages) {
376 if (imi.getPackageName().equals(pkg)) {
377 if (!doit) {
378 return true;
379 }
satok723a27e2010-11-11 14:58:11 +0900380 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800381 chooseNewDefaultIMELocked();
382 return true;
383 }
384 }
385 }
386 }
387 }
388 }
389 return false;
390 }
391
392 @Override
393 public void onSomePackagesChanged() {
394 synchronized (mMethodMap) {
395 InputMethodInfo curIm = null;
396 String curInputMethodId = Settings.Secure.getString(mContext
397 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
398 final int N = mMethodList.size();
399 if (curInputMethodId != null) {
400 for (int i=0; i<N; i++) {
401 InputMethodInfo imi = mMethodList.get(i);
402 if (imi.getId().equals(curInputMethodId)) {
403 curIm = imi;
404 }
405 int change = isPackageDisappearing(imi.getPackageName());
406 if (change == PACKAGE_TEMPORARY_CHANGE
407 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800408 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800409 + imi.getComponent());
410 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 }
412 }
413 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800414
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800415 buildInputMethodListLocked(mMethodList, mMethodMap);
416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800418
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800419 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800420 int change = isPackageDisappearing(curIm.getPackageName());
421 if (change == PACKAGE_TEMPORARY_CHANGE
422 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800423 ServiceInfo si = null;
424 try {
425 si = mContext.getPackageManager().getServiceInfo(
426 curIm.getComponent(), 0);
427 } catch (PackageManager.NameNotFoundException ex) {
428 }
429 if (si == null) {
430 // Uh oh, current input method is no longer around!
431 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800432 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800433 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800434 changed = true;
435 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800436 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900437 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800438 }
439 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800440 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800441 }
satokab751aa2010-09-14 19:17:36 +0900442
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800443 if (curIm == null) {
444 // We currently don't have a default input method... is
445 // one now available?
446 changed = chooseNewDefaultIMELocked();
447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800448
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800449 if (changed) {
450 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 }
452 }
453 }
454 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 class MethodCallback extends IInputMethodCallback.Stub {
457 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 MethodCallback(IInputMethod method) {
460 mMethod = method;
461 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 public void finishedEvent(int seq, boolean handled) throws RemoteException {
464 }
465
466 public void sessionCreated(IInputMethodSession session) throws RemoteException {
467 onSessionCreated(mMethod, session);
468 }
469 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800470
Joe Onorato089de882010-04-12 08:18:45 -0700471 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800473 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 mHandler = new Handler(this);
475 mIWindowManager = IWindowManager.Stub.asInterface(
476 ServiceManager.getService(Context.WINDOW_SERVICE));
477 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
478 public void executeMessage(Message msg) {
479 handleMessage(msg);
480 }
481 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800482
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800483 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 IntentFilter screenOnOffFilt = new IntentFilter();
486 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
487 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700488 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800490
satok913a8922010-08-26 21:53:41 +0900491 mStatusBar = statusBar;
492 statusBar.setIconVisibility("ime", false);
493
satokd87c2592010-09-29 11:52:06 +0900494 // mSettings should be created before buildInputMethodListLocked
495 mSettings = new InputMethodSettings(context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900497 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498
satokd87c2592010-09-29 11:52:06 +0900499 if (TextUtils.isEmpty(Settings.Secure.getString(
500 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900502 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
504 try {
satokd87c2592010-09-29 11:52:06 +0900505 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 imi.getPackageName(), 0).getResources();
507 if (res.getBoolean(imi.getIsDefaultResourceId())) {
508 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800509 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 }
511 } catch (PackageManager.NameNotFoundException ex) {
512 } catch (Resources.NotFoundException ex) {
513 }
514 }
515 }
satokd87c2592010-09-29 11:52:06 +0900516 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800518 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900521 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 }
523 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 mSettingsObserver = new SettingsObserver(mHandler);
526 updateFromSettingsLocked();
527 }
528
529 @Override
530 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
531 throws RemoteException {
532 try {
533 return super.onTransact(code, data, reply, flags);
534 } catch (RuntimeException e) {
535 // The input method manager only throws security exceptions, so let's
536 // log all others.
537 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800538 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 }
540 throw e;
541 }
542 }
543
544 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700545 synchronized (mMethodMap) {
546 if (!mSystemReady) {
547 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700548 try {
549 startInputInnerLocked();
550 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800551 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700552 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700553 }
554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 public List<InputMethodInfo> getInputMethodList() {
558 synchronized (mMethodMap) {
559 return new ArrayList<InputMethodInfo>(mMethodList);
560 }
561 }
562
563 public List<InputMethodInfo> getEnabledInputMethodList() {
564 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900565 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
567 }
568
satok67ddf9c2010-11-17 09:45:54 +0900569 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi) {
570 synchronized (mMethodMap) {
satok884ef9a2010-11-18 10:39:46 +0900571 if (imi == null && mCurMethodId != null) {
572 imi = mMethodMap.get(mCurMethodId);
573 }
satok67ddf9c2010-11-17 09:45:54 +0900574 return mSettings.getEnabledInputMethodSubtypeListLocked(imi);
575 }
576 }
577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 public void addClient(IInputMethodClient client,
579 IInputContext inputContext, int uid, int pid) {
580 synchronized (mMethodMap) {
581 mClients.put(client.asBinder(), new ClientState(client,
582 inputContext, uid, pid));
583 }
584 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 public void removeClient(IInputMethodClient client) {
587 synchronized (mMethodMap) {
588 mClients.remove(client.asBinder());
589 }
590 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 void executeOrSendMessage(IInterface target, Message msg) {
593 if (target.asBinder() instanceof Binder) {
594 mCaller.sendMessage(msg);
595 } else {
596 handleMessage(msg);
597 msg.recycle();
598 }
599 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800600
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700601 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800603 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 + mCurClient.client.asBinder());
605 if (mBoundToMethod) {
606 mBoundToMethod = false;
607 if (mCurMethod != null) {
608 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
609 MSG_UNBIND_INPUT, mCurMethod));
610 }
611 }
612 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
613 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
614 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 // Call setActive(false) on the old client
617 try {
618 mCurClient.client.setActive(false);
619 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800620 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 + mCurClient.pid + " uid " + mCurClient.uid);
622 }
623 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800624
The Android Open Source Project10592532009-03-18 17:39:46 -0700625 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 }
627 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 private int getImeShowFlags() {
630 int flags = 0;
631 if (mShowForced) {
632 flags |= InputMethod.SHOW_FORCED
633 | InputMethod.SHOW_EXPLICIT;
634 } else if (mShowExplicitlyRequested) {
635 flags |= InputMethod.SHOW_EXPLICIT;
636 }
637 return flags;
638 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 private int getAppShowFlags() {
641 int flags = 0;
642 if (mShowForced) {
643 flags |= InputMethodManager.SHOW_FORCED;
644 } else if (!mShowExplicitlyRequested) {
645 flags |= InputMethodManager.SHOW_IMPLICIT;
646 }
647 return flags;
648 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
651 if (!mBoundToMethod) {
652 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
653 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
654 mBoundToMethod = true;
655 }
656 final SessionState session = mCurClient.curSession;
657 if (initial) {
658 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
659 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
660 } else {
661 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
662 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
663 }
664 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800665 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800666 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 }
668 return needResult
669 ? new InputBindResult(session.session, mCurId, mCurSeq)
670 : null;
671 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 InputBindResult startInputLocked(IInputMethodClient client,
674 IInputContext inputContext, EditorInfo attribute,
675 boolean initial, boolean needResult) {
676 // If no method is currently selected, do nothing.
677 if (mCurMethodId == null) {
678 return mNoBinding;
679 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 ClientState cs = mClients.get(client.asBinder());
682 if (cs == null) {
683 throw new IllegalArgumentException("unknown client "
684 + client.asBinder());
685 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 try {
688 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
689 // Check with the window manager to make sure this client actually
690 // has a window with focus. If not, reject. This is thread safe
691 // because if the focus changes some time before or after, the
692 // next client receiving focus that has any interest in input will
693 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800694 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
696 return null;
697 }
698 } catch (RemoteException e) {
699 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 if (mCurClient != cs) {
702 // If the client is changing, we need to switch over to the new
703 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700704 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800705 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 + cs.client.asBinder());
707
708 // If the screen is on, inform the new client it is active
709 if (mScreenOn) {
710 try {
711 cs.client.setActive(mScreenOn);
712 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800713 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 + cs.pid + " uid " + cs.uid);
715 }
716 }
717 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 // Bump up the sequence for this client and attach it.
720 mCurSeq++;
721 if (mCurSeq <= 0) mCurSeq = 1;
722 mCurClient = cs;
723 mCurInputContext = inputContext;
724 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 // Check if the input method is changing.
727 if (mCurId != null && mCurId.equals(mCurMethodId)) {
728 if (cs.curSession != null) {
729 // Fast case: if we are already connected to the input method,
730 // then just return it.
731 return attachNewInputLocked(initial, needResult);
732 }
733 if (mHaveConnection) {
734 if (mCurMethod != null) {
735 if (!cs.sessionRequested) {
736 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800737 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
739 MSG_CREATE_SESSION, mCurMethod,
740 new MethodCallback(mCurMethod)));
741 }
742 // Return to client, and we will get back with it when
743 // we have had a session made for it.
744 return new InputBindResult(null, mCurId, mCurSeq);
745 } else if (SystemClock.uptimeMillis()
746 < (mLastBindTime+TIME_TO_RECONNECT)) {
747 // In this case we have connected to the service, but
748 // don't yet have its interface. If it hasn't been too
749 // long since we did the connection, we'll return to
750 // the client and wait to get the service interface so
751 // we can report back. If it has been too long, we want
752 // to fall through so we can try a disconnect/reconnect
753 // to see if we can get back in touch with the service.
754 return new InputBindResult(null, mCurId, mCurSeq);
755 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800756 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
757 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
759 }
760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800761
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700762 return startInputInnerLocked();
763 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800764
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700765 InputBindResult startInputInnerLocked() {
766 if (mCurMethodId == null) {
767 return mNoBinding;
768 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800769
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700770 if (!mSystemReady) {
771 // If the system is not yet ready, we shouldn't be running third
772 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700773 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700774 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 InputMethodInfo info = mMethodMap.get(mCurMethodId);
777 if (info == null) {
778 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
779 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800780
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700781 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
784 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700785 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
786 com.android.internal.R.string.input_method_binding_label);
787 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
788 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
790 mLastBindTime = SystemClock.uptimeMillis();
791 mHaveConnection = true;
792 mCurId = info.getId();
793 mCurToken = new Binder();
794 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800795 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 mIWindowManager.addWindowToken(mCurToken,
797 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
798 } catch (RemoteException e) {
799 }
800 return new InputBindResult(null, mCurId, mCurSeq);
801 } else {
802 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800803 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 + mCurIntent);
805 }
806 return null;
807 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 public InputBindResult startInput(IInputMethodClient client,
810 IInputContext inputContext, EditorInfo attribute,
811 boolean initial, boolean needResult) {
812 synchronized (mMethodMap) {
813 final long ident = Binder.clearCallingIdentity();
814 try {
815 return startInputLocked(client, inputContext, attribute,
816 initial, needResult);
817 } finally {
818 Binder.restoreCallingIdentity(ident);
819 }
820 }
821 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 public void finishInput(IInputMethodClient client) {
824 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 public void onServiceConnected(ComponentName name, IBinder service) {
827 synchronized (mMethodMap) {
828 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
829 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700830 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800831 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700832 unbindCurrentMethodLocked(false);
833 return;
834 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800835 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700836 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
837 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800839 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700840 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700842 MSG_CREATE_SESSION, mCurMethod,
843 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 }
845 }
846 }
847 }
848
849 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
850 synchronized (mMethodMap) {
851 if (mCurMethod != null && method != null
852 && mCurMethod.asBinder() == method.asBinder()) {
853 if (mCurClient != null) {
854 mCurClient.curSession = new SessionState(mCurClient,
855 method, session);
856 mCurClient.sessionRequested = false;
857 InputBindResult res = attachNewInputLocked(true, true);
858 if (res.method != null) {
859 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
860 MSG_BIND_METHOD, mCurClient.client, res));
861 }
862 }
863 }
864 }
865 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800866
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700867 void unbindCurrentMethodLocked(boolean reportToClient) {
868 if (mHaveConnection) {
869 mContext.unbindService(this);
870 mHaveConnection = false;
871 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800872
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700873 if (mCurToken != null) {
874 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800875 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700876 mIWindowManager.removeWindowToken(mCurToken);
877 } catch (RemoteException e) {
878 }
879 mCurToken = null;
880 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800881
The Android Open Source Project10592532009-03-18 17:39:46 -0700882 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700883 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800884
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700885 if (reportToClient && mCurClient != null) {
886 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
887 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
888 }
889 }
890
Devin Taylor0c33ed22010-02-23 13:26:46 -0600891 private void finishSession(SessionState sessionState) {
892 if (sessionState != null && sessionState.session != null) {
893 try {
894 sessionState.session.finishSession();
895 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700896 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600897 }
898 }
899 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800900
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700901 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 if (mCurMethod != null) {
903 for (ClientState cs : mClients.values()) {
904 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600905 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 cs.curSession = null;
907 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600908
909 finishSession(mEnabledSession);
910 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 mCurMethod = null;
912 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700913 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 public void onServiceDisconnected(ComponentName name) {
917 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800918 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 + " mCurIntent=" + mCurIntent);
920 if (mCurMethod != null && mCurIntent != null
921 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700922 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 // We consider this to be a new bind attempt, since the system
924 // should now try to restart the service for us.
925 mLastBindTime = SystemClock.uptimeMillis();
926 mShowRequested = mInputShown;
927 mInputShown = false;
928 if (mCurClient != null) {
929 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
930 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
931 }
932 }
933 }
934 }
935
936 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700937 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 long ident = Binder.clearCallingIdentity();
939 try {
940 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700941 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 return;
943 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 synchronized (mMethodMap) {
946 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800947 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700948 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800950 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700951 mStatusBar.setIcon("ime", packageName, iconId, 0);
952 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 }
954 }
955 } finally {
956 Binder.restoreCallingIdentity(ident);
957 }
958 }
959
satok06487a52010-10-29 11:37:18 +0900960 public void setIMEButtonVisible(IBinder token, boolean visible) {
961 int uid = Binder.getCallingUid();
962 long ident = Binder.clearCallingIdentity();
963 try {
964 if (token == null || mCurToken != token) {
965 Slog.w(TAG, "Ignoring setIMEButtonVisible of uid " + uid + " token: " + token);
966 return;
967 }
968
969 synchronized (mMethodMap) {
satokcd7cd292010-11-20 15:46:23 +0900970 mStatusBar.setIMEButtonVisible(token, visible);
satok06487a52010-10-29 11:37:18 +0900971 }
972 } finally {
973 Binder.restoreCallingIdentity(ident);
974 }
975 }
976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700978 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
979 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
980 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
981 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +0900983 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +0900984 // There is no input method selected, try to choose new applicable input method.
985 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
986 id = Settings.Secure.getString(mContext.getContentResolver(),
987 Settings.Secure.DEFAULT_INPUT_METHOD);
988 }
989 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 try {
satokab751aa2010-09-14 19:17:36 +0900991 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800993 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -0700994 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700995 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 }
satokf3db1af2010-11-23 13:34:33 +0900997 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700998 } else {
999 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001000 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001001 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
1003 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001004
satokab751aa2010-09-14 19:17:36 +09001005 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 InputMethodInfo info = mMethodMap.get(id);
1007 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001008 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 if (id.equals(mCurMethodId)) {
satokb66d2872010-11-10 01:04:04 +09001012 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
satokcd7cd292010-11-20 15:46:23 +09001013 InputMethodSubtype subtype = null;
satokb66d2872010-11-10 01:04:04 +09001014 if (subtypeId >= 0 && subtypeId < subtypes.size()) {
satokcd7cd292010-11-20 15:46:23 +09001015 subtype = subtypes.get(subtypeId);
1016 }
1017 if (subtype != mCurrentSubtype) {
1018 synchronized (mMethodMap) {
1019 if (mCurMethod != null) {
1020 try {
1021 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1022 if (mInputShown) {
1023 // If mInputShown is false, there is no IME button on the
1024 // system bar.
1025 // Thus there is no need to make it invisible explicitly.
1026 mStatusBar.setIMEButtonVisible(mCurToken, true);
satokab751aa2010-09-14 19:17:36 +09001027 }
satokcd7cd292010-11-20 15:46:23 +09001028 // If subtype is null, try to find the most applicable one from
1029 // getCurrentInputMethodSubtype.
1030 if (subtype == null) {
1031 subtype = getCurrentInputMethodSubtype();
1032 }
1033 mCurMethod.changeInputMethodSubtype(subtype);
1034 } catch (RemoteException e) {
1035 return;
satokab751aa2010-09-14 19:17:36 +09001036 }
1037 }
1038 }
1039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 return;
1041 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 final long ident = Binder.clearCallingIdentity();
1044 try {
satokab751aa2010-09-14 19:17:36 +09001045 // Set a subtype to this input method.
1046 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001047 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1048 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1049 // because mCurMethodId is stored as a history in
1050 // setSelectedInputMethodAndSubtypeLocked().
1051 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052
1053 if (ActivityManagerNative.isSystemReady()) {
1054 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001055 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 intent.putExtra("input_method_id", id);
1057 mContext.sendBroadcast(intent);
1058 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001059 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 } finally {
1061 Binder.restoreCallingIdentity(ident);
1062 }
1063 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001064
The Android Open Source Project4df24232009-03-05 14:34:35 -08001065 public boolean showSoftInput(IInputMethodClient client, int flags,
1066 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001067 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 long ident = Binder.clearCallingIdentity();
1069 try {
1070 synchronized (mMethodMap) {
1071 if (mCurClient == null || client == null
1072 || mCurClient.client.asBinder() != client.asBinder()) {
1073 try {
1074 // We need to check if this is the current client with
1075 // focus in the window manager, to allow this call to
1076 // be made before input is started in it.
1077 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001078 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001079 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 }
1081 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001082 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 }
1084 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001085
Joe Onorato8a9b2202010-02-26 18:56:32 -08001086 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001087 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 }
1089 } finally {
1090 Binder.restoreCallingIdentity(ident);
1091 }
1092 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001093
The Android Open Source Project4df24232009-03-05 14:34:35 -08001094 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 mShowRequested = true;
1096 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1097 mShowExplicitlyRequested = true;
1098 }
1099 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1100 mShowExplicitlyRequested = true;
1101 mShowForced = true;
1102 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001103
Dianne Hackborncc278702009-09-02 23:07:23 -07001104 if (!mSystemReady) {
1105 return false;
1106 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001107
The Android Open Source Project4df24232009-03-05 14:34:35 -08001108 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001110 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1111 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1112 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001114 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 } else if (mHaveConnection && SystemClock.uptimeMillis()
1116 < (mLastBindTime+TIME_TO_RECONNECT)) {
1117 // The client has asked to have the input method shown, but
1118 // we have been sitting here too long with a connection to the
1119 // service and no interface received, so let's disconnect/connect
1120 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001121 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 SystemClock.uptimeMillis()-mLastBindTime,1);
1123 mContext.unbindService(this);
1124 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1125 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001126
The Android Open Source Project4df24232009-03-05 14:34:35 -08001127 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001129
The Android Open Source Project4df24232009-03-05 14:34:35 -08001130 public boolean hideSoftInput(IInputMethodClient client, int flags,
1131 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001132 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 long ident = Binder.clearCallingIdentity();
1134 try {
1135 synchronized (mMethodMap) {
1136 if (mCurClient == null || client == null
1137 || mCurClient.client.asBinder() != client.asBinder()) {
1138 try {
1139 // We need to check if this is the current client with
1140 // focus in the window manager, to allow this call to
1141 // be made before input is started in it.
1142 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001143 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1144 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001145 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 }
1147 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001148 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 }
1150 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001151
Joe Onorato8a9b2202010-02-26 18:56:32 -08001152 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001153 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 }
1155 } finally {
1156 Binder.restoreCallingIdentity(ident);
1157 }
1158 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001159
The Android Open Source Project4df24232009-03-05 14:34:35 -08001160 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1162 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001163 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001165 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 }
1167 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001168 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001170 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001172 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001174 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1175 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1176 res = true;
1177 } else {
1178 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 }
1180 mInputShown = false;
1181 mShowRequested = false;
1182 mShowExplicitlyRequested = false;
1183 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001184 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001186
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001187 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1189 boolean first, int windowFlags) {
1190 long ident = Binder.clearCallingIdentity();
1191 try {
1192 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001193 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 + " viewHasFocus=" + viewHasFocus
1195 + " isTextEditor=" + isTextEditor
1196 + " softInputMode=#" + Integer.toHexString(softInputMode)
1197 + " first=" + first + " flags=#"
1198 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 if (mCurClient == null || client == null
1201 || mCurClient.client.asBinder() != client.asBinder()) {
1202 try {
1203 // We need to check if this is the current client with
1204 // focus in the window manager, to allow this call to
1205 // be made before input is started in it.
1206 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001207 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 return;
1209 }
1210 } catch (RemoteException e) {
1211 }
1212 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001213
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001214 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001215 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001216 return;
1217 }
1218 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001219
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001220 // Should we auto-show the IME even if the caller has not
1221 // specified what should be done with it?
1222 // We only do this automatically if the window can resize
1223 // to accommodate the IME (so what the user sees will give
1224 // them good context without input information being obscured
1225 // by the IME) or if running on a large screen where there
1226 // is more room for the target window + IME.
1227 final boolean doAutoShow =
1228 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1229 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1230 || mRes.getConfiguration().isLayoutSizeAtLeast(
1231 Configuration.SCREENLAYOUT_SIZE_LARGE);
1232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1234 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001235 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1237 // There is no focus view, and this window will
1238 // be behind any soft input window, so hide the
1239 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001240 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001241 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001243 } else if (isTextEditor && doAutoShow && (softInputMode &
1244 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 // There is a focus view, and we are navigating forward
1246 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001247 // We only do this automatically if the window an resize
1248 // to accomodate the IME (so what the user sees will give
1249 // them good context without input information being obscured
1250 // by the IME) or if running on a large screen where there
1251 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001252 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001253 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 }
1255 break;
1256 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1257 // Do nothing.
1258 break;
1259 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1260 if ((softInputMode &
1261 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001262 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001263 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 }
1265 break;
1266 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001267 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001268 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 break;
1270 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1271 if ((softInputMode &
1272 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001273 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001274 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 }
1276 break;
1277 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001278 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001279 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 break;
1281 }
1282 }
1283 } finally {
1284 Binder.restoreCallingIdentity(ident);
1285 }
1286 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1289 synchronized (mMethodMap) {
1290 if (mCurClient == null || client == null
1291 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001292 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001293 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 }
1295
satok440aab52010-11-25 09:43:11 +09001296 // Always call subtype picker, because subtype picker is a superset of input method
1297 // picker.
satokab751aa2010-09-14 19:17:36 +09001298 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
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
satokb416a712010-11-25 20:42:14 +09001317 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001318 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001319 synchronized (mMethodMap) {
1320 if (mCurClient == null || client == null
1321 || mCurClient.client.asBinder() != client.asBinder()) {
1322 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1323 }
satok7fee71f2010-12-17 18:54:26 +09001324 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1325 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001326 }
1327 }
1328
satok735cf382010-11-11 20:40:09 +09001329 public boolean switchToLastInputMethod(IBinder token) {
1330 synchronized (mMethodMap) {
1331 Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1332 if (lastIme != null) {
1333 InputMethodInfo imi = mMethodMap.get(lastIme.first);
1334 if (imi != null) {
satok28203512010-11-24 11:06:49 +09001335 setInputMethodWithSubtypeId(token, lastIme.first, getSubtypeIdFromHashCode(
satok735cf382010-11-11 20:40:09 +09001336 imi, Integer.valueOf(lastIme.second)));
1337 return true;
1338 }
1339 }
1340 return false;
1341 }
1342 }
1343
satok28203512010-11-24 11:06:49 +09001344 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 synchronized (mMethodMap) {
1346 if (token == null) {
1347 if (mContext.checkCallingOrSelfPermission(
1348 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1349 != PackageManager.PERMISSION_GRANTED) {
1350 throw new SecurityException(
1351 "Using null token requires permission "
1352 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1353 }
1354 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001355 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1356 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 return;
1358 }
1359
1360 long ident = Binder.clearCallingIdentity();
1361 try {
satokab751aa2010-09-14 19:17:36 +09001362 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 } finally {
1364 Binder.restoreCallingIdentity(ident);
1365 }
1366 }
1367 }
1368
1369 public void hideMySoftInput(IBinder token, int flags) {
1370 synchronized (mMethodMap) {
1371 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001372 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1373 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 return;
1375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 long ident = Binder.clearCallingIdentity();
1377 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001378 hideCurrentInputLocked(flags, null);
1379 } finally {
1380 Binder.restoreCallingIdentity(ident);
1381 }
1382 }
1383 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001384
The Android Open Source Project4df24232009-03-05 14:34:35 -08001385 public void showMySoftInput(IBinder token, int flags) {
1386 synchronized (mMethodMap) {
1387 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001388 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1389 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001390 return;
1391 }
1392 long ident = Binder.clearCallingIdentity();
1393 try {
1394 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 } finally {
1396 Binder.restoreCallingIdentity(ident);
1397 }
1398 }
1399 }
1400
1401 void setEnabledSessionInMainThread(SessionState session) {
1402 if (mEnabledSession != session) {
1403 if (mEnabledSession != null) {
1404 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001405 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 mEnabledSession.method.setSessionEnabled(
1407 mEnabledSession.session, false);
1408 } catch (RemoteException e) {
1409 }
1410 }
1411 mEnabledSession = session;
1412 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001413 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 session.method.setSessionEnabled(
1415 session.session, true);
1416 } catch (RemoteException e) {
1417 }
1418 }
1419 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 public boolean handleMessage(Message msg) {
1422 HandlerCaller.SomeArgs args;
1423 switch (msg.what) {
1424 case MSG_SHOW_IM_PICKER:
1425 showInputMethodMenu();
1426 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001427
satokab751aa2010-09-14 19:17:36 +09001428 case MSG_SHOW_IM_SUBTYPE_PICKER:
1429 showInputMethodSubtypeMenu();
1430 return true;
1431
satok47a44912010-10-06 16:03:58 +09001432 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001433 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001434 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001435 return true;
1436
1437 case MSG_SHOW_IM_CONFIG:
1438 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001439 return true;
1440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 case MSG_UNBIND_INPUT:
1444 try {
1445 ((IInputMethod)msg.obj).unbindInput();
1446 } catch (RemoteException e) {
1447 // There is nothing interesting about the method dying.
1448 }
1449 return true;
1450 case MSG_BIND_INPUT:
1451 args = (HandlerCaller.SomeArgs)msg.obj;
1452 try {
1453 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1454 } catch (RemoteException e) {
1455 }
1456 return true;
1457 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001458 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001460 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1461 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 } catch (RemoteException e) {
1463 }
1464 return true;
1465 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001466 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001468 ((IInputMethod)args.arg1).hideSoftInput(0,
1469 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 } catch (RemoteException e) {
1471 }
1472 return true;
1473 case MSG_ATTACH_TOKEN:
1474 args = (HandlerCaller.SomeArgs)msg.obj;
1475 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001476 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1478 } catch (RemoteException e) {
1479 }
1480 return true;
1481 case MSG_CREATE_SESSION:
1482 args = (HandlerCaller.SomeArgs)msg.obj;
1483 try {
1484 ((IInputMethod)args.arg1).createSession(
1485 (IInputMethodCallback)args.arg2);
1486 } catch (RemoteException e) {
1487 }
1488 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 case MSG_START_INPUT:
1492 args = (HandlerCaller.SomeArgs)msg.obj;
1493 try {
1494 SessionState session = (SessionState)args.arg1;
1495 setEnabledSessionInMainThread(session);
1496 session.method.startInput((IInputContext)args.arg2,
1497 (EditorInfo)args.arg3);
1498 } catch (RemoteException e) {
1499 }
1500 return true;
1501 case MSG_RESTART_INPUT:
1502 args = (HandlerCaller.SomeArgs)msg.obj;
1503 try {
1504 SessionState session = (SessionState)args.arg1;
1505 setEnabledSessionInMainThread(session);
1506 session.method.restartInput((IInputContext)args.arg2,
1507 (EditorInfo)args.arg3);
1508 } catch (RemoteException e) {
1509 }
1510 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 case MSG_UNBIND_METHOD:
1515 try {
1516 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1517 } catch (RemoteException e) {
1518 // There is nothing interesting about the last client dying.
1519 }
1520 return true;
1521 case MSG_BIND_METHOD:
1522 args = (HandlerCaller.SomeArgs)msg.obj;
1523 try {
1524 ((IInputMethodClient)args.arg1).onBindMethod(
1525 (InputBindResult)args.arg2);
1526 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001527 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 }
1529 return true;
1530 }
1531 return false;
1532 }
1533
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001534 private boolean isSystemIme(InputMethodInfo inputMethod) {
1535 return (inputMethod.getServiceInfo().applicationInfo.flags
1536 & ApplicationInfo.FLAG_SYSTEM) != 0;
1537 }
1538
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001539 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001540 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001541 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001542 // We'd prefer to fall back on a system IME, since that is safer.
1543 int i=enabled.size();
1544 while (i > 0) {
1545 i--;
1546 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1547 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1548 break;
1549 }
1550 }
satokab751aa2010-09-14 19:17:36 +09001551 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001552 if (DEBUG) {
1553 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1554 }
satok723a27e2010-11-11 14:58:11 +09001555 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001556 return true;
1557 }
1558
1559 return false;
1560 }
1561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1563 HashMap<String, InputMethodInfo> map) {
1564 list.clear();
1565 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001568 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001569 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1570 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1571 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1572 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573
1574 List<ResolveInfo> services = pm.queryIntentServices(
1575 new Intent(InputMethod.SERVICE_INTERFACE),
1576 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 for (int i = 0; i < services.size(); ++i) {
1579 ResolveInfo ri = services.get(i);
1580 ServiceInfo si = ri.serviceInfo;
1581 ComponentName compName = new ComponentName(si.packageName, si.name);
1582 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1583 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001584 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 + ": it does not require the permission "
1586 + android.Manifest.permission.BIND_INPUT_METHOD);
1587 continue;
1588 }
1589
Joe Onorato8a9b2202010-02-26 18:56:32 -08001590 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591
1592 try {
1593 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1594 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001595 final String id = p.getId();
1596 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597
Amith Yamasanie861ec12010-03-24 21:39:27 -07001598 // System IMEs are enabled by default, unless there's a hard keyboard
1599 // and the system IME was explicitly disabled
1600 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1601 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001602 }
1603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001605 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001609 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001611 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 }
1613 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001614
1615 String defaultIme = Settings.Secure.getString(mContext
1616 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001617 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001618 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001619 updateFromSettingsLocked();
1620 }
1621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001625
satokab751aa2010-09-14 19:17:36 +09001626 private void showInputMethodMenu() {
1627 showInputMethodMenuInternal(false);
1628 }
1629
1630 private void showInputMethodSubtypeMenu() {
1631 showInputMethodMenuInternal(true);
1632 }
1633
satok217f5482010-12-15 05:19:19 +09001634 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
satok86417ea2010-10-27 14:11:03 +09001635 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_AND_SUBTYPE_ENABLER);
satok47a44912010-10-06 16:03:58 +09001636 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001637 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1638 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001639 if (!TextUtils.isEmpty(inputMethodId)) {
1640 intent.putExtra(EXTRA_INPUT_METHOD_ID, inputMethodId);
1641 }
satok217f5482010-12-15 05:19:19 +09001642 mContext.startActivity(intent);
1643 }
1644
1645 private void showConfigureInputMethods() {
1646 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1647 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1648 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1649 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001650 mContext.startActivity(intent);
1651 }
1652
satokab751aa2010-09-14 19:17:36 +09001653 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001654 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 String lastInputMethodId = Settings.Secure.getString(context
1661 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001662 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001663 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001664
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001665 synchronized (mMethodMap) {
satok7f35c8c2010-10-07 21:13:11 +09001666 final List<Pair<InputMethodInfo, ArrayList<String>>> immis =
satok67ddf9c2010-11-17 09:45:54 +09001667 mSettings.getEnabledInputMethodAndSubtypeHashCodeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001668 ArrayList<Integer> subtypeIds = new ArrayList<Integer>();
1669
1670 if (immis == null || immis.size() == 0) {
1671 return;
1672 }
1673
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001674 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001676 int N = immis.size();
satok913a8922010-08-26 21:53:41 +09001677
satokab751aa2010-09-14 19:17:36 +09001678 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1679 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001680
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001681 for (int i = 0; i < N; ++i) {
satok7f35c8c2010-10-07 21:13:11 +09001682 InputMethodInfo property = immis.get(i).first;
1683 final ArrayList<String> enabledSubtypeIds = immis.get(i).second;
1684 HashSet<String> enabledSubtypeSet = new HashSet<String>();
1685 for (String s : enabledSubtypeIds) {
1686 enabledSubtypeSet.add(s);
1687 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001688 if (property == null) {
1689 continue;
1690 }
satokab751aa2010-09-14 19:17:36 +09001691 ArrayList<InputMethodSubtype> subtypes = property.getSubtypes();
1692 CharSequence label = property.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001693 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokab751aa2010-09-14 19:17:36 +09001694 for (int j = 0; j < subtypes.size(); ++j) {
1695 InputMethodSubtype subtype = subtypes.get(j);
satok7f35c8c2010-10-07 21:13:11 +09001696 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1697 CharSequence title;
1698 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001699 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001700 if (nameResId != 0) {
1701 title = pm.getText(property.getPackageName(), nameResId,
1702 property.getServiceInfo().applicationInfo);
1703 } else {
1704 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001705 // TODO: Use more friendly Title and UI
1706 title = label + "," + (mode == null ? "" : mode) + ","
1707 + (language == null ? "" : language);
1708 }
1709 imMap.put(title, new Pair<InputMethodInfo, Integer>(property, j));
satokab751aa2010-09-14 19:17:36 +09001710 }
satokab751aa2010-09-14 19:17:36 +09001711 }
1712 } else {
1713 imMap.put(label,
1714 new Pair<InputMethodInfo, Integer>(property, NOT_A_SUBTYPE_ID));
1715 subtypeIds.add(0);
1716 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001717 }
satok913a8922010-08-26 21:53:41 +09001718
1719 N = imMap.size();
1720 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001721 mIms = new InputMethodInfo[N];
1722 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001723 int checkedItem = 0;
1724 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001725 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1726 mIms[i] = value.first;
1727 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001728 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001729 int subtypeId = mSubtypeIds[i];
1730 if ((subtypeId == NOT_A_SUBTYPE_ID)
1731 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1732 || (subtypeId == lastInputMethodSubtypeId)) {
1733 checkedItem = i;
1734 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 }
satokab751aa2010-09-14 19:17:36 +09001737
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001738 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1739 public void onClick(DialogInterface dialog, int which) {
1740 hideInputMethodMenu();
1741 }
1742 };
satokd87c2592010-09-29 11:52:06 +09001743
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001744 TypedArray a = context.obtainStyledAttributes(null,
1745 com.android.internal.R.styleable.DialogPreference,
1746 com.android.internal.R.attr.alertDialogStyle, 0);
1747 mDialogBuilder = new AlertDialog.Builder(context)
1748 .setTitle(com.android.internal.R.string.select_input_method)
1749 .setOnCancelListener(new OnCancelListener() {
1750 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001752 }
1753 })
1754 .setIcon(a.getDrawable(
1755 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1756 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001757
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001758 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1759 new AlertDialog.OnClickListener() {
1760 public void onClick(DialogInterface dialog, int which) {
1761 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001762 if (mIms == null || mIms.length <= which
1763 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001764 return;
1765 }
1766 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001767 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001768 hideInputMethodMenu();
1769 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001770 if ((subtypeId < 0)
1771 || (subtypeId >= im.getSubtypes().size())) {
1772 subtypeId = NOT_A_SUBTYPE_ID;
1773 }
1774 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001775 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001778 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779
satok7f35c8c2010-10-07 21:13:11 +09001780 if (showSubtypes) {
1781 mDialogBuilder.setPositiveButton(com.android.internal.R.string.more_item_label,
1782 new DialogInterface.OnClickListener() {
1783 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09001784 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09001785 }
1786 });
1787 }
satok0ff647b2010-10-08 13:49:28 +09001788 mDialogBuilder.setNegativeButton(com.android.internal.R.string.cancel,
1789 new DialogInterface.OnClickListener() {
1790 public void onClick(DialogInterface dialog, int whichButton) {
1791 hideInputMethodMenu();
1792 }
1793 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 mSwitchingDialog = mDialogBuilder.create();
1795 mSwitchingDialog.getWindow().setType(
1796 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1797 mSwitchingDialog.show();
1798 }
1799 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001802 synchronized (mMethodMap) {
1803 hideInputMethodMenuLocked();
1804 }
1805 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001806
The Android Open Source Project10592532009-03-18 17:39:46 -07001807 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001808 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809
The Android Open Source Project10592532009-03-18 17:39:46 -07001810 if (mSwitchingDialog != null) {
1811 mSwitchingDialog.dismiss();
1812 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001814
The Android Open Source Project10592532009-03-18 17:39:46 -07001815 mDialogBuilder = null;
1816 mItems = null;
1817 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 public boolean setInputMethodEnabled(String id, boolean enabled) {
1823 synchronized (mMethodMap) {
1824 if (mContext.checkCallingOrSelfPermission(
1825 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1826 != PackageManager.PERMISSION_GRANTED) {
1827 throw new SecurityException(
1828 "Requires permission "
1829 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1830 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 long ident = Binder.clearCallingIdentity();
1833 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001834 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 } finally {
1836 Binder.restoreCallingIdentity(ident);
1837 }
1838 }
1839 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001840
1841 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1842 // Make sure this is a valid input method.
1843 InputMethodInfo imm = mMethodMap.get(id);
1844 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001845 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001846 }
1847
satokd87c2592010-09-29 11:52:06 +09001848 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1849 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001850
satokd87c2592010-09-29 11:52:06 +09001851 if (enabled) {
1852 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1853 if (pair.first.equals(id)) {
1854 // We are enabling this input method, but it is already enabled.
1855 // Nothing to do. The previous state was enabled.
1856 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001857 }
1858 }
satokd87c2592010-09-29 11:52:06 +09001859 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1860 // Previous state was disabled.
1861 return false;
1862 } else {
1863 StringBuilder builder = new StringBuilder();
1864 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1865 builder, enabledInputMethodsList, id)) {
1866 // Disabled input method is currently selected, switch to another one.
1867 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1868 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001869 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
1870 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
1871 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09001872 }
1873 // Previous state was enabled.
1874 return true;
1875 } else {
1876 // We are disabling the input method but it is already disabled.
1877 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001878 return false;
1879 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001880 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001881 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001882
satok723a27e2010-11-11 14:58:11 +09001883 private void saveCurrentInputMethodAndSubtypeToHistory() {
1884 String subtypeId = NOT_A_SUBTYPE_ID_STR;
1885 if (mCurrentSubtype != null) {
1886 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
1887 }
1888 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
satokab751aa2010-09-14 19:17:36 +09001889 }
1890
satok723a27e2010-11-11 14:58:11 +09001891 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
1892 boolean setSubtypeOnly) {
1893 // Update the history of InputMethod and Subtype
1894 saveCurrentInputMethodAndSubtypeToHistory();
1895
1896 // Set Subtype here
1897 if (imi == null || subtypeId < 0) {
1898 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001899 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09001900 } else {
1901 final ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1902 if (subtypeId < subtypes.size()) {
1903 mSettings.putSelectedSubtype(subtypes.get(subtypeId).hashCode());
1904 mCurrentSubtype = subtypes.get(subtypeId);
1905 } else {
1906 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
1907 mCurrentSubtype = null;
1908 }
satokab751aa2010-09-14 19:17:36 +09001909 }
satok723a27e2010-11-11 14:58:11 +09001910
1911 if (!setSubtypeOnly) {
1912 // Set InputMethod here
1913 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
1914 }
1915 }
1916
1917 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
1918 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
1919 int lastSubtypeId = NOT_A_SUBTYPE_ID;
1920 // newDefaultIme is empty when there is no candidate for the selected IME.
1921 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
1922 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
1923 if (subtypeHashCode != null) {
1924 try {
1925 lastSubtypeId = getSubtypeIdFromHashCode(
1926 imi, Integer.valueOf(subtypeHashCode));
1927 } catch (NumberFormatException e) {
1928 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
1929 }
1930 }
1931 }
1932 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09001933 }
1934
1935 private int getSelectedInputMethodSubtypeId(String id) {
1936 InputMethodInfo imi = mMethodMap.get(id);
1937 if (imi == null) {
1938 return NOT_A_SUBTYPE_ID;
1939 }
satokab751aa2010-09-14 19:17:36 +09001940 int subtypeId;
1941 try {
1942 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
1943 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
1944 } catch (SettingNotFoundException e) {
1945 return NOT_A_SUBTYPE_ID;
1946 }
satok723a27e2010-11-11 14:58:11 +09001947 return getSubtypeIdFromHashCode(imi, subtypeId);
1948 }
1949
1950 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09001951 if (imi != null) {
1952 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1953 for (int i = 0; i < subtypes.size(); ++i) {
1954 InputMethodSubtype ims = subtypes.get(i);
1955 if (subtypeHashCode == ims.hashCode()) {
1956 return i;
1957 }
satokab751aa2010-09-14 19:17:36 +09001958 }
1959 }
1960 return NOT_A_SUBTYPE_ID;
1961 }
1962
satok4e4569d2010-11-19 18:45:53 +09001963 /**
1964 * If there are no selected subtypes, tries finding the most applicable one according to the
1965 * given locale.
1966 * @param subtypes this function will search the most applicable subtype in subtypes
1967 * @param mode subtypes will be filtered by mode
1968 * @param locale subtypes will be filtered by locale
1969 * @param defaultSubtypeId if this function can't find the most applicable subtype, it will
1970 * return defaultSubtypeId
1971 * @return the most applicable subtypeId
1972 */
satokcd7cd292010-11-20 15:46:23 +09001973 private InputMethodSubtype findLastResortApplicableSubtypeLocked(
satok4e4569d2010-11-19 18:45:53 +09001974 List<InputMethodSubtype> subtypes, String mode, String locale, int defaultSubtypeId) {
satok8fbb1e82010-11-02 23:15:58 +09001975 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09001976 return null;
satok8fbb1e82010-11-02 23:15:58 +09001977 }
satok4e4569d2010-11-19 18:45:53 +09001978 if (TextUtils.isEmpty(locale)) {
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001979 locale = mRes.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09001980 }
satok8fbb1e82010-11-02 23:15:58 +09001981 final String language = locale.substring(0, 2);
1982 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09001983 InputMethodSubtype applicableSubtype = null;
satok8fbb1e82010-11-02 23:15:58 +09001984 for (int i = 0; i < subtypes.size(); ++i) {
satokcd7cd292010-11-20 15:46:23 +09001985 InputMethodSubtype subtype = subtypes.get(i);
1986 final String subtypeLocale = subtype.getLocale();
satok4e4569d2010-11-19 18:45:53 +09001987 // An applicable subtype should match "mode".
1988 if (subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok9ef02832010-11-04 21:17:48 +09001989 if (locale.equals(subtypeLocale)) {
1990 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09001991 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09001992 break;
1993 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
1994 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09001995 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09001996 partialMatchFound = true;
1997 }
satok8fbb1e82010-11-02 23:15:58 +09001998 }
1999 }
2000
2001 // The first subtype applicable to the system locale will be defined as the most applicable
2002 // subtype.
2003 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002004 Slog.d(TAG, "Applicable InputMethodSubtype was found: " + applicableSubtype.getMode()
2005 + "," + applicableSubtype.getLocale());
satok8fbb1e82010-11-02 23:15:58 +09002006 }
satokcd7cd292010-11-20 15:46:23 +09002007 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002008 }
2009
satok4e4569d2010-11-19 18:45:53 +09002010 // If there are no selected shortcuts, tries finding the most applicable ones.
2011 private Pair<InputMethodInfo, InputMethodSubtype>
2012 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2013 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2014 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002015 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002016 boolean foundInSystemIME = false;
2017
2018 // Search applicable subtype for each InputMethodInfo
2019 for (InputMethodInfo imi: imis) {
satokcd7cd292010-11-20 15:46:23 +09002020 InputMethodSubtype subtype = null;
satok4e4569d2010-11-19 18:45:53 +09002021 if (mCurrentSubtype != null) {
2022 // 1. Search with the current subtype's locale and the enabled subtypes
satokcd7cd292010-11-20 15:46:23 +09002023 subtype = findLastResortApplicableSubtypeLocked(
satok4e4569d2010-11-19 18:45:53 +09002024 mSettings.getEnabledInputMethodSubtypeListLocked(
2025 imi), mode, mCurrentSubtype.getLocale(), NOT_A_SUBTYPE_ID);
satokcd7cd292010-11-20 15:46:23 +09002026 if (subtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002027 // 2. Search with the current subtype's locale and all subtypes
satokcd7cd292010-11-20 15:46:23 +09002028 subtype = findLastResortApplicableSubtypeLocked(imi.getSubtypes(),
satok4e4569d2010-11-19 18:45:53 +09002029 mode, mCurrentSubtype.getLocale(), NOT_A_SUBTYPE_ID);
2030 }
2031 }
2032 // 3. Search with the system locale and the enabled subtypes
satokcd7cd292010-11-20 15:46:23 +09002033 if (subtype == null) {
2034 subtype = findLastResortApplicableSubtypeLocked(
satok4e4569d2010-11-19 18:45:53 +09002035 mSettings.getEnabledInputMethodSubtypeListLocked(
2036 imi), mode, null, NOT_A_SUBTYPE_ID);
2037 }
satokcd7cd292010-11-20 15:46:23 +09002038 if (subtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002039 // 4. Search with the system locale and all subtypes
satokcd7cd292010-11-20 15:46:23 +09002040 subtype = findLastResortApplicableSubtypeLocked(imi.getSubtypes(),
satok4e4569d2010-11-19 18:45:53 +09002041 mode, null, NOT_A_SUBTYPE_ID);
2042 }
satokcd7cd292010-11-20 15:46:23 +09002043 if (subtype != null) {
satok4e4569d2010-11-19 18:45:53 +09002044 if (imi.getId().equals(mCurMethodId)) {
2045 // The current input method is the most applicable IME.
2046 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002047 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002048 break;
2049 } else if ((imi.getServiceInfo().applicationInfo.flags
2050 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2051 // The system input method is 2nd applicable IME.
2052 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002053 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002054 foundInSystemIME = true;
2055 } else if (!foundInSystemIME) {
2056 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002057 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002058 }
2059 }
2060 }
2061 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002062 if (mostApplicableIMI != null) {
2063 Slog.w(TAG, "Most applicable shortcut input method was:"
2064 + mostApplicableIMI.getId());
2065 if (mostApplicableSubtype != null) {
2066 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2067 + "," + mostApplicableSubtype.getMode() + ","
2068 + mostApplicableSubtype.getLocale());
2069 }
2070 }
satok4e4569d2010-11-19 18:45:53 +09002071 }
satokcd7cd292010-11-20 15:46:23 +09002072 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002073 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002074 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002075 } else {
2076 return null;
2077 }
2078 }
2079
satokab751aa2010-09-14 19:17:36 +09002080 /**
2081 * @return Return the current subtype of this input method.
2082 */
2083 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002084 boolean subtypeIsSelected = false;
2085 try {
2086 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2087 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2088 } catch (SettingNotFoundException e) {
2089 }
satok3ef8b292010-11-23 06:06:29 +09002090 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002091 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002092 String lastInputMethodId = Settings.Secure.getString(
2093 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002094 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2095 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002096 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2097 if (imi != null) {
2098 // If there are no selected subtypes, the framework will try to find
2099 // the most applicable subtype from all subtypes whose mode is
2100 // SUBTYPE_MODE_KEYBOARD. This is an exceptional case, so we will hardcode
2101 // the mode.
satokcd7cd292010-11-20 15:46:23 +09002102 mCurrentSubtype = findLastResortApplicableSubtypeLocked(imi.getSubtypes(),
satok4e4569d2010-11-19 18:45:53 +09002103 SUBTYPE_MODE_KEYBOARD, null, DEFAULT_SUBTYPE_ID);
2104 }
satokcd7cd292010-11-20 15:46:23 +09002105 } else {
satok3ef8b292010-11-23 06:06:29 +09002106 mCurrentSubtype =
2107 mMethodMap.get(lastInputMethodId).getSubtypes().get(subtypeId);
2108 }
satok8fbb1e82010-11-02 23:15:58 +09002109 }
satok3ef8b292010-11-23 06:06:29 +09002110 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002111 }
satokab751aa2010-09-14 19:17:36 +09002112 }
2113
satokf3db1af2010-11-23 13:34:33 +09002114 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2115 InputMethodSubtype subtype) {
2116 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2117 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2118 } else {
2119 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2120 subtypes.add(subtype);
2121 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2122 }
2123 }
2124
satok4e4569d2010-11-19 18:45:53 +09002125 // TODO: We should change the return type from List to List<Parcelable>
2126 public List getShortcutInputMethodsAndSubtypes() {
2127 synchronized (mMethodMap) {
satokf3db1af2010-11-23 13:34:33 +09002128 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002129 // If there are no selected shortcut subtypes, the framework will try to find
2130 // the most applicable subtype from all subtypes whose mode is
2131 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002132 Pair<InputMethodInfo, InputMethodSubtype> info =
2133 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2134 SUBTYPE_MODE_VOICE);
2135 addShortcutInputMethodAndSubtypes(info.first, info.second);
2136 }
satokcd7cd292010-11-20 15:46:23 +09002137 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002138 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2139 ret.add(imi);
2140 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2141 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002142 }
2143 }
satokf3db1af2010-11-23 13:34:33 +09002144 return ret;
satok4e4569d2010-11-19 18:45:53 +09002145 }
2146 }
2147
satokb66d2872010-11-10 01:04:04 +09002148 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2149 synchronized (mMethodMap) {
2150 if (subtype != null && mCurMethodId != null) {
2151 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2152 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2153 if (subtypeId != NOT_A_SUBTYPE_ID) {
2154 setInputMethodLocked(mCurMethodId, subtypeId);
2155 return true;
2156 }
2157 }
2158 return false;
2159 }
2160 }
2161
satokd87c2592010-09-29 11:52:06 +09002162 /**
2163 * Utility class for putting and getting settings for InputMethod
2164 * TODO: Move all putters and getters of settings to this class.
2165 */
2166 private static class InputMethodSettings {
2167 // The string for enabled input method is saved as follows:
2168 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2169 private static final char INPUT_METHOD_SEPARATER = ':';
2170 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002171 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002172 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2173
satok723a27e2010-11-11 14:58:11 +09002174 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002175 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2176
2177 private final ContentResolver mResolver;
2178 private final HashMap<String, InputMethodInfo> mMethodMap;
2179 private final ArrayList<InputMethodInfo> mMethodList;
2180
2181 private String mEnabledInputMethodsStrCache;
2182
2183 private static void buildEnabledInputMethodsSettingString(
2184 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2185 String id = pair.first;
2186 ArrayList<String> subtypes = pair.second;
2187 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002188 // Inputmethod and subtypes are saved in the settings as follows:
2189 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2190 for (String subtypeId: subtypes) {
2191 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002192 }
2193 }
2194
2195 public InputMethodSettings(
2196 ContentResolver resolver, HashMap<String, InputMethodInfo> methodMap,
2197 ArrayList<InputMethodInfo> methodList) {
2198 mResolver = resolver;
2199 mMethodMap = methodMap;
2200 mMethodList = methodList;
2201 }
2202
2203 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2204 return createEnabledInputMethodListLocked(
2205 getEnabledInputMethodsAndSubtypeListLocked());
2206 }
2207
satok7f35c8c2010-10-07 21:13:11 +09002208 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002209 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2210 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002211 getEnabledInputMethodsAndSubtypeListLocked());
2212 }
2213
satok67ddf9c2010-11-17 09:45:54 +09002214 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2215 InputMethodInfo imi) {
2216 List<Pair<String, ArrayList<String>>> imsList =
2217 getEnabledInputMethodsAndSubtypeListLocked();
2218 ArrayList<InputMethodSubtype> enabledSubtypes =
2219 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002220 if (imi != null) {
2221 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2222 InputMethodInfo info = mMethodMap.get(imsPair.first);
2223 if (info != null && info.getId().equals(imi.getId())) {
2224 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
2225 for (InputMethodSubtype ims: subtypes) {
2226 for (String s: imsPair.second) {
2227 if (String.valueOf(ims.hashCode()).equals(s)) {
2228 enabledSubtypes.add(ims);
2229 }
satok67ddf9c2010-11-17 09:45:54 +09002230 }
2231 }
satok884ef9a2010-11-18 10:39:46 +09002232 break;
satok67ddf9c2010-11-17 09:45:54 +09002233 }
satok67ddf9c2010-11-17 09:45:54 +09002234 }
2235 }
2236 return enabledSubtypes;
2237 }
2238
satokd87c2592010-09-29 11:52:06 +09002239 // At the initial boot, the settings for input methods are not set,
2240 // so we need to enable IME in that case.
2241 public void enableAllIMEsIfThereIsNoEnabledIME() {
2242 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2243 StringBuilder sb = new StringBuilder();
2244 final int N = mMethodList.size();
2245 for (int i = 0; i < N; i++) {
2246 InputMethodInfo imi = mMethodList.get(i);
2247 Slog.i(TAG, "Adding: " + imi.getId());
2248 if (i > 0) sb.append(':');
2249 sb.append(imi.getId());
2250 }
2251 putEnabledInputMethodsStr(sb.toString());
2252 }
2253 }
2254
2255 public List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
2256 ArrayList<Pair<String, ArrayList<String>>> imsList
2257 = new ArrayList<Pair<String, ArrayList<String>>>();
2258 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2259 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2260 return imsList;
2261 }
satok723a27e2010-11-11 14:58:11 +09002262 mInputMethodSplitter.setString(enabledInputMethodsStr);
2263 while (mInputMethodSplitter.hasNext()) {
2264 String nextImsStr = mInputMethodSplitter.next();
2265 mSubtypeSplitter.setString(nextImsStr);
2266 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002267 ArrayList<String> subtypeHashes = new ArrayList<String>();
2268 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002269 String imeId = mSubtypeSplitter.next();
2270 while (mSubtypeSplitter.hasNext()) {
2271 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002272 }
2273 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2274 }
2275 }
2276 return imsList;
2277 }
2278
2279 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2280 if (reloadInputMethodStr) {
2281 getEnabledInputMethodsStr();
2282 }
2283 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2284 // Add in the newly enabled input method.
2285 putEnabledInputMethodsStr(id);
2286 } else {
2287 putEnabledInputMethodsStr(
2288 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2289 }
2290 }
2291
2292 /**
2293 * Build and put a string of EnabledInputMethods with removing specified Id.
2294 * @return the specified id was removed or not.
2295 */
2296 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2297 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2298 boolean isRemoved = false;
2299 boolean needsAppendSeparator = false;
2300 for (Pair<String, ArrayList<String>> ims: imsList) {
2301 String curId = ims.first;
2302 if (curId.equals(id)) {
2303 // We are disabling this input method, and it is
2304 // currently enabled. Skip it to remove from the
2305 // new list.
2306 isRemoved = true;
2307 } else {
2308 if (needsAppendSeparator) {
2309 builder.append(INPUT_METHOD_SEPARATER);
2310 } else {
2311 needsAppendSeparator = true;
2312 }
2313 buildEnabledInputMethodsSettingString(builder, ims);
2314 }
2315 }
2316 if (isRemoved) {
2317 // Update the setting with the new list of input methods.
2318 putEnabledInputMethodsStr(builder.toString());
2319 }
2320 return isRemoved;
2321 }
2322
2323 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2324 List<Pair<String, ArrayList<String>>> imsList) {
2325 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2326 for (Pair<String, ArrayList<String>> ims: imsList) {
2327 InputMethodInfo info = mMethodMap.get(ims.first);
2328 if (info != null) {
2329 res.add(info);
2330 }
2331 }
2332 return res;
2333 }
2334
satok7f35c8c2010-10-07 21:13:11 +09002335 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002336 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002337 List<Pair<String, ArrayList<String>>> imsList) {
2338 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2339 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2340 for (Pair<String, ArrayList<String>> ims : imsList) {
2341 InputMethodInfo info = mMethodMap.get(ims.first);
2342 if (info != null) {
2343 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2344 }
2345 }
2346 return res;
2347 }
2348
satokd87c2592010-09-29 11:52:06 +09002349 private void putEnabledInputMethodsStr(String str) {
2350 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2351 mEnabledInputMethodsStrCache = str;
2352 }
2353
2354 private String getEnabledInputMethodsStr() {
2355 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2356 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002357 if (DEBUG) {
2358 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2359 }
satokd87c2592010-09-29 11:52:06 +09002360 return mEnabledInputMethodsStrCache;
2361 }
satok723a27e2010-11-11 14:58:11 +09002362
2363 private void saveSubtypeHistory(
2364 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2365 StringBuilder builder = new StringBuilder();
2366 boolean isImeAdded = false;
2367 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2368 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2369 newSubtypeId);
2370 isImeAdded = true;
2371 }
2372 for (Pair<String, String> ime: savedImes) {
2373 String imeId = ime.first;
2374 String subtypeId = ime.second;
2375 if (TextUtils.isEmpty(subtypeId)) {
2376 subtypeId = NOT_A_SUBTYPE_ID_STR;
2377 }
2378 if (isImeAdded) {
2379 builder.append(INPUT_METHOD_SEPARATER);
2380 } else {
2381 isImeAdded = true;
2382 }
2383 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2384 subtypeId);
2385 }
2386 // Remove the last INPUT_METHOD_SEPARATER
2387 putSubtypeHistoryStr(builder.toString());
2388 }
2389
2390 public void addSubtypeToHistory(String imeId, String subtypeId) {
2391 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2392 for (Pair<String, String> ime: subtypeHistory) {
2393 if (ime.first.equals(imeId)) {
2394 if (DEBUG) {
2395 Slog.v(TAG, "Subtype found in the history: " + imeId
2396 + ime.second);
2397 }
2398 // We should break here
2399 subtypeHistory.remove(ime);
2400 break;
2401 }
2402 }
2403 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2404 }
2405
2406 private void putSubtypeHistoryStr(String str) {
2407 if (DEBUG) {
2408 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2409 }
2410 Settings.Secure.putString(
2411 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2412 }
2413
2414 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2415 // Gets the first one from the history
2416 return getLastSubtypeForInputMethodLockedInternal(null);
2417 }
2418
2419 public String getLastSubtypeForInputMethodLocked(String imeId) {
2420 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2421 if (ime != null) {
2422 return ime.second;
2423 } else {
2424 return null;
2425 }
2426 }
2427
2428 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2429 List<Pair<String, ArrayList<String>>> enabledImes =
2430 getEnabledInputMethodsAndSubtypeListLocked();
2431 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2432 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2433 final String imeInTheHistory = imeAndSubtype.first;
2434 // If imeId is empty, returns the first IME and subtype in the history
2435 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2436 final String subtypeInTheHistory = imeAndSubtype.second;
2437 final String subtypeHashCode = getEnabledSubtypeForInputMethodAndSubtypeLocked(
2438 enabledImes, imeInTheHistory, subtypeInTheHistory);
2439 if (!TextUtils.isEmpty(subtypeHashCode)) {
2440 if (DEBUG) {
2441 Slog.d(TAG, "Enabled subtype found in the history:" + subtypeHashCode);
2442 }
2443 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2444 }
2445 }
2446 }
2447 if (DEBUG) {
2448 Slog.d(TAG, "No enabled IME found in the history");
2449 }
2450 return null;
2451 }
2452
2453 private String getEnabledSubtypeForInputMethodAndSubtypeLocked(List<Pair<String,
2454 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2455 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2456 if (enabledIme.first.equals(imeId)) {
2457 for (String s: enabledIme.second) {
2458 if (s.equals(subtypeHashCode)) {
2459 // If both imeId and subtypeId are enabled, return subtypeId.
2460 return s;
2461 }
2462 }
2463 // If imeId was enabled but subtypeId was disabled.
2464 return NOT_A_SUBTYPE_ID_STR;
2465 }
2466 }
2467 // If both imeId and subtypeId are disabled, return null
2468 return null;
2469 }
2470
2471 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2472 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2473 final String subtypeHistoryStr = getSubtypeHistoryStr();
2474 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2475 return imsList;
2476 }
2477 mInputMethodSplitter.setString(subtypeHistoryStr);
2478 while (mInputMethodSplitter.hasNext()) {
2479 String nextImsStr = mInputMethodSplitter.next();
2480 mSubtypeSplitter.setString(nextImsStr);
2481 if (mSubtypeSplitter.hasNext()) {
2482 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2483 // The first element is ime id.
2484 String imeId = mSubtypeSplitter.next();
2485 while (mSubtypeSplitter.hasNext()) {
2486 subtypeId = mSubtypeSplitter.next();
2487 break;
2488 }
2489 imsList.add(new Pair<String, String>(imeId, subtypeId));
2490 }
2491 }
2492 return imsList;
2493 }
2494
2495 private String getSubtypeHistoryStr() {
2496 if (DEBUG) {
2497 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2498 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2499 }
2500 return Settings.Secure.getString(
2501 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2502 }
2503
2504 public void putSelectedInputMethod(String imeId) {
2505 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2506 }
2507
2508 public void putSelectedSubtype(int subtypeId) {
2509 Settings.Secure.putInt(
2510 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2511 }
satokd87c2592010-09-29 11:52:06 +09002512 }
2513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 @Override
2517 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2518 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2519 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2522 + Binder.getCallingPid()
2523 + ", uid=" + Binder.getCallingUid());
2524 return;
2525 }
2526
2527 IInputMethod method;
2528 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002530 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 synchronized (mMethodMap) {
2533 p.println("Current Input Method Manager state:");
2534 int N = mMethodList.size();
2535 p.println(" Input Methods:");
2536 for (int i=0; i<N; i++) {
2537 InputMethodInfo info = mMethodList.get(i);
2538 p.println(" InputMethod #" + i + ":");
2539 info.dump(p, " ");
2540 }
2541 p.println(" Clients:");
2542 for (ClientState ci : mClients.values()) {
2543 p.println(" Client " + ci + ":");
2544 p.println(" client=" + ci.client);
2545 p.println(" inputContext=" + ci.inputContext);
2546 p.println(" sessionRequested=" + ci.sessionRequested);
2547 p.println(" curSession=" + ci.curSession);
2548 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002549 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002551 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2552 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2554 + " mBoundToMethod=" + mBoundToMethod);
2555 p.println(" mCurToken=" + mCurToken);
2556 p.println(" mCurIntent=" + mCurIntent);
2557 method = mCurMethod;
2558 p.println(" mCurMethod=" + mCurMethod);
2559 p.println(" mEnabledSession=" + mEnabledSession);
2560 p.println(" mShowRequested=" + mShowRequested
2561 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2562 + " mShowForced=" + mShowForced
2563 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002564 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002566
Jeff Brownb88102f2010-09-08 11:49:43 -07002567 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 pw.flush();
2570 try {
2571 client.client.asBinder().dump(fd, args);
2572 } catch (RemoteException e) {
2573 p.println("Input method client dead: " + e);
2574 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002575 } else {
2576 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002578
Jeff Brownb88102f2010-09-08 11:49:43 -07002579 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 pw.flush();
2582 try {
2583 method.asBinder().dump(fd, args);
2584 } catch (RemoteException e) {
2585 p.println("Input method service dead: " + e);
2586 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002587 } else {
2588 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 }
2590 }
2591}