blob: 7a0dc3eaee5eab15324560f3817dd903a6773a3d [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
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080029import com.android.server.EventLogTags;
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;
Joe Onorato857fd9b2011-01-27 15:08:35 -080052import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.Binder;
54import android.os.Handler;
55import android.os.IBinder;
56import android.os.IInterface;
57import android.os.Message;
58import android.os.Parcel;
59import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080060import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.ServiceManager;
62import android.os.SystemClock;
63import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070064import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090065import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.text.TextUtils;
67import android.util.EventLog;
satokab751aa2010-09-14 19:17:36 +090068import android.util.Pair;
Joe Onorato8a9b2202010-02-26 18:56:32 -080069import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.util.PrintWriterPrinter;
71import android.util.Printer;
72import android.view.IWindowManager;
73import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputBinding;
76import android.view.inputmethod.InputMethod;
77import android.view.inputmethod.InputMethodInfo;
78import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090079import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
81import java.io.FileDescriptor;
82import java.io.IOException;
83import java.io.PrintWriter;
84import java.util.ArrayList;
Ken Wakasa761eb372011-03-04 19:06:18 +090085import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +090087import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import java.util.List;
satok913a8922010-08-26 21:53:41 +090089import java.util.Map;
90import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92/**
93 * This class provides a system service that manages input methods.
94 */
95public class InputMethodManagerService extends IInputMethodManager.Stub
96 implements ServiceConnection, Handler.Callback {
97 static final boolean DEBUG = false;
98 static final String TAG = "InputManagerService";
99
100 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900101 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900102 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900103 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 static final int MSG_UNBIND_INPUT = 1000;
106 static final int MSG_BIND_INPUT = 1010;
107 static final int MSG_SHOW_SOFT_INPUT = 1020;
108 static final int MSG_HIDE_SOFT_INPUT = 1030;
109 static final int MSG_ATTACH_TOKEN = 1040;
110 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 static final int MSG_START_INPUT = 2000;
113 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 static final int MSG_UNBIND_METHOD = 3000;
116 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800119
satokab751aa2010-09-14 19:17:36 +0900120 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900121 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900122 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
123 private static final String SUBTYPE_MODE_VOICE = "voice";
124
satok57ffc002011-01-25 00:11:47 +0900125 // TODO: Will formalize this value as API
126 private static final String SUBTYPE_EXTRAVALUE_EXCLUDE_FROM_LAST_IME =
127 "excludeFromLastInputMethod";
128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800130 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900132 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700134 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 final IWindowManager mIWindowManager;
136 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 // All known input methods. mMethodMap also serves as the global
141 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900142 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
143 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 class SessionState {
146 final ClientState client;
147 final IInputMethod method;
148 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 @Override
151 public String toString() {
152 return "SessionState{uid " + client.uid + " pid " + client.pid
153 + " method " + Integer.toHexString(
154 System.identityHashCode(method))
155 + " session " + Integer.toHexString(
156 System.identityHashCode(session))
157 + "}";
158 }
159
160 SessionState(ClientState _client, IInputMethod _method,
161 IInputMethodSession _session) {
162 client = _client;
163 method = _method;
164 session = _session;
165 }
166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 class ClientState {
169 final IInputMethodClient client;
170 final IInputContext inputContext;
171 final int uid;
172 final int pid;
173 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 boolean sessionRequested;
176 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 @Override
179 public String toString() {
180 return "ClientState{" + Integer.toHexString(
181 System.identityHashCode(this)) + " uid " + uid
182 + " pid " + pid + "}";
183 }
184
185 ClientState(IInputMethodClient _client, IInputContext _inputContext,
186 int _uid, int _pid) {
187 client = _client;
188 inputContext = _inputContext;
189 uid = _uid;
190 pid = _pid;
191 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
192 }
193 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 final HashMap<IBinder, ClientState> mClients
196 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700199 * Set once the system is ready to run third party code.
200 */
201 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800202
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700203 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 * Id of the currently selected input method.
205 */
206 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 /**
209 * The current binding sequence number, incremented every time there is
210 * a new bind performed.
211 */
212 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 /**
215 * The client that is currently bound to an input method.
216 */
217 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700220 * The last window token that gained focus.
221 */
222 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800223
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700224 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 * The input context last provided by the current client.
226 */
227 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 /**
230 * The attributes last provided by the current client.
231 */
232 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 /**
235 * The input method ID of the input method service that we are currently
236 * connected to or in the process of connecting to.
237 */
238 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 /**
satokab751aa2010-09-14 19:17:36 +0900241 * The current subtype of the current input method.
242 */
243 private InputMethodSubtype mCurrentSubtype;
244
satok4e4569d2010-11-19 18:45:53 +0900245 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900246 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
247 mShortcutInputMethodsAndSubtypes =
248 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900249
250 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 * Set to true if our ServiceConnection is currently actively bound to
252 * a service (whether or not we have gotten its IBinder back yet).
253 */
254 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 /**
257 * Set if the client has asked for the input method to be shown.
258 */
259 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 /**
262 * Set if we were explicitly told to show the input method.
263 */
264 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 /**
267 * Set if we were forced to be shown.
268 */
269 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 /**
272 * Set if we last told the input method to show itself.
273 */
274 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 /**
277 * The Intent used to connect to the current input method.
278 */
279 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 /**
282 * The token we have made for the currently active input method, to
283 * identify it in the future.
284 */
285 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 /**
288 * If non-null, this is the input method service we are currently connected
289 * to.
290 */
291 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 /**
294 * Time that we last initiated a bind to the input method, to determine
295 * if we should try to disconnect and reconnect to it.
296 */
297 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * Have we called mCurMethod.bindInput()?
301 */
302 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 /**
305 * Currently enabled session. Only touched by service thread, not
306 * protected by a lock.
307 */
308 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 /**
311 * True if the screen is on. The value is true initially.
312 */
313 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800314
Joe Onorato857fd9b2011-01-27 15:08:35 -0800315 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
316 int mImeWindowVis;
317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 AlertDialog.Builder mDialogBuilder;
319 AlertDialog mSwitchingDialog;
320 InputMethodInfo[] mIms;
321 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900322 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 class SettingsObserver extends ContentObserver {
325 SettingsObserver(Handler handler) {
326 super(handler);
327 ContentResolver resolver = mContext.getContentResolver();
328 resolver.registerContentObserver(Settings.Secure.getUriFor(
329 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900330 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900331 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
332 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900333 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 @Override public void onChange(boolean selfChange) {
337 synchronized (mMethodMap) {
338 updateFromSettingsLocked();
339 }
340 }
341 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
344 @Override
345 public void onReceive(Context context, Intent intent) {
346 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
347 mScreenOn = true;
348 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
349 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700350 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
351 hideInputMethodMenu();
352 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800354 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 }
356
357 // Inform the current client of the change in active status
358 try {
359 if (mCurClient != null && mCurClient.client != null) {
360 mCurClient.client.setActive(mScreenOn);
361 }
362 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800363 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 + mCurClient.pid + " uid " + mCurClient.uid);
365 }
366 }
367 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800368
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800369 class MyPackageMonitor extends PackageMonitor {
370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800372 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800374 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
376 final int N = mMethodList.size();
377 if (curInputMethodId != null) {
378 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800379 InputMethodInfo imi = mMethodList.get(i);
380 if (imi.getId().equals(curInputMethodId)) {
381 for (String pkg : packages) {
382 if (imi.getPackageName().equals(pkg)) {
383 if (!doit) {
384 return true;
385 }
satok723a27e2010-11-11 14:58:11 +0900386 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800387 chooseNewDefaultIMELocked();
388 return true;
389 }
390 }
391 }
392 }
393 }
394 }
395 return false;
396 }
397
398 @Override
399 public void onSomePackagesChanged() {
400 synchronized (mMethodMap) {
401 InputMethodInfo curIm = null;
402 String curInputMethodId = Settings.Secure.getString(mContext
403 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
404 final int N = mMethodList.size();
405 if (curInputMethodId != null) {
406 for (int i=0; i<N; i++) {
407 InputMethodInfo imi = mMethodList.get(i);
408 if (imi.getId().equals(curInputMethodId)) {
409 curIm = imi;
410 }
411 int change = isPackageDisappearing(imi.getPackageName());
412 if (change == PACKAGE_TEMPORARY_CHANGE
413 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800414 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800415 + imi.getComponent());
416 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 }
418 }
419 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800420
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800421 buildInputMethodListLocked(mMethodList, mMethodMap);
422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800424
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800425 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800426 int change = isPackageDisappearing(curIm.getPackageName());
427 if (change == PACKAGE_TEMPORARY_CHANGE
428 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800429 ServiceInfo si = null;
430 try {
431 si = mContext.getPackageManager().getServiceInfo(
432 curIm.getComponent(), 0);
433 } catch (PackageManager.NameNotFoundException ex) {
434 }
435 if (si == null) {
436 // Uh oh, current input method is no longer around!
437 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800438 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800439 mImeWindowVis = 0;
440 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
441 mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800442 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800443 changed = true;
444 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800445 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900446 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800447 }
448 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800449 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800450 }
satokab751aa2010-09-14 19:17:36 +0900451
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800452 if (curIm == null) {
453 // We currently don't have a default input method... is
454 // one now available?
455 changed = chooseNewDefaultIMELocked();
456 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800457
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800458 if (changed) {
459 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 }
461 }
462 }
463 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 class MethodCallback extends IInputMethodCallback.Stub {
466 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 MethodCallback(IInputMethod method) {
469 mMethod = method;
470 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 public void finishedEvent(int seq, boolean handled) throws RemoteException {
473 }
474
475 public void sessionCreated(IInputMethodSession session) throws RemoteException {
476 onSessionCreated(mMethod, session);
477 }
478 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800479
Joe Onorato089de882010-04-12 08:18:45 -0700480 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800482 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 mHandler = new Handler(this);
484 mIWindowManager = IWindowManager.Stub.asInterface(
485 ServiceManager.getService(Context.WINDOW_SERVICE));
486 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
487 public void executeMessage(Message msg) {
488 handleMessage(msg);
489 }
490 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800491
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800492 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 IntentFilter screenOnOffFilt = new IntentFilter();
495 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
496 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700497 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800499
satok913a8922010-08-26 21:53:41 +0900500 mStatusBar = statusBar;
501 statusBar.setIconVisibility("ime", false);
502
satokd87c2592010-09-29 11:52:06 +0900503 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900504 mSettings = new InputMethodSettings(
505 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900507 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508
satokd87c2592010-09-29 11:52:06 +0900509 if (TextUtils.isEmpty(Settings.Secure.getString(
510 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900512 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
514 try {
satokd87c2592010-09-29 11:52:06 +0900515 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 imi.getPackageName(), 0).getResources();
517 if (res.getBoolean(imi.getIsDefaultResourceId())) {
518 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800519 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 }
521 } catch (PackageManager.NameNotFoundException ex) {
522 } catch (Resources.NotFoundException ex) {
523 }
524 }
525 }
satokd87c2592010-09-29 11:52:06 +0900526 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800528 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900531 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 }
533 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 mSettingsObserver = new SettingsObserver(mHandler);
536 updateFromSettingsLocked();
537 }
538
539 @Override
540 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
541 throws RemoteException {
542 try {
543 return super.onTransact(code, data, reply, flags);
544 } catch (RuntimeException e) {
545 // The input method manager only throws security exceptions, so let's
546 // log all others.
547 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800548 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 }
550 throw e;
551 }
552 }
553
554 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700555 synchronized (mMethodMap) {
556 if (!mSystemReady) {
557 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700558 try {
559 startInputInnerLocked();
560 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800561 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700562 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700563 }
564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 public List<InputMethodInfo> getInputMethodList() {
568 synchronized (mMethodMap) {
569 return new ArrayList<InputMethodInfo>(mMethodList);
570 }
571 }
572
573 public List<InputMethodInfo> getEnabledInputMethodList() {
574 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900575 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 }
577 }
578
satokbb4aa062011-01-19 21:40:27 +0900579 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
580 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
581 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
582 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
583 for (InputMethodInfo imi: getEnabledInputMethodList()) {
584 enabledInputMethodAndSubtypes.put(
585 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
586 }
587 return enabledInputMethodAndSubtypes;
588 }
589
590 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
591 boolean allowsImplicitlySelectedSubtypes) {
592 if (imi == null && mCurMethodId != null) {
593 imi = mMethodMap.get(mCurMethodId);
594 }
satok7265d9b2011-02-14 15:47:30 +0900595 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900596 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900597 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
598 enabledSubtypes = getApplicableSubtypesLocked(mRes, getSubtypes(imi));
satokbb4aa062011-01-19 21:40:27 +0900599 }
satok7265d9b2011-02-14 15:47:30 +0900600 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900601 }
602
satok16331c82010-12-20 23:48:46 +0900603 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
604 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900605 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900606 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900607 }
608 }
609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 public void addClient(IInputMethodClient client,
611 IInputContext inputContext, int uid, int pid) {
612 synchronized (mMethodMap) {
613 mClients.put(client.asBinder(), new ClientState(client,
614 inputContext, uid, pid));
615 }
616 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 public void removeClient(IInputMethodClient client) {
619 synchronized (mMethodMap) {
620 mClients.remove(client.asBinder());
621 }
622 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 void executeOrSendMessage(IInterface target, Message msg) {
625 if (target.asBinder() instanceof Binder) {
626 mCaller.sendMessage(msg);
627 } else {
628 handleMessage(msg);
629 msg.recycle();
630 }
631 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800632
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700633 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800635 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 + mCurClient.client.asBinder());
637 if (mBoundToMethod) {
638 mBoundToMethod = false;
639 if (mCurMethod != null) {
640 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
641 MSG_UNBIND_INPUT, mCurMethod));
642 }
643 }
644 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
645 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
646 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 // Call setActive(false) on the old client
649 try {
650 mCurClient.client.setActive(false);
651 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800652 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 + mCurClient.pid + " uid " + mCurClient.uid);
654 }
655 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800656
The Android Open Source Project10592532009-03-18 17:39:46 -0700657 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 }
659 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 private int getImeShowFlags() {
662 int flags = 0;
663 if (mShowForced) {
664 flags |= InputMethod.SHOW_FORCED
665 | InputMethod.SHOW_EXPLICIT;
666 } else if (mShowExplicitlyRequested) {
667 flags |= InputMethod.SHOW_EXPLICIT;
668 }
669 return flags;
670 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 private int getAppShowFlags() {
673 int flags = 0;
674 if (mShowForced) {
675 flags |= InputMethodManager.SHOW_FORCED;
676 } else if (!mShowExplicitlyRequested) {
677 flags |= InputMethodManager.SHOW_IMPLICIT;
678 }
679 return flags;
680 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
683 if (!mBoundToMethod) {
684 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
685 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
686 mBoundToMethod = true;
687 }
688 final SessionState session = mCurClient.curSession;
689 if (initial) {
690 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
691 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
692 } else {
693 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
694 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
695 }
696 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800697 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800698 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 }
700 return needResult
701 ? new InputBindResult(session.session, mCurId, mCurSeq)
702 : null;
703 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 InputBindResult startInputLocked(IInputMethodClient client,
706 IInputContext inputContext, EditorInfo attribute,
707 boolean initial, boolean needResult) {
708 // If no method is currently selected, do nothing.
709 if (mCurMethodId == null) {
710 return mNoBinding;
711 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 ClientState cs = mClients.get(client.asBinder());
714 if (cs == null) {
715 throw new IllegalArgumentException("unknown client "
716 + client.asBinder());
717 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 try {
720 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
721 // Check with the window manager to make sure this client actually
722 // has a window with focus. If not, reject. This is thread safe
723 // because if the focus changes some time before or after, the
724 // next client receiving focus that has any interest in input will
725 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800726 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
728 return null;
729 }
730 } catch (RemoteException e) {
731 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 if (mCurClient != cs) {
734 // If the client is changing, we need to switch over to the new
735 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700736 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800737 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 + cs.client.asBinder());
739
740 // If the screen is on, inform the new client it is active
741 if (mScreenOn) {
742 try {
743 cs.client.setActive(mScreenOn);
744 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800745 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 + cs.pid + " uid " + cs.uid);
747 }
748 }
749 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 // Bump up the sequence for this client and attach it.
752 mCurSeq++;
753 if (mCurSeq <= 0) mCurSeq = 1;
754 mCurClient = cs;
755 mCurInputContext = inputContext;
756 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 // Check if the input method is changing.
759 if (mCurId != null && mCurId.equals(mCurMethodId)) {
760 if (cs.curSession != null) {
761 // Fast case: if we are already connected to the input method,
762 // then just return it.
763 return attachNewInputLocked(initial, needResult);
764 }
765 if (mHaveConnection) {
766 if (mCurMethod != null) {
767 if (!cs.sessionRequested) {
768 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800769 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
771 MSG_CREATE_SESSION, mCurMethod,
772 new MethodCallback(mCurMethod)));
773 }
774 // Return to client, and we will get back with it when
775 // we have had a session made for it.
776 return new InputBindResult(null, mCurId, mCurSeq);
777 } else if (SystemClock.uptimeMillis()
778 < (mLastBindTime+TIME_TO_RECONNECT)) {
779 // In this case we have connected to the service, but
780 // don't yet have its interface. If it hasn't been too
781 // long since we did the connection, we'll return to
782 // the client and wait to get the service interface so
783 // we can report back. If it has been too long, we want
784 // to fall through so we can try a disconnect/reconnect
785 // to see if we can get back in touch with the service.
786 return new InputBindResult(null, mCurId, mCurSeq);
787 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800788 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
789 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 }
791 }
792 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800793
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700794 return startInputInnerLocked();
795 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800796
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700797 InputBindResult startInputInnerLocked() {
798 if (mCurMethodId == null) {
799 return mNoBinding;
800 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800801
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700802 if (!mSystemReady) {
803 // If the system is not yet ready, we shouldn't be running third
804 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700805 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700806 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 InputMethodInfo info = mMethodMap.get(mCurMethodId);
809 if (info == null) {
810 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
811 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800812
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700813 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
816 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700817 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
818 com.android.internal.R.string.input_method_binding_label);
819 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
820 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
822 mLastBindTime = SystemClock.uptimeMillis();
823 mHaveConnection = true;
824 mCurId = info.getId();
825 mCurToken = new Binder();
826 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800827 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 mIWindowManager.addWindowToken(mCurToken,
829 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
830 } catch (RemoteException e) {
831 }
832 return new InputBindResult(null, mCurId, mCurSeq);
833 } else {
834 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800835 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 + mCurIntent);
837 }
838 return null;
839 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 public InputBindResult startInput(IInputMethodClient client,
842 IInputContext inputContext, EditorInfo attribute,
843 boolean initial, boolean needResult) {
844 synchronized (mMethodMap) {
845 final long ident = Binder.clearCallingIdentity();
846 try {
847 return startInputLocked(client, inputContext, attribute,
848 initial, needResult);
849 } finally {
850 Binder.restoreCallingIdentity(ident);
851 }
852 }
853 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 public void finishInput(IInputMethodClient client) {
856 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 public void onServiceConnected(ComponentName name, IBinder service) {
859 synchronized (mMethodMap) {
860 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
861 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700862 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800863 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700864 unbindCurrentMethodLocked(false);
865 return;
866 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800867 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700868 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
869 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800871 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700872 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700874 MSG_CREATE_SESSION, mCurMethod,
875 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 }
877 }
878 }
879 }
880
881 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
882 synchronized (mMethodMap) {
883 if (mCurMethod != null && method != null
884 && mCurMethod.asBinder() == method.asBinder()) {
885 if (mCurClient != null) {
886 mCurClient.curSession = new SessionState(mCurClient,
887 method, session);
888 mCurClient.sessionRequested = false;
889 InputBindResult res = attachNewInputLocked(true, true);
890 if (res.method != null) {
891 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
892 MSG_BIND_METHOD, mCurClient.client, res));
893 }
894 }
895 }
896 }
897 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800898
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700899 void unbindCurrentMethodLocked(boolean reportToClient) {
900 if (mHaveConnection) {
901 mContext.unbindService(this);
902 mHaveConnection = false;
903 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800904
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700905 if (mCurToken != null) {
906 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800907 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700908 mIWindowManager.removeWindowToken(mCurToken);
909 } catch (RemoteException e) {
910 }
911 mCurToken = null;
912 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800913
The Android Open Source Project10592532009-03-18 17:39:46 -0700914 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700915 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800916
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700917 if (reportToClient && mCurClient != null) {
918 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
919 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
920 }
921 }
922
Devin Taylor0c33ed22010-02-23 13:26:46 -0600923 private void finishSession(SessionState sessionState) {
924 if (sessionState != null && sessionState.session != null) {
925 try {
926 sessionState.session.finishSession();
927 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700928 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600929 }
930 }
931 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800932
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700933 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 if (mCurMethod != null) {
935 for (ClientState cs : mClients.values()) {
936 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600937 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 cs.curSession = null;
939 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600940
941 finishSession(mEnabledSession);
942 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 mCurMethod = null;
944 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700945 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 public void onServiceDisconnected(ComponentName name) {
949 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800950 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 + " mCurIntent=" + mCurIntent);
952 if (mCurMethod != null && mCurIntent != null
953 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700954 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 // We consider this to be a new bind attempt, since the system
956 // should now try to restart the service for us.
957 mLastBindTime = SystemClock.uptimeMillis();
958 mShowRequested = mInputShown;
959 mInputShown = false;
960 if (mCurClient != null) {
961 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
962 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
963 }
964 }
965 }
966 }
967
968 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700969 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 long ident = Binder.clearCallingIdentity();
971 try {
972 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700973 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 return;
975 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 synchronized (mMethodMap) {
978 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800979 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700980 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800982 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700983 mStatusBar.setIcon("ime", packageName, iconId, 0);
984 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 }
986 }
987 } finally {
988 Binder.restoreCallingIdentity(ident);
989 }
990 }
991
Joe Onorato857fd9b2011-01-27 15:08:35 -0800992 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +0900993 int uid = Binder.getCallingUid();
994 long ident = Binder.clearCallingIdentity();
995 try {
996 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -0800997 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +0900998 return;
999 }
1000
1001 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001002 mImeWindowVis = vis;
1003 mBackDisposition = backDisposition;
1004 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
satok06487a52010-10-29 11:37:18 +09001005 }
1006 } finally {
1007 Binder.restoreCallingIdentity(ident);
1008 }
1009 }
1010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001012 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1013 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1014 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1015 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001017 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001018 // There is no input method selected, try to choose new applicable input method.
1019 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1020 id = Settings.Secure.getString(mContext.getContentResolver(),
1021 Settings.Secure.DEFAULT_INPUT_METHOD);
1022 }
1023 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 try {
satokab751aa2010-09-14 19:17:36 +09001025 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001027 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001028 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001029 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 }
satokf3db1af2010-11-23 13:34:33 +09001031 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001032 } else {
1033 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001034 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001035 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 }
1037 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001038
satokab751aa2010-09-14 19:17:36 +09001039 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 InputMethodInfo info = mMethodMap.get(id);
1041 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001042 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001046 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001047 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1048 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001049 }
1050 if (subtype != mCurrentSubtype) {
1051 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001052 if (subtype != null) {
1053 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1054 }
satokcd7cd292010-11-20 15:46:23 +09001055 if (mCurMethod != null) {
1056 try {
satoke40dea02011-01-30 01:14:02 +09001057 final Configuration conf = mRes.getConfiguration();
1058 final boolean haveHardKeyboard = conf.keyboard
1059 != Configuration.KEYBOARD_NOKEYS;
1060 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001061 && conf.hardKeyboardHidden
1062 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001063 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1064 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1065 : 0;
Joe Onorato857fd9b2011-01-27 15:08:35 -08001066 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1067 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001068 // If subtype is null, try to find the most applicable one from
1069 // getCurrentInputMethodSubtype.
1070 if (subtype == null) {
1071 subtype = getCurrentInputMethodSubtype();
1072 }
1073 mCurMethod.changeInputMethodSubtype(subtype);
1074 } catch (RemoteException e) {
1075 return;
satokab751aa2010-09-14 19:17:36 +09001076 }
1077 }
1078 }
1079 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 return;
1081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 final long ident = Binder.clearCallingIdentity();
1084 try {
satokab751aa2010-09-14 19:17:36 +09001085 // Set a subtype to this input method.
1086 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001087 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1088 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1089 // because mCurMethodId is stored as a history in
1090 // setSelectedInputMethodAndSubtypeLocked().
1091 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092
1093 if (ActivityManagerNative.isSystemReady()) {
1094 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001095 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 intent.putExtra("input_method_id", id);
1097 mContext.sendBroadcast(intent);
1098 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001099 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 } finally {
1101 Binder.restoreCallingIdentity(ident);
1102 }
1103 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001104
The Android Open Source Project4df24232009-03-05 14:34:35 -08001105 public boolean showSoftInput(IInputMethodClient client, int flags,
1106 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001107 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 long ident = Binder.clearCallingIdentity();
1109 try {
1110 synchronized (mMethodMap) {
1111 if (mCurClient == null || client == null
1112 || mCurClient.client.asBinder() != client.asBinder()) {
1113 try {
1114 // We need to check if this is the current client with
1115 // focus in the window manager, to allow this call to
1116 // be made before input is started in it.
1117 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001118 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001119 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 }
1121 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001122 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 }
1124 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001125
Joe Onorato8a9b2202010-02-26 18:56:32 -08001126 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001127 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 }
1129 } finally {
1130 Binder.restoreCallingIdentity(ident);
1131 }
1132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001133
The Android Open Source Project4df24232009-03-05 14:34:35 -08001134 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 mShowRequested = true;
1136 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1137 mShowExplicitlyRequested = true;
1138 }
1139 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1140 mShowExplicitlyRequested = true;
1141 mShowForced = true;
1142 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001143
Dianne Hackborncc278702009-09-02 23:07:23 -07001144 if (!mSystemReady) {
1145 return false;
1146 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001147
The Android Open Source Project4df24232009-03-05 14:34:35 -08001148 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001150 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1151 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1152 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001154 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 } else if (mHaveConnection && SystemClock.uptimeMillis()
1156 < (mLastBindTime+TIME_TO_RECONNECT)) {
1157 // The client has asked to have the input method shown, but
1158 // we have been sitting here too long with a connection to the
1159 // service and no interface received, so let's disconnect/connect
1160 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001161 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 SystemClock.uptimeMillis()-mLastBindTime,1);
1163 mContext.unbindService(this);
1164 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1165 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001166
The Android Open Source Project4df24232009-03-05 14:34:35 -08001167 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001169
The Android Open Source Project4df24232009-03-05 14:34:35 -08001170 public boolean hideSoftInput(IInputMethodClient client, int flags,
1171 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001172 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 long ident = Binder.clearCallingIdentity();
1174 try {
1175 synchronized (mMethodMap) {
1176 if (mCurClient == null || client == null
1177 || mCurClient.client.asBinder() != client.asBinder()) {
1178 try {
1179 // We need to check if this is the current client with
1180 // focus in the window manager, to allow this call to
1181 // be made before input is started in it.
1182 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001183 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1184 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001185 mImeWindowVis = 0;
1186 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1187 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001188 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001191 mImeWindowVis = 0;
1192 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001193 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
1195 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001196
Joe Onorato8a9b2202010-02-26 18:56:32 -08001197 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001198 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 }
1200 } finally {
1201 Binder.restoreCallingIdentity(ident);
1202 }
1203 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001204
The Android Open Source Project4df24232009-03-05 14:34:35 -08001205 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1207 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001208 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001210 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
1212 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001213 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001215 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001217 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001219 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1220 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1221 res = true;
1222 } else {
1223 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 }
1225 mInputShown = false;
1226 mShowRequested = false;
1227 mShowExplicitlyRequested = false;
1228 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001229 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001231
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001232 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1234 boolean first, int windowFlags) {
1235 long ident = Binder.clearCallingIdentity();
1236 try {
1237 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001238 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 + " viewHasFocus=" + viewHasFocus
1240 + " isTextEditor=" + isTextEditor
1241 + " softInputMode=#" + Integer.toHexString(softInputMode)
1242 + " first=" + first + " flags=#"
1243 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 if (mCurClient == null || client == null
1246 || mCurClient.client.asBinder() != client.asBinder()) {
1247 try {
1248 // We need to check if this is the current client with
1249 // focus in the window manager, to allow this call to
1250 // be made before input is started in it.
1251 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001252 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 return;
1254 }
1255 } catch (RemoteException e) {
1256 }
1257 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001258
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001259 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001260 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001261 return;
1262 }
1263 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001264
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001265 // Should we auto-show the IME even if the caller has not
1266 // specified what should be done with it?
1267 // We only do this automatically if the window can resize
1268 // to accommodate the IME (so what the user sees will give
1269 // them good context without input information being obscured
1270 // by the IME) or if running on a large screen where there
1271 // is more room for the target window + IME.
1272 final boolean doAutoShow =
1273 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1274 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1275 || mRes.getConfiguration().isLayoutSizeAtLeast(
1276 Configuration.SCREENLAYOUT_SIZE_LARGE);
1277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1279 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001280 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1282 // There is no focus view, and this window will
1283 // be behind any soft input window, so hide the
1284 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001285 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001286 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001288 } else if (isTextEditor && doAutoShow && (softInputMode &
1289 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 // There is a focus view, and we are navigating forward
1291 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001292 // We only do this automatically if the window an resize
1293 // to accomodate the IME (so what the user sees will give
1294 // them good context without input information being obscured
1295 // by the IME) or if running on a large screen where there
1296 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001297 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001298 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 }
1300 break;
1301 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1302 // Do nothing.
1303 break;
1304 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1305 if ((softInputMode &
1306 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001307 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001308 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 }
1310 break;
1311 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001312 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001313 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 break;
1315 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1316 if ((softInputMode &
1317 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001318 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001319 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 }
1321 break;
1322 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001323 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001324 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 break;
1326 }
1327 }
1328 } finally {
1329 Binder.restoreCallingIdentity(ident);
1330 }
1331 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1334 synchronized (mMethodMap) {
1335 if (mCurClient == null || client == null
1336 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001337 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001338 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 }
1340
satok440aab52010-11-25 09:43:11 +09001341 // Always call subtype picker, because subtype picker is a superset of input method
1342 // picker.
satokab751aa2010-09-14 19:17:36 +09001343 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1344 }
1345 }
1346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001348 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1349 }
1350
1351 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1352 synchronized (mMethodMap) {
1353 if (subtype != null) {
1354 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1355 mMethodMap.get(id), subtype.hashCode()));
1356 } else {
1357 setInputMethod(token, id);
1358 }
1359 }
satokab751aa2010-09-14 19:17:36 +09001360 }
1361
satokb416a71e2010-11-25 20:42:14 +09001362 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001363 IInputMethodClient client, String inputMethodId) {
satokb416a71e2010-11-25 20:42:14 +09001364 synchronized (mMethodMap) {
1365 if (mCurClient == null || client == null
1366 || mCurClient.client.asBinder() != client.asBinder()) {
1367 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1368 }
satok7fee71f2010-12-17 18:54:26 +09001369 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1370 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09001371 }
1372 }
1373
satokb4788fd2011-05-20 18:19:53 +09001374 @Override
satok735cf382010-11-11 20:40:09 +09001375 public boolean switchToLastInputMethod(IBinder token) {
1376 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001377 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satokb4788fd2011-05-20 18:19:53 +09001378 final InputMethodInfo lastImi;
1379 if (lastIme != null) {
1380 lastImi = mMethodMap.get(lastIme.first);
1381 } else {
1382 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001383 }
satokb4788fd2011-05-20 18:19:53 +09001384 String targetLastImiId = null;
1385 int subtypeId = NOT_A_SUBTYPE_ID;
1386 if (lastIme != null && lastImi != null) {
1387 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1388 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1389 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1390 : mCurrentSubtype.hashCode();
1391 // If the last IME is the same as the current IME and the last subtype is not
1392 // defined, there is no need to switch to the last IME.
1393 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1394 targetLastImiId = lastIme.first;
1395 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1396 }
1397 }
1398
1399 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1400 // This is a safety net. If the currentSubtype can't be added to the history
1401 // and the framework couldn't find the last ime, we will make the last ime be
1402 // the most applicable enabled keyboard subtype of the system imes.
1403 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1404 if (enabled != null) {
1405 final int N = enabled.size();
1406 final String locale = mCurrentSubtype == null
1407 ? mRes.getConfiguration().locale.toString()
1408 : mCurrentSubtype.getLocale();
1409 for (int i = 0; i < N; ++i) {
1410 final InputMethodInfo imi = enabled.get(i);
1411 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1412 InputMethodSubtype keyboardSubtype =
1413 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1414 SUBTYPE_MODE_KEYBOARD, locale, true);
1415 if (keyboardSubtype != null) {
1416 targetLastImiId = imi.getId();
1417 subtypeId = getSubtypeIdFromHashCode(
1418 imi, keyboardSubtype.hashCode());
1419 if(keyboardSubtype.getLocale().equals(locale)) {
1420 break;
1421 }
1422 }
1423 }
1424 }
1425 }
1426 }
1427
1428 if (!TextUtils.isEmpty(targetLastImiId)) {
1429 if (DEBUG) {
1430 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1431 + ", from: " + mCurMethodId + ", " + subtypeId);
1432 }
1433 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1434 return true;
1435 } else {
1436 return false;
1437 }
satok735cf382010-11-11 20:40:09 +09001438 }
1439 }
1440
satok28203512010-11-24 11:06:49 +09001441 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 synchronized (mMethodMap) {
1443 if (token == null) {
1444 if (mContext.checkCallingOrSelfPermission(
1445 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1446 != PackageManager.PERMISSION_GRANTED) {
1447 throw new SecurityException(
1448 "Using null token requires permission "
1449 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1450 }
1451 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001452 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1453 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 return;
1455 }
1456
1457 long ident = Binder.clearCallingIdentity();
1458 try {
satokab751aa2010-09-14 19:17:36 +09001459 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 } finally {
1461 Binder.restoreCallingIdentity(ident);
1462 }
1463 }
1464 }
1465
1466 public void hideMySoftInput(IBinder token, int flags) {
1467 synchronized (mMethodMap) {
1468 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001469 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1470 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 return;
1472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 long ident = Binder.clearCallingIdentity();
1474 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001475 hideCurrentInputLocked(flags, null);
1476 } finally {
1477 Binder.restoreCallingIdentity(ident);
1478 }
1479 }
1480 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001481
The Android Open Source Project4df24232009-03-05 14:34:35 -08001482 public void showMySoftInput(IBinder token, int flags) {
1483 synchronized (mMethodMap) {
1484 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001485 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1486 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001487 return;
1488 }
1489 long ident = Binder.clearCallingIdentity();
1490 try {
1491 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 } finally {
1493 Binder.restoreCallingIdentity(ident);
1494 }
1495 }
1496 }
1497
1498 void setEnabledSessionInMainThread(SessionState session) {
1499 if (mEnabledSession != session) {
1500 if (mEnabledSession != null) {
1501 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001502 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 mEnabledSession.method.setSessionEnabled(
1504 mEnabledSession.session, false);
1505 } catch (RemoteException e) {
1506 }
1507 }
1508 mEnabledSession = session;
1509 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001510 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 session.method.setSessionEnabled(
1512 session.session, true);
1513 } catch (RemoteException e) {
1514 }
1515 }
1516 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 public boolean handleMessage(Message msg) {
1519 HandlerCaller.SomeArgs args;
1520 switch (msg.what) {
1521 case MSG_SHOW_IM_PICKER:
1522 showInputMethodMenu();
1523 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001524
satokab751aa2010-09-14 19:17:36 +09001525 case MSG_SHOW_IM_SUBTYPE_PICKER:
1526 showInputMethodSubtypeMenu();
1527 return true;
1528
satok47a44912010-10-06 16:03:58 +09001529 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001530 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001531 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001532 return true;
1533
1534 case MSG_SHOW_IM_CONFIG:
1535 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001536 return true;
1537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 case MSG_UNBIND_INPUT:
1541 try {
1542 ((IInputMethod)msg.obj).unbindInput();
1543 } catch (RemoteException e) {
1544 // There is nothing interesting about the method dying.
1545 }
1546 return true;
1547 case MSG_BIND_INPUT:
1548 args = (HandlerCaller.SomeArgs)msg.obj;
1549 try {
1550 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1551 } catch (RemoteException e) {
1552 }
1553 return true;
1554 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001555 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001557 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1558 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 } catch (RemoteException e) {
1560 }
1561 return true;
1562 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001563 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001565 ((IInputMethod)args.arg1).hideSoftInput(0,
1566 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 } catch (RemoteException e) {
1568 }
1569 return true;
1570 case MSG_ATTACH_TOKEN:
1571 args = (HandlerCaller.SomeArgs)msg.obj;
1572 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001573 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1575 } catch (RemoteException e) {
1576 }
1577 return true;
1578 case MSG_CREATE_SESSION:
1579 args = (HandlerCaller.SomeArgs)msg.obj;
1580 try {
1581 ((IInputMethod)args.arg1).createSession(
1582 (IInputMethodCallback)args.arg2);
1583 } catch (RemoteException e) {
1584 }
1585 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 case MSG_START_INPUT:
1589 args = (HandlerCaller.SomeArgs)msg.obj;
1590 try {
1591 SessionState session = (SessionState)args.arg1;
1592 setEnabledSessionInMainThread(session);
1593 session.method.startInput((IInputContext)args.arg2,
1594 (EditorInfo)args.arg3);
1595 } catch (RemoteException e) {
1596 }
1597 return true;
1598 case MSG_RESTART_INPUT:
1599 args = (HandlerCaller.SomeArgs)msg.obj;
1600 try {
1601 SessionState session = (SessionState)args.arg1;
1602 setEnabledSessionInMainThread(session);
1603 session.method.restartInput((IInputContext)args.arg2,
1604 (EditorInfo)args.arg3);
1605 } catch (RemoteException e) {
1606 }
1607 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 case MSG_UNBIND_METHOD:
1612 try {
1613 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1614 } catch (RemoteException e) {
1615 // There is nothing interesting about the last client dying.
1616 }
1617 return true;
1618 case MSG_BIND_METHOD:
1619 args = (HandlerCaller.SomeArgs)msg.obj;
1620 try {
1621 ((IInputMethodClient)args.arg1).onBindMethod(
1622 (InputBindResult)args.arg2);
1623 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001624 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 }
1626 return true;
1627 }
1628 return false;
1629 }
1630
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001631 private boolean isSystemIme(InputMethodInfo inputMethod) {
1632 return (inputMethod.getServiceInfo().applicationInfo.flags
1633 & ApplicationInfo.FLAG_SYSTEM) != 0;
1634 }
1635
Ken Wakasa586f0512011-01-20 22:31:01 +09001636 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1637 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1638 final int subtypeCount = imi.getSubtypeCount();
1639 for (int i = 0; i < subtypeCount; ++i) {
1640 subtypes.add(imi.getSubtypeAt(i));
1641 }
1642 return subtypes;
1643 }
1644
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001645 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001646 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001647 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001648 // We'd prefer to fall back on a system IME, since that is safer.
1649 int i=enabled.size();
1650 while (i > 0) {
1651 i--;
1652 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1653 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1654 break;
1655 }
1656 }
satokab751aa2010-09-14 19:17:36 +09001657 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001658 if (DEBUG) {
1659 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1660 }
satok723a27e2010-11-11 14:58:11 +09001661 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001662 return true;
1663 }
1664
1665 return false;
1666 }
1667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1669 HashMap<String, InputMethodInfo> map) {
1670 list.clear();
1671 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001674 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001675 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1676 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1677 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1678 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679
1680 List<ResolveInfo> services = pm.queryIntentServices(
1681 new Intent(InputMethod.SERVICE_INTERFACE),
1682 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 for (int i = 0; i < services.size(); ++i) {
1685 ResolveInfo ri = services.get(i);
1686 ServiceInfo si = ri.serviceInfo;
1687 ComponentName compName = new ComponentName(si.packageName, si.name);
1688 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1689 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001690 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 + ": it does not require the permission "
1692 + android.Manifest.permission.BIND_INPUT_METHOD);
1693 continue;
1694 }
1695
Joe Onorato8a9b2202010-02-26 18:56:32 -08001696 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697
1698 try {
1699 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1700 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001701 final String id = p.getId();
1702 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703
Amith Yamasanie861ec12010-03-24 21:39:27 -07001704 // System IMEs are enabled by default, unless there's a hard keyboard
1705 // and the system IME was explicitly disabled
1706 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1707 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001708 }
1709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001711 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001715 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001717 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 }
1719 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001720
1721 String defaultIme = Settings.Secure.getString(mContext
1722 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001723 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001724 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001725 updateFromSettingsLocked();
1726 }
1727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001731
satokab751aa2010-09-14 19:17:36 +09001732 private void showInputMethodMenu() {
1733 showInputMethodMenuInternal(false);
1734 }
1735
1736 private void showInputMethodSubtypeMenu() {
1737 showInputMethodMenuInternal(true);
1738 }
1739
satok217f5482010-12-15 05:19:19 +09001740 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001741 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001742 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001743 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1744 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001745 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001746 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001747 }
satok217f5482010-12-15 05:19:19 +09001748 mContext.startActivity(intent);
1749 }
1750
1751 private void showConfigureInputMethods() {
1752 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1753 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1754 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1755 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001756 mContext.startActivity(intent);
1757 }
1758
satokab751aa2010-09-14 19:17:36 +09001759 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001760 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 String lastInputMethodId = Settings.Secure.getString(context
1767 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001768 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001769 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001770
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001771 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09001772 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
1773 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001774 if (immis == null || immis.size() == 0) {
1775 return;
1776 }
1777
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001778 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779
Ken Wakasa761eb372011-03-04 19:06:18 +09001780 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
1781 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
1782 new Comparator<InputMethodInfo>() {
1783 @Override
1784 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
1785 if (imi2 == null) return 0;
1786 if (imi1 == null) return 1;
1787 if (pm == null) {
1788 return imi1.getId().compareTo(imi2.getId());
1789 }
1790 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
1791 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
1792 return imiId1.toString().compareTo(imiId2.toString());
1793 }
1794 });
satok913a8922010-08-26 21:53:41 +09001795
Ken Wakasa761eb372011-03-04 19:06:18 +09001796 sortedImmis.putAll(immis);
1797
1798 final ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>> imList =
1799 new ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>>();
1800
1801 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09001802 if (imi == null) continue;
1803 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09001804 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09001805 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
1806 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09001807 }
satokbb4aa062011-01-19 21:40:27 +09001808 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa81f6e3d2011-03-04 09:59:52 +09001809 final CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001810 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09001811 final int subtypeCount = imi.getSubtypeCount();
Ken Wakasa586f0512011-01-20 22:31:01 +09001812 for (int j = 0; j < subtypeCount; ++j) {
satokbb4aa062011-01-19 21:40:27 +09001813 InputMethodSubtype subtype = imi.getSubtypeAt(j);
satok7f35c8c2010-10-07 21:13:11 +09001814 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
Ken Wakasa761eb372011-03-04 19:06:18 +09001815 final CharSequence title;
satok7f35c8c2010-10-07 21:13:11 +09001816 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001817 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001818 if (nameResId != 0) {
Ken Wakasa81f6e3d2011-03-04 09:59:52 +09001819 title = TextUtils.concat(pm.getText(imi.getPackageName(),
1820 nameResId, imi.getServiceInfo().applicationInfo),
Ken Wakasa761eb372011-03-04 19:06:18 +09001821 (TextUtils.isEmpty(label) ? "" : " (" + label + ")"));
satok7f35c8c2010-10-07 21:13:11 +09001822 } else {
1823 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001824 // TODO: Use more friendly Title and UI
1825 title = label + "," + (mode == null ? "" : mode) + ","
1826 + (language == null ? "" : language);
1827 }
Ken Wakasa761eb372011-03-04 19:06:18 +09001828 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
1829 title, new Pair<InputMethodInfo, Integer>(imi, j)));
satokab751aa2010-09-14 19:17:36 +09001830 }
satokab751aa2010-09-14 19:17:36 +09001831 }
1832 } else {
Ken Wakasa761eb372011-03-04 19:06:18 +09001833 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
1834 label, new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID)));
satokab751aa2010-09-14 19:17:36 +09001835 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001836 }
satok913a8922010-08-26 21:53:41 +09001837
Ken Wakasa761eb372011-03-04 19:06:18 +09001838 final int N = imList.size();
1839 mItems = new CharSequence[N];
1840 for (int i = 0; i < N; ++i) {
1841 mItems[i] = imList.get(i).first;
1842 }
satokab751aa2010-09-14 19:17:36 +09001843 mIms = new InputMethodInfo[N];
1844 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001845 int checkedItem = 0;
1846 for (int i = 0; i < N; ++i) {
Ken Wakasa761eb372011-03-04 19:06:18 +09001847 Pair<InputMethodInfo, Integer> value = imList.get(i).second;
satokab751aa2010-09-14 19:17:36 +09001848 mIms[i] = value.first;
1849 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001850 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001851 int subtypeId = mSubtypeIds[i];
1852 if ((subtypeId == NOT_A_SUBTYPE_ID)
1853 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1854 || (subtypeId == lastInputMethodSubtypeId)) {
1855 checkedItem = i;
1856 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 }
satokab751aa2010-09-14 19:17:36 +09001859
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001860 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1861 public void onClick(DialogInterface dialog, int which) {
1862 hideInputMethodMenu();
1863 }
1864 };
satokd87c2592010-09-29 11:52:06 +09001865
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001866 TypedArray a = context.obtainStyledAttributes(null,
1867 com.android.internal.R.styleable.DialogPreference,
1868 com.android.internal.R.attr.alertDialogStyle, 0);
1869 mDialogBuilder = new AlertDialog.Builder(context)
1870 .setTitle(com.android.internal.R.string.select_input_method)
1871 .setOnCancelListener(new OnCancelListener() {
1872 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001874 }
1875 })
1876 .setIcon(a.getDrawable(
1877 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1878 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001879
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001880 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1881 new AlertDialog.OnClickListener() {
1882 public void onClick(DialogInterface dialog, int which) {
1883 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001884 if (mIms == null || mIms.length <= which
1885 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001886 return;
1887 }
1888 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001889 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001890 hideInputMethodMenu();
1891 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001892 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09001893 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09001894 subtypeId = NOT_A_SUBTYPE_ID;
1895 }
1896 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001897 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001898 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001900 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901
satok7f35c8c2010-10-07 21:13:11 +09001902 if (showSubtypes) {
satok82beadf2010-12-27 19:03:06 +09001903 mDialogBuilder.setPositiveButton(
1904 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09001905 new DialogInterface.OnClickListener() {
1906 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09001907 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09001908 }
1909 });
1910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001912 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 mSwitchingDialog.getWindow().setType(
1914 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001915 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 mSwitchingDialog.show();
1917 }
1918 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001921 synchronized (mMethodMap) {
1922 hideInputMethodMenuLocked();
1923 }
1924 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001925
The Android Open Source Project10592532009-03-18 17:39:46 -07001926 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001927 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928
The Android Open Source Project10592532009-03-18 17:39:46 -07001929 if (mSwitchingDialog != null) {
1930 mSwitchingDialog.dismiss();
1931 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001933
The Android Open Source Project10592532009-03-18 17:39:46 -07001934 mDialogBuilder = null;
1935 mItems = null;
1936 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 public boolean setInputMethodEnabled(String id, boolean enabled) {
1942 synchronized (mMethodMap) {
1943 if (mContext.checkCallingOrSelfPermission(
1944 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1945 != PackageManager.PERMISSION_GRANTED) {
1946 throw new SecurityException(
1947 "Requires permission "
1948 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1949 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 long ident = Binder.clearCallingIdentity();
1952 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001953 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 } finally {
1955 Binder.restoreCallingIdentity(ident);
1956 }
1957 }
1958 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001959
1960 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1961 // Make sure this is a valid input method.
1962 InputMethodInfo imm = mMethodMap.get(id);
1963 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001964 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001965 }
1966
satokd87c2592010-09-29 11:52:06 +09001967 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1968 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001969
satokd87c2592010-09-29 11:52:06 +09001970 if (enabled) {
1971 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1972 if (pair.first.equals(id)) {
1973 // We are enabling this input method, but it is already enabled.
1974 // Nothing to do. The previous state was enabled.
1975 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001976 }
1977 }
satokd87c2592010-09-29 11:52:06 +09001978 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1979 // Previous state was disabled.
1980 return false;
1981 } else {
1982 StringBuilder builder = new StringBuilder();
1983 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1984 builder, enabledInputMethodsList, id)) {
1985 // Disabled input method is currently selected, switch to another one.
1986 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1987 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001988 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
1989 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
1990 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09001991 }
1992 // Previous state was enabled.
1993 return true;
1994 } else {
1995 // We are disabling the input method but it is already disabled.
1996 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001997 return false;
1998 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001999 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002000 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002001
satok57ffc002011-01-25 00:11:47 +09002002 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2003 if (subtype == null) return true;
Ken Wakasaa5e9ffa2011-02-16 11:55:23 +09002004 return !subtype.containsExtraValueKey(SUBTYPE_EXTRAVALUE_EXCLUDE_FROM_LAST_IME);
satok57ffc002011-01-25 00:11:47 +09002005 }
2006
satok723a27e2010-11-11 14:58:11 +09002007 private void saveCurrentInputMethodAndSubtypeToHistory() {
2008 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2009 if (mCurrentSubtype != null) {
2010 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2011 }
satok57ffc002011-01-25 00:11:47 +09002012 if (canAddToLastInputMethod(mCurrentSubtype)) {
2013 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2014 }
satokab751aa2010-09-14 19:17:36 +09002015 }
2016
satok723a27e2010-11-11 14:58:11 +09002017 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2018 boolean setSubtypeOnly) {
2019 // Update the history of InputMethod and Subtype
2020 saveCurrentInputMethodAndSubtypeToHistory();
2021
2022 // Set Subtype here
2023 if (imi == null || subtypeId < 0) {
2024 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002025 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002026 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002027 if (subtypeId < imi.getSubtypeCount()) {
2028 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2029 mSettings.putSelectedSubtype(subtype.hashCode());
2030 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002031 } else {
2032 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2033 mCurrentSubtype = null;
2034 }
satokab751aa2010-09-14 19:17:36 +09002035 }
satok723a27e2010-11-11 14:58:11 +09002036
2037 if (!setSubtypeOnly) {
2038 // Set InputMethod here
2039 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2040 }
2041 }
2042
2043 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2044 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2045 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2046 // newDefaultIme is empty when there is no candidate for the selected IME.
2047 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2048 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2049 if (subtypeHashCode != null) {
2050 try {
2051 lastSubtypeId = getSubtypeIdFromHashCode(
2052 imi, Integer.valueOf(subtypeHashCode));
2053 } catch (NumberFormatException e) {
2054 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2055 }
2056 }
2057 }
2058 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002059 }
2060
2061 private int getSelectedInputMethodSubtypeId(String id) {
2062 InputMethodInfo imi = mMethodMap.get(id);
2063 if (imi == null) {
2064 return NOT_A_SUBTYPE_ID;
2065 }
satokab751aa2010-09-14 19:17:36 +09002066 int subtypeId;
2067 try {
2068 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2069 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2070 } catch (SettingNotFoundException e) {
2071 return NOT_A_SUBTYPE_ID;
2072 }
satok723a27e2010-11-11 14:58:11 +09002073 return getSubtypeIdFromHashCode(imi, subtypeId);
2074 }
2075
2076 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002077 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002078 final int subtypeCount = imi.getSubtypeCount();
2079 for (int i = 0; i < subtypeCount; ++i) {
2080 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002081 if (subtypeHashCode == ims.hashCode()) {
2082 return i;
2083 }
satokab751aa2010-09-14 19:17:36 +09002084 }
2085 }
2086 return NOT_A_SUBTYPE_ID;
2087 }
2088
satokdf31ae62011-01-15 06:19:44 +09002089 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2090 Resources res, List<InputMethodSubtype> subtypes) {
2091 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002092 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2093 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2094 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002095 final int N = subtypes.size();
2096 boolean containsKeyboardSubtype = false;
2097 for (int i = 0; i < N; ++i) {
2098 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002099 final String locale = subtype.getLocale();
2100 final String mode = subtype.getMode();
2101 // When system locale starts with subtype's locale, that subtype will be applicable
2102 // for system locale
2103 // For instance, it's clearly applicable for cases like system locale = en_US and
2104 // subtype = en, but it is not necessarily considered applicable for cases like system
2105 // locale = en and subtype = en_US.
2106 // We just call systemLocale.startsWith(locale) in this function because there is no
2107 // need to find applicable subtypes aggressively unlike
2108 // findLastResortApplicableSubtypeLocked.
2109 if (systemLocale.startsWith(locale)) {
2110 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2111 // If more applicable subtypes are contained, skip.
2112 if (applicableSubtype != null
2113 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2114 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002115 if (!containsKeyboardSubtype
2116 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2117 containsKeyboardSubtype = true;
2118 }
2119 }
2120 }
satok3da92232011-01-11 22:46:30 +09002121 ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2122 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002123 if (!containsKeyboardSubtype) {
2124 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002125 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002126 if (lastResortKeyboardSubtype != null) {
2127 applicableSubtypes.add(lastResortKeyboardSubtype);
2128 }
2129 }
2130 return applicableSubtypes;
2131 }
2132
satok4e4569d2010-11-19 18:45:53 +09002133 /**
2134 * If there are no selected subtypes, tries finding the most applicable one according to the
2135 * given locale.
2136 * @param subtypes this function will search the most applicable subtype in subtypes
2137 * @param mode subtypes will be filtered by mode
2138 * @param locale subtypes will be filtered by locale
satok7599a7fb2010-12-22 13:45:23 +09002139 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2140 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002141 * @return the most applicable subtypeId
2142 */
satokdf31ae62011-01-15 06:19:44 +09002143 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2144 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7fb2010-12-22 13:45:23 +09002145 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002146 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002147 return null;
satok8fbb1e82010-11-02 23:15:58 +09002148 }
satok4e4569d2010-11-19 18:45:53 +09002149 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002150 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002151 }
satok8fbb1e82010-11-02 23:15:58 +09002152 final String language = locale.substring(0, 2);
2153 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002154 InputMethodSubtype applicableSubtype = null;
satok7599a7fb2010-12-22 13:45:23 +09002155 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002156 final int N = subtypes.size();
2157 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002158 InputMethodSubtype subtype = subtypes.get(i);
2159 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002160 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2161 // and all subtypes with all modes can be candidates.
2162 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7fb2010-12-22 13:45:23 +09002163 if (firstMatchedModeSubtype == null) {
2164 firstMatchedModeSubtype = subtype;
2165 }
satok9ef02832010-11-04 21:17:48 +09002166 if (locale.equals(subtypeLocale)) {
2167 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002168 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002169 break;
2170 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2171 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002172 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002173 partialMatchFound = true;
2174 }
satok8fbb1e82010-11-02 23:15:58 +09002175 }
2176 }
2177
satok7599a7fb2010-12-22 13:45:23 +09002178 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2179 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002180 }
2181
satok8fbb1e82010-11-02 23:15:58 +09002182 // The first subtype applicable to the system locale will be defined as the most applicable
2183 // subtype.
2184 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002185 if (applicableSubtype != null) {
2186 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2187 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2188 }
satok8fbb1e82010-11-02 23:15:58 +09002189 }
satokcd7cd292010-11-20 15:46:23 +09002190 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002191 }
2192
satok4e4569d2010-11-19 18:45:53 +09002193 // If there are no selected shortcuts, tries finding the most applicable ones.
2194 private Pair<InputMethodInfo, InputMethodSubtype>
2195 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2196 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2197 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002198 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002199 boolean foundInSystemIME = false;
2200
2201 // Search applicable subtype for each InputMethodInfo
2202 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002203 final String imiId = imi.getId();
2204 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2205 continue;
2206 }
satokcd7cd292010-11-20 15:46:23 +09002207 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002208 final List<InputMethodSubtype> enabledSubtypes =
2209 getEnabledInputMethodSubtypeList(imi, true);
2210 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002211 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002212 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002213 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002214 }
satokdf31ae62011-01-15 06:19:44 +09002215 // 2. Search by the system locale from enabledSubtypes.
2216 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002217 if (subtype == null) {
2218 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002219 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002220 }
satok7599a7fb2010-12-22 13:45:23 +09002221 // 4. Search by the current subtype's locale from all subtypes.
2222 if (subtype == null && mCurrentSubtype != null) {
2223 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002224 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09002225 }
2226 // 5. Search by the system locale from all subtypes.
2227 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002228 if (subtype == null) {
satok7599a7fb2010-12-22 13:45:23 +09002229 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002230 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002231 }
satokcd7cd292010-11-20 15:46:23 +09002232 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09002233 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002234 // The current input method is the most applicable IME.
2235 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002236 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002237 break;
satok7599a7fb2010-12-22 13:45:23 +09002238 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002239 // The system input method is 2nd applicable IME.
2240 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002241 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09002242 if ((imi.getServiceInfo().applicationInfo.flags
2243 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2244 foundInSystemIME = true;
2245 }
satok4e4569d2010-11-19 18:45:53 +09002246 }
2247 }
2248 }
2249 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002250 if (mostApplicableIMI != null) {
2251 Slog.w(TAG, "Most applicable shortcut input method was:"
2252 + mostApplicableIMI.getId());
2253 if (mostApplicableSubtype != null) {
2254 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2255 + "," + mostApplicableSubtype.getMode() + ","
2256 + mostApplicableSubtype.getLocale());
2257 }
2258 }
satok4e4569d2010-11-19 18:45:53 +09002259 }
satokcd7cd292010-11-20 15:46:23 +09002260 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002261 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002262 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002263 } else {
2264 return null;
2265 }
2266 }
2267
satokab751aa2010-09-14 19:17:36 +09002268 /**
2269 * @return Return the current subtype of this input method.
2270 */
2271 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002272 boolean subtypeIsSelected = false;
2273 try {
2274 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2275 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2276 } catch (SettingNotFoundException e) {
2277 }
satok3ef8b292010-11-23 06:06:29 +09002278 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002279 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002280 String lastInputMethodId = Settings.Secure.getString(
2281 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002282 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2283 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002284 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2285 if (imi != null) {
2286 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002287 // the most applicable subtype from explicitly or implicitly enabled
2288 // subtypes.
2289 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2290 getEnabledInputMethodSubtypeList(imi, true);
2291 // If there is only one explicitly or implicitly enabled subtype,
2292 // just returns it.
2293 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2294 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2295 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2296 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2297 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2298 SUBTYPE_MODE_KEYBOARD, null, true);
2299 if (mCurrentSubtype == null) {
2300 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2301 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2302 true);
2303 }
2304 }
satok4e4569d2010-11-19 18:45:53 +09002305 }
satokcd7cd292010-11-20 15:46:23 +09002306 } else {
satok3ef8b292010-11-23 06:06:29 +09002307 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002308 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002309 }
satok8fbb1e82010-11-02 23:15:58 +09002310 }
satok3ef8b292010-11-23 06:06:29 +09002311 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002312 }
satokab751aa2010-09-14 19:17:36 +09002313 }
2314
satokf3db1af2010-11-23 13:34:33 +09002315 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2316 InputMethodSubtype subtype) {
2317 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2318 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2319 } else {
2320 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2321 subtypes.add(subtype);
2322 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2323 }
2324 }
2325
satok4e4569d2010-11-19 18:45:53 +09002326 // TODO: We should change the return type from List to List<Parcelable>
2327 public List getShortcutInputMethodsAndSubtypes() {
2328 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002329 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002330 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002331 // If there are no selected shortcut subtypes, the framework will try to find
2332 // the most applicable subtype from all subtypes whose mode is
2333 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002334 Pair<InputMethodInfo, InputMethodSubtype> info =
2335 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2336 SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09002337 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002338 ret.add(info.first);
2339 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09002340 }
satok3da92232011-01-11 22:46:30 +09002341 return ret;
satokf3db1af2010-11-23 13:34:33 +09002342 }
satokf3db1af2010-11-23 13:34:33 +09002343 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2344 ret.add(imi);
2345 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2346 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002347 }
2348 }
satokf3db1af2010-11-23 13:34:33 +09002349 return ret;
satok4e4569d2010-11-19 18:45:53 +09002350 }
2351 }
2352
satokb66d2872010-11-10 01:04:04 +09002353 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2354 synchronized (mMethodMap) {
2355 if (subtype != null && mCurMethodId != null) {
2356 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2357 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2358 if (subtypeId != NOT_A_SUBTYPE_ID) {
2359 setInputMethodLocked(mCurMethodId, subtypeId);
2360 return true;
2361 }
2362 }
2363 return false;
2364 }
2365 }
2366
satokd87c2592010-09-29 11:52:06 +09002367 /**
2368 * Utility class for putting and getting settings for InputMethod
2369 * TODO: Move all putters and getters of settings to this class.
2370 */
2371 private static class InputMethodSettings {
2372 // The string for enabled input method is saved as follows:
2373 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2374 private static final char INPUT_METHOD_SEPARATER = ':';
2375 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002376 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002377 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2378
satok723a27e2010-11-11 14:58:11 +09002379 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002380 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2381
satokdf31ae62011-01-15 06:19:44 +09002382 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002383 private final ContentResolver mResolver;
2384 private final HashMap<String, InputMethodInfo> mMethodMap;
2385 private final ArrayList<InputMethodInfo> mMethodList;
2386
2387 private String mEnabledInputMethodsStrCache;
2388
2389 private static void buildEnabledInputMethodsSettingString(
2390 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2391 String id = pair.first;
2392 ArrayList<String> subtypes = pair.second;
2393 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002394 // Inputmethod and subtypes are saved in the settings as follows:
2395 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2396 for (String subtypeId: subtypes) {
2397 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002398 }
2399 }
2400
2401 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002402 Resources res, ContentResolver resolver,
2403 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2404 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002405 mResolver = resolver;
2406 mMethodMap = methodMap;
2407 mMethodList = methodList;
2408 }
2409
2410 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2411 return createEnabledInputMethodListLocked(
2412 getEnabledInputMethodsAndSubtypeListLocked());
2413 }
2414
satok7f35c8c2010-10-07 21:13:11 +09002415 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002416 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2417 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002418 getEnabledInputMethodsAndSubtypeListLocked());
2419 }
2420
satok67ddf9c2010-11-17 09:45:54 +09002421 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2422 InputMethodInfo imi) {
2423 List<Pair<String, ArrayList<String>>> imsList =
2424 getEnabledInputMethodsAndSubtypeListLocked();
2425 ArrayList<InputMethodSubtype> enabledSubtypes =
2426 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002427 if (imi != null) {
2428 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2429 InputMethodInfo info = mMethodMap.get(imsPair.first);
2430 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002431 final int subtypeCount = info.getSubtypeCount();
2432 for (int i = 0; i < subtypeCount; ++i) {
2433 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002434 for (String s: imsPair.second) {
2435 if (String.valueOf(ims.hashCode()).equals(s)) {
2436 enabledSubtypes.add(ims);
2437 }
satok67ddf9c2010-11-17 09:45:54 +09002438 }
2439 }
satok884ef9a2010-11-18 10:39:46 +09002440 break;
satok67ddf9c2010-11-17 09:45:54 +09002441 }
satok67ddf9c2010-11-17 09:45:54 +09002442 }
2443 }
2444 return enabledSubtypes;
2445 }
2446
satokd87c2592010-09-29 11:52:06 +09002447 // At the initial boot, the settings for input methods are not set,
2448 // so we need to enable IME in that case.
2449 public void enableAllIMEsIfThereIsNoEnabledIME() {
2450 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2451 StringBuilder sb = new StringBuilder();
2452 final int N = mMethodList.size();
2453 for (int i = 0; i < N; i++) {
2454 InputMethodInfo imi = mMethodList.get(i);
2455 Slog.i(TAG, "Adding: " + imi.getId());
2456 if (i > 0) sb.append(':');
2457 sb.append(imi.getId());
2458 }
2459 putEnabledInputMethodsStr(sb.toString());
2460 }
2461 }
2462
satokbb4aa062011-01-19 21:40:27 +09002463 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002464 ArrayList<Pair<String, ArrayList<String>>> imsList
2465 = new ArrayList<Pair<String, ArrayList<String>>>();
2466 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2467 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2468 return imsList;
2469 }
satok723a27e2010-11-11 14:58:11 +09002470 mInputMethodSplitter.setString(enabledInputMethodsStr);
2471 while (mInputMethodSplitter.hasNext()) {
2472 String nextImsStr = mInputMethodSplitter.next();
2473 mSubtypeSplitter.setString(nextImsStr);
2474 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002475 ArrayList<String> subtypeHashes = new ArrayList<String>();
2476 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002477 String imeId = mSubtypeSplitter.next();
2478 while (mSubtypeSplitter.hasNext()) {
2479 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002480 }
2481 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2482 }
2483 }
2484 return imsList;
2485 }
2486
2487 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2488 if (reloadInputMethodStr) {
2489 getEnabledInputMethodsStr();
2490 }
2491 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2492 // Add in the newly enabled input method.
2493 putEnabledInputMethodsStr(id);
2494 } else {
2495 putEnabledInputMethodsStr(
2496 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2497 }
2498 }
2499
2500 /**
2501 * Build and put a string of EnabledInputMethods with removing specified Id.
2502 * @return the specified id was removed or not.
2503 */
2504 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2505 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2506 boolean isRemoved = false;
2507 boolean needsAppendSeparator = false;
2508 for (Pair<String, ArrayList<String>> ims: imsList) {
2509 String curId = ims.first;
2510 if (curId.equals(id)) {
2511 // We are disabling this input method, and it is
2512 // currently enabled. Skip it to remove from the
2513 // new list.
2514 isRemoved = true;
2515 } else {
2516 if (needsAppendSeparator) {
2517 builder.append(INPUT_METHOD_SEPARATER);
2518 } else {
2519 needsAppendSeparator = true;
2520 }
2521 buildEnabledInputMethodsSettingString(builder, ims);
2522 }
2523 }
2524 if (isRemoved) {
2525 // Update the setting with the new list of input methods.
2526 putEnabledInputMethodsStr(builder.toString());
2527 }
2528 return isRemoved;
2529 }
2530
2531 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2532 List<Pair<String, ArrayList<String>>> imsList) {
2533 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2534 for (Pair<String, ArrayList<String>> ims: imsList) {
2535 InputMethodInfo info = mMethodMap.get(ims.first);
2536 if (info != null) {
2537 res.add(info);
2538 }
2539 }
2540 return res;
2541 }
2542
satok7f35c8c2010-10-07 21:13:11 +09002543 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002544 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002545 List<Pair<String, ArrayList<String>>> imsList) {
2546 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2547 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2548 for (Pair<String, ArrayList<String>> ims : imsList) {
2549 InputMethodInfo info = mMethodMap.get(ims.first);
2550 if (info != null) {
2551 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2552 }
2553 }
2554 return res;
2555 }
2556
satokd87c2592010-09-29 11:52:06 +09002557 private void putEnabledInputMethodsStr(String str) {
2558 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2559 mEnabledInputMethodsStrCache = str;
2560 }
2561
2562 private String getEnabledInputMethodsStr() {
2563 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2564 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002565 if (DEBUG) {
2566 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2567 }
satokd87c2592010-09-29 11:52:06 +09002568 return mEnabledInputMethodsStrCache;
2569 }
satok723a27e2010-11-11 14:58:11 +09002570
2571 private void saveSubtypeHistory(
2572 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2573 StringBuilder builder = new StringBuilder();
2574 boolean isImeAdded = false;
2575 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2576 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2577 newSubtypeId);
2578 isImeAdded = true;
2579 }
2580 for (Pair<String, String> ime: savedImes) {
2581 String imeId = ime.first;
2582 String subtypeId = ime.second;
2583 if (TextUtils.isEmpty(subtypeId)) {
2584 subtypeId = NOT_A_SUBTYPE_ID_STR;
2585 }
2586 if (isImeAdded) {
2587 builder.append(INPUT_METHOD_SEPARATER);
2588 } else {
2589 isImeAdded = true;
2590 }
2591 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2592 subtypeId);
2593 }
2594 // Remove the last INPUT_METHOD_SEPARATER
2595 putSubtypeHistoryStr(builder.toString());
2596 }
2597
2598 public void addSubtypeToHistory(String imeId, String subtypeId) {
2599 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2600 for (Pair<String, String> ime: subtypeHistory) {
2601 if (ime.first.equals(imeId)) {
2602 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002603 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002604 + ime.second);
2605 }
2606 // We should break here
2607 subtypeHistory.remove(ime);
2608 break;
2609 }
2610 }
satokbb4aa062011-01-19 21:40:27 +09002611 if (DEBUG) {
2612 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2613 }
satok723a27e2010-11-11 14:58:11 +09002614 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2615 }
2616
2617 private void putSubtypeHistoryStr(String str) {
2618 if (DEBUG) {
2619 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2620 }
2621 Settings.Secure.putString(
2622 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2623 }
2624
2625 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2626 // Gets the first one from the history
2627 return getLastSubtypeForInputMethodLockedInternal(null);
2628 }
2629
2630 public String getLastSubtypeForInputMethodLocked(String imeId) {
2631 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2632 if (ime != null) {
2633 return ime.second;
2634 } else {
2635 return null;
2636 }
2637 }
2638
2639 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2640 List<Pair<String, ArrayList<String>>> enabledImes =
2641 getEnabledInputMethodsAndSubtypeListLocked();
2642 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satokb4788fd2011-05-20 18:19:53 +09002643 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09002644 final String imeInTheHistory = imeAndSubtype.first;
2645 // If imeId is empty, returns the first IME and subtype in the history
2646 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2647 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002648 final String subtypeHashCode =
2649 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2650 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002651 if (!TextUtils.isEmpty(subtypeHashCode)) {
2652 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002653 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002654 }
2655 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2656 }
2657 }
2658 }
2659 if (DEBUG) {
2660 Slog.d(TAG, "No enabled IME found in the history");
2661 }
2662 return null;
2663 }
2664
satokdf31ae62011-01-15 06:19:44 +09002665 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002666 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2667 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2668 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002669 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2670 if (explicitlyEnabledSubtypes.size() == 0) {
2671 // If there are no explicitly enabled subtypes, applicable subtypes are
2672 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002673 InputMethodInfo ime = mMethodMap.get(imeId);
2674 // If IME is enabled and no subtypes are enabled, applicable subtypes
2675 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002676 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002677 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002678 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002679 if (implicitlySelectedSubtypes != null) {
2680 final int N = implicitlySelectedSubtypes.size();
2681 for (int i = 0; i < N; ++i) {
2682 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2683 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2684 return subtypeHashCode;
2685 }
2686 }
2687 }
2688 }
2689 } else {
satokf6cafb62011-01-17 16:29:02 +09002690 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002691 if (s.equals(subtypeHashCode)) {
2692 // If both imeId and subtypeId are enabled, return subtypeId.
2693 return s;
2694 }
satok723a27e2010-11-11 14:58:11 +09002695 }
2696 }
2697 // If imeId was enabled but subtypeId was disabled.
2698 return NOT_A_SUBTYPE_ID_STR;
2699 }
2700 }
2701 // If both imeId and subtypeId are disabled, return null
2702 return null;
2703 }
2704
2705 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2706 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2707 final String subtypeHistoryStr = getSubtypeHistoryStr();
2708 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2709 return imsList;
2710 }
2711 mInputMethodSplitter.setString(subtypeHistoryStr);
2712 while (mInputMethodSplitter.hasNext()) {
2713 String nextImsStr = mInputMethodSplitter.next();
2714 mSubtypeSplitter.setString(nextImsStr);
2715 if (mSubtypeSplitter.hasNext()) {
2716 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2717 // The first element is ime id.
2718 String imeId = mSubtypeSplitter.next();
2719 while (mSubtypeSplitter.hasNext()) {
2720 subtypeId = mSubtypeSplitter.next();
2721 break;
2722 }
2723 imsList.add(new Pair<String, String>(imeId, subtypeId));
2724 }
2725 }
2726 return imsList;
2727 }
2728
2729 private String getSubtypeHistoryStr() {
2730 if (DEBUG) {
2731 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2732 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2733 }
2734 return Settings.Secure.getString(
2735 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2736 }
2737
2738 public void putSelectedInputMethod(String imeId) {
2739 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2740 }
2741
2742 public void putSelectedSubtype(int subtypeId) {
2743 Settings.Secure.putInt(
2744 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2745 }
satokd87c2592010-09-29 11:52:06 +09002746 }
2747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 @Override
2751 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2752 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2753 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2756 + Binder.getCallingPid()
2757 + ", uid=" + Binder.getCallingUid());
2758 return;
2759 }
2760
2761 IInputMethod method;
2762 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 synchronized (mMethodMap) {
2767 p.println("Current Input Method Manager state:");
2768 int N = mMethodList.size();
2769 p.println(" Input Methods:");
2770 for (int i=0; i<N; i++) {
2771 InputMethodInfo info = mMethodList.get(i);
2772 p.println(" InputMethod #" + i + ":");
2773 info.dump(p, " ");
2774 }
2775 p.println(" Clients:");
2776 for (ClientState ci : mClients.values()) {
2777 p.println(" Client " + ci + ":");
2778 p.println(" client=" + ci.client);
2779 p.println(" inputContext=" + ci.inputContext);
2780 p.println(" sessionRequested=" + ci.sessionRequested);
2781 p.println(" curSession=" + ci.curSession);
2782 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002783 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002785 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2786 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2788 + " mBoundToMethod=" + mBoundToMethod);
2789 p.println(" mCurToken=" + mCurToken);
2790 p.println(" mCurIntent=" + mCurIntent);
2791 method = mCurMethod;
2792 p.println(" mCurMethod=" + mCurMethod);
2793 p.println(" mEnabledSession=" + mEnabledSession);
2794 p.println(" mShowRequested=" + mShowRequested
2795 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2796 + " mShowForced=" + mShowForced
2797 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002798 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002800
Jeff Brownb88102f2010-09-08 11:49:43 -07002801 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 pw.flush();
2804 try {
2805 client.client.asBinder().dump(fd, args);
2806 } catch (RemoteException e) {
2807 p.println("Input method client dead: " + e);
2808 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002809 } else {
2810 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002812
Jeff Brownb88102f2010-09-08 11:49:43 -07002813 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 pw.flush();
2816 try {
2817 method.asBinder().dump(fd, args);
2818 } catch (RemoteException e) {
2819 p.println("Input method service dead: " + e);
2820 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002821 } else {
2822 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 }
2824 }
2825}