blob: 465fd2e608dd2e3f4f616e88c1d09e417347e57c [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;
satokf9f01002011-05-19 21:31:50 +090067import android.text.style.SuggestionSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090069import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090070import android.util.Pair;
Joe Onorato8a9b2202010-02-26 18:56:32 -080071import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.util.PrintWriterPrinter;
73import android.util.Printer;
74import android.view.IWindowManager;
75import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090076import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.view.inputmethod.InputBinding;
78import android.view.inputmethod.InputMethod;
79import android.view.inputmethod.InputMethodInfo;
80import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090081import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
83import java.io.FileDescriptor;
84import java.io.IOException;
85import java.io.PrintWriter;
86import java.util.ArrayList;
Ken Wakasa761eb372011-03-04 19:06:18 +090087import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +090089import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import java.util.List;
satok913a8922010-08-26 21:53:41 +090091import java.util.Map;
92import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94/**
95 * This class provides a system service that manages input methods.
96 */
97public class InputMethodManagerService extends IInputMethodManager.Stub
98 implements ServiceConnection, Handler.Callback {
99 static final boolean DEBUG = false;
100 static final String TAG = "InputManagerService";
101
102 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900103 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900104 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900105 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 static final int MSG_UNBIND_INPUT = 1000;
108 static final int MSG_BIND_INPUT = 1010;
109 static final int MSG_SHOW_SOFT_INPUT = 1020;
110 static final int MSG_HIDE_SOFT_INPUT = 1030;
111 static final int MSG_ATTACH_TOKEN = 1040;
112 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 static final int MSG_START_INPUT = 2000;
115 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 static final int MSG_UNBIND_METHOD = 3000;
118 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800121
satokf9f01002011-05-19 21:31:50 +0900122 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
123
satokab751aa2010-09-14 19:17:36 +0900124 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900125 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900126 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
127 private static final String SUBTYPE_MODE_VOICE = "voice";
128
satok57ffc002011-01-25 00:11:47 +0900129 // TODO: Will formalize this value as API
130 private static final String SUBTYPE_EXTRAVALUE_EXCLUDE_FROM_LAST_IME =
131 "excludeFromLastInputMethod";
132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800134 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900136 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700138 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 final IWindowManager mIWindowManager;
140 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 // All known input methods. mMethodMap also serves as the global
145 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900146 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
147 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900148 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
149 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 class SessionState {
152 final ClientState client;
153 final IInputMethod method;
154 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 @Override
157 public String toString() {
158 return "SessionState{uid " + client.uid + " pid " + client.pid
159 + " method " + Integer.toHexString(
160 System.identityHashCode(method))
161 + " session " + Integer.toHexString(
162 System.identityHashCode(session))
163 + "}";
164 }
165
166 SessionState(ClientState _client, IInputMethod _method,
167 IInputMethodSession _session) {
168 client = _client;
169 method = _method;
170 session = _session;
171 }
172 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 class ClientState {
175 final IInputMethodClient client;
176 final IInputContext inputContext;
177 final int uid;
178 final int pid;
179 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 boolean sessionRequested;
182 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 @Override
185 public String toString() {
186 return "ClientState{" + Integer.toHexString(
187 System.identityHashCode(this)) + " uid " + uid
188 + " pid " + pid + "}";
189 }
190
191 ClientState(IInputMethodClient _client, IInputContext _inputContext,
192 int _uid, int _pid) {
193 client = _client;
194 inputContext = _inputContext;
195 uid = _uid;
196 pid = _pid;
197 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
198 }
199 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 final HashMap<IBinder, ClientState> mClients
202 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700205 * Set once the system is ready to run third party code.
206 */
207 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800208
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700209 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 * Id of the currently selected input method.
211 */
212 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 /**
215 * The current binding sequence number, incremented every time there is
216 * a new bind performed.
217 */
218 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 /**
221 * The client that is currently bound to an input method.
222 */
223 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700226 * The last window token that gained focus.
227 */
228 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800229
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700230 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 * The input context last provided by the current client.
232 */
233 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 /**
236 * The attributes last provided by the current client.
237 */
238 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 /**
241 * The input method ID of the input method service that we are currently
242 * connected to or in the process of connecting to.
243 */
244 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 /**
satokab751aa2010-09-14 19:17:36 +0900247 * The current subtype of the current input method.
248 */
249 private InputMethodSubtype mCurrentSubtype;
250
satok4e4569d2010-11-19 18:45:53 +0900251 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900252 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
253 mShortcutInputMethodsAndSubtypes =
254 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900255
256 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 * Set to true if our ServiceConnection is currently actively bound to
258 * a service (whether or not we have gotten its IBinder back yet).
259 */
260 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
263 * Set if the client has asked for the input method to be shown.
264 */
265 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 /**
268 * Set if we were explicitly told to show the input method.
269 */
270 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 /**
273 * Set if we were forced to be shown.
274 */
275 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
278 * Set if we last told the input method to show itself.
279 */
280 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
283 * The Intent used to connect to the current input method.
284 */
285 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 /**
288 * The token we have made for the currently active input method, to
289 * identify it in the future.
290 */
291 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 /**
294 * If non-null, this is the input method service we are currently connected
295 * to.
296 */
297 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * Time that we last initiated a bind to the input method, to determine
301 * if we should try to disconnect and reconnect to it.
302 */
303 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 /**
306 * Have we called mCurMethod.bindInput()?
307 */
308 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 /**
311 * Currently enabled session. Only touched by service thread, not
312 * protected by a lock.
313 */
314 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 /**
317 * True if the screen is on. The value is true initially.
318 */
319 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800320
Joe Onorato857fd9b2011-01-27 15:08:35 -0800321 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
322 int mImeWindowVis;
323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 AlertDialog.Builder mDialogBuilder;
325 AlertDialog mSwitchingDialog;
326 InputMethodInfo[] mIms;
327 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900328 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 class SettingsObserver extends ContentObserver {
331 SettingsObserver(Handler handler) {
332 super(handler);
333 ContentResolver resolver = mContext.getContentResolver();
334 resolver.registerContentObserver(Settings.Secure.getUriFor(
335 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900336 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900337 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
338 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900339 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 @Override public void onChange(boolean selfChange) {
343 synchronized (mMethodMap) {
344 updateFromSettingsLocked();
345 }
346 }
347 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
350 @Override
351 public void onReceive(Context context, Intent intent) {
352 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
353 mScreenOn = true;
354 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
355 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700356 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
357 hideInputMethodMenu();
358 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800360 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
362
363 // Inform the current client of the change in active status
364 try {
365 if (mCurClient != null && mCurClient.client != null) {
366 mCurClient.client.setActive(mScreenOn);
367 }
368 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800369 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 + mCurClient.pid + " uid " + mCurClient.uid);
371 }
372 }
373 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800374
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800375 class MyPackageMonitor extends PackageMonitor {
376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800378 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800380 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
382 final int N = mMethodList.size();
383 if (curInputMethodId != null) {
384 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800385 InputMethodInfo imi = mMethodList.get(i);
386 if (imi.getId().equals(curInputMethodId)) {
387 for (String pkg : packages) {
388 if (imi.getPackageName().equals(pkg)) {
389 if (!doit) {
390 return true;
391 }
satok723a27e2010-11-11 14:58:11 +0900392 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800393 chooseNewDefaultIMELocked();
394 return true;
395 }
396 }
397 }
398 }
399 }
400 }
401 return false;
402 }
403
404 @Override
405 public void onSomePackagesChanged() {
406 synchronized (mMethodMap) {
407 InputMethodInfo curIm = null;
408 String curInputMethodId = Settings.Secure.getString(mContext
409 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
410 final int N = mMethodList.size();
411 if (curInputMethodId != null) {
412 for (int i=0; i<N; i++) {
413 InputMethodInfo imi = mMethodList.get(i);
414 if (imi.getId().equals(curInputMethodId)) {
415 curIm = imi;
416 }
417 int change = isPackageDisappearing(imi.getPackageName());
418 if (change == PACKAGE_TEMPORARY_CHANGE
419 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800420 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800421 + imi.getComponent());
422 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 }
424 }
425 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800426
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800427 buildInputMethodListLocked(mMethodList, mMethodMap);
428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800430
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800431 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800432 int change = isPackageDisappearing(curIm.getPackageName());
433 if (change == PACKAGE_TEMPORARY_CHANGE
434 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800435 ServiceInfo si = null;
436 try {
437 si = mContext.getPackageManager().getServiceInfo(
438 curIm.getComponent(), 0);
439 } catch (PackageManager.NameNotFoundException ex) {
440 }
441 if (si == null) {
442 // Uh oh, current input method is no longer around!
443 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800444 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800445 mImeWindowVis = 0;
446 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
447 mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800448 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800449 changed = true;
450 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800451 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900452 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800453 }
454 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800455 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800456 }
satokab751aa2010-09-14 19:17:36 +0900457
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800458 if (curIm == null) {
459 // We currently don't have a default input method... is
460 // one now available?
461 changed = chooseNewDefaultIMELocked();
462 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800463
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800464 if (changed) {
465 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 }
467 }
468 }
469 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 class MethodCallback extends IInputMethodCallback.Stub {
472 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 MethodCallback(IInputMethod method) {
475 mMethod = method;
476 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 public void finishedEvent(int seq, boolean handled) throws RemoteException {
479 }
480
481 public void sessionCreated(IInputMethodSession session) throws RemoteException {
482 onSessionCreated(mMethod, session);
483 }
484 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800485
Joe Onorato089de882010-04-12 08:18:45 -0700486 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800488 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 mHandler = new Handler(this);
490 mIWindowManager = IWindowManager.Stub.asInterface(
491 ServiceManager.getService(Context.WINDOW_SERVICE));
492 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
493 public void executeMessage(Message msg) {
494 handleMessage(msg);
495 }
496 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800497
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800498 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 IntentFilter screenOnOffFilt = new IntentFilter();
501 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
502 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700503 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800505
satok913a8922010-08-26 21:53:41 +0900506 mStatusBar = statusBar;
507 statusBar.setIconVisibility("ime", false);
508
satokd87c2592010-09-29 11:52:06 +0900509 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900510 mSettings = new InputMethodSettings(
511 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900513 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514
satokd87c2592010-09-29 11:52:06 +0900515 if (TextUtils.isEmpty(Settings.Secure.getString(
516 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900518 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
520 try {
satokd87c2592010-09-29 11:52:06 +0900521 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 imi.getPackageName(), 0).getResources();
523 if (res.getBoolean(imi.getIsDefaultResourceId())) {
524 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800525 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
527 } catch (PackageManager.NameNotFoundException ex) {
528 } catch (Resources.NotFoundException ex) {
529 }
530 }
531 }
satokd87c2592010-09-29 11:52:06 +0900532 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800534 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900537 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 }
539 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 mSettingsObserver = new SettingsObserver(mHandler);
542 updateFromSettingsLocked();
543 }
544
545 @Override
546 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
547 throws RemoteException {
548 try {
549 return super.onTransact(code, data, reply, flags);
550 } catch (RuntimeException e) {
551 // The input method manager only throws security exceptions, so let's
552 // log all others.
553 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800554 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 }
556 throw e;
557 }
558 }
559
560 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700561 synchronized (mMethodMap) {
562 if (!mSystemReady) {
563 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700564 try {
565 startInputInnerLocked();
566 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800567 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700568 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700569 }
570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 public List<InputMethodInfo> getInputMethodList() {
574 synchronized (mMethodMap) {
575 return new ArrayList<InputMethodInfo>(mMethodList);
576 }
577 }
578
579 public List<InputMethodInfo> getEnabledInputMethodList() {
580 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900581 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 }
583 }
584
satokbb4aa062011-01-19 21:40:27 +0900585 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
586 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
587 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
588 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
589 for (InputMethodInfo imi: getEnabledInputMethodList()) {
590 enabledInputMethodAndSubtypes.put(
591 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
592 }
593 return enabledInputMethodAndSubtypes;
594 }
595
596 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
597 boolean allowsImplicitlySelectedSubtypes) {
598 if (imi == null && mCurMethodId != null) {
599 imi = mMethodMap.get(mCurMethodId);
600 }
satok7265d9b2011-02-14 15:47:30 +0900601 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900602 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900603 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
604 enabledSubtypes = getApplicableSubtypesLocked(mRes, getSubtypes(imi));
satokbb4aa062011-01-19 21:40:27 +0900605 }
satok7265d9b2011-02-14 15:47:30 +0900606 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900607 }
608
satok16331c82010-12-20 23:48:46 +0900609 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
610 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900611 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900612 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900613 }
614 }
615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 public void addClient(IInputMethodClient client,
617 IInputContext inputContext, int uid, int pid) {
618 synchronized (mMethodMap) {
619 mClients.put(client.asBinder(), new ClientState(client,
620 inputContext, uid, pid));
621 }
622 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 public void removeClient(IInputMethodClient client) {
625 synchronized (mMethodMap) {
626 mClients.remove(client.asBinder());
627 }
628 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 void executeOrSendMessage(IInterface target, Message msg) {
631 if (target.asBinder() instanceof Binder) {
632 mCaller.sendMessage(msg);
633 } else {
634 handleMessage(msg);
635 msg.recycle();
636 }
637 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800638
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700639 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800641 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 + mCurClient.client.asBinder());
643 if (mBoundToMethod) {
644 mBoundToMethod = false;
645 if (mCurMethod != null) {
646 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
647 MSG_UNBIND_INPUT, mCurMethod));
648 }
649 }
650 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
651 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
652 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 // Call setActive(false) on the old client
655 try {
656 mCurClient.client.setActive(false);
657 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800658 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 + mCurClient.pid + " uid " + mCurClient.uid);
660 }
661 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800662
The Android Open Source Project10592532009-03-18 17:39:46 -0700663 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 }
665 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 private int getImeShowFlags() {
668 int flags = 0;
669 if (mShowForced) {
670 flags |= InputMethod.SHOW_FORCED
671 | InputMethod.SHOW_EXPLICIT;
672 } else if (mShowExplicitlyRequested) {
673 flags |= InputMethod.SHOW_EXPLICIT;
674 }
675 return flags;
676 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 private int getAppShowFlags() {
679 int flags = 0;
680 if (mShowForced) {
681 flags |= InputMethodManager.SHOW_FORCED;
682 } else if (!mShowExplicitlyRequested) {
683 flags |= InputMethodManager.SHOW_IMPLICIT;
684 }
685 return flags;
686 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
689 if (!mBoundToMethod) {
690 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
691 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
692 mBoundToMethod = true;
693 }
694 final SessionState session = mCurClient.curSession;
695 if (initial) {
696 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
697 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
698 } else {
699 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
700 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
701 }
702 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800703 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800704 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 }
706 return needResult
707 ? new InputBindResult(session.session, mCurId, mCurSeq)
708 : null;
709 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 InputBindResult startInputLocked(IInputMethodClient client,
712 IInputContext inputContext, EditorInfo attribute,
713 boolean initial, boolean needResult) {
714 // If no method is currently selected, do nothing.
715 if (mCurMethodId == null) {
716 return mNoBinding;
717 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 ClientState cs = mClients.get(client.asBinder());
720 if (cs == null) {
721 throw new IllegalArgumentException("unknown client "
722 + client.asBinder());
723 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 try {
726 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
727 // Check with the window manager to make sure this client actually
728 // has a window with focus. If not, reject. This is thread safe
729 // because if the focus changes some time before or after, the
730 // next client receiving focus that has any interest in input will
731 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800732 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
734 return null;
735 }
736 } catch (RemoteException e) {
737 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 if (mCurClient != cs) {
740 // If the client is changing, we need to switch over to the new
741 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700742 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800743 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 + cs.client.asBinder());
745
746 // If the screen is on, inform the new client it is active
747 if (mScreenOn) {
748 try {
749 cs.client.setActive(mScreenOn);
750 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800751 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 + cs.pid + " uid " + cs.uid);
753 }
754 }
755 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 // Bump up the sequence for this client and attach it.
758 mCurSeq++;
759 if (mCurSeq <= 0) mCurSeq = 1;
760 mCurClient = cs;
761 mCurInputContext = inputContext;
762 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 // Check if the input method is changing.
765 if (mCurId != null && mCurId.equals(mCurMethodId)) {
766 if (cs.curSession != null) {
767 // Fast case: if we are already connected to the input method,
768 // then just return it.
769 return attachNewInputLocked(initial, needResult);
770 }
771 if (mHaveConnection) {
772 if (mCurMethod != null) {
773 if (!cs.sessionRequested) {
774 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800775 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
777 MSG_CREATE_SESSION, mCurMethod,
778 new MethodCallback(mCurMethod)));
779 }
780 // Return to client, and we will get back with it when
781 // we have had a session made for it.
782 return new InputBindResult(null, mCurId, mCurSeq);
783 } else if (SystemClock.uptimeMillis()
784 < (mLastBindTime+TIME_TO_RECONNECT)) {
785 // In this case we have connected to the service, but
786 // don't yet have its interface. If it hasn't been too
787 // long since we did the connection, we'll return to
788 // the client and wait to get the service interface so
789 // we can report back. If it has been too long, we want
790 // to fall through so we can try a disconnect/reconnect
791 // to see if we can get back in touch with the service.
792 return new InputBindResult(null, mCurId, mCurSeq);
793 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800794 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
795 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 }
797 }
798 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800799
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700800 return startInputInnerLocked();
801 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800802
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700803 InputBindResult startInputInnerLocked() {
804 if (mCurMethodId == null) {
805 return mNoBinding;
806 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800807
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700808 if (!mSystemReady) {
809 // If the system is not yet ready, we shouldn't be running third
810 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700811 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700812 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 InputMethodInfo info = mMethodMap.get(mCurMethodId);
815 if (info == null) {
816 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
817 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800818
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700819 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
822 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700823 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
824 com.android.internal.R.string.input_method_binding_label);
825 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
826 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
828 mLastBindTime = SystemClock.uptimeMillis();
829 mHaveConnection = true;
830 mCurId = info.getId();
831 mCurToken = new Binder();
832 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800833 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 mIWindowManager.addWindowToken(mCurToken,
835 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
836 } catch (RemoteException e) {
837 }
838 return new InputBindResult(null, mCurId, mCurSeq);
839 } else {
840 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800841 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 + mCurIntent);
843 }
844 return null;
845 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 public InputBindResult startInput(IInputMethodClient client,
848 IInputContext inputContext, EditorInfo attribute,
849 boolean initial, boolean needResult) {
850 synchronized (mMethodMap) {
851 final long ident = Binder.clearCallingIdentity();
852 try {
853 return startInputLocked(client, inputContext, attribute,
854 initial, needResult);
855 } finally {
856 Binder.restoreCallingIdentity(ident);
857 }
858 }
859 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 public void finishInput(IInputMethodClient client) {
862 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 public void onServiceConnected(ComponentName name, IBinder service) {
865 synchronized (mMethodMap) {
866 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
867 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700868 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800869 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700870 unbindCurrentMethodLocked(false);
871 return;
872 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800873 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700874 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
875 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800877 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700878 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700880 MSG_CREATE_SESSION, mCurMethod,
881 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 }
883 }
884 }
885 }
886
887 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
888 synchronized (mMethodMap) {
889 if (mCurMethod != null && method != null
890 && mCurMethod.asBinder() == method.asBinder()) {
891 if (mCurClient != null) {
892 mCurClient.curSession = new SessionState(mCurClient,
893 method, session);
894 mCurClient.sessionRequested = false;
895 InputBindResult res = attachNewInputLocked(true, true);
896 if (res.method != null) {
897 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
898 MSG_BIND_METHOD, mCurClient.client, res));
899 }
900 }
901 }
902 }
903 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800904
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700905 void unbindCurrentMethodLocked(boolean reportToClient) {
906 if (mHaveConnection) {
907 mContext.unbindService(this);
908 mHaveConnection = false;
909 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800910
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700911 if (mCurToken != null) {
912 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800913 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700914 mIWindowManager.removeWindowToken(mCurToken);
915 } catch (RemoteException e) {
916 }
917 mCurToken = null;
918 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800919
The Android Open Source Project10592532009-03-18 17:39:46 -0700920 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700921 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800922
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700923 if (reportToClient && mCurClient != null) {
924 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
925 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
926 }
927 }
928
Devin Taylor0c33ed22010-02-23 13:26:46 -0600929 private void finishSession(SessionState sessionState) {
930 if (sessionState != null && sessionState.session != null) {
931 try {
932 sessionState.session.finishSession();
933 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700934 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600935 }
936 }
937 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800938
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700939 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 if (mCurMethod != null) {
941 for (ClientState cs : mClients.values()) {
942 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600943 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 cs.curSession = null;
945 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600946
947 finishSession(mEnabledSession);
948 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 mCurMethod = null;
950 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700951 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 public void onServiceDisconnected(ComponentName name) {
955 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800956 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 + " mCurIntent=" + mCurIntent);
958 if (mCurMethod != null && mCurIntent != null
959 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700960 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 // We consider this to be a new bind attempt, since the system
962 // should now try to restart the service for us.
963 mLastBindTime = SystemClock.uptimeMillis();
964 mShowRequested = mInputShown;
965 mInputShown = false;
966 if (mCurClient != null) {
967 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
968 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
969 }
970 }
971 }
972 }
973
satokf9f01002011-05-19 21:31:50 +0900974 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700976 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 long ident = Binder.clearCallingIdentity();
978 try {
979 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700980 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 return;
982 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 synchronized (mMethodMap) {
985 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800986 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700987 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800989 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700990 mStatusBar.setIcon("ime", packageName, iconId, 0);
991 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 }
993 }
994 } finally {
995 Binder.restoreCallingIdentity(ident);
996 }
997 }
998
satokf9f01002011-05-19 21:31:50 +0900999 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001000 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001001 int uid = Binder.getCallingUid();
1002 long ident = Binder.clearCallingIdentity();
1003 try {
1004 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001005 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001006 return;
1007 }
1008
1009 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001010 mImeWindowVis = vis;
1011 mBackDisposition = backDisposition;
1012 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
satok06487a52010-10-29 11:37:18 +09001013 }
1014 } finally {
1015 Binder.restoreCallingIdentity(ident);
1016 }
1017 }
1018
satokf9f01002011-05-19 21:31:50 +09001019 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1020 synchronized (mMethodMap) {
1021 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1022 for (int i = 0; i < spans.length; ++i) {
1023 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001024 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001025 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001026 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001027 }
1028 }
1029 }
1030 }
1031
1032 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1033 synchronized (mMethodMap) {
1034 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1035 // TODO: Do not send the intent if the process of the targetImi is already dead.
1036 if (targetImi != null) {
1037 final String[] suggestions = span.getSuggestions();
1038 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001039 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001040 final Intent intent = new Intent();
1041 // Ensures that only a class in the original IME package will receive the
1042 // notification.
satok42c5a162011-05-26 16:46:14 +09001043 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001044 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1045 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1046 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1047 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1048 mContext.sendBroadcast(intent);
1049 return true;
1050 }
1051 }
1052 return false;
1053 }
1054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001056 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1057 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1058 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1059 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001061 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001062 // There is no input method selected, try to choose new applicable input method.
1063 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1064 id = Settings.Secure.getString(mContext.getContentResolver(),
1065 Settings.Secure.DEFAULT_INPUT_METHOD);
1066 }
1067 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 try {
satokab751aa2010-09-14 19:17:36 +09001069 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001071 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001072 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001073 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 }
satokf3db1af2010-11-23 13:34:33 +09001075 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001076 } else {
1077 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001078 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001079 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 }
1081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001082
satokab751aa2010-09-14 19:17:36 +09001083 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 InputMethodInfo info = mMethodMap.get(id);
1085 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001086 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001090 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001091 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1092 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001093 }
1094 if (subtype != mCurrentSubtype) {
1095 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001096 if (subtype != null) {
1097 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1098 }
satokcd7cd292010-11-20 15:46:23 +09001099 if (mCurMethod != null) {
1100 try {
satoke40dea02011-01-30 01:14:02 +09001101 final Configuration conf = mRes.getConfiguration();
1102 final boolean haveHardKeyboard = conf.keyboard
1103 != Configuration.KEYBOARD_NOKEYS;
1104 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001105 && conf.hardKeyboardHidden
1106 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001107 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1108 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1109 : 0;
Joe Onorato857fd9b2011-01-27 15:08:35 -08001110 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1111 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001112 // If subtype is null, try to find the most applicable one from
1113 // getCurrentInputMethodSubtype.
1114 if (subtype == null) {
1115 subtype = getCurrentInputMethodSubtype();
1116 }
1117 mCurMethod.changeInputMethodSubtype(subtype);
1118 } catch (RemoteException e) {
1119 return;
satokab751aa2010-09-14 19:17:36 +09001120 }
1121 }
1122 }
1123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 return;
1125 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 final long ident = Binder.clearCallingIdentity();
1128 try {
satokab751aa2010-09-14 19:17:36 +09001129 // Set a subtype to this input method.
1130 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001131 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1132 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1133 // because mCurMethodId is stored as a history in
1134 // setSelectedInputMethodAndSubtypeLocked().
1135 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136
1137 if (ActivityManagerNative.isSystemReady()) {
1138 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001139 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 intent.putExtra("input_method_id", id);
1141 mContext.sendBroadcast(intent);
1142 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001143 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 } finally {
1145 Binder.restoreCallingIdentity(ident);
1146 }
1147 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001148
satok42c5a162011-05-26 16:46:14 +09001149 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001150 public boolean showSoftInput(IInputMethodClient client, int flags,
1151 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001152 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 long ident = Binder.clearCallingIdentity();
1154 try {
1155 synchronized (mMethodMap) {
1156 if (mCurClient == null || client == null
1157 || mCurClient.client.asBinder() != client.asBinder()) {
1158 try {
1159 // We need to check if this is the current client with
1160 // focus in the window manager, to allow this call to
1161 // be made before input is started in it.
1162 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001163 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001164 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 }
1166 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001167 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 }
1169 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001170
Joe Onorato8a9b2202010-02-26 18:56:32 -08001171 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001172 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 }
1174 } finally {
1175 Binder.restoreCallingIdentity(ident);
1176 }
1177 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001178
The Android Open Source Project4df24232009-03-05 14:34:35 -08001179 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 mShowRequested = true;
1181 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1182 mShowExplicitlyRequested = true;
1183 }
1184 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1185 mShowExplicitlyRequested = true;
1186 mShowForced = true;
1187 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001188
Dianne Hackborncc278702009-09-02 23:07:23 -07001189 if (!mSystemReady) {
1190 return false;
1191 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001192
The Android Open Source Project4df24232009-03-05 14:34:35 -08001193 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001195 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1196 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1197 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001199 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 } else if (mHaveConnection && SystemClock.uptimeMillis()
1201 < (mLastBindTime+TIME_TO_RECONNECT)) {
1202 // The client has asked to have the input method shown, but
1203 // we have been sitting here too long with a connection to the
1204 // service and no interface received, so let's disconnect/connect
1205 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001206 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 SystemClock.uptimeMillis()-mLastBindTime,1);
1208 mContext.unbindService(this);
1209 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1210 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001211
The Android Open Source Project4df24232009-03-05 14:34:35 -08001212 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001214
satok42c5a162011-05-26 16:46:14 +09001215 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001216 public boolean hideSoftInput(IInputMethodClient client, int flags,
1217 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001218 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 long ident = Binder.clearCallingIdentity();
1220 try {
1221 synchronized (mMethodMap) {
1222 if (mCurClient == null || client == null
1223 || mCurClient.client.asBinder() != client.asBinder()) {
1224 try {
1225 // We need to check if this is the current client with
1226 // focus in the window manager, to allow this call to
1227 // be made before input is started in it.
1228 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001229 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1230 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001231 mImeWindowVis = 0;
1232 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1233 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001234 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 }
1236 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001237 mImeWindowVis = 0;
1238 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001239 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 }
1241 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001242
Joe Onorato8a9b2202010-02-26 18:56:32 -08001243 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001244 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 }
1246 } finally {
1247 Binder.restoreCallingIdentity(ident);
1248 }
1249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001250
The Android Open Source Project4df24232009-03-05 14:34:35 -08001251 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1253 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001254 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001256 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 }
1258 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001259 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001261 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001263 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001265 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1266 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1267 res = true;
1268 } else {
1269 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 }
1271 mInputShown = false;
1272 mShowRequested = false;
1273 mShowExplicitlyRequested = false;
1274 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001275 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001277
satok42c5a162011-05-26 16:46:14 +09001278 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001279 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1281 boolean first, int windowFlags) {
1282 long ident = Binder.clearCallingIdentity();
1283 try {
1284 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001285 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 + " viewHasFocus=" + viewHasFocus
1287 + " isTextEditor=" + isTextEditor
1288 + " softInputMode=#" + Integer.toHexString(softInputMode)
1289 + " first=" + first + " flags=#"
1290 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 if (mCurClient == null || client == null
1293 || mCurClient.client.asBinder() != client.asBinder()) {
1294 try {
1295 // We need to check if this is the current client with
1296 // focus in the window manager, to allow this call to
1297 // be made before input is started in it.
1298 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001299 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 return;
1301 }
1302 } catch (RemoteException e) {
1303 }
1304 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001305
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001306 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001307 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001308 return;
1309 }
1310 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001311
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001312 // Should we auto-show the IME even if the caller has not
1313 // specified what should be done with it?
1314 // We only do this automatically if the window can resize
1315 // to accommodate the IME (so what the user sees will give
1316 // them good context without input information being obscured
1317 // by the IME) or if running on a large screen where there
1318 // is more room for the target window + IME.
1319 final boolean doAutoShow =
1320 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1321 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1322 || mRes.getConfiguration().isLayoutSizeAtLeast(
1323 Configuration.SCREENLAYOUT_SIZE_LARGE);
1324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1326 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001327 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1329 // There is no focus view, and this window will
1330 // be behind any soft input window, so hide the
1331 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001332 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001333 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001335 } else if (isTextEditor && doAutoShow && (softInputMode &
1336 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 // There is a focus view, and we are navigating forward
1338 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001339 // We only do this automatically if the window an resize
1340 // to accomodate the IME (so what the user sees will give
1341 // them good context without input information being obscured
1342 // by the IME) or if running on a large screen where there
1343 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001344 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001345 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 }
1347 break;
1348 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1349 // Do nothing.
1350 break;
1351 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1352 if ((softInputMode &
1353 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001354 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001355 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 }
1357 break;
1358 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001359 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001360 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 break;
1362 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1363 if ((softInputMode &
1364 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001365 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001366 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 }
1368 break;
1369 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001370 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001371 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 break;
1373 }
1374 }
1375 } finally {
1376 Binder.restoreCallingIdentity(ident);
1377 }
1378 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001379
satok42c5a162011-05-26 16:46:14 +09001380 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1382 synchronized (mMethodMap) {
1383 if (mCurClient == null || client == null
1384 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001385 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001386 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 }
1388
satok440aab52010-11-25 09:43:11 +09001389 // Always call subtype picker, because subtype picker is a superset of input method
1390 // picker.
satokab751aa2010-09-14 19:17:36 +09001391 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1392 }
1393 }
1394
satok42c5a162011-05-26 16:46:14 +09001395 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001397 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1398 }
1399
satok42c5a162011-05-26 16:46:14 +09001400 @Override
satok28203512010-11-24 11:06:49 +09001401 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1402 synchronized (mMethodMap) {
1403 if (subtype != null) {
1404 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1405 mMethodMap.get(id), subtype.hashCode()));
1406 } else {
1407 setInputMethod(token, id);
1408 }
1409 }
satokab751aa2010-09-14 19:17:36 +09001410 }
1411
satok42c5a162011-05-26 16:46:14 +09001412 @Override
satokb416a712010-11-25 20:42:14 +09001413 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001414 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001415 synchronized (mMethodMap) {
1416 if (mCurClient == null || client == null
1417 || mCurClient.client.asBinder() != client.asBinder()) {
1418 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1419 }
satok7fee71f2010-12-17 18:54:26 +09001420 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1421 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001422 }
1423 }
1424
satok4fc87d62011-05-20 16:13:43 +09001425 @Override
satok735cf382010-11-11 20:40:09 +09001426 public boolean switchToLastInputMethod(IBinder token) {
1427 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001428 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001429 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001430 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001431 lastImi = mMethodMap.get(lastIme.first);
1432 } else {
1433 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001434 }
satok4fc87d62011-05-20 16:13:43 +09001435 String targetLastImiId = null;
1436 int subtypeId = NOT_A_SUBTYPE_ID;
1437 if (lastIme != null && lastImi != null) {
1438 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1439 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1440 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1441 : mCurrentSubtype.hashCode();
1442 // If the last IME is the same as the current IME and the last subtype is not
1443 // defined, there is no need to switch to the last IME.
1444 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1445 targetLastImiId = lastIme.first;
1446 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1447 }
1448 }
1449
1450 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1451 // This is a safety net. If the currentSubtype can't be added to the history
1452 // and the framework couldn't find the last ime, we will make the last ime be
1453 // the most applicable enabled keyboard subtype of the system imes.
1454 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1455 if (enabled != null) {
1456 final int N = enabled.size();
1457 final String locale = mCurrentSubtype == null
1458 ? mRes.getConfiguration().locale.toString()
1459 : mCurrentSubtype.getLocale();
1460 for (int i = 0; i < N; ++i) {
1461 final InputMethodInfo imi = enabled.get(i);
1462 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1463 InputMethodSubtype keyboardSubtype =
1464 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1465 SUBTYPE_MODE_KEYBOARD, locale, true);
1466 if (keyboardSubtype != null) {
1467 targetLastImiId = imi.getId();
1468 subtypeId = getSubtypeIdFromHashCode(
1469 imi, keyboardSubtype.hashCode());
1470 if(keyboardSubtype.getLocale().equals(locale)) {
1471 break;
1472 }
1473 }
1474 }
1475 }
1476 }
1477 }
1478
1479 if (!TextUtils.isEmpty(targetLastImiId)) {
1480 if (DEBUG) {
1481 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1482 + ", from: " + mCurMethodId + ", " + subtypeId);
1483 }
1484 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1485 return true;
1486 } else {
1487 return false;
1488 }
satok735cf382010-11-11 20:40:09 +09001489 }
1490 }
1491
satok68f1b782011-04-11 14:26:04 +09001492 public InputMethodSubtype getLastInputMethodSubtype() {
1493 synchronized (mMethodMap) {
1494 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1495 // TODO: Handle the case of the last IME with no subtypes
1496 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1497 || TextUtils.isEmpty(lastIme.second)) return null;
1498 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1499 if (lastImi == null) return null;
1500 try {
1501 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1502 return lastImi.getSubtypeAt(getSubtypeIdFromHashCode(
1503 lastImi, lastSubtypeHash));
1504 } catch (NumberFormatException e) {
1505 return null;
1506 }
1507 }
1508 }
1509
satok28203512010-11-24 11:06:49 +09001510 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 synchronized (mMethodMap) {
1512 if (token == null) {
1513 if (mContext.checkCallingOrSelfPermission(
1514 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1515 != PackageManager.PERMISSION_GRANTED) {
1516 throw new SecurityException(
1517 "Using null token requires permission "
1518 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1519 }
1520 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001521 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1522 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 return;
1524 }
1525
1526 long ident = Binder.clearCallingIdentity();
1527 try {
satokab751aa2010-09-14 19:17:36 +09001528 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 } finally {
1530 Binder.restoreCallingIdentity(ident);
1531 }
1532 }
1533 }
1534
satok42c5a162011-05-26 16:46:14 +09001535 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 public void hideMySoftInput(IBinder token, int flags) {
1537 synchronized (mMethodMap) {
1538 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001539 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1540 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 return;
1542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 long ident = Binder.clearCallingIdentity();
1544 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001545 hideCurrentInputLocked(flags, null);
1546 } finally {
1547 Binder.restoreCallingIdentity(ident);
1548 }
1549 }
1550 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001551
satok42c5a162011-05-26 16:46:14 +09001552 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001553 public void showMySoftInput(IBinder token, int flags) {
1554 synchronized (mMethodMap) {
1555 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001556 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1557 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001558 return;
1559 }
1560 long ident = Binder.clearCallingIdentity();
1561 try {
1562 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 } finally {
1564 Binder.restoreCallingIdentity(ident);
1565 }
1566 }
1567 }
1568
1569 void setEnabledSessionInMainThread(SessionState session) {
1570 if (mEnabledSession != session) {
1571 if (mEnabledSession != null) {
1572 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001573 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 mEnabledSession.method.setSessionEnabled(
1575 mEnabledSession.session, false);
1576 } catch (RemoteException e) {
1577 }
1578 }
1579 mEnabledSession = session;
1580 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001581 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 session.method.setSessionEnabled(
1583 session.session, true);
1584 } catch (RemoteException e) {
1585 }
1586 }
1587 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001588
satok42c5a162011-05-26 16:46:14 +09001589 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 public boolean handleMessage(Message msg) {
1591 HandlerCaller.SomeArgs args;
1592 switch (msg.what) {
1593 case MSG_SHOW_IM_PICKER:
1594 showInputMethodMenu();
1595 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001596
satokab751aa2010-09-14 19:17:36 +09001597 case MSG_SHOW_IM_SUBTYPE_PICKER:
1598 showInputMethodSubtypeMenu();
1599 return true;
1600
satok47a44912010-10-06 16:03:58 +09001601 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001602 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001603 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001604 return true;
1605
1606 case MSG_SHOW_IM_CONFIG:
1607 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001608 return true;
1609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 case MSG_UNBIND_INPUT:
1613 try {
1614 ((IInputMethod)msg.obj).unbindInput();
1615 } catch (RemoteException e) {
1616 // There is nothing interesting about the method dying.
1617 }
1618 return true;
1619 case MSG_BIND_INPUT:
1620 args = (HandlerCaller.SomeArgs)msg.obj;
1621 try {
1622 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1623 } catch (RemoteException e) {
1624 }
1625 return true;
1626 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001627 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001629 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1630 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 } catch (RemoteException e) {
1632 }
1633 return true;
1634 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001635 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001637 ((IInputMethod)args.arg1).hideSoftInput(0,
1638 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 } catch (RemoteException e) {
1640 }
1641 return true;
1642 case MSG_ATTACH_TOKEN:
1643 args = (HandlerCaller.SomeArgs)msg.obj;
1644 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001645 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1647 } catch (RemoteException e) {
1648 }
1649 return true;
1650 case MSG_CREATE_SESSION:
1651 args = (HandlerCaller.SomeArgs)msg.obj;
1652 try {
1653 ((IInputMethod)args.arg1).createSession(
1654 (IInputMethodCallback)args.arg2);
1655 } catch (RemoteException e) {
1656 }
1657 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 case MSG_START_INPUT:
1661 args = (HandlerCaller.SomeArgs)msg.obj;
1662 try {
1663 SessionState session = (SessionState)args.arg1;
1664 setEnabledSessionInMainThread(session);
1665 session.method.startInput((IInputContext)args.arg2,
1666 (EditorInfo)args.arg3);
1667 } catch (RemoteException e) {
1668 }
1669 return true;
1670 case MSG_RESTART_INPUT:
1671 args = (HandlerCaller.SomeArgs)msg.obj;
1672 try {
1673 SessionState session = (SessionState)args.arg1;
1674 setEnabledSessionInMainThread(session);
1675 session.method.restartInput((IInputContext)args.arg2,
1676 (EditorInfo)args.arg3);
1677 } catch (RemoteException e) {
1678 }
1679 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 case MSG_UNBIND_METHOD:
1684 try {
1685 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1686 } catch (RemoteException e) {
1687 // There is nothing interesting about the last client dying.
1688 }
1689 return true;
1690 case MSG_BIND_METHOD:
1691 args = (HandlerCaller.SomeArgs)msg.obj;
1692 try {
1693 ((IInputMethodClient)args.arg1).onBindMethod(
1694 (InputBindResult)args.arg2);
1695 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001696 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 }
1698 return true;
1699 }
1700 return false;
1701 }
1702
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001703 private boolean isSystemIme(InputMethodInfo inputMethod) {
1704 return (inputMethod.getServiceInfo().applicationInfo.flags
1705 & ApplicationInfo.FLAG_SYSTEM) != 0;
1706 }
1707
Ken Wakasa586f0512011-01-20 22:31:01 +09001708 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1709 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1710 final int subtypeCount = imi.getSubtypeCount();
1711 for (int i = 0; i < subtypeCount; ++i) {
1712 subtypes.add(imi.getSubtypeAt(i));
1713 }
1714 return subtypes;
1715 }
1716
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001717 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001718 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001719 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001720 // We'd prefer to fall back on a system IME, since that is safer.
1721 int i=enabled.size();
1722 while (i > 0) {
1723 i--;
1724 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1725 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1726 break;
1727 }
1728 }
satokab751aa2010-09-14 19:17:36 +09001729 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001730 if (DEBUG) {
1731 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1732 }
satok723a27e2010-11-11 14:58:11 +09001733 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001734 return true;
1735 }
1736
1737 return false;
1738 }
1739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1741 HashMap<String, InputMethodInfo> map) {
1742 list.clear();
1743 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001746 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001747 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1748 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1749 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1750 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751
1752 List<ResolveInfo> services = pm.queryIntentServices(
1753 new Intent(InputMethod.SERVICE_INTERFACE),
1754 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 for (int i = 0; i < services.size(); ++i) {
1757 ResolveInfo ri = services.get(i);
1758 ServiceInfo si = ri.serviceInfo;
1759 ComponentName compName = new ComponentName(si.packageName, si.name);
1760 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1761 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001762 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 + ": it does not require the permission "
1764 + android.Manifest.permission.BIND_INPUT_METHOD);
1765 continue;
1766 }
1767
Joe Onorato8a9b2202010-02-26 18:56:32 -08001768 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769
1770 try {
1771 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1772 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001773 final String id = p.getId();
1774 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775
Amith Yamasanie861ec12010-03-24 21:39:27 -07001776 // System IMEs are enabled by default, unless there's a hard keyboard
1777 // and the system IME was explicitly disabled
1778 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1779 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001780 }
1781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001783 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001787 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001789 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 }
1791 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001792
1793 String defaultIme = Settings.Secure.getString(mContext
1794 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001795 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001796 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001797 updateFromSettingsLocked();
1798 }
1799 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001803
satokab751aa2010-09-14 19:17:36 +09001804 private void showInputMethodMenu() {
1805 showInputMethodMenuInternal(false);
1806 }
1807
1808 private void showInputMethodSubtypeMenu() {
1809 showInputMethodMenuInternal(true);
1810 }
1811
satok217f5482010-12-15 05:19:19 +09001812 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001813 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001814 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001815 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1816 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001817 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001818 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001819 }
satok217f5482010-12-15 05:19:19 +09001820 mContext.startActivity(intent);
1821 }
1822
1823 private void showConfigureInputMethods() {
1824 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1825 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1826 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1827 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001828 mContext.startActivity(intent);
1829 }
1830
satokab751aa2010-09-14 19:17:36 +09001831 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001832 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 String lastInputMethodId = Settings.Secure.getString(context
1839 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001840 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001841 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001842
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001843 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09001844 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
1845 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001846 if (immis == null || immis.size() == 0) {
1847 return;
1848 }
1849
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001850 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851
Ken Wakasa761eb372011-03-04 19:06:18 +09001852 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
1853 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
1854 new Comparator<InputMethodInfo>() {
1855 @Override
1856 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
1857 if (imi2 == null) return 0;
1858 if (imi1 == null) return 1;
1859 if (pm == null) {
1860 return imi1.getId().compareTo(imi2.getId());
1861 }
1862 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
1863 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
1864 return imiId1.toString().compareTo(imiId2.toString());
1865 }
1866 });
satok913a8922010-08-26 21:53:41 +09001867
Ken Wakasa761eb372011-03-04 19:06:18 +09001868 sortedImmis.putAll(immis);
1869
1870 final ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>> imList =
1871 new ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>>();
1872
1873 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09001874 if (imi == null) continue;
1875 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09001876 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09001877 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
1878 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09001879 }
satokbb4aa062011-01-19 21:40:27 +09001880 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa81f6e3d2011-03-04 09:59:52 +09001881 final CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001882 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09001883 final int subtypeCount = imi.getSubtypeCount();
Ken Wakasa586f0512011-01-20 22:31:01 +09001884 for (int j = 0; j < subtypeCount; ++j) {
satokbb4aa062011-01-19 21:40:27 +09001885 InputMethodSubtype subtype = imi.getSubtypeAt(j);
satok7f35c8c2010-10-07 21:13:11 +09001886 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
Ken Wakasa761eb372011-03-04 19:06:18 +09001887 final CharSequence title;
satok7f35c8c2010-10-07 21:13:11 +09001888 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001889 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001890 if (nameResId != 0) {
Ken Wakasa81f6e3d2011-03-04 09:59:52 +09001891 title = TextUtils.concat(pm.getText(imi.getPackageName(),
1892 nameResId, imi.getServiceInfo().applicationInfo),
Ken Wakasa761eb372011-03-04 19:06:18 +09001893 (TextUtils.isEmpty(label) ? "" : " (" + label + ")"));
satok7f35c8c2010-10-07 21:13:11 +09001894 } else {
1895 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001896 // TODO: Use more friendly Title and UI
1897 title = label + "," + (mode == null ? "" : mode) + ","
1898 + (language == null ? "" : language);
1899 }
Ken Wakasa761eb372011-03-04 19:06:18 +09001900 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
1901 title, new Pair<InputMethodInfo, Integer>(imi, j)));
satokab751aa2010-09-14 19:17:36 +09001902 }
satokab751aa2010-09-14 19:17:36 +09001903 }
1904 } else {
Ken Wakasa761eb372011-03-04 19:06:18 +09001905 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
1906 label, new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID)));
satokab751aa2010-09-14 19:17:36 +09001907 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001908 }
satok913a8922010-08-26 21:53:41 +09001909
Ken Wakasa761eb372011-03-04 19:06:18 +09001910 final int N = imList.size();
1911 mItems = new CharSequence[N];
1912 for (int i = 0; i < N; ++i) {
1913 mItems[i] = imList.get(i).first;
1914 }
satokab751aa2010-09-14 19:17:36 +09001915 mIms = new InputMethodInfo[N];
1916 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001917 int checkedItem = 0;
1918 for (int i = 0; i < N; ++i) {
Ken Wakasa761eb372011-03-04 19:06:18 +09001919 Pair<InputMethodInfo, Integer> value = imList.get(i).second;
satokab751aa2010-09-14 19:17:36 +09001920 mIms[i] = value.first;
1921 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001922 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001923 int subtypeId = mSubtypeIds[i];
1924 if ((subtypeId == NOT_A_SUBTYPE_ID)
1925 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1926 || (subtypeId == lastInputMethodSubtypeId)) {
1927 checkedItem = i;
1928 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001929 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 }
satokab751aa2010-09-14 19:17:36 +09001931
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001932 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09001933 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001934 public void onClick(DialogInterface dialog, int which) {
1935 hideInputMethodMenu();
1936 }
1937 };
satokd87c2592010-09-29 11:52:06 +09001938
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001939 TypedArray a = context.obtainStyledAttributes(null,
1940 com.android.internal.R.styleable.DialogPreference,
1941 com.android.internal.R.attr.alertDialogStyle, 0);
1942 mDialogBuilder = new AlertDialog.Builder(context)
1943 .setTitle(com.android.internal.R.string.select_input_method)
1944 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09001945 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001946 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001948 }
1949 })
1950 .setIcon(a.getDrawable(
1951 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1952 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001953
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001954 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1955 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09001956 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001957 public void onClick(DialogInterface dialog, int which) {
1958 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001959 if (mIms == null || mIms.length <= which
1960 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001961 return;
1962 }
1963 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001964 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001965 hideInputMethodMenu();
1966 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001967 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09001968 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09001969 subtypeId = NOT_A_SUBTYPE_ID;
1970 }
1971 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001972 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001973 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001975 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976
satok7f35c8c2010-10-07 21:13:11 +09001977 if (showSubtypes) {
satok82beadf2010-12-27 19:03:06 +09001978 mDialogBuilder.setPositiveButton(
1979 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09001980 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09001981 @Override
satok7f35c8c2010-10-07 21:13:11 +09001982 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09001983 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09001984 }
1985 });
1986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001988 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 mSwitchingDialog.getWindow().setType(
1990 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001991 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 mSwitchingDialog.show();
1993 }
1994 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001997 synchronized (mMethodMap) {
1998 hideInputMethodMenuLocked();
1999 }
2000 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002001
The Android Open Source Project10592532009-03-18 17:39:46 -07002002 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002003 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004
The Android Open Source Project10592532009-03-18 17:39:46 -07002005 if (mSwitchingDialog != null) {
2006 mSwitchingDialog.dismiss();
2007 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002009
The Android Open Source Project10592532009-03-18 17:39:46 -07002010 mDialogBuilder = null;
2011 mItems = null;
2012 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002016
satok42c5a162011-05-26 16:46:14 +09002017 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 public boolean setInputMethodEnabled(String id, boolean enabled) {
2019 synchronized (mMethodMap) {
2020 if (mContext.checkCallingOrSelfPermission(
2021 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2022 != PackageManager.PERMISSION_GRANTED) {
2023 throw new SecurityException(
2024 "Requires permission "
2025 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2026 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 long ident = Binder.clearCallingIdentity();
2029 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002030 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 } finally {
2032 Binder.restoreCallingIdentity(ident);
2033 }
2034 }
2035 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002036
2037 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2038 // Make sure this is a valid input method.
2039 InputMethodInfo imm = mMethodMap.get(id);
2040 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002041 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002042 }
2043
satokd87c2592010-09-29 11:52:06 +09002044 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2045 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002046
satokd87c2592010-09-29 11:52:06 +09002047 if (enabled) {
2048 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2049 if (pair.first.equals(id)) {
2050 // We are enabling this input method, but it is already enabled.
2051 // Nothing to do. The previous state was enabled.
2052 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002053 }
2054 }
satokd87c2592010-09-29 11:52:06 +09002055 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2056 // Previous state was disabled.
2057 return false;
2058 } else {
2059 StringBuilder builder = new StringBuilder();
2060 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2061 builder, enabledInputMethodsList, id)) {
2062 // Disabled input method is currently selected, switch to another one.
2063 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2064 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002065 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2066 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2067 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002068 }
2069 // Previous state was enabled.
2070 return true;
2071 } else {
2072 // We are disabling the input method but it is already disabled.
2073 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002074 return false;
2075 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002076 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002077 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002078
satok57ffc002011-01-25 00:11:47 +09002079 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2080 if (subtype == null) return true;
Ken Wakasaa5e9ffa2011-02-16 11:55:23 +09002081 return !subtype.containsExtraValueKey(SUBTYPE_EXTRAVALUE_EXCLUDE_FROM_LAST_IME);
satok57ffc002011-01-25 00:11:47 +09002082 }
2083
satok723a27e2010-11-11 14:58:11 +09002084 private void saveCurrentInputMethodAndSubtypeToHistory() {
2085 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2086 if (mCurrentSubtype != null) {
2087 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2088 }
satok57ffc002011-01-25 00:11:47 +09002089 if (canAddToLastInputMethod(mCurrentSubtype)) {
2090 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2091 }
satokab751aa2010-09-14 19:17:36 +09002092 }
2093
satok723a27e2010-11-11 14:58:11 +09002094 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2095 boolean setSubtypeOnly) {
2096 // Update the history of InputMethod and Subtype
2097 saveCurrentInputMethodAndSubtypeToHistory();
2098
2099 // Set Subtype here
2100 if (imi == null || subtypeId < 0) {
2101 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002102 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002103 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002104 if (subtypeId < imi.getSubtypeCount()) {
2105 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2106 mSettings.putSelectedSubtype(subtype.hashCode());
2107 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002108 } else {
2109 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2110 mCurrentSubtype = null;
2111 }
satokab751aa2010-09-14 19:17:36 +09002112 }
satok723a27e2010-11-11 14:58:11 +09002113
2114 if (!setSubtypeOnly) {
2115 // Set InputMethod here
2116 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2117 }
2118 }
2119
2120 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2121 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2122 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2123 // newDefaultIme is empty when there is no candidate for the selected IME.
2124 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2125 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2126 if (subtypeHashCode != null) {
2127 try {
2128 lastSubtypeId = getSubtypeIdFromHashCode(
2129 imi, Integer.valueOf(subtypeHashCode));
2130 } catch (NumberFormatException e) {
2131 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2132 }
2133 }
2134 }
2135 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002136 }
2137
2138 private int getSelectedInputMethodSubtypeId(String id) {
2139 InputMethodInfo imi = mMethodMap.get(id);
2140 if (imi == null) {
2141 return NOT_A_SUBTYPE_ID;
2142 }
satokab751aa2010-09-14 19:17:36 +09002143 int subtypeId;
2144 try {
2145 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2146 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2147 } catch (SettingNotFoundException e) {
2148 return NOT_A_SUBTYPE_ID;
2149 }
satok723a27e2010-11-11 14:58:11 +09002150 return getSubtypeIdFromHashCode(imi, subtypeId);
2151 }
2152
2153 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002154 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002155 final int subtypeCount = imi.getSubtypeCount();
2156 for (int i = 0; i < subtypeCount; ++i) {
2157 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002158 if (subtypeHashCode == ims.hashCode()) {
2159 return i;
2160 }
satokab751aa2010-09-14 19:17:36 +09002161 }
2162 }
2163 return NOT_A_SUBTYPE_ID;
2164 }
2165
satokdf31ae62011-01-15 06:19:44 +09002166 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2167 Resources res, List<InputMethodSubtype> subtypes) {
2168 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002169 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2170 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2171 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002172 final int N = subtypes.size();
2173 boolean containsKeyboardSubtype = false;
2174 for (int i = 0; i < N; ++i) {
2175 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002176 final String locale = subtype.getLocale();
2177 final String mode = subtype.getMode();
2178 // When system locale starts with subtype's locale, that subtype will be applicable
2179 // for system locale
2180 // For instance, it's clearly applicable for cases like system locale = en_US and
2181 // subtype = en, but it is not necessarily considered applicable for cases like system
2182 // locale = en and subtype = en_US.
2183 // We just call systemLocale.startsWith(locale) in this function because there is no
2184 // need to find applicable subtypes aggressively unlike
2185 // findLastResortApplicableSubtypeLocked.
2186 if (systemLocale.startsWith(locale)) {
2187 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2188 // If more applicable subtypes are contained, skip.
2189 if (applicableSubtype != null
2190 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2191 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002192 if (!containsKeyboardSubtype
2193 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2194 containsKeyboardSubtype = true;
2195 }
2196 }
2197 }
satok3da92232011-01-11 22:46:30 +09002198 ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2199 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002200 if (!containsKeyboardSubtype) {
2201 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002202 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002203 if (lastResortKeyboardSubtype != null) {
2204 applicableSubtypes.add(lastResortKeyboardSubtype);
2205 }
2206 }
2207 return applicableSubtypes;
2208 }
2209
satok4e4569d2010-11-19 18:45:53 +09002210 /**
2211 * If there are no selected subtypes, tries finding the most applicable one according to the
2212 * given locale.
2213 * @param subtypes this function will search the most applicable subtype in subtypes
2214 * @param mode subtypes will be filtered by mode
2215 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002216 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2217 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002218 * @return the most applicable subtypeId
2219 */
satokdf31ae62011-01-15 06:19:44 +09002220 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2221 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002222 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002223 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002224 return null;
satok8fbb1e82010-11-02 23:15:58 +09002225 }
satok4e4569d2010-11-19 18:45:53 +09002226 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002227 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002228 }
satok8fbb1e82010-11-02 23:15:58 +09002229 final String language = locale.substring(0, 2);
2230 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002231 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002232 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002233 final int N = subtypes.size();
2234 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002235 InputMethodSubtype subtype = subtypes.get(i);
2236 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002237 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2238 // and all subtypes with all modes can be candidates.
2239 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002240 if (firstMatchedModeSubtype == null) {
2241 firstMatchedModeSubtype = subtype;
2242 }
satok9ef02832010-11-04 21:17:48 +09002243 if (locale.equals(subtypeLocale)) {
2244 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002245 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002246 break;
2247 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2248 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002249 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002250 partialMatchFound = true;
2251 }
satok8fbb1e82010-11-02 23:15:58 +09002252 }
2253 }
2254
satok7599a7f2010-12-22 13:45:23 +09002255 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2256 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002257 }
2258
satok8fbb1e82010-11-02 23:15:58 +09002259 // The first subtype applicable to the system locale will be defined as the most applicable
2260 // subtype.
2261 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002262 if (applicableSubtype != null) {
2263 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2264 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2265 }
satok8fbb1e82010-11-02 23:15:58 +09002266 }
satokcd7cd292010-11-20 15:46:23 +09002267 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002268 }
2269
satok4e4569d2010-11-19 18:45:53 +09002270 // If there are no selected shortcuts, tries finding the most applicable ones.
2271 private Pair<InputMethodInfo, InputMethodSubtype>
2272 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2273 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2274 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002275 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002276 boolean foundInSystemIME = false;
2277
2278 // Search applicable subtype for each InputMethodInfo
2279 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002280 final String imiId = imi.getId();
2281 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2282 continue;
2283 }
satokcd7cd292010-11-20 15:46:23 +09002284 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002285 final List<InputMethodSubtype> enabledSubtypes =
2286 getEnabledInputMethodSubtypeList(imi, true);
2287 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002288 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002289 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002290 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002291 }
satokdf31ae62011-01-15 06:19:44 +09002292 // 2. Search by the system locale from enabledSubtypes.
2293 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002294 if (subtype == null) {
2295 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002296 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002297 }
satok7599a7f2010-12-22 13:45:23 +09002298 // 4. Search by the current subtype's locale from all subtypes.
2299 if (subtype == null && mCurrentSubtype != null) {
2300 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002301 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002302 }
2303 // 5. Search by the system locale from all subtypes.
2304 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002305 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002306 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002307 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002308 }
satokcd7cd292010-11-20 15:46:23 +09002309 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002310 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002311 // The current input method is the most applicable IME.
2312 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002313 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002314 break;
satok7599a7f2010-12-22 13:45:23 +09002315 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002316 // The system input method is 2nd applicable IME.
2317 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002318 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002319 if ((imi.getServiceInfo().applicationInfo.flags
2320 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2321 foundInSystemIME = true;
2322 }
satok4e4569d2010-11-19 18:45:53 +09002323 }
2324 }
2325 }
2326 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002327 if (mostApplicableIMI != null) {
2328 Slog.w(TAG, "Most applicable shortcut input method was:"
2329 + mostApplicableIMI.getId());
2330 if (mostApplicableSubtype != null) {
2331 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2332 + "," + mostApplicableSubtype.getMode() + ","
2333 + mostApplicableSubtype.getLocale());
2334 }
2335 }
satok4e4569d2010-11-19 18:45:53 +09002336 }
satokcd7cd292010-11-20 15:46:23 +09002337 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002338 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002339 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002340 } else {
2341 return null;
2342 }
2343 }
2344
satokab751aa2010-09-14 19:17:36 +09002345 /**
2346 * @return Return the current subtype of this input method.
2347 */
satok42c5a162011-05-26 16:46:14 +09002348 @Override
satokab751aa2010-09-14 19:17:36 +09002349 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002350 boolean subtypeIsSelected = false;
2351 try {
2352 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2353 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2354 } catch (SettingNotFoundException e) {
2355 }
satok3ef8b292010-11-23 06:06:29 +09002356 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002357 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002358 String lastInputMethodId = Settings.Secure.getString(
2359 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002360 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2361 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002362 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2363 if (imi != null) {
2364 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002365 // the most applicable subtype from explicitly or implicitly enabled
2366 // subtypes.
2367 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2368 getEnabledInputMethodSubtypeList(imi, true);
2369 // If there is only one explicitly or implicitly enabled subtype,
2370 // just returns it.
2371 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2372 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2373 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2374 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2375 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2376 SUBTYPE_MODE_KEYBOARD, null, true);
2377 if (mCurrentSubtype == null) {
2378 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2379 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2380 true);
2381 }
2382 }
satok4e4569d2010-11-19 18:45:53 +09002383 }
satokcd7cd292010-11-20 15:46:23 +09002384 } else {
satok3ef8b292010-11-23 06:06:29 +09002385 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002386 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002387 }
satok8fbb1e82010-11-02 23:15:58 +09002388 }
satok3ef8b292010-11-23 06:06:29 +09002389 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002390 }
satokab751aa2010-09-14 19:17:36 +09002391 }
2392
satokf3db1af2010-11-23 13:34:33 +09002393 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2394 InputMethodSubtype subtype) {
2395 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2396 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2397 } else {
2398 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2399 subtypes.add(subtype);
2400 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2401 }
2402 }
2403
satok4e4569d2010-11-19 18:45:53 +09002404 // TODO: We should change the return type from List to List<Parcelable>
2405 public List getShortcutInputMethodsAndSubtypes() {
2406 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002407 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002408 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002409 // If there are no selected shortcut subtypes, the framework will try to find
2410 // the most applicable subtype from all subtypes whose mode is
2411 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002412 Pair<InputMethodInfo, InputMethodSubtype> info =
2413 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2414 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002415 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002416 ret.add(info.first);
2417 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002418 }
satok3da92232011-01-11 22:46:30 +09002419 return ret;
satokf3db1af2010-11-23 13:34:33 +09002420 }
satokf3db1af2010-11-23 13:34:33 +09002421 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2422 ret.add(imi);
2423 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2424 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002425 }
2426 }
satokf3db1af2010-11-23 13:34:33 +09002427 return ret;
satok4e4569d2010-11-19 18:45:53 +09002428 }
2429 }
2430
satok42c5a162011-05-26 16:46:14 +09002431 @Override
satokb66d2872010-11-10 01:04:04 +09002432 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2433 synchronized (mMethodMap) {
2434 if (subtype != null && mCurMethodId != null) {
2435 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2436 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2437 if (subtypeId != NOT_A_SUBTYPE_ID) {
2438 setInputMethodLocked(mCurMethodId, subtypeId);
2439 return true;
2440 }
2441 }
2442 return false;
2443 }
2444 }
2445
satokd87c2592010-09-29 11:52:06 +09002446 /**
2447 * Utility class for putting and getting settings for InputMethod
2448 * TODO: Move all putters and getters of settings to this class.
2449 */
2450 private static class InputMethodSettings {
2451 // The string for enabled input method is saved as follows:
2452 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2453 private static final char INPUT_METHOD_SEPARATER = ':';
2454 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002455 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002456 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2457
satok723a27e2010-11-11 14:58:11 +09002458 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002459 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2460
satokdf31ae62011-01-15 06:19:44 +09002461 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002462 private final ContentResolver mResolver;
2463 private final HashMap<String, InputMethodInfo> mMethodMap;
2464 private final ArrayList<InputMethodInfo> mMethodList;
2465
2466 private String mEnabledInputMethodsStrCache;
2467
2468 private static void buildEnabledInputMethodsSettingString(
2469 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2470 String id = pair.first;
2471 ArrayList<String> subtypes = pair.second;
2472 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002473 // Inputmethod and subtypes are saved in the settings as follows:
2474 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2475 for (String subtypeId: subtypes) {
2476 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002477 }
2478 }
2479
2480 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002481 Resources res, ContentResolver resolver,
2482 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2483 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002484 mResolver = resolver;
2485 mMethodMap = methodMap;
2486 mMethodList = methodList;
2487 }
2488
2489 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2490 return createEnabledInputMethodListLocked(
2491 getEnabledInputMethodsAndSubtypeListLocked());
2492 }
2493
satok7f35c8c2010-10-07 21:13:11 +09002494 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002495 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2496 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002497 getEnabledInputMethodsAndSubtypeListLocked());
2498 }
2499
satok67ddf9c2010-11-17 09:45:54 +09002500 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2501 InputMethodInfo imi) {
2502 List<Pair<String, ArrayList<String>>> imsList =
2503 getEnabledInputMethodsAndSubtypeListLocked();
2504 ArrayList<InputMethodSubtype> enabledSubtypes =
2505 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002506 if (imi != null) {
2507 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2508 InputMethodInfo info = mMethodMap.get(imsPair.first);
2509 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002510 final int subtypeCount = info.getSubtypeCount();
2511 for (int i = 0; i < subtypeCount; ++i) {
2512 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002513 for (String s: imsPair.second) {
2514 if (String.valueOf(ims.hashCode()).equals(s)) {
2515 enabledSubtypes.add(ims);
2516 }
satok67ddf9c2010-11-17 09:45:54 +09002517 }
2518 }
satok884ef9a2010-11-18 10:39:46 +09002519 break;
satok67ddf9c2010-11-17 09:45:54 +09002520 }
satok67ddf9c2010-11-17 09:45:54 +09002521 }
2522 }
2523 return enabledSubtypes;
2524 }
2525
satokd87c2592010-09-29 11:52:06 +09002526 // At the initial boot, the settings for input methods are not set,
2527 // so we need to enable IME in that case.
2528 public void enableAllIMEsIfThereIsNoEnabledIME() {
2529 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2530 StringBuilder sb = new StringBuilder();
2531 final int N = mMethodList.size();
2532 for (int i = 0; i < N; i++) {
2533 InputMethodInfo imi = mMethodList.get(i);
2534 Slog.i(TAG, "Adding: " + imi.getId());
2535 if (i > 0) sb.append(':');
2536 sb.append(imi.getId());
2537 }
2538 putEnabledInputMethodsStr(sb.toString());
2539 }
2540 }
2541
satokbb4aa062011-01-19 21:40:27 +09002542 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002543 ArrayList<Pair<String, ArrayList<String>>> imsList
2544 = new ArrayList<Pair<String, ArrayList<String>>>();
2545 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2546 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2547 return imsList;
2548 }
satok723a27e2010-11-11 14:58:11 +09002549 mInputMethodSplitter.setString(enabledInputMethodsStr);
2550 while (mInputMethodSplitter.hasNext()) {
2551 String nextImsStr = mInputMethodSplitter.next();
2552 mSubtypeSplitter.setString(nextImsStr);
2553 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002554 ArrayList<String> subtypeHashes = new ArrayList<String>();
2555 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002556 String imeId = mSubtypeSplitter.next();
2557 while (mSubtypeSplitter.hasNext()) {
2558 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002559 }
2560 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2561 }
2562 }
2563 return imsList;
2564 }
2565
2566 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2567 if (reloadInputMethodStr) {
2568 getEnabledInputMethodsStr();
2569 }
2570 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2571 // Add in the newly enabled input method.
2572 putEnabledInputMethodsStr(id);
2573 } else {
2574 putEnabledInputMethodsStr(
2575 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2576 }
2577 }
2578
2579 /**
2580 * Build and put a string of EnabledInputMethods with removing specified Id.
2581 * @return the specified id was removed or not.
2582 */
2583 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2584 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2585 boolean isRemoved = false;
2586 boolean needsAppendSeparator = false;
2587 for (Pair<String, ArrayList<String>> ims: imsList) {
2588 String curId = ims.first;
2589 if (curId.equals(id)) {
2590 // We are disabling this input method, and it is
2591 // currently enabled. Skip it to remove from the
2592 // new list.
2593 isRemoved = true;
2594 } else {
2595 if (needsAppendSeparator) {
2596 builder.append(INPUT_METHOD_SEPARATER);
2597 } else {
2598 needsAppendSeparator = true;
2599 }
2600 buildEnabledInputMethodsSettingString(builder, ims);
2601 }
2602 }
2603 if (isRemoved) {
2604 // Update the setting with the new list of input methods.
2605 putEnabledInputMethodsStr(builder.toString());
2606 }
2607 return isRemoved;
2608 }
2609
2610 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2611 List<Pair<String, ArrayList<String>>> imsList) {
2612 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2613 for (Pair<String, ArrayList<String>> ims: imsList) {
2614 InputMethodInfo info = mMethodMap.get(ims.first);
2615 if (info != null) {
2616 res.add(info);
2617 }
2618 }
2619 return res;
2620 }
2621
satok7f35c8c2010-10-07 21:13:11 +09002622 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002623 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002624 List<Pair<String, ArrayList<String>>> imsList) {
2625 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2626 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2627 for (Pair<String, ArrayList<String>> ims : imsList) {
2628 InputMethodInfo info = mMethodMap.get(ims.first);
2629 if (info != null) {
2630 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2631 }
2632 }
2633 return res;
2634 }
2635
satokd87c2592010-09-29 11:52:06 +09002636 private void putEnabledInputMethodsStr(String str) {
2637 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2638 mEnabledInputMethodsStrCache = str;
2639 }
2640
2641 private String getEnabledInputMethodsStr() {
2642 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2643 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002644 if (DEBUG) {
2645 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2646 }
satokd87c2592010-09-29 11:52:06 +09002647 return mEnabledInputMethodsStrCache;
2648 }
satok723a27e2010-11-11 14:58:11 +09002649
2650 private void saveSubtypeHistory(
2651 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2652 StringBuilder builder = new StringBuilder();
2653 boolean isImeAdded = false;
2654 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2655 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2656 newSubtypeId);
2657 isImeAdded = true;
2658 }
2659 for (Pair<String, String> ime: savedImes) {
2660 String imeId = ime.first;
2661 String subtypeId = ime.second;
2662 if (TextUtils.isEmpty(subtypeId)) {
2663 subtypeId = NOT_A_SUBTYPE_ID_STR;
2664 }
2665 if (isImeAdded) {
2666 builder.append(INPUT_METHOD_SEPARATER);
2667 } else {
2668 isImeAdded = true;
2669 }
2670 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2671 subtypeId);
2672 }
2673 // Remove the last INPUT_METHOD_SEPARATER
2674 putSubtypeHistoryStr(builder.toString());
2675 }
2676
2677 public void addSubtypeToHistory(String imeId, String subtypeId) {
2678 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2679 for (Pair<String, String> ime: subtypeHistory) {
2680 if (ime.first.equals(imeId)) {
2681 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002682 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002683 + ime.second);
2684 }
2685 // We should break here
2686 subtypeHistory.remove(ime);
2687 break;
2688 }
2689 }
satokbb4aa062011-01-19 21:40:27 +09002690 if (DEBUG) {
2691 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2692 }
satok723a27e2010-11-11 14:58:11 +09002693 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2694 }
2695
2696 private void putSubtypeHistoryStr(String str) {
2697 if (DEBUG) {
2698 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2699 }
2700 Settings.Secure.putString(
2701 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2702 }
2703
2704 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2705 // Gets the first one from the history
2706 return getLastSubtypeForInputMethodLockedInternal(null);
2707 }
2708
2709 public String getLastSubtypeForInputMethodLocked(String imeId) {
2710 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2711 if (ime != null) {
2712 return ime.second;
2713 } else {
2714 return null;
2715 }
2716 }
2717
2718 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2719 List<Pair<String, ArrayList<String>>> enabledImes =
2720 getEnabledInputMethodsAndSubtypeListLocked();
2721 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09002722 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09002723 final String imeInTheHistory = imeAndSubtype.first;
2724 // If imeId is empty, returns the first IME and subtype in the history
2725 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2726 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002727 final String subtypeHashCode =
2728 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2729 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002730 if (!TextUtils.isEmpty(subtypeHashCode)) {
2731 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002732 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002733 }
2734 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2735 }
2736 }
2737 }
2738 if (DEBUG) {
2739 Slog.d(TAG, "No enabled IME found in the history");
2740 }
2741 return null;
2742 }
2743
satokdf31ae62011-01-15 06:19:44 +09002744 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002745 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2746 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2747 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002748 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2749 if (explicitlyEnabledSubtypes.size() == 0) {
2750 // If there are no explicitly enabled subtypes, applicable subtypes are
2751 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002752 InputMethodInfo ime = mMethodMap.get(imeId);
2753 // If IME is enabled and no subtypes are enabled, applicable subtypes
2754 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002755 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002756 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002757 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002758 if (implicitlySelectedSubtypes != null) {
2759 final int N = implicitlySelectedSubtypes.size();
2760 for (int i = 0; i < N; ++i) {
2761 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2762 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2763 return subtypeHashCode;
2764 }
2765 }
2766 }
2767 }
2768 } else {
satokf6cafb62011-01-17 16:29:02 +09002769 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002770 if (s.equals(subtypeHashCode)) {
2771 // If both imeId and subtypeId are enabled, return subtypeId.
2772 return s;
2773 }
satok723a27e2010-11-11 14:58:11 +09002774 }
2775 }
2776 // If imeId was enabled but subtypeId was disabled.
2777 return NOT_A_SUBTYPE_ID_STR;
2778 }
2779 }
2780 // If both imeId and subtypeId are disabled, return null
2781 return null;
2782 }
2783
2784 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2785 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2786 final String subtypeHistoryStr = getSubtypeHistoryStr();
2787 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2788 return imsList;
2789 }
2790 mInputMethodSplitter.setString(subtypeHistoryStr);
2791 while (mInputMethodSplitter.hasNext()) {
2792 String nextImsStr = mInputMethodSplitter.next();
2793 mSubtypeSplitter.setString(nextImsStr);
2794 if (mSubtypeSplitter.hasNext()) {
2795 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2796 // The first element is ime id.
2797 String imeId = mSubtypeSplitter.next();
2798 while (mSubtypeSplitter.hasNext()) {
2799 subtypeId = mSubtypeSplitter.next();
2800 break;
2801 }
2802 imsList.add(new Pair<String, String>(imeId, subtypeId));
2803 }
2804 }
2805 return imsList;
2806 }
2807
2808 private String getSubtypeHistoryStr() {
2809 if (DEBUG) {
2810 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2811 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2812 }
2813 return Settings.Secure.getString(
2814 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2815 }
2816
2817 public void putSelectedInputMethod(String imeId) {
2818 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2819 }
2820
2821 public void putSelectedSubtype(int subtypeId) {
2822 Settings.Secure.putInt(
2823 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2824 }
satokd87c2592010-09-29 11:52:06 +09002825 }
2826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 @Override
2830 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2831 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2832 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2835 + Binder.getCallingPid()
2836 + ", uid=" + Binder.getCallingUid());
2837 return;
2838 }
2839
2840 IInputMethod method;
2841 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 synchronized (mMethodMap) {
2846 p.println("Current Input Method Manager state:");
2847 int N = mMethodList.size();
2848 p.println(" Input Methods:");
2849 for (int i=0; i<N; i++) {
2850 InputMethodInfo info = mMethodList.get(i);
2851 p.println(" InputMethod #" + i + ":");
2852 info.dump(p, " ");
2853 }
2854 p.println(" Clients:");
2855 for (ClientState ci : mClients.values()) {
2856 p.println(" Client " + ci + ":");
2857 p.println(" client=" + ci.client);
2858 p.println(" inputContext=" + ci.inputContext);
2859 p.println(" sessionRequested=" + ci.sessionRequested);
2860 p.println(" curSession=" + ci.curSession);
2861 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002862 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002864 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2865 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2867 + " mBoundToMethod=" + mBoundToMethod);
2868 p.println(" mCurToken=" + mCurToken);
2869 p.println(" mCurIntent=" + mCurIntent);
2870 method = mCurMethod;
2871 p.println(" mCurMethod=" + mCurMethod);
2872 p.println(" mEnabledSession=" + mEnabledSession);
2873 p.println(" mShowRequested=" + mShowRequested
2874 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2875 + " mShowForced=" + mShowForced
2876 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002877 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002879
Jeff Brownb88102f2010-09-08 11:49:43 -07002880 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 pw.flush();
2883 try {
2884 client.client.asBinder().dump(fd, args);
2885 } catch (RemoteException e) {
2886 p.println("Input method client dead: " + e);
2887 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002888 } else {
2889 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002891
Jeff Brownb88102f2010-09-08 11:49:43 -07002892 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 pw.flush();
2895 try {
2896 method.asBinder().dump(fd, args);
2897 } catch (RemoteException e) {
2898 p.println("Input method service dead: " + e);
2899 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002900 } else {
2901 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 }
2903 }
2904}