blob: 7361ef7b9c35b12bb5f19743e8fe9561ac24b685 [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];
1024 if (ss.getNotificationTargetClass() != null) {
1025 mSecureSuggestionSpans.put(ss, currentImi);
1026 }
1027 }
1028 }
1029 }
1030
1031 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1032 synchronized (mMethodMap) {
1033 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1034 // TODO: Do not send the intent if the process of the targetImi is already dead.
1035 if (targetImi != null) {
1036 final String[] suggestions = span.getSuggestions();
1037 if (index < 0 || index >= suggestions.length) return false;
1038 final Class<?> c = span.getNotificationTargetClass();
1039 final Intent intent = new Intent();
1040 // Ensures that only a class in the original IME package will receive the
1041 // notification.
1042 intent.setClassName(targetImi.getPackageName(), c.getCanonicalName());
1043 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1044 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1045 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1046 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1047 mContext.sendBroadcast(intent);
1048 return true;
1049 }
1050 }
1051 return false;
1052 }
1053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001055 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1056 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1057 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1058 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001060 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001061 // There is no input method selected, try to choose new applicable input method.
1062 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1063 id = Settings.Secure.getString(mContext.getContentResolver(),
1064 Settings.Secure.DEFAULT_INPUT_METHOD);
1065 }
1066 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 try {
satokab751aa2010-09-14 19:17:36 +09001068 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001070 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001071 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001072 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 }
satokf3db1af2010-11-23 13:34:33 +09001074 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001075 } else {
1076 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001077 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001078 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 }
1080 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001081
satokab751aa2010-09-14 19:17:36 +09001082 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 InputMethodInfo info = mMethodMap.get(id);
1084 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001085 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001089 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001090 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1091 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001092 }
1093 if (subtype != mCurrentSubtype) {
1094 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001095 if (subtype != null) {
1096 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1097 }
satokcd7cd292010-11-20 15:46:23 +09001098 if (mCurMethod != null) {
1099 try {
satoke40dea02011-01-30 01:14:02 +09001100 final Configuration conf = mRes.getConfiguration();
1101 final boolean haveHardKeyboard = conf.keyboard
1102 != Configuration.KEYBOARD_NOKEYS;
1103 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001104 && conf.hardKeyboardHidden
1105 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001106 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1107 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1108 : 0;
Joe Onorato857fd9b2011-01-27 15:08:35 -08001109 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1110 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001111 // If subtype is null, try to find the most applicable one from
1112 // getCurrentInputMethodSubtype.
1113 if (subtype == null) {
1114 subtype = getCurrentInputMethodSubtype();
1115 }
1116 mCurMethod.changeInputMethodSubtype(subtype);
1117 } catch (RemoteException e) {
1118 return;
satokab751aa2010-09-14 19:17:36 +09001119 }
1120 }
1121 }
1122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 return;
1124 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 final long ident = Binder.clearCallingIdentity();
1127 try {
satokab751aa2010-09-14 19:17:36 +09001128 // Set a subtype to this input method.
1129 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001130 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1131 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1132 // because mCurMethodId is stored as a history in
1133 // setSelectedInputMethodAndSubtypeLocked().
1134 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135
1136 if (ActivityManagerNative.isSystemReady()) {
1137 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001138 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 intent.putExtra("input_method_id", id);
1140 mContext.sendBroadcast(intent);
1141 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001142 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 } finally {
1144 Binder.restoreCallingIdentity(ident);
1145 }
1146 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001147
The Android Open Source Project4df24232009-03-05 14:34:35 -08001148 public boolean showSoftInput(IInputMethodClient client, int flags,
1149 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001150 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 long ident = Binder.clearCallingIdentity();
1152 try {
1153 synchronized (mMethodMap) {
1154 if (mCurClient == null || client == null
1155 || mCurClient.client.asBinder() != client.asBinder()) {
1156 try {
1157 // We need to check if this is the current client with
1158 // focus in the window manager, to allow this call to
1159 // be made before input is started in it.
1160 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001161 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001162 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 }
1164 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001165 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 }
1167 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001168
Joe Onorato8a9b2202010-02-26 18:56:32 -08001169 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001170 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
1172 } finally {
1173 Binder.restoreCallingIdentity(ident);
1174 }
1175 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001176
The Android Open Source Project4df24232009-03-05 14:34:35 -08001177 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 mShowRequested = true;
1179 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1180 mShowExplicitlyRequested = true;
1181 }
1182 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1183 mShowExplicitlyRequested = true;
1184 mShowForced = true;
1185 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001186
Dianne Hackborncc278702009-09-02 23:07:23 -07001187 if (!mSystemReady) {
1188 return false;
1189 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001190
The Android Open Source Project4df24232009-03-05 14:34:35 -08001191 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001193 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1194 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1195 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001197 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 } else if (mHaveConnection && SystemClock.uptimeMillis()
1199 < (mLastBindTime+TIME_TO_RECONNECT)) {
1200 // The client has asked to have the input method shown, but
1201 // we have been sitting here too long with a connection to the
1202 // service and no interface received, so let's disconnect/connect
1203 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001204 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 SystemClock.uptimeMillis()-mLastBindTime,1);
1206 mContext.unbindService(this);
1207 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1208 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001209
The Android Open Source Project4df24232009-03-05 14:34:35 -08001210 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001212
The Android Open Source Project4df24232009-03-05 14:34:35 -08001213 public boolean hideSoftInput(IInputMethodClient client, int flags,
1214 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001215 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 long ident = Binder.clearCallingIdentity();
1217 try {
1218 synchronized (mMethodMap) {
1219 if (mCurClient == null || client == null
1220 || mCurClient.client.asBinder() != client.asBinder()) {
1221 try {
1222 // We need to check if this is the current client with
1223 // focus in the window manager, to allow this call to
1224 // be made before input is started in it.
1225 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001226 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1227 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001228 mImeWindowVis = 0;
1229 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1230 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001231 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 }
1233 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001234 mImeWindowVis = 0;
1235 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001236 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 }
1238 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001239
Joe Onorato8a9b2202010-02-26 18:56:32 -08001240 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001241 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 }
1243 } finally {
1244 Binder.restoreCallingIdentity(ident);
1245 }
1246 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001247
The Android Open Source Project4df24232009-03-05 14:34:35 -08001248 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1250 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001251 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001253 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 }
1255 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001256 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001258 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001260 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001262 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1263 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1264 res = true;
1265 } else {
1266 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 }
1268 mInputShown = false;
1269 mShowRequested = false;
1270 mShowExplicitlyRequested = false;
1271 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001272 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001274
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001275 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1277 boolean first, int windowFlags) {
1278 long ident = Binder.clearCallingIdentity();
1279 try {
1280 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001281 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 + " viewHasFocus=" + viewHasFocus
1283 + " isTextEditor=" + isTextEditor
1284 + " softInputMode=#" + Integer.toHexString(softInputMode)
1285 + " first=" + first + " flags=#"
1286 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 if (mCurClient == null || client == null
1289 || mCurClient.client.asBinder() != client.asBinder()) {
1290 try {
1291 // We need to check if this is the current client with
1292 // focus in the window manager, to allow this call to
1293 // be made before input is started in it.
1294 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001295 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 return;
1297 }
1298 } catch (RemoteException e) {
1299 }
1300 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001301
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001302 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001303 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001304 return;
1305 }
1306 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001307
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001308 // Should we auto-show the IME even if the caller has not
1309 // specified what should be done with it?
1310 // We only do this automatically if the window can resize
1311 // to accommodate the IME (so what the user sees will give
1312 // them good context without input information being obscured
1313 // by the IME) or if running on a large screen where there
1314 // is more room for the target window + IME.
1315 final boolean doAutoShow =
1316 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1317 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1318 || mRes.getConfiguration().isLayoutSizeAtLeast(
1319 Configuration.SCREENLAYOUT_SIZE_LARGE);
1320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1322 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001323 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1325 // There is no focus view, and this window will
1326 // be behind any soft input window, so hide the
1327 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001328 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001329 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001331 } else if (isTextEditor && doAutoShow && (softInputMode &
1332 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 // There is a focus view, and we are navigating forward
1334 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001335 // We only do this automatically if the window an resize
1336 // to accomodate the IME (so what the user sees will give
1337 // them good context without input information being obscured
1338 // by the IME) or if running on a large screen where there
1339 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001340 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001341 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 }
1343 break;
1344 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1345 // Do nothing.
1346 break;
1347 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1348 if ((softInputMode &
1349 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001350 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001351 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 }
1353 break;
1354 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001355 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001356 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 break;
1358 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1359 if ((softInputMode &
1360 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001361 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001362 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 }
1364 break;
1365 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001366 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001367 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 break;
1369 }
1370 }
1371 } finally {
1372 Binder.restoreCallingIdentity(ident);
1373 }
1374 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1377 synchronized (mMethodMap) {
1378 if (mCurClient == null || client == null
1379 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001380 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001381 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 }
1383
satok440aab52010-11-25 09:43:11 +09001384 // Always call subtype picker, because subtype picker is a superset of input method
1385 // picker.
satokab751aa2010-09-14 19:17:36 +09001386 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1387 }
1388 }
1389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001391 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1392 }
1393
1394 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1395 synchronized (mMethodMap) {
1396 if (subtype != null) {
1397 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1398 mMethodMap.get(id), subtype.hashCode()));
1399 } else {
1400 setInputMethod(token, id);
1401 }
1402 }
satokab751aa2010-09-14 19:17:36 +09001403 }
1404
satokb416a712010-11-25 20:42:14 +09001405 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001406 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001407 synchronized (mMethodMap) {
1408 if (mCurClient == null || client == null
1409 || mCurClient.client.asBinder() != client.asBinder()) {
1410 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1411 }
satok7fee71f2010-12-17 18:54:26 +09001412 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1413 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001414 }
1415 }
1416
satok4fc87d62011-05-20 16:13:43 +09001417 @Override
satok735cf382010-11-11 20:40:09 +09001418 public boolean switchToLastInputMethod(IBinder token) {
1419 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001420 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001421 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001422 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001423 lastImi = mMethodMap.get(lastIme.first);
1424 } else {
1425 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001426 }
satok4fc87d62011-05-20 16:13:43 +09001427 String targetLastImiId = null;
1428 int subtypeId = NOT_A_SUBTYPE_ID;
1429 if (lastIme != null && lastImi != null) {
1430 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1431 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1432 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1433 : mCurrentSubtype.hashCode();
1434 // If the last IME is the same as the current IME and the last subtype is not
1435 // defined, there is no need to switch to the last IME.
1436 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1437 targetLastImiId = lastIme.first;
1438 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1439 }
1440 }
1441
1442 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1443 // This is a safety net. If the currentSubtype can't be added to the history
1444 // and the framework couldn't find the last ime, we will make the last ime be
1445 // the most applicable enabled keyboard subtype of the system imes.
1446 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1447 if (enabled != null) {
1448 final int N = enabled.size();
1449 final String locale = mCurrentSubtype == null
1450 ? mRes.getConfiguration().locale.toString()
1451 : mCurrentSubtype.getLocale();
1452 for (int i = 0; i < N; ++i) {
1453 final InputMethodInfo imi = enabled.get(i);
1454 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1455 InputMethodSubtype keyboardSubtype =
1456 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1457 SUBTYPE_MODE_KEYBOARD, locale, true);
1458 if (keyboardSubtype != null) {
1459 targetLastImiId = imi.getId();
1460 subtypeId = getSubtypeIdFromHashCode(
1461 imi, keyboardSubtype.hashCode());
1462 if(keyboardSubtype.getLocale().equals(locale)) {
1463 break;
1464 }
1465 }
1466 }
1467 }
1468 }
1469 }
1470
1471 if (!TextUtils.isEmpty(targetLastImiId)) {
1472 if (DEBUG) {
1473 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1474 + ", from: " + mCurMethodId + ", " + subtypeId);
1475 }
1476 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1477 return true;
1478 } else {
1479 return false;
1480 }
satok735cf382010-11-11 20:40:09 +09001481 }
1482 }
1483
satok68f1b782011-04-11 14:26:04 +09001484 public InputMethodSubtype getLastInputMethodSubtype() {
1485 synchronized (mMethodMap) {
1486 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1487 // TODO: Handle the case of the last IME with no subtypes
1488 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1489 || TextUtils.isEmpty(lastIme.second)) return null;
1490 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1491 if (lastImi == null) return null;
1492 try {
1493 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1494 return lastImi.getSubtypeAt(getSubtypeIdFromHashCode(
1495 lastImi, lastSubtypeHash));
1496 } catch (NumberFormatException e) {
1497 return null;
1498 }
1499 }
1500 }
1501
satok28203512010-11-24 11:06:49 +09001502 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 synchronized (mMethodMap) {
1504 if (token == null) {
1505 if (mContext.checkCallingOrSelfPermission(
1506 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1507 != PackageManager.PERMISSION_GRANTED) {
1508 throw new SecurityException(
1509 "Using null token requires permission "
1510 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1511 }
1512 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001513 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1514 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 return;
1516 }
1517
1518 long ident = Binder.clearCallingIdentity();
1519 try {
satokab751aa2010-09-14 19:17:36 +09001520 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 } finally {
1522 Binder.restoreCallingIdentity(ident);
1523 }
1524 }
1525 }
1526
1527 public void hideMySoftInput(IBinder token, int flags) {
1528 synchronized (mMethodMap) {
1529 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001530 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1531 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 return;
1533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 long ident = Binder.clearCallingIdentity();
1535 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001536 hideCurrentInputLocked(flags, null);
1537 } finally {
1538 Binder.restoreCallingIdentity(ident);
1539 }
1540 }
1541 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001542
The Android Open Source Project4df24232009-03-05 14:34:35 -08001543 public void showMySoftInput(IBinder token, int flags) {
1544 synchronized (mMethodMap) {
1545 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001546 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1547 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001548 return;
1549 }
1550 long ident = Binder.clearCallingIdentity();
1551 try {
1552 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 } finally {
1554 Binder.restoreCallingIdentity(ident);
1555 }
1556 }
1557 }
1558
1559 void setEnabledSessionInMainThread(SessionState session) {
1560 if (mEnabledSession != session) {
1561 if (mEnabledSession != null) {
1562 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001563 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 mEnabledSession.method.setSessionEnabled(
1565 mEnabledSession.session, false);
1566 } catch (RemoteException e) {
1567 }
1568 }
1569 mEnabledSession = session;
1570 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001571 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 session.method.setSessionEnabled(
1573 session.session, true);
1574 } catch (RemoteException e) {
1575 }
1576 }
1577 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 public boolean handleMessage(Message msg) {
1580 HandlerCaller.SomeArgs args;
1581 switch (msg.what) {
1582 case MSG_SHOW_IM_PICKER:
1583 showInputMethodMenu();
1584 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001585
satokab751aa2010-09-14 19:17:36 +09001586 case MSG_SHOW_IM_SUBTYPE_PICKER:
1587 showInputMethodSubtypeMenu();
1588 return true;
1589
satok47a44912010-10-06 16:03:58 +09001590 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001591 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001592 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001593 return true;
1594
1595 case MSG_SHOW_IM_CONFIG:
1596 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001597 return true;
1598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 case MSG_UNBIND_INPUT:
1602 try {
1603 ((IInputMethod)msg.obj).unbindInput();
1604 } catch (RemoteException e) {
1605 // There is nothing interesting about the method dying.
1606 }
1607 return true;
1608 case MSG_BIND_INPUT:
1609 args = (HandlerCaller.SomeArgs)msg.obj;
1610 try {
1611 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1612 } catch (RemoteException e) {
1613 }
1614 return true;
1615 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001616 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001618 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1619 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 } catch (RemoteException e) {
1621 }
1622 return true;
1623 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001624 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001626 ((IInputMethod)args.arg1).hideSoftInput(0,
1627 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 } catch (RemoteException e) {
1629 }
1630 return true;
1631 case MSG_ATTACH_TOKEN:
1632 args = (HandlerCaller.SomeArgs)msg.obj;
1633 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001634 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1636 } catch (RemoteException e) {
1637 }
1638 return true;
1639 case MSG_CREATE_SESSION:
1640 args = (HandlerCaller.SomeArgs)msg.obj;
1641 try {
1642 ((IInputMethod)args.arg1).createSession(
1643 (IInputMethodCallback)args.arg2);
1644 } catch (RemoteException e) {
1645 }
1646 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 case MSG_START_INPUT:
1650 args = (HandlerCaller.SomeArgs)msg.obj;
1651 try {
1652 SessionState session = (SessionState)args.arg1;
1653 setEnabledSessionInMainThread(session);
1654 session.method.startInput((IInputContext)args.arg2,
1655 (EditorInfo)args.arg3);
1656 } catch (RemoteException e) {
1657 }
1658 return true;
1659 case MSG_RESTART_INPUT:
1660 args = (HandlerCaller.SomeArgs)msg.obj;
1661 try {
1662 SessionState session = (SessionState)args.arg1;
1663 setEnabledSessionInMainThread(session);
1664 session.method.restartInput((IInputContext)args.arg2,
1665 (EditorInfo)args.arg3);
1666 } catch (RemoteException e) {
1667 }
1668 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 case MSG_UNBIND_METHOD:
1673 try {
1674 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1675 } catch (RemoteException e) {
1676 // There is nothing interesting about the last client dying.
1677 }
1678 return true;
1679 case MSG_BIND_METHOD:
1680 args = (HandlerCaller.SomeArgs)msg.obj;
1681 try {
1682 ((IInputMethodClient)args.arg1).onBindMethod(
1683 (InputBindResult)args.arg2);
1684 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001685 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 }
1687 return true;
1688 }
1689 return false;
1690 }
1691
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001692 private boolean isSystemIme(InputMethodInfo inputMethod) {
1693 return (inputMethod.getServiceInfo().applicationInfo.flags
1694 & ApplicationInfo.FLAG_SYSTEM) != 0;
1695 }
1696
Ken Wakasa586f0512011-01-20 22:31:01 +09001697 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1698 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1699 final int subtypeCount = imi.getSubtypeCount();
1700 for (int i = 0; i < subtypeCount; ++i) {
1701 subtypes.add(imi.getSubtypeAt(i));
1702 }
1703 return subtypes;
1704 }
1705
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001706 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001707 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001708 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001709 // We'd prefer to fall back on a system IME, since that is safer.
1710 int i=enabled.size();
1711 while (i > 0) {
1712 i--;
1713 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1714 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1715 break;
1716 }
1717 }
satokab751aa2010-09-14 19:17:36 +09001718 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001719 if (DEBUG) {
1720 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1721 }
satok723a27e2010-11-11 14:58:11 +09001722 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001723 return true;
1724 }
1725
1726 return false;
1727 }
1728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1730 HashMap<String, InputMethodInfo> map) {
1731 list.clear();
1732 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001735 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001736 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1737 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1738 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1739 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740
1741 List<ResolveInfo> services = pm.queryIntentServices(
1742 new Intent(InputMethod.SERVICE_INTERFACE),
1743 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 for (int i = 0; i < services.size(); ++i) {
1746 ResolveInfo ri = services.get(i);
1747 ServiceInfo si = ri.serviceInfo;
1748 ComponentName compName = new ComponentName(si.packageName, si.name);
1749 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1750 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001751 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 + ": it does not require the permission "
1753 + android.Manifest.permission.BIND_INPUT_METHOD);
1754 continue;
1755 }
1756
Joe Onorato8a9b2202010-02-26 18:56:32 -08001757 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758
1759 try {
1760 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1761 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001762 final String id = p.getId();
1763 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764
Amith Yamasanie861ec12010-03-24 21:39:27 -07001765 // System IMEs are enabled by default, unless there's a hard keyboard
1766 // and the system IME was explicitly disabled
1767 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1768 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001769 }
1770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001772 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001776 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001778 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 }
1780 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001781
1782 String defaultIme = Settings.Secure.getString(mContext
1783 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001784 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001785 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001786 updateFromSettingsLocked();
1787 }
1788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001792
satokab751aa2010-09-14 19:17:36 +09001793 private void showInputMethodMenu() {
1794 showInputMethodMenuInternal(false);
1795 }
1796
1797 private void showInputMethodSubtypeMenu() {
1798 showInputMethodMenuInternal(true);
1799 }
1800
satok217f5482010-12-15 05:19:19 +09001801 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001802 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001803 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001804 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1805 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001806 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001807 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001808 }
satok217f5482010-12-15 05:19:19 +09001809 mContext.startActivity(intent);
1810 }
1811
1812 private void showConfigureInputMethods() {
1813 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1814 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1815 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1816 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001817 mContext.startActivity(intent);
1818 }
1819
satokab751aa2010-09-14 19:17:36 +09001820 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001821 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 String lastInputMethodId = Settings.Secure.getString(context
1828 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001829 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001830 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001831
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001832 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09001833 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
1834 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001835 if (immis == null || immis.size() == 0) {
1836 return;
1837 }
1838
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001839 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840
Ken Wakasa761eb372011-03-04 19:06:18 +09001841 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
1842 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
1843 new Comparator<InputMethodInfo>() {
1844 @Override
1845 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
1846 if (imi2 == null) return 0;
1847 if (imi1 == null) return 1;
1848 if (pm == null) {
1849 return imi1.getId().compareTo(imi2.getId());
1850 }
1851 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
1852 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
1853 return imiId1.toString().compareTo(imiId2.toString());
1854 }
1855 });
satok913a8922010-08-26 21:53:41 +09001856
Ken Wakasa761eb372011-03-04 19:06:18 +09001857 sortedImmis.putAll(immis);
1858
1859 final ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>> imList =
1860 new ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>>();
1861
1862 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09001863 if (imi == null) continue;
1864 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09001865 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09001866 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
1867 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09001868 }
satokbb4aa062011-01-19 21:40:27 +09001869 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa81f6e3d2011-03-04 09:59:52 +09001870 final CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001871 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09001872 final int subtypeCount = imi.getSubtypeCount();
Ken Wakasa586f0512011-01-20 22:31:01 +09001873 for (int j = 0; j < subtypeCount; ++j) {
satokbb4aa062011-01-19 21:40:27 +09001874 InputMethodSubtype subtype = imi.getSubtypeAt(j);
satok7f35c8c2010-10-07 21:13:11 +09001875 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
Ken Wakasa761eb372011-03-04 19:06:18 +09001876 final CharSequence title;
satok7f35c8c2010-10-07 21:13:11 +09001877 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001878 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001879 if (nameResId != 0) {
Ken Wakasa81f6e3d2011-03-04 09:59:52 +09001880 title = TextUtils.concat(pm.getText(imi.getPackageName(),
1881 nameResId, imi.getServiceInfo().applicationInfo),
Ken Wakasa761eb372011-03-04 19:06:18 +09001882 (TextUtils.isEmpty(label) ? "" : " (" + label + ")"));
satok7f35c8c2010-10-07 21:13:11 +09001883 } else {
1884 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001885 // TODO: Use more friendly Title and UI
1886 title = label + "," + (mode == null ? "" : mode) + ","
1887 + (language == null ? "" : language);
1888 }
Ken Wakasa761eb372011-03-04 19:06:18 +09001889 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
1890 title, new Pair<InputMethodInfo, Integer>(imi, j)));
satokab751aa2010-09-14 19:17:36 +09001891 }
satokab751aa2010-09-14 19:17:36 +09001892 }
1893 } else {
Ken Wakasa761eb372011-03-04 19:06:18 +09001894 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
1895 label, new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID)));
satokab751aa2010-09-14 19:17:36 +09001896 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001897 }
satok913a8922010-08-26 21:53:41 +09001898
Ken Wakasa761eb372011-03-04 19:06:18 +09001899 final int N = imList.size();
1900 mItems = new CharSequence[N];
1901 for (int i = 0; i < N; ++i) {
1902 mItems[i] = imList.get(i).first;
1903 }
satokab751aa2010-09-14 19:17:36 +09001904 mIms = new InputMethodInfo[N];
1905 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001906 int checkedItem = 0;
1907 for (int i = 0; i < N; ++i) {
Ken Wakasa761eb372011-03-04 19:06:18 +09001908 Pair<InputMethodInfo, Integer> value = imList.get(i).second;
satokab751aa2010-09-14 19:17:36 +09001909 mIms[i] = value.first;
1910 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001911 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001912 int subtypeId = mSubtypeIds[i];
1913 if ((subtypeId == NOT_A_SUBTYPE_ID)
1914 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1915 || (subtypeId == lastInputMethodSubtypeId)) {
1916 checkedItem = i;
1917 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 }
satokab751aa2010-09-14 19:17:36 +09001920
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001921 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1922 public void onClick(DialogInterface dialog, int which) {
1923 hideInputMethodMenu();
1924 }
1925 };
satokd87c2592010-09-29 11:52:06 +09001926
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001927 TypedArray a = context.obtainStyledAttributes(null,
1928 com.android.internal.R.styleable.DialogPreference,
1929 com.android.internal.R.attr.alertDialogStyle, 0);
1930 mDialogBuilder = new AlertDialog.Builder(context)
1931 .setTitle(com.android.internal.R.string.select_input_method)
1932 .setOnCancelListener(new OnCancelListener() {
1933 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001935 }
1936 })
1937 .setIcon(a.getDrawable(
1938 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1939 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001940
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001941 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1942 new AlertDialog.OnClickListener() {
1943 public void onClick(DialogInterface dialog, int which) {
1944 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001945 if (mIms == null || mIms.length <= which
1946 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001947 return;
1948 }
1949 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001950 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001951 hideInputMethodMenu();
1952 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001953 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09001954 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09001955 subtypeId = NOT_A_SUBTYPE_ID;
1956 }
1957 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001958 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001961 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962
satok7f35c8c2010-10-07 21:13:11 +09001963 if (showSubtypes) {
satok82beadf2010-12-27 19:03:06 +09001964 mDialogBuilder.setPositiveButton(
1965 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09001966 new DialogInterface.OnClickListener() {
1967 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09001968 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09001969 }
1970 });
1971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001973 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 mSwitchingDialog.getWindow().setType(
1975 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001976 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 mSwitchingDialog.show();
1978 }
1979 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001982 synchronized (mMethodMap) {
1983 hideInputMethodMenuLocked();
1984 }
1985 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001986
The Android Open Source Project10592532009-03-18 17:39:46 -07001987 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001988 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989
The Android Open Source Project10592532009-03-18 17:39:46 -07001990 if (mSwitchingDialog != null) {
1991 mSwitchingDialog.dismiss();
1992 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001994
The Android Open Source Project10592532009-03-18 17:39:46 -07001995 mDialogBuilder = null;
1996 mItems = null;
1997 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 public boolean setInputMethodEnabled(String id, boolean enabled) {
2003 synchronized (mMethodMap) {
2004 if (mContext.checkCallingOrSelfPermission(
2005 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2006 != PackageManager.PERMISSION_GRANTED) {
2007 throw new SecurityException(
2008 "Requires permission "
2009 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2010 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 long ident = Binder.clearCallingIdentity();
2013 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002014 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 } finally {
2016 Binder.restoreCallingIdentity(ident);
2017 }
2018 }
2019 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002020
2021 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2022 // Make sure this is a valid input method.
2023 InputMethodInfo imm = mMethodMap.get(id);
2024 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002025 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002026 }
2027
satokd87c2592010-09-29 11:52:06 +09002028 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2029 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002030
satokd87c2592010-09-29 11:52:06 +09002031 if (enabled) {
2032 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2033 if (pair.first.equals(id)) {
2034 // We are enabling this input method, but it is already enabled.
2035 // Nothing to do. The previous state was enabled.
2036 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002037 }
2038 }
satokd87c2592010-09-29 11:52:06 +09002039 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2040 // Previous state was disabled.
2041 return false;
2042 } else {
2043 StringBuilder builder = new StringBuilder();
2044 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2045 builder, enabledInputMethodsList, id)) {
2046 // Disabled input method is currently selected, switch to another one.
2047 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2048 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002049 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2050 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2051 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002052 }
2053 // Previous state was enabled.
2054 return true;
2055 } else {
2056 // We are disabling the input method but it is already disabled.
2057 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002058 return false;
2059 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002060 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002061 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002062
satok57ffc002011-01-25 00:11:47 +09002063 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2064 if (subtype == null) return true;
Ken Wakasaa5e9ffa2011-02-16 11:55:23 +09002065 return !subtype.containsExtraValueKey(SUBTYPE_EXTRAVALUE_EXCLUDE_FROM_LAST_IME);
satok57ffc002011-01-25 00:11:47 +09002066 }
2067
satok723a27e2010-11-11 14:58:11 +09002068 private void saveCurrentInputMethodAndSubtypeToHistory() {
2069 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2070 if (mCurrentSubtype != null) {
2071 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2072 }
satok57ffc002011-01-25 00:11:47 +09002073 if (canAddToLastInputMethod(mCurrentSubtype)) {
2074 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2075 }
satokab751aa2010-09-14 19:17:36 +09002076 }
2077
satok723a27e2010-11-11 14:58:11 +09002078 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2079 boolean setSubtypeOnly) {
2080 // Update the history of InputMethod and Subtype
2081 saveCurrentInputMethodAndSubtypeToHistory();
2082
2083 // Set Subtype here
2084 if (imi == null || subtypeId < 0) {
2085 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002086 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002087 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002088 if (subtypeId < imi.getSubtypeCount()) {
2089 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2090 mSettings.putSelectedSubtype(subtype.hashCode());
2091 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002092 } else {
2093 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2094 mCurrentSubtype = null;
2095 }
satokab751aa2010-09-14 19:17:36 +09002096 }
satok723a27e2010-11-11 14:58:11 +09002097
2098 if (!setSubtypeOnly) {
2099 // Set InputMethod here
2100 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2101 }
2102 }
2103
2104 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2105 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2106 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2107 // newDefaultIme is empty when there is no candidate for the selected IME.
2108 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2109 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2110 if (subtypeHashCode != null) {
2111 try {
2112 lastSubtypeId = getSubtypeIdFromHashCode(
2113 imi, Integer.valueOf(subtypeHashCode));
2114 } catch (NumberFormatException e) {
2115 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2116 }
2117 }
2118 }
2119 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002120 }
2121
2122 private int getSelectedInputMethodSubtypeId(String id) {
2123 InputMethodInfo imi = mMethodMap.get(id);
2124 if (imi == null) {
2125 return NOT_A_SUBTYPE_ID;
2126 }
satokab751aa2010-09-14 19:17:36 +09002127 int subtypeId;
2128 try {
2129 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2130 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2131 } catch (SettingNotFoundException e) {
2132 return NOT_A_SUBTYPE_ID;
2133 }
satok723a27e2010-11-11 14:58:11 +09002134 return getSubtypeIdFromHashCode(imi, subtypeId);
2135 }
2136
2137 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002138 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002139 final int subtypeCount = imi.getSubtypeCount();
2140 for (int i = 0; i < subtypeCount; ++i) {
2141 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002142 if (subtypeHashCode == ims.hashCode()) {
2143 return i;
2144 }
satokab751aa2010-09-14 19:17:36 +09002145 }
2146 }
2147 return NOT_A_SUBTYPE_ID;
2148 }
2149
satokdf31ae62011-01-15 06:19:44 +09002150 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2151 Resources res, List<InputMethodSubtype> subtypes) {
2152 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002153 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2154 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2155 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002156 final int N = subtypes.size();
2157 boolean containsKeyboardSubtype = false;
2158 for (int i = 0; i < N; ++i) {
2159 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002160 final String locale = subtype.getLocale();
2161 final String mode = subtype.getMode();
2162 // When system locale starts with subtype's locale, that subtype will be applicable
2163 // for system locale
2164 // For instance, it's clearly applicable for cases like system locale = en_US and
2165 // subtype = en, but it is not necessarily considered applicable for cases like system
2166 // locale = en and subtype = en_US.
2167 // We just call systemLocale.startsWith(locale) in this function because there is no
2168 // need to find applicable subtypes aggressively unlike
2169 // findLastResortApplicableSubtypeLocked.
2170 if (systemLocale.startsWith(locale)) {
2171 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2172 // If more applicable subtypes are contained, skip.
2173 if (applicableSubtype != null
2174 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2175 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002176 if (!containsKeyboardSubtype
2177 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2178 containsKeyboardSubtype = true;
2179 }
2180 }
2181 }
satok3da92232011-01-11 22:46:30 +09002182 ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2183 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002184 if (!containsKeyboardSubtype) {
2185 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002186 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002187 if (lastResortKeyboardSubtype != null) {
2188 applicableSubtypes.add(lastResortKeyboardSubtype);
2189 }
2190 }
2191 return applicableSubtypes;
2192 }
2193
satok4e4569d2010-11-19 18:45:53 +09002194 /**
2195 * If there are no selected subtypes, tries finding the most applicable one according to the
2196 * given locale.
2197 * @param subtypes this function will search the most applicable subtype in subtypes
2198 * @param mode subtypes will be filtered by mode
2199 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002200 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2201 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002202 * @return the most applicable subtypeId
2203 */
satokdf31ae62011-01-15 06:19:44 +09002204 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2205 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002206 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002207 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002208 return null;
satok8fbb1e82010-11-02 23:15:58 +09002209 }
satok4e4569d2010-11-19 18:45:53 +09002210 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002211 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002212 }
satok8fbb1e82010-11-02 23:15:58 +09002213 final String language = locale.substring(0, 2);
2214 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002215 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002216 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002217 final int N = subtypes.size();
2218 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002219 InputMethodSubtype subtype = subtypes.get(i);
2220 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002221 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2222 // and all subtypes with all modes can be candidates.
2223 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002224 if (firstMatchedModeSubtype == null) {
2225 firstMatchedModeSubtype = subtype;
2226 }
satok9ef02832010-11-04 21:17:48 +09002227 if (locale.equals(subtypeLocale)) {
2228 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002229 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002230 break;
2231 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2232 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002233 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002234 partialMatchFound = true;
2235 }
satok8fbb1e82010-11-02 23:15:58 +09002236 }
2237 }
2238
satok7599a7f2010-12-22 13:45:23 +09002239 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2240 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002241 }
2242
satok8fbb1e82010-11-02 23:15:58 +09002243 // The first subtype applicable to the system locale will be defined as the most applicable
2244 // subtype.
2245 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002246 if (applicableSubtype != null) {
2247 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2248 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2249 }
satok8fbb1e82010-11-02 23:15:58 +09002250 }
satokcd7cd292010-11-20 15:46:23 +09002251 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002252 }
2253
satok4e4569d2010-11-19 18:45:53 +09002254 // If there are no selected shortcuts, tries finding the most applicable ones.
2255 private Pair<InputMethodInfo, InputMethodSubtype>
2256 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2257 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2258 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002259 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002260 boolean foundInSystemIME = false;
2261
2262 // Search applicable subtype for each InputMethodInfo
2263 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002264 final String imiId = imi.getId();
2265 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2266 continue;
2267 }
satokcd7cd292010-11-20 15:46:23 +09002268 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002269 final List<InputMethodSubtype> enabledSubtypes =
2270 getEnabledInputMethodSubtypeList(imi, true);
2271 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002272 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002273 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002274 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002275 }
satokdf31ae62011-01-15 06:19:44 +09002276 // 2. Search by the system locale from enabledSubtypes.
2277 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002278 if (subtype == null) {
2279 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002280 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002281 }
satok7599a7f2010-12-22 13:45:23 +09002282 // 4. Search by the current subtype's locale from all subtypes.
2283 if (subtype == null && mCurrentSubtype != null) {
2284 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002285 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002286 }
2287 // 5. Search by the system locale from all subtypes.
2288 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002289 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002290 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002291 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002292 }
satokcd7cd292010-11-20 15:46:23 +09002293 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002294 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002295 // The current input method is the most applicable IME.
2296 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002297 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002298 break;
satok7599a7f2010-12-22 13:45:23 +09002299 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002300 // The system input method is 2nd applicable IME.
2301 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002302 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002303 if ((imi.getServiceInfo().applicationInfo.flags
2304 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2305 foundInSystemIME = true;
2306 }
satok4e4569d2010-11-19 18:45:53 +09002307 }
2308 }
2309 }
2310 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002311 if (mostApplicableIMI != null) {
2312 Slog.w(TAG, "Most applicable shortcut input method was:"
2313 + mostApplicableIMI.getId());
2314 if (mostApplicableSubtype != null) {
2315 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2316 + "," + mostApplicableSubtype.getMode() + ","
2317 + mostApplicableSubtype.getLocale());
2318 }
2319 }
satok4e4569d2010-11-19 18:45:53 +09002320 }
satokcd7cd292010-11-20 15:46:23 +09002321 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002322 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002323 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002324 } else {
2325 return null;
2326 }
2327 }
2328
satokab751aa2010-09-14 19:17:36 +09002329 /**
2330 * @return Return the current subtype of this input method.
2331 */
2332 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002333 boolean subtypeIsSelected = false;
2334 try {
2335 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2336 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2337 } catch (SettingNotFoundException e) {
2338 }
satok3ef8b292010-11-23 06:06:29 +09002339 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002340 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002341 String lastInputMethodId = Settings.Secure.getString(
2342 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002343 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2344 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002345 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2346 if (imi != null) {
2347 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002348 // the most applicable subtype from explicitly or implicitly enabled
2349 // subtypes.
2350 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2351 getEnabledInputMethodSubtypeList(imi, true);
2352 // If there is only one explicitly or implicitly enabled subtype,
2353 // just returns it.
2354 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2355 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2356 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2357 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2358 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2359 SUBTYPE_MODE_KEYBOARD, null, true);
2360 if (mCurrentSubtype == null) {
2361 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2362 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2363 true);
2364 }
2365 }
satok4e4569d2010-11-19 18:45:53 +09002366 }
satokcd7cd292010-11-20 15:46:23 +09002367 } else {
satok3ef8b292010-11-23 06:06:29 +09002368 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002369 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002370 }
satok8fbb1e82010-11-02 23:15:58 +09002371 }
satok3ef8b292010-11-23 06:06:29 +09002372 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002373 }
satokab751aa2010-09-14 19:17:36 +09002374 }
2375
satokf3db1af2010-11-23 13:34:33 +09002376 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2377 InputMethodSubtype subtype) {
2378 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2379 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2380 } else {
2381 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2382 subtypes.add(subtype);
2383 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2384 }
2385 }
2386
satok4e4569d2010-11-19 18:45:53 +09002387 // TODO: We should change the return type from List to List<Parcelable>
2388 public List getShortcutInputMethodsAndSubtypes() {
2389 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002390 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002391 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002392 // If there are no selected shortcut subtypes, the framework will try to find
2393 // the most applicable subtype from all subtypes whose mode is
2394 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002395 Pair<InputMethodInfo, InputMethodSubtype> info =
2396 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2397 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002398 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002399 ret.add(info.first);
2400 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002401 }
satok3da92232011-01-11 22:46:30 +09002402 return ret;
satokf3db1af2010-11-23 13:34:33 +09002403 }
satokf3db1af2010-11-23 13:34:33 +09002404 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2405 ret.add(imi);
2406 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2407 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002408 }
2409 }
satokf3db1af2010-11-23 13:34:33 +09002410 return ret;
satok4e4569d2010-11-19 18:45:53 +09002411 }
2412 }
2413
satokb66d2872010-11-10 01:04:04 +09002414 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2415 synchronized (mMethodMap) {
2416 if (subtype != null && mCurMethodId != null) {
2417 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2418 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2419 if (subtypeId != NOT_A_SUBTYPE_ID) {
2420 setInputMethodLocked(mCurMethodId, subtypeId);
2421 return true;
2422 }
2423 }
2424 return false;
2425 }
2426 }
2427
satokd87c2592010-09-29 11:52:06 +09002428 /**
2429 * Utility class for putting and getting settings for InputMethod
2430 * TODO: Move all putters and getters of settings to this class.
2431 */
2432 private static class InputMethodSettings {
2433 // The string for enabled input method is saved as follows:
2434 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2435 private static final char INPUT_METHOD_SEPARATER = ':';
2436 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002437 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002438 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2439
satok723a27e2010-11-11 14:58:11 +09002440 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002441 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2442
satokdf31ae62011-01-15 06:19:44 +09002443 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002444 private final ContentResolver mResolver;
2445 private final HashMap<String, InputMethodInfo> mMethodMap;
2446 private final ArrayList<InputMethodInfo> mMethodList;
2447
2448 private String mEnabledInputMethodsStrCache;
2449
2450 private static void buildEnabledInputMethodsSettingString(
2451 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2452 String id = pair.first;
2453 ArrayList<String> subtypes = pair.second;
2454 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002455 // Inputmethod and subtypes are saved in the settings as follows:
2456 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2457 for (String subtypeId: subtypes) {
2458 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002459 }
2460 }
2461
2462 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002463 Resources res, ContentResolver resolver,
2464 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2465 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002466 mResolver = resolver;
2467 mMethodMap = methodMap;
2468 mMethodList = methodList;
2469 }
2470
2471 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2472 return createEnabledInputMethodListLocked(
2473 getEnabledInputMethodsAndSubtypeListLocked());
2474 }
2475
satok7f35c8c2010-10-07 21:13:11 +09002476 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002477 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2478 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002479 getEnabledInputMethodsAndSubtypeListLocked());
2480 }
2481
satok67ddf9c2010-11-17 09:45:54 +09002482 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2483 InputMethodInfo imi) {
2484 List<Pair<String, ArrayList<String>>> imsList =
2485 getEnabledInputMethodsAndSubtypeListLocked();
2486 ArrayList<InputMethodSubtype> enabledSubtypes =
2487 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002488 if (imi != null) {
2489 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2490 InputMethodInfo info = mMethodMap.get(imsPair.first);
2491 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002492 final int subtypeCount = info.getSubtypeCount();
2493 for (int i = 0; i < subtypeCount; ++i) {
2494 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002495 for (String s: imsPair.second) {
2496 if (String.valueOf(ims.hashCode()).equals(s)) {
2497 enabledSubtypes.add(ims);
2498 }
satok67ddf9c2010-11-17 09:45:54 +09002499 }
2500 }
satok884ef9a2010-11-18 10:39:46 +09002501 break;
satok67ddf9c2010-11-17 09:45:54 +09002502 }
satok67ddf9c2010-11-17 09:45:54 +09002503 }
2504 }
2505 return enabledSubtypes;
2506 }
2507
satokd87c2592010-09-29 11:52:06 +09002508 // At the initial boot, the settings for input methods are not set,
2509 // so we need to enable IME in that case.
2510 public void enableAllIMEsIfThereIsNoEnabledIME() {
2511 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2512 StringBuilder sb = new StringBuilder();
2513 final int N = mMethodList.size();
2514 for (int i = 0; i < N; i++) {
2515 InputMethodInfo imi = mMethodList.get(i);
2516 Slog.i(TAG, "Adding: " + imi.getId());
2517 if (i > 0) sb.append(':');
2518 sb.append(imi.getId());
2519 }
2520 putEnabledInputMethodsStr(sb.toString());
2521 }
2522 }
2523
satokbb4aa062011-01-19 21:40:27 +09002524 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002525 ArrayList<Pair<String, ArrayList<String>>> imsList
2526 = new ArrayList<Pair<String, ArrayList<String>>>();
2527 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2528 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2529 return imsList;
2530 }
satok723a27e2010-11-11 14:58:11 +09002531 mInputMethodSplitter.setString(enabledInputMethodsStr);
2532 while (mInputMethodSplitter.hasNext()) {
2533 String nextImsStr = mInputMethodSplitter.next();
2534 mSubtypeSplitter.setString(nextImsStr);
2535 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002536 ArrayList<String> subtypeHashes = new ArrayList<String>();
2537 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002538 String imeId = mSubtypeSplitter.next();
2539 while (mSubtypeSplitter.hasNext()) {
2540 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002541 }
2542 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2543 }
2544 }
2545 return imsList;
2546 }
2547
2548 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2549 if (reloadInputMethodStr) {
2550 getEnabledInputMethodsStr();
2551 }
2552 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2553 // Add in the newly enabled input method.
2554 putEnabledInputMethodsStr(id);
2555 } else {
2556 putEnabledInputMethodsStr(
2557 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2558 }
2559 }
2560
2561 /**
2562 * Build and put a string of EnabledInputMethods with removing specified Id.
2563 * @return the specified id was removed or not.
2564 */
2565 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2566 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2567 boolean isRemoved = false;
2568 boolean needsAppendSeparator = false;
2569 for (Pair<String, ArrayList<String>> ims: imsList) {
2570 String curId = ims.first;
2571 if (curId.equals(id)) {
2572 // We are disabling this input method, and it is
2573 // currently enabled. Skip it to remove from the
2574 // new list.
2575 isRemoved = true;
2576 } else {
2577 if (needsAppendSeparator) {
2578 builder.append(INPUT_METHOD_SEPARATER);
2579 } else {
2580 needsAppendSeparator = true;
2581 }
2582 buildEnabledInputMethodsSettingString(builder, ims);
2583 }
2584 }
2585 if (isRemoved) {
2586 // Update the setting with the new list of input methods.
2587 putEnabledInputMethodsStr(builder.toString());
2588 }
2589 return isRemoved;
2590 }
2591
2592 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2593 List<Pair<String, ArrayList<String>>> imsList) {
2594 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2595 for (Pair<String, ArrayList<String>> ims: imsList) {
2596 InputMethodInfo info = mMethodMap.get(ims.first);
2597 if (info != null) {
2598 res.add(info);
2599 }
2600 }
2601 return res;
2602 }
2603
satok7f35c8c2010-10-07 21:13:11 +09002604 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002605 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002606 List<Pair<String, ArrayList<String>>> imsList) {
2607 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2608 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2609 for (Pair<String, ArrayList<String>> ims : imsList) {
2610 InputMethodInfo info = mMethodMap.get(ims.first);
2611 if (info != null) {
2612 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2613 }
2614 }
2615 return res;
2616 }
2617
satokd87c2592010-09-29 11:52:06 +09002618 private void putEnabledInputMethodsStr(String str) {
2619 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2620 mEnabledInputMethodsStrCache = str;
2621 }
2622
2623 private String getEnabledInputMethodsStr() {
2624 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2625 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002626 if (DEBUG) {
2627 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2628 }
satokd87c2592010-09-29 11:52:06 +09002629 return mEnabledInputMethodsStrCache;
2630 }
satok723a27e2010-11-11 14:58:11 +09002631
2632 private void saveSubtypeHistory(
2633 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2634 StringBuilder builder = new StringBuilder();
2635 boolean isImeAdded = false;
2636 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2637 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2638 newSubtypeId);
2639 isImeAdded = true;
2640 }
2641 for (Pair<String, String> ime: savedImes) {
2642 String imeId = ime.first;
2643 String subtypeId = ime.second;
2644 if (TextUtils.isEmpty(subtypeId)) {
2645 subtypeId = NOT_A_SUBTYPE_ID_STR;
2646 }
2647 if (isImeAdded) {
2648 builder.append(INPUT_METHOD_SEPARATER);
2649 } else {
2650 isImeAdded = true;
2651 }
2652 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2653 subtypeId);
2654 }
2655 // Remove the last INPUT_METHOD_SEPARATER
2656 putSubtypeHistoryStr(builder.toString());
2657 }
2658
2659 public void addSubtypeToHistory(String imeId, String subtypeId) {
2660 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2661 for (Pair<String, String> ime: subtypeHistory) {
2662 if (ime.first.equals(imeId)) {
2663 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002664 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002665 + ime.second);
2666 }
2667 // We should break here
2668 subtypeHistory.remove(ime);
2669 break;
2670 }
2671 }
satokbb4aa062011-01-19 21:40:27 +09002672 if (DEBUG) {
2673 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2674 }
satok723a27e2010-11-11 14:58:11 +09002675 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2676 }
2677
2678 private void putSubtypeHistoryStr(String str) {
2679 if (DEBUG) {
2680 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2681 }
2682 Settings.Secure.putString(
2683 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2684 }
2685
2686 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2687 // Gets the first one from the history
2688 return getLastSubtypeForInputMethodLockedInternal(null);
2689 }
2690
2691 public String getLastSubtypeForInputMethodLocked(String imeId) {
2692 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2693 if (ime != null) {
2694 return ime.second;
2695 } else {
2696 return null;
2697 }
2698 }
2699
2700 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2701 List<Pair<String, ArrayList<String>>> enabledImes =
2702 getEnabledInputMethodsAndSubtypeListLocked();
2703 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09002704 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09002705 final String imeInTheHistory = imeAndSubtype.first;
2706 // If imeId is empty, returns the first IME and subtype in the history
2707 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2708 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002709 final String subtypeHashCode =
2710 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2711 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002712 if (!TextUtils.isEmpty(subtypeHashCode)) {
2713 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002714 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002715 }
2716 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2717 }
2718 }
2719 }
2720 if (DEBUG) {
2721 Slog.d(TAG, "No enabled IME found in the history");
2722 }
2723 return null;
2724 }
2725
satokdf31ae62011-01-15 06:19:44 +09002726 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002727 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2728 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2729 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002730 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2731 if (explicitlyEnabledSubtypes.size() == 0) {
2732 // If there are no explicitly enabled subtypes, applicable subtypes are
2733 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002734 InputMethodInfo ime = mMethodMap.get(imeId);
2735 // If IME is enabled and no subtypes are enabled, applicable subtypes
2736 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002737 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002738 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002739 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002740 if (implicitlySelectedSubtypes != null) {
2741 final int N = implicitlySelectedSubtypes.size();
2742 for (int i = 0; i < N; ++i) {
2743 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2744 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2745 return subtypeHashCode;
2746 }
2747 }
2748 }
2749 }
2750 } else {
satokf6cafb62011-01-17 16:29:02 +09002751 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002752 if (s.equals(subtypeHashCode)) {
2753 // If both imeId and subtypeId are enabled, return subtypeId.
2754 return s;
2755 }
satok723a27e2010-11-11 14:58:11 +09002756 }
2757 }
2758 // If imeId was enabled but subtypeId was disabled.
2759 return NOT_A_SUBTYPE_ID_STR;
2760 }
2761 }
2762 // If both imeId and subtypeId are disabled, return null
2763 return null;
2764 }
2765
2766 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2767 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2768 final String subtypeHistoryStr = getSubtypeHistoryStr();
2769 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2770 return imsList;
2771 }
2772 mInputMethodSplitter.setString(subtypeHistoryStr);
2773 while (mInputMethodSplitter.hasNext()) {
2774 String nextImsStr = mInputMethodSplitter.next();
2775 mSubtypeSplitter.setString(nextImsStr);
2776 if (mSubtypeSplitter.hasNext()) {
2777 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2778 // The first element is ime id.
2779 String imeId = mSubtypeSplitter.next();
2780 while (mSubtypeSplitter.hasNext()) {
2781 subtypeId = mSubtypeSplitter.next();
2782 break;
2783 }
2784 imsList.add(new Pair<String, String>(imeId, subtypeId));
2785 }
2786 }
2787 return imsList;
2788 }
2789
2790 private String getSubtypeHistoryStr() {
2791 if (DEBUG) {
2792 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2793 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2794 }
2795 return Settings.Secure.getString(
2796 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2797 }
2798
2799 public void putSelectedInputMethod(String imeId) {
2800 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2801 }
2802
2803 public void putSelectedSubtype(int subtypeId) {
2804 Settings.Secure.putInt(
2805 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2806 }
satokd87c2592010-09-29 11:52:06 +09002807 }
2808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 @Override
2812 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2813 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2814 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2817 + Binder.getCallingPid()
2818 + ", uid=" + Binder.getCallingUid());
2819 return;
2820 }
2821
2822 IInputMethod method;
2823 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 synchronized (mMethodMap) {
2828 p.println("Current Input Method Manager state:");
2829 int N = mMethodList.size();
2830 p.println(" Input Methods:");
2831 for (int i=0; i<N; i++) {
2832 InputMethodInfo info = mMethodList.get(i);
2833 p.println(" InputMethod #" + i + ":");
2834 info.dump(p, " ");
2835 }
2836 p.println(" Clients:");
2837 for (ClientState ci : mClients.values()) {
2838 p.println(" Client " + ci + ":");
2839 p.println(" client=" + ci.client);
2840 p.println(" inputContext=" + ci.inputContext);
2841 p.println(" sessionRequested=" + ci.sessionRequested);
2842 p.println(" curSession=" + ci.curSession);
2843 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002844 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002846 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2847 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2849 + " mBoundToMethod=" + mBoundToMethod);
2850 p.println(" mCurToken=" + mCurToken);
2851 p.println(" mCurIntent=" + mCurIntent);
2852 method = mCurMethod;
2853 p.println(" mCurMethod=" + mCurMethod);
2854 p.println(" mEnabledSession=" + mEnabledSession);
2855 p.println(" mShowRequested=" + mShowRequested
2856 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2857 + " mShowForced=" + mShowForced
2858 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002859 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002861
Jeff Brownb88102f2010-09-08 11:49:43 -07002862 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 pw.flush();
2865 try {
2866 client.client.asBinder().dump(fd, args);
2867 } catch (RemoteException e) {
2868 p.println("Input method client dead: " + e);
2869 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002870 } else {
2871 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002873
Jeff Brownb88102f2010-09-08 11:49:43 -07002874 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 pw.flush();
2877 try {
2878 method.asBinder().dump(fd, args);
2879 } catch (RemoteException e) {
2880 p.println("Input method service dead: " + e);
2881 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002882 } else {
2883 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 }
2885 }
2886}