blob: f4308cdd4a45c4f601fee0351c505587aae13f72 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800130 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900132 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700134 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 final IWindowManager mIWindowManager;
136 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 // All known input methods. mMethodMap also serves as the global
141 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900142 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
143 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900144 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
145 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 class SessionState {
148 final ClientState client;
149 final IInputMethod method;
150 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 @Override
153 public String toString() {
154 return "SessionState{uid " + client.uid + " pid " + client.pid
155 + " method " + Integer.toHexString(
156 System.identityHashCode(method))
157 + " session " + Integer.toHexString(
158 System.identityHashCode(session))
159 + "}";
160 }
161
162 SessionState(ClientState _client, IInputMethod _method,
163 IInputMethodSession _session) {
164 client = _client;
165 method = _method;
166 session = _session;
167 }
168 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 class ClientState {
171 final IInputMethodClient client;
172 final IInputContext inputContext;
173 final int uid;
174 final int pid;
175 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 boolean sessionRequested;
178 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 @Override
181 public String toString() {
182 return "ClientState{" + Integer.toHexString(
183 System.identityHashCode(this)) + " uid " + uid
184 + " pid " + pid + "}";
185 }
186
187 ClientState(IInputMethodClient _client, IInputContext _inputContext,
188 int _uid, int _pid) {
189 client = _client;
190 inputContext = _inputContext;
191 uid = _uid;
192 pid = _pid;
193 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
194 }
195 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 final HashMap<IBinder, ClientState> mClients
198 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700201 * Set once the system is ready to run third party code.
202 */
203 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800204
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700205 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 * Id of the currently selected input method.
207 */
208 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 /**
211 * The current binding sequence number, incremented every time there is
212 * a new bind performed.
213 */
214 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 /**
217 * The client that is currently bound to an input method.
218 */
219 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700222 * The last window token that gained focus.
223 */
224 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800225
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700226 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 * The input context last provided by the current client.
228 */
229 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 /**
232 * The attributes last provided by the current client.
233 */
234 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 /**
237 * The input method ID of the input method service that we are currently
238 * connected to or in the process of connecting to.
239 */
240 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 /**
satokab751aa2010-09-14 19:17:36 +0900243 * The current subtype of the current input method.
244 */
245 private InputMethodSubtype mCurrentSubtype;
246
satok4e4569d2010-11-19 18:45:53 +0900247 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900248 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
249 mShortcutInputMethodsAndSubtypes =
250 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900251
252 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 * Set to true if our ServiceConnection is currently actively bound to
254 * a service (whether or not we have gotten its IBinder back yet).
255 */
256 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 /**
259 * Set if the client has asked for the input method to be shown.
260 */
261 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 /**
264 * Set if we were explicitly told to show the input method.
265 */
266 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 /**
269 * Set if we were forced to be shown.
270 */
271 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 /**
274 * Set if we last told the input method to show itself.
275 */
276 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
279 * The Intent used to connect to the current input method.
280 */
281 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 /**
284 * The token we have made for the currently active input method, to
285 * identify it in the future.
286 */
287 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 /**
290 * If non-null, this is the input method service we are currently connected
291 * to.
292 */
293 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 /**
296 * Time that we last initiated a bind to the input method, to determine
297 * if we should try to disconnect and reconnect to it.
298 */
299 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 /**
302 * Have we called mCurMethod.bindInput()?
303 */
304 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 /**
307 * Currently enabled session. Only touched by service thread, not
308 * protected by a lock.
309 */
310 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 /**
313 * True if the screen is on. The value is true initially.
314 */
315 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
Joe Onorato857fd9b2011-01-27 15:08:35 -0800317 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
318 int mImeWindowVis;
319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 AlertDialog.Builder mDialogBuilder;
321 AlertDialog mSwitchingDialog;
322 InputMethodInfo[] mIms;
323 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900324 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 class SettingsObserver extends ContentObserver {
327 SettingsObserver(Handler handler) {
328 super(handler);
329 ContentResolver resolver = mContext.getContentResolver();
330 resolver.registerContentObserver(Settings.Secure.getUriFor(
331 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900332 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900333 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
334 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900335 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 @Override public void onChange(boolean selfChange) {
339 synchronized (mMethodMap) {
340 updateFromSettingsLocked();
341 }
342 }
343 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
346 @Override
347 public void onReceive(Context context, Intent intent) {
348 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
349 mScreenOn = true;
350 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
351 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700352 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
353 hideInputMethodMenu();
354 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800356 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 }
358
359 // Inform the current client of the change in active status
360 try {
361 if (mCurClient != null && mCurClient.client != null) {
362 mCurClient.client.setActive(mScreenOn);
363 }
364 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800365 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 + mCurClient.pid + " uid " + mCurClient.uid);
367 }
368 }
369 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800371 class MyPackageMonitor extends PackageMonitor {
372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800374 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800376 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
378 final int N = mMethodList.size();
379 if (curInputMethodId != null) {
380 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800381 InputMethodInfo imi = mMethodList.get(i);
382 if (imi.getId().equals(curInputMethodId)) {
383 for (String pkg : packages) {
384 if (imi.getPackageName().equals(pkg)) {
385 if (!doit) {
386 return true;
387 }
satok723a27e2010-11-11 14:58:11 +0900388 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800389 chooseNewDefaultIMELocked();
390 return true;
391 }
392 }
393 }
394 }
395 }
396 }
397 return false;
398 }
399
400 @Override
401 public void onSomePackagesChanged() {
402 synchronized (mMethodMap) {
403 InputMethodInfo curIm = null;
404 String curInputMethodId = Settings.Secure.getString(mContext
405 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
406 final int N = mMethodList.size();
407 if (curInputMethodId != null) {
408 for (int i=0; i<N; i++) {
409 InputMethodInfo imi = mMethodList.get(i);
410 if (imi.getId().equals(curInputMethodId)) {
411 curIm = imi;
412 }
413 int change = isPackageDisappearing(imi.getPackageName());
414 if (change == PACKAGE_TEMPORARY_CHANGE
415 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800416 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800417 + imi.getComponent());
418 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 }
420 }
421 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800422
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800423 buildInputMethodListLocked(mMethodList, mMethodMap);
424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800426
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800427 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800428 int change = isPackageDisappearing(curIm.getPackageName());
429 if (change == PACKAGE_TEMPORARY_CHANGE
430 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800431 ServiceInfo si = null;
432 try {
433 si = mContext.getPackageManager().getServiceInfo(
434 curIm.getComponent(), 0);
435 } catch (PackageManager.NameNotFoundException ex) {
436 }
437 if (si == null) {
438 // Uh oh, current input method is no longer around!
439 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800440 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800441 mImeWindowVis = 0;
442 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
443 mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800444 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800445 changed = true;
446 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800447 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900448 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800449 }
450 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800451 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800452 }
satokab751aa2010-09-14 19:17:36 +0900453
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800454 if (curIm == null) {
455 // We currently don't have a default input method... is
456 // one now available?
457 changed = chooseNewDefaultIMELocked();
458 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800459
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800460 if (changed) {
461 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 }
463 }
464 }
465 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 class MethodCallback extends IInputMethodCallback.Stub {
468 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 MethodCallback(IInputMethod method) {
471 mMethod = method;
472 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 public void finishedEvent(int seq, boolean handled) throws RemoteException {
475 }
476
477 public void sessionCreated(IInputMethodSession session) throws RemoteException {
478 onSessionCreated(mMethod, session);
479 }
480 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800481
Joe Onorato089de882010-04-12 08:18:45 -0700482 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800484 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 mHandler = new Handler(this);
486 mIWindowManager = IWindowManager.Stub.asInterface(
487 ServiceManager.getService(Context.WINDOW_SERVICE));
488 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
489 public void executeMessage(Message msg) {
490 handleMessage(msg);
491 }
492 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800493
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800494 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 IntentFilter screenOnOffFilt = new IntentFilter();
497 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
498 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700499 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800501
satok913a8922010-08-26 21:53:41 +0900502 mStatusBar = statusBar;
503 statusBar.setIconVisibility("ime", false);
504
satokd87c2592010-09-29 11:52:06 +0900505 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900506 mSettings = new InputMethodSettings(
507 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900509 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510
satokd87c2592010-09-29 11:52:06 +0900511 if (TextUtils.isEmpty(Settings.Secure.getString(
512 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900514 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
516 try {
satokd87c2592010-09-29 11:52:06 +0900517 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 imi.getPackageName(), 0).getResources();
519 if (res.getBoolean(imi.getIsDefaultResourceId())) {
520 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800521 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 }
523 } catch (PackageManager.NameNotFoundException ex) {
524 } catch (Resources.NotFoundException ex) {
525 }
526 }
527 }
satokd87c2592010-09-29 11:52:06 +0900528 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800530 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900533 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 }
535 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 mSettingsObserver = new SettingsObserver(mHandler);
538 updateFromSettingsLocked();
539 }
540
541 @Override
542 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
543 throws RemoteException {
544 try {
545 return super.onTransact(code, data, reply, flags);
546 } catch (RuntimeException e) {
547 // The input method manager only throws security exceptions, so let's
548 // log all others.
549 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800550 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 }
552 throw e;
553 }
554 }
555
556 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700557 synchronized (mMethodMap) {
558 if (!mSystemReady) {
559 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700560 try {
561 startInputInnerLocked();
562 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800563 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700564 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700565 }
566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 public List<InputMethodInfo> getInputMethodList() {
570 synchronized (mMethodMap) {
571 return new ArrayList<InputMethodInfo>(mMethodList);
572 }
573 }
574
575 public List<InputMethodInfo> getEnabledInputMethodList() {
576 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900577 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
579 }
580
satokbb4aa062011-01-19 21:40:27 +0900581 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
582 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
583 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
584 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
585 for (InputMethodInfo imi: getEnabledInputMethodList()) {
586 enabledInputMethodAndSubtypes.put(
587 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
588 }
589 return enabledInputMethodAndSubtypes;
590 }
591
592 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
593 boolean allowsImplicitlySelectedSubtypes) {
594 if (imi == null && mCurMethodId != null) {
595 imi = mMethodMap.get(mCurMethodId);
596 }
satok7265d9b2011-02-14 15:47:30 +0900597 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900598 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900599 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
600 enabledSubtypes = getApplicableSubtypesLocked(mRes, getSubtypes(imi));
satokbb4aa062011-01-19 21:40:27 +0900601 }
satok7265d9b2011-02-14 15:47:30 +0900602 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900603 }
604
satok16331c82010-12-20 23:48:46 +0900605 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
606 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900607 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900608 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900609 }
610 }
611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 public void addClient(IInputMethodClient client,
613 IInputContext inputContext, int uid, int pid) {
614 synchronized (mMethodMap) {
615 mClients.put(client.asBinder(), new ClientState(client,
616 inputContext, uid, pid));
617 }
618 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 public void removeClient(IInputMethodClient client) {
621 synchronized (mMethodMap) {
622 mClients.remove(client.asBinder());
623 }
624 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 void executeOrSendMessage(IInterface target, Message msg) {
627 if (target.asBinder() instanceof Binder) {
628 mCaller.sendMessage(msg);
629 } else {
630 handleMessage(msg);
631 msg.recycle();
632 }
633 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800634
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700635 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800637 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 + mCurClient.client.asBinder());
639 if (mBoundToMethod) {
640 mBoundToMethod = false;
641 if (mCurMethod != null) {
642 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
643 MSG_UNBIND_INPUT, mCurMethod));
644 }
645 }
646 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
647 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
648 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 // Call setActive(false) on the old client
651 try {
652 mCurClient.client.setActive(false);
653 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800654 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 + mCurClient.pid + " uid " + mCurClient.uid);
656 }
657 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800658
The Android Open Source Project10592532009-03-18 17:39:46 -0700659 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 }
661 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 private int getImeShowFlags() {
664 int flags = 0;
665 if (mShowForced) {
666 flags |= InputMethod.SHOW_FORCED
667 | InputMethod.SHOW_EXPLICIT;
668 } else if (mShowExplicitlyRequested) {
669 flags |= InputMethod.SHOW_EXPLICIT;
670 }
671 return flags;
672 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 private int getAppShowFlags() {
675 int flags = 0;
676 if (mShowForced) {
677 flags |= InputMethodManager.SHOW_FORCED;
678 } else if (!mShowExplicitlyRequested) {
679 flags |= InputMethodManager.SHOW_IMPLICIT;
680 }
681 return flags;
682 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
685 if (!mBoundToMethod) {
686 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
687 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
688 mBoundToMethod = true;
689 }
690 final SessionState session = mCurClient.curSession;
691 if (initial) {
692 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
693 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
694 } else {
695 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
696 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
697 }
698 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800699 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800700 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 }
702 return needResult
703 ? new InputBindResult(session.session, mCurId, mCurSeq)
704 : null;
705 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 InputBindResult startInputLocked(IInputMethodClient client,
708 IInputContext inputContext, EditorInfo attribute,
709 boolean initial, boolean needResult) {
710 // If no method is currently selected, do nothing.
711 if (mCurMethodId == null) {
712 return mNoBinding;
713 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 ClientState cs = mClients.get(client.asBinder());
716 if (cs == null) {
717 throw new IllegalArgumentException("unknown client "
718 + client.asBinder());
719 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 try {
722 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
723 // Check with the window manager to make sure this client actually
724 // has a window with focus. If not, reject. This is thread safe
725 // because if the focus changes some time before or after, the
726 // next client receiving focus that has any interest in input will
727 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800728 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
730 return null;
731 }
732 } catch (RemoteException e) {
733 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 if (mCurClient != cs) {
736 // If the client is changing, we need to switch over to the new
737 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700738 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800739 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 + cs.client.asBinder());
741
742 // If the screen is on, inform the new client it is active
743 if (mScreenOn) {
744 try {
745 cs.client.setActive(mScreenOn);
746 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800747 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 + cs.pid + " uid " + cs.uid);
749 }
750 }
751 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 // Bump up the sequence for this client and attach it.
754 mCurSeq++;
755 if (mCurSeq <= 0) mCurSeq = 1;
756 mCurClient = cs;
757 mCurInputContext = inputContext;
758 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 // Check if the input method is changing.
761 if (mCurId != null && mCurId.equals(mCurMethodId)) {
762 if (cs.curSession != null) {
763 // Fast case: if we are already connected to the input method,
764 // then just return it.
765 return attachNewInputLocked(initial, needResult);
766 }
767 if (mHaveConnection) {
768 if (mCurMethod != null) {
769 if (!cs.sessionRequested) {
770 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800771 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
773 MSG_CREATE_SESSION, mCurMethod,
774 new MethodCallback(mCurMethod)));
775 }
776 // Return to client, and we will get back with it when
777 // we have had a session made for it.
778 return new InputBindResult(null, mCurId, mCurSeq);
779 } else if (SystemClock.uptimeMillis()
780 < (mLastBindTime+TIME_TO_RECONNECT)) {
781 // In this case we have connected to the service, but
782 // don't yet have its interface. If it hasn't been too
783 // long since we did the connection, we'll return to
784 // the client and wait to get the service interface so
785 // we can report back. If it has been too long, we want
786 // to fall through so we can try a disconnect/reconnect
787 // to see if we can get back in touch with the service.
788 return new InputBindResult(null, mCurId, mCurSeq);
789 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800790 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
791 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 }
793 }
794 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800795
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700796 return startInputInnerLocked();
797 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800798
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700799 InputBindResult startInputInnerLocked() {
800 if (mCurMethodId == null) {
801 return mNoBinding;
802 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800803
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700804 if (!mSystemReady) {
805 // If the system is not yet ready, we shouldn't be running third
806 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700807 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700808 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 InputMethodInfo info = mMethodMap.get(mCurMethodId);
811 if (info == null) {
812 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
813 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800814
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700815 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
818 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700819 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
820 com.android.internal.R.string.input_method_binding_label);
821 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
822 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
824 mLastBindTime = SystemClock.uptimeMillis();
825 mHaveConnection = true;
826 mCurId = info.getId();
827 mCurToken = new Binder();
828 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800829 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 mIWindowManager.addWindowToken(mCurToken,
831 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
832 } catch (RemoteException e) {
833 }
834 return new InputBindResult(null, mCurId, mCurSeq);
835 } else {
836 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800837 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 + mCurIntent);
839 }
840 return null;
841 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 public InputBindResult startInput(IInputMethodClient client,
844 IInputContext inputContext, EditorInfo attribute,
845 boolean initial, boolean needResult) {
846 synchronized (mMethodMap) {
847 final long ident = Binder.clearCallingIdentity();
848 try {
849 return startInputLocked(client, inputContext, attribute,
850 initial, needResult);
851 } finally {
852 Binder.restoreCallingIdentity(ident);
853 }
854 }
855 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 public void finishInput(IInputMethodClient client) {
858 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 public void onServiceConnected(ComponentName name, IBinder service) {
861 synchronized (mMethodMap) {
862 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
863 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700864 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800865 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700866 unbindCurrentMethodLocked(false);
867 return;
868 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800869 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700870 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
871 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800873 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700874 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700876 MSG_CREATE_SESSION, mCurMethod,
877 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 }
879 }
880 }
881 }
882
883 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
884 synchronized (mMethodMap) {
885 if (mCurMethod != null && method != null
886 && mCurMethod.asBinder() == method.asBinder()) {
887 if (mCurClient != null) {
888 mCurClient.curSession = new SessionState(mCurClient,
889 method, session);
890 mCurClient.sessionRequested = false;
891 InputBindResult res = attachNewInputLocked(true, true);
892 if (res.method != null) {
893 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
894 MSG_BIND_METHOD, mCurClient.client, res));
895 }
896 }
897 }
898 }
899 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800900
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700901 void unbindCurrentMethodLocked(boolean reportToClient) {
902 if (mHaveConnection) {
903 mContext.unbindService(this);
904 mHaveConnection = false;
905 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800906
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700907 if (mCurToken != null) {
908 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800909 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700910 mIWindowManager.removeWindowToken(mCurToken);
911 } catch (RemoteException e) {
912 }
913 mCurToken = null;
914 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800915
The Android Open Source Project10592532009-03-18 17:39:46 -0700916 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700917 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800918
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700919 if (reportToClient && mCurClient != null) {
920 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
921 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
922 }
923 }
924
Devin Taylor0c33ed22010-02-23 13:26:46 -0600925 private void finishSession(SessionState sessionState) {
926 if (sessionState != null && sessionState.session != null) {
927 try {
928 sessionState.session.finishSession();
929 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700930 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600931 }
932 }
933 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800934
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700935 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 if (mCurMethod != null) {
937 for (ClientState cs : mClients.values()) {
938 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600939 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 cs.curSession = null;
941 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600942
943 finishSession(mEnabledSession);
944 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 mCurMethod = null;
946 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700947 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 public void onServiceDisconnected(ComponentName name) {
951 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800952 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 + " mCurIntent=" + mCurIntent);
954 if (mCurMethod != null && mCurIntent != null
955 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700956 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 // We consider this to be a new bind attempt, since the system
958 // should now try to restart the service for us.
959 mLastBindTime = SystemClock.uptimeMillis();
960 mShowRequested = mInputShown;
961 mInputShown = false;
962 if (mCurClient != null) {
963 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
964 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
965 }
966 }
967 }
968 }
969
satokf9f01002011-05-19 21:31:50 +0900970 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700972 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 long ident = Binder.clearCallingIdentity();
974 try {
975 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700976 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 return;
978 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 synchronized (mMethodMap) {
981 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800982 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700983 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800985 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700986 mStatusBar.setIcon("ime", packageName, iconId, 0);
987 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 }
989 }
990 } finally {
991 Binder.restoreCallingIdentity(ident);
992 }
993 }
994
satokf9f01002011-05-19 21:31:50 +0900995 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -0800996 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +0900997 int uid = Binder.getCallingUid();
998 long ident = Binder.clearCallingIdentity();
999 try {
1000 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001001 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001002 return;
1003 }
1004
1005 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001006 mImeWindowVis = vis;
1007 mBackDisposition = backDisposition;
1008 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
satok06487a52010-10-29 11:37:18 +09001009 }
1010 } finally {
1011 Binder.restoreCallingIdentity(ident);
1012 }
1013 }
1014
satokf9f01002011-05-19 21:31:50 +09001015 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1016 synchronized (mMethodMap) {
1017 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1018 for (int i = 0; i < spans.length; ++i) {
1019 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001020 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001021 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001022 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001023 }
1024 }
1025 }
1026 }
1027
1028 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1029 synchronized (mMethodMap) {
1030 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1031 // TODO: Do not send the intent if the process of the targetImi is already dead.
1032 if (targetImi != null) {
1033 final String[] suggestions = span.getSuggestions();
1034 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001035 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001036 final Intent intent = new Intent();
1037 // Ensures that only a class in the original IME package will receive the
1038 // notification.
satok42c5a162011-05-26 16:46:14 +09001039 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001040 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1041 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1042 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1043 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1044 mContext.sendBroadcast(intent);
1045 return true;
1046 }
1047 }
1048 return false;
1049 }
1050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001052 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1053 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1054 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1055 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001057 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001058 // There is no input method selected, try to choose new applicable input method.
1059 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1060 id = Settings.Secure.getString(mContext.getContentResolver(),
1061 Settings.Secure.DEFAULT_INPUT_METHOD);
1062 }
1063 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 try {
satokab751aa2010-09-14 19:17:36 +09001065 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001067 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001068 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001069 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 }
satokf3db1af2010-11-23 13:34:33 +09001071 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001072 } else {
1073 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001074 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001075 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 }
1077 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001078
satokab751aa2010-09-14 19:17:36 +09001079 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 InputMethodInfo info = mMethodMap.get(id);
1081 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001082 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001086 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001087 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1088 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001089 }
1090 if (subtype != mCurrentSubtype) {
1091 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001092 if (subtype != null) {
1093 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1094 }
satokcd7cd292010-11-20 15:46:23 +09001095 if (mCurMethod != null) {
1096 try {
satoke40dea02011-01-30 01:14:02 +09001097 final Configuration conf = mRes.getConfiguration();
1098 final boolean haveHardKeyboard = conf.keyboard
1099 != Configuration.KEYBOARD_NOKEYS;
1100 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001101 && conf.hardKeyboardHidden
1102 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001103 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1104 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1105 : 0;
Joe Onorato857fd9b2011-01-27 15:08:35 -08001106 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1107 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001108 // If subtype is null, try to find the most applicable one from
1109 // getCurrentInputMethodSubtype.
1110 if (subtype == null) {
1111 subtype = getCurrentInputMethodSubtype();
1112 }
1113 mCurMethod.changeInputMethodSubtype(subtype);
1114 } catch (RemoteException e) {
1115 return;
satokab751aa2010-09-14 19:17:36 +09001116 }
1117 }
1118 }
1119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 return;
1121 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 final long ident = Binder.clearCallingIdentity();
1124 try {
satokab751aa2010-09-14 19:17:36 +09001125 // Set a subtype to this input method.
1126 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001127 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1128 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1129 // because mCurMethodId is stored as a history in
1130 // setSelectedInputMethodAndSubtypeLocked().
1131 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132
1133 if (ActivityManagerNative.isSystemReady()) {
1134 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001135 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 intent.putExtra("input_method_id", id);
1137 mContext.sendBroadcast(intent);
1138 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001139 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 } finally {
1141 Binder.restoreCallingIdentity(ident);
1142 }
1143 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001144
satok42c5a162011-05-26 16:46:14 +09001145 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001146 public boolean showSoftInput(IInputMethodClient client, int flags,
1147 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001148 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 long ident = Binder.clearCallingIdentity();
1150 try {
1151 synchronized (mMethodMap) {
1152 if (mCurClient == null || client == null
1153 || mCurClient.client.asBinder() != client.asBinder()) {
1154 try {
1155 // We need to check if this is the current client with
1156 // focus in the window manager, to allow this call to
1157 // be made before input is started in it.
1158 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001159 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001160 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 }
1162 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001163 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 }
1165 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001166
Joe Onorato8a9b2202010-02-26 18:56:32 -08001167 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001168 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 }
1170 } finally {
1171 Binder.restoreCallingIdentity(ident);
1172 }
1173 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001174
The Android Open Source Project4df24232009-03-05 14:34:35 -08001175 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 mShowRequested = true;
1177 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1178 mShowExplicitlyRequested = true;
1179 }
1180 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1181 mShowExplicitlyRequested = true;
1182 mShowForced = true;
1183 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001184
Dianne Hackborncc278702009-09-02 23:07:23 -07001185 if (!mSystemReady) {
1186 return false;
1187 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001188
The Android Open Source Project4df24232009-03-05 14:34:35 -08001189 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001191 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1192 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1193 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001195 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 } else if (mHaveConnection && SystemClock.uptimeMillis()
1197 < (mLastBindTime+TIME_TO_RECONNECT)) {
1198 // The client has asked to have the input method shown, but
1199 // we have been sitting here too long with a connection to the
1200 // service and no interface received, so let's disconnect/connect
1201 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001202 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 SystemClock.uptimeMillis()-mLastBindTime,1);
1204 mContext.unbindService(this);
1205 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1206 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001207
The Android Open Source Project4df24232009-03-05 14:34:35 -08001208 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001210
satok42c5a162011-05-26 16:46:14 +09001211 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001212 public boolean hideSoftInput(IInputMethodClient client, int flags,
1213 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001214 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 long ident = Binder.clearCallingIdentity();
1216 try {
1217 synchronized (mMethodMap) {
1218 if (mCurClient == null || client == null
1219 || mCurClient.client.asBinder() != client.asBinder()) {
1220 try {
1221 // We need to check if this is the current client with
1222 // focus in the window manager, to allow this call to
1223 // be made before input is started in it.
1224 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001225 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1226 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001227 mImeWindowVis = 0;
1228 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1229 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001230 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 }
1232 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001233 mImeWindowVis = 0;
1234 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001235 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 }
1237 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001238
Joe Onorato8a9b2202010-02-26 18:56:32 -08001239 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001240 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 }
1242 } finally {
1243 Binder.restoreCallingIdentity(ident);
1244 }
1245 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001246
The Android Open Source Project4df24232009-03-05 14:34:35 -08001247 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1249 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001250 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001252 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 }
1254 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001255 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001257 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001259 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001261 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1262 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1263 res = true;
1264 } else {
1265 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 }
1267 mInputShown = false;
1268 mShowRequested = false;
1269 mShowExplicitlyRequested = false;
1270 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001271 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001273
satok42c5a162011-05-26 16:46:14 +09001274 @Override
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
satok42c5a162011-05-26 16:46:14 +09001376 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1378 synchronized (mMethodMap) {
1379 if (mCurClient == null || client == null
1380 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001381 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001382 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 }
1384
satok440aab52010-11-25 09:43:11 +09001385 // Always call subtype picker, because subtype picker is a superset of input method
1386 // picker.
satokab751aa2010-09-14 19:17:36 +09001387 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1388 }
1389 }
1390
satok42c5a162011-05-26 16:46:14 +09001391 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001393 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1394 }
1395
satok42c5a162011-05-26 16:46:14 +09001396 @Override
satok28203512010-11-24 11:06:49 +09001397 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1398 synchronized (mMethodMap) {
1399 if (subtype != null) {
1400 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1401 mMethodMap.get(id), subtype.hashCode()));
1402 } else {
1403 setInputMethod(token, id);
1404 }
1405 }
satokab751aa2010-09-14 19:17:36 +09001406 }
1407
satok42c5a162011-05-26 16:46:14 +09001408 @Override
satokb416a712010-11-25 20:42:14 +09001409 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001410 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001411 synchronized (mMethodMap) {
1412 if (mCurClient == null || client == null
1413 || mCurClient.client.asBinder() != client.asBinder()) {
1414 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1415 }
satok7fee71f2010-12-17 18:54:26 +09001416 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1417 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001418 }
1419 }
1420
satok4fc87d62011-05-20 16:13:43 +09001421 @Override
satok735cf382010-11-11 20:40:09 +09001422 public boolean switchToLastInputMethod(IBinder token) {
1423 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001424 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001425 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001426 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001427 lastImi = mMethodMap.get(lastIme.first);
1428 } else {
1429 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001430 }
satok4fc87d62011-05-20 16:13:43 +09001431 String targetLastImiId = null;
1432 int subtypeId = NOT_A_SUBTYPE_ID;
1433 if (lastIme != null && lastImi != null) {
1434 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1435 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1436 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1437 : mCurrentSubtype.hashCode();
1438 // If the last IME is the same as the current IME and the last subtype is not
1439 // defined, there is no need to switch to the last IME.
1440 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1441 targetLastImiId = lastIme.first;
1442 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1443 }
1444 }
1445
1446 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1447 // This is a safety net. If the currentSubtype can't be added to the history
1448 // and the framework couldn't find the last ime, we will make the last ime be
1449 // the most applicable enabled keyboard subtype of the system imes.
1450 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1451 if (enabled != null) {
1452 final int N = enabled.size();
1453 final String locale = mCurrentSubtype == null
1454 ? mRes.getConfiguration().locale.toString()
1455 : mCurrentSubtype.getLocale();
1456 for (int i = 0; i < N; ++i) {
1457 final InputMethodInfo imi = enabled.get(i);
1458 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1459 InputMethodSubtype keyboardSubtype =
1460 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1461 SUBTYPE_MODE_KEYBOARD, locale, true);
1462 if (keyboardSubtype != null) {
1463 targetLastImiId = imi.getId();
1464 subtypeId = getSubtypeIdFromHashCode(
1465 imi, keyboardSubtype.hashCode());
1466 if(keyboardSubtype.getLocale().equals(locale)) {
1467 break;
1468 }
1469 }
1470 }
1471 }
1472 }
1473 }
1474
1475 if (!TextUtils.isEmpty(targetLastImiId)) {
1476 if (DEBUG) {
1477 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1478 + ", from: " + mCurMethodId + ", " + subtypeId);
1479 }
1480 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1481 return true;
1482 } else {
1483 return false;
1484 }
satok735cf382010-11-11 20:40:09 +09001485 }
1486 }
1487
satok68f1b782011-04-11 14:26:04 +09001488 public InputMethodSubtype getLastInputMethodSubtype() {
1489 synchronized (mMethodMap) {
1490 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1491 // TODO: Handle the case of the last IME with no subtypes
1492 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1493 || TextUtils.isEmpty(lastIme.second)) return null;
1494 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1495 if (lastImi == null) return null;
1496 try {
1497 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1498 return lastImi.getSubtypeAt(getSubtypeIdFromHashCode(
1499 lastImi, lastSubtypeHash));
1500 } catch (NumberFormatException e) {
1501 return null;
1502 }
1503 }
1504 }
1505
satok28203512010-11-24 11:06:49 +09001506 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 synchronized (mMethodMap) {
1508 if (token == null) {
1509 if (mContext.checkCallingOrSelfPermission(
1510 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1511 != PackageManager.PERMISSION_GRANTED) {
1512 throw new SecurityException(
1513 "Using null token requires permission "
1514 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1515 }
1516 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001517 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1518 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 return;
1520 }
1521
1522 long ident = Binder.clearCallingIdentity();
1523 try {
satokab751aa2010-09-14 19:17:36 +09001524 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 } finally {
1526 Binder.restoreCallingIdentity(ident);
1527 }
1528 }
1529 }
1530
satok42c5a162011-05-26 16:46:14 +09001531 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 public void hideMySoftInput(IBinder token, int flags) {
1533 synchronized (mMethodMap) {
1534 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001535 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1536 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 return;
1538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 long ident = Binder.clearCallingIdentity();
1540 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001541 hideCurrentInputLocked(flags, null);
1542 } finally {
1543 Binder.restoreCallingIdentity(ident);
1544 }
1545 }
1546 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001547
satok42c5a162011-05-26 16:46:14 +09001548 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001549 public void showMySoftInput(IBinder token, int flags) {
1550 synchronized (mMethodMap) {
1551 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001552 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1553 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001554 return;
1555 }
1556 long ident = Binder.clearCallingIdentity();
1557 try {
1558 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 } finally {
1560 Binder.restoreCallingIdentity(ident);
1561 }
1562 }
1563 }
1564
1565 void setEnabledSessionInMainThread(SessionState session) {
1566 if (mEnabledSession != session) {
1567 if (mEnabledSession != null) {
1568 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001569 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 mEnabledSession.method.setSessionEnabled(
1571 mEnabledSession.session, false);
1572 } catch (RemoteException e) {
1573 }
1574 }
1575 mEnabledSession = session;
1576 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001577 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 session.method.setSessionEnabled(
1579 session.session, true);
1580 } catch (RemoteException e) {
1581 }
1582 }
1583 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001584
satok42c5a162011-05-26 16:46:14 +09001585 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 public boolean handleMessage(Message msg) {
1587 HandlerCaller.SomeArgs args;
1588 switch (msg.what) {
1589 case MSG_SHOW_IM_PICKER:
1590 showInputMethodMenu();
1591 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001592
satokab751aa2010-09-14 19:17:36 +09001593 case MSG_SHOW_IM_SUBTYPE_PICKER:
1594 showInputMethodSubtypeMenu();
1595 return true;
1596
satok47a44912010-10-06 16:03:58 +09001597 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001598 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001599 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001600 return true;
1601
1602 case MSG_SHOW_IM_CONFIG:
1603 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001604 return true;
1605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 case MSG_UNBIND_INPUT:
1609 try {
1610 ((IInputMethod)msg.obj).unbindInput();
1611 } catch (RemoteException e) {
1612 // There is nothing interesting about the method dying.
1613 }
1614 return true;
1615 case MSG_BIND_INPUT:
1616 args = (HandlerCaller.SomeArgs)msg.obj;
1617 try {
1618 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1619 } catch (RemoteException e) {
1620 }
1621 return true;
1622 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001623 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001625 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1626 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 } catch (RemoteException e) {
1628 }
1629 return true;
1630 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001631 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001633 ((IInputMethod)args.arg1).hideSoftInput(0,
1634 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 } catch (RemoteException e) {
1636 }
1637 return true;
1638 case MSG_ATTACH_TOKEN:
1639 args = (HandlerCaller.SomeArgs)msg.obj;
1640 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001641 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1643 } catch (RemoteException e) {
1644 }
1645 return true;
1646 case MSG_CREATE_SESSION:
1647 args = (HandlerCaller.SomeArgs)msg.obj;
1648 try {
1649 ((IInputMethod)args.arg1).createSession(
1650 (IInputMethodCallback)args.arg2);
1651 } catch (RemoteException e) {
1652 }
1653 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 case MSG_START_INPUT:
1657 args = (HandlerCaller.SomeArgs)msg.obj;
1658 try {
1659 SessionState session = (SessionState)args.arg1;
1660 setEnabledSessionInMainThread(session);
1661 session.method.startInput((IInputContext)args.arg2,
1662 (EditorInfo)args.arg3);
1663 } catch (RemoteException e) {
1664 }
1665 return true;
1666 case MSG_RESTART_INPUT:
1667 args = (HandlerCaller.SomeArgs)msg.obj;
1668 try {
1669 SessionState session = (SessionState)args.arg1;
1670 setEnabledSessionInMainThread(session);
1671 session.method.restartInput((IInputContext)args.arg2,
1672 (EditorInfo)args.arg3);
1673 } catch (RemoteException e) {
1674 }
1675 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 case MSG_UNBIND_METHOD:
1680 try {
1681 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1682 } catch (RemoteException e) {
1683 // There is nothing interesting about the last client dying.
1684 }
1685 return true;
1686 case MSG_BIND_METHOD:
1687 args = (HandlerCaller.SomeArgs)msg.obj;
1688 try {
1689 ((IInputMethodClient)args.arg1).onBindMethod(
1690 (InputBindResult)args.arg2);
1691 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001692 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 }
1694 return true;
1695 }
1696 return false;
1697 }
1698
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001699 private boolean isSystemIme(InputMethodInfo inputMethod) {
1700 return (inputMethod.getServiceInfo().applicationInfo.flags
1701 & ApplicationInfo.FLAG_SYSTEM) != 0;
1702 }
1703
Ken Wakasa586f0512011-01-20 22:31:01 +09001704 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1705 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1706 final int subtypeCount = imi.getSubtypeCount();
1707 for (int i = 0; i < subtypeCount; ++i) {
1708 subtypes.add(imi.getSubtypeAt(i));
1709 }
1710 return subtypes;
1711 }
1712
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001713 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001714 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001715 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001716 // We'd prefer to fall back on a system IME, since that is safer.
1717 int i=enabled.size();
1718 while (i > 0) {
1719 i--;
1720 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1721 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1722 break;
1723 }
1724 }
satokab751aa2010-09-14 19:17:36 +09001725 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001726 if (DEBUG) {
1727 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1728 }
satok723a27e2010-11-11 14:58:11 +09001729 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001730 return true;
1731 }
1732
1733 return false;
1734 }
1735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1737 HashMap<String, InputMethodInfo> map) {
1738 list.clear();
1739 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001742 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001743 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1744 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1745 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1746 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747
1748 List<ResolveInfo> services = pm.queryIntentServices(
1749 new Intent(InputMethod.SERVICE_INTERFACE),
1750 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 for (int i = 0; i < services.size(); ++i) {
1753 ResolveInfo ri = services.get(i);
1754 ServiceInfo si = ri.serviceInfo;
1755 ComponentName compName = new ComponentName(si.packageName, si.name);
1756 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1757 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001758 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 + ": it does not require the permission "
1760 + android.Manifest.permission.BIND_INPUT_METHOD);
1761 continue;
1762 }
1763
Joe Onorato8a9b2202010-02-26 18:56:32 -08001764 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765
1766 try {
1767 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1768 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001769 final String id = p.getId();
1770 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771
Amith Yamasanie861ec12010-03-24 21:39:27 -07001772 // System IMEs are enabled by default, unless there's a hard keyboard
1773 // and the system IME was explicitly disabled
1774 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1775 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001776 }
1777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001779 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001783 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001785 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 }
1787 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001788
1789 String defaultIme = Settings.Secure.getString(mContext
1790 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001791 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001792 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001793 updateFromSettingsLocked();
1794 }
1795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001799
satokab751aa2010-09-14 19:17:36 +09001800 private void showInputMethodMenu() {
1801 showInputMethodMenuInternal(false);
1802 }
1803
1804 private void showInputMethodSubtypeMenu() {
1805 showInputMethodMenuInternal(true);
1806 }
1807
satok217f5482010-12-15 05:19:19 +09001808 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001809 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001810 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001811 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1812 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001813 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001814 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001815 }
satok217f5482010-12-15 05:19:19 +09001816 mContext.startActivity(intent);
1817 }
1818
1819 private void showConfigureInputMethods() {
1820 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1821 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1822 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1823 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001824 mContext.startActivity(intent);
1825 }
1826
satokab751aa2010-09-14 19:17:36 +09001827 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001828 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 String lastInputMethodId = Settings.Secure.getString(context
1835 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001836 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001837 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001838
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001839 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09001840 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
1841 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001842 if (immis == null || immis.size() == 0) {
1843 return;
1844 }
1845
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001846 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847
Ken Wakasa761eb372011-03-04 19:06:18 +09001848 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
1849 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
1850 new Comparator<InputMethodInfo>() {
1851 @Override
1852 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
1853 if (imi2 == null) return 0;
1854 if (imi1 == null) return 1;
1855 if (pm == null) {
1856 return imi1.getId().compareTo(imi2.getId());
1857 }
1858 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
1859 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
1860 return imiId1.toString().compareTo(imiId2.toString());
1861 }
1862 });
satok913a8922010-08-26 21:53:41 +09001863
Ken Wakasa761eb372011-03-04 19:06:18 +09001864 sortedImmis.putAll(immis);
1865
1866 final ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>> imList =
1867 new ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>>();
1868
1869 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09001870 if (imi == null) continue;
1871 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09001872 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09001873 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
1874 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09001875 }
satokbb4aa062011-01-19 21:40:27 +09001876 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa81f6e3d2011-03-04 09:59:52 +09001877 final CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001878 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09001879 final int subtypeCount = imi.getSubtypeCount();
Ken Wakasa586f0512011-01-20 22:31:01 +09001880 for (int j = 0; j < subtypeCount; ++j) {
satokbb4aa062011-01-19 21:40:27 +09001881 InputMethodSubtype subtype = imi.getSubtypeAt(j);
satok9b415792011-05-30 12:37:52 +09001882 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))
1883 && !subtype.isAuxiliary()) {
Ken Wakasa761eb372011-03-04 19:06:18 +09001884 final CharSequence title;
satok7f35c8c2010-10-07 21:13:11 +09001885 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001886 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001887 if (nameResId != 0) {
satok4f313532011-06-01 16:13:45 +09001888 title = TextUtils.concat(subtype.getDisplayName(context,
1889 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
Ken Wakasa761eb372011-03-04 19:06:18 +09001890 (TextUtils.isEmpty(label) ? "" : " (" + label + ")"));
satok7f35c8c2010-10-07 21:13:11 +09001891 } else {
1892 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001893 // TODO: Use more friendly Title and UI
1894 title = label + "," + (mode == null ? "" : mode) + ","
1895 + (language == null ? "" : language);
1896 }
Ken Wakasa761eb372011-03-04 19:06:18 +09001897 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
1898 title, new Pair<InputMethodInfo, Integer>(imi, j)));
satokab751aa2010-09-14 19:17:36 +09001899 }
satokab751aa2010-09-14 19:17:36 +09001900 }
1901 } else {
Ken Wakasa761eb372011-03-04 19:06:18 +09001902 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
1903 label, new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID)));
satokab751aa2010-09-14 19:17:36 +09001904 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001905 }
satok913a8922010-08-26 21:53:41 +09001906
Ken Wakasa761eb372011-03-04 19:06:18 +09001907 final int N = imList.size();
1908 mItems = new CharSequence[N];
1909 for (int i = 0; i < N; ++i) {
1910 mItems[i] = imList.get(i).first;
1911 }
satokab751aa2010-09-14 19:17:36 +09001912 mIms = new InputMethodInfo[N];
1913 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001914 int checkedItem = 0;
1915 for (int i = 0; i < N; ++i) {
Ken Wakasa761eb372011-03-04 19:06:18 +09001916 Pair<InputMethodInfo, Integer> value = imList.get(i).second;
satokab751aa2010-09-14 19:17:36 +09001917 mIms[i] = value.first;
1918 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001919 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001920 int subtypeId = mSubtypeIds[i];
1921 if ((subtypeId == NOT_A_SUBTYPE_ID)
1922 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1923 || (subtypeId == lastInputMethodSubtypeId)) {
1924 checkedItem = i;
1925 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 }
satokab751aa2010-09-14 19:17:36 +09001928
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001929 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09001930 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001931 public void onClick(DialogInterface dialog, int which) {
1932 hideInputMethodMenu();
1933 }
1934 };
satokd87c2592010-09-29 11:52:06 +09001935
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001936 TypedArray a = context.obtainStyledAttributes(null,
1937 com.android.internal.R.styleable.DialogPreference,
1938 com.android.internal.R.attr.alertDialogStyle, 0);
1939 mDialogBuilder = new AlertDialog.Builder(context)
1940 .setTitle(com.android.internal.R.string.select_input_method)
1941 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09001942 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001943 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001945 }
1946 })
1947 .setIcon(a.getDrawable(
1948 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1949 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001950
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001951 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1952 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09001953 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001954 public void onClick(DialogInterface dialog, int which) {
1955 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001956 if (mIms == null || mIms.length <= which
1957 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001958 return;
1959 }
1960 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001961 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001962 hideInputMethodMenu();
1963 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001964 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09001965 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09001966 subtypeId = NOT_A_SUBTYPE_ID;
1967 }
1968 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001969 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001972 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973
satok7f35c8c2010-10-07 21:13:11 +09001974 if (showSubtypes) {
satok82beadf2010-12-27 19:03:06 +09001975 mDialogBuilder.setPositiveButton(
1976 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09001977 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09001978 @Override
satok7f35c8c2010-10-07 21:13:11 +09001979 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09001980 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09001981 }
1982 });
1983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001985 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 mSwitchingDialog.getWindow().setType(
1987 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001988 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 mSwitchingDialog.show();
1990 }
1991 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001994 synchronized (mMethodMap) {
1995 hideInputMethodMenuLocked();
1996 }
1997 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001998
The Android Open Source Project10592532009-03-18 17:39:46 -07001999 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002000 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001
The Android Open Source Project10592532009-03-18 17:39:46 -07002002 if (mSwitchingDialog != null) {
2003 mSwitchingDialog.dismiss();
2004 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002006
The Android Open Source Project10592532009-03-18 17:39:46 -07002007 mDialogBuilder = null;
2008 mItems = null;
2009 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002013
satok42c5a162011-05-26 16:46:14 +09002014 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 public boolean setInputMethodEnabled(String id, boolean enabled) {
2016 synchronized (mMethodMap) {
2017 if (mContext.checkCallingOrSelfPermission(
2018 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2019 != PackageManager.PERMISSION_GRANTED) {
2020 throw new SecurityException(
2021 "Requires permission "
2022 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2023 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 long ident = Binder.clearCallingIdentity();
2026 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002027 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 } finally {
2029 Binder.restoreCallingIdentity(ident);
2030 }
2031 }
2032 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002033
2034 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2035 // Make sure this is a valid input method.
2036 InputMethodInfo imm = mMethodMap.get(id);
2037 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002038 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002039 }
2040
satokd87c2592010-09-29 11:52:06 +09002041 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2042 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002043
satokd87c2592010-09-29 11:52:06 +09002044 if (enabled) {
2045 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2046 if (pair.first.equals(id)) {
2047 // We are enabling this input method, but it is already enabled.
2048 // Nothing to do. The previous state was enabled.
2049 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002050 }
2051 }
satokd87c2592010-09-29 11:52:06 +09002052 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2053 // Previous state was disabled.
2054 return false;
2055 } else {
2056 StringBuilder builder = new StringBuilder();
2057 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2058 builder, enabledInputMethodsList, id)) {
2059 // Disabled input method is currently selected, switch to another one.
2060 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2061 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002062 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2063 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2064 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002065 }
2066 // Previous state was enabled.
2067 return true;
2068 } else {
2069 // We are disabling the input method but it is already disabled.
2070 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002071 return false;
2072 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002073 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002074 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002075
satok57ffc002011-01-25 00:11:47 +09002076 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2077 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002078 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002079 }
2080
satok723a27e2010-11-11 14:58:11 +09002081 private void saveCurrentInputMethodAndSubtypeToHistory() {
2082 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2083 if (mCurrentSubtype != null) {
2084 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2085 }
satok57ffc002011-01-25 00:11:47 +09002086 if (canAddToLastInputMethod(mCurrentSubtype)) {
2087 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2088 }
satokab751aa2010-09-14 19:17:36 +09002089 }
2090
satok723a27e2010-11-11 14:58:11 +09002091 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2092 boolean setSubtypeOnly) {
2093 // Update the history of InputMethod and Subtype
2094 saveCurrentInputMethodAndSubtypeToHistory();
2095
2096 // Set Subtype here
2097 if (imi == null || subtypeId < 0) {
2098 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002099 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002100 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002101 if (subtypeId < imi.getSubtypeCount()) {
2102 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2103 mSettings.putSelectedSubtype(subtype.hashCode());
2104 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002105 } else {
2106 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2107 mCurrentSubtype = null;
2108 }
satokab751aa2010-09-14 19:17:36 +09002109 }
satok723a27e2010-11-11 14:58:11 +09002110
2111 if (!setSubtypeOnly) {
2112 // Set InputMethod here
2113 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2114 }
2115 }
2116
2117 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2118 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2119 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2120 // newDefaultIme is empty when there is no candidate for the selected IME.
2121 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2122 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2123 if (subtypeHashCode != null) {
2124 try {
2125 lastSubtypeId = getSubtypeIdFromHashCode(
2126 imi, Integer.valueOf(subtypeHashCode));
2127 } catch (NumberFormatException e) {
2128 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2129 }
2130 }
2131 }
2132 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002133 }
2134
2135 private int getSelectedInputMethodSubtypeId(String id) {
2136 InputMethodInfo imi = mMethodMap.get(id);
2137 if (imi == null) {
2138 return NOT_A_SUBTYPE_ID;
2139 }
satokab751aa2010-09-14 19:17:36 +09002140 int subtypeId;
2141 try {
2142 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2143 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2144 } catch (SettingNotFoundException e) {
2145 return NOT_A_SUBTYPE_ID;
2146 }
satok723a27e2010-11-11 14:58:11 +09002147 return getSubtypeIdFromHashCode(imi, subtypeId);
2148 }
2149
2150 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002151 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002152 final int subtypeCount = imi.getSubtypeCount();
2153 for (int i = 0; i < subtypeCount; ++i) {
2154 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002155 if (subtypeHashCode == ims.hashCode()) {
2156 return i;
2157 }
satokab751aa2010-09-14 19:17:36 +09002158 }
2159 }
2160 return NOT_A_SUBTYPE_ID;
2161 }
2162
satokdf31ae62011-01-15 06:19:44 +09002163 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2164 Resources res, List<InputMethodSubtype> subtypes) {
2165 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002166 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2167 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2168 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002169 final int N = subtypes.size();
2170 boolean containsKeyboardSubtype = false;
2171 for (int i = 0; i < N; ++i) {
2172 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002173 final String locale = subtype.getLocale();
2174 final String mode = subtype.getMode();
2175 // When system locale starts with subtype's locale, that subtype will be applicable
2176 // for system locale
2177 // For instance, it's clearly applicable for cases like system locale = en_US and
2178 // subtype = en, but it is not necessarily considered applicable for cases like system
2179 // locale = en and subtype = en_US.
2180 // We just call systemLocale.startsWith(locale) in this function because there is no
2181 // need to find applicable subtypes aggressively unlike
2182 // findLastResortApplicableSubtypeLocked.
2183 if (systemLocale.startsWith(locale)) {
2184 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2185 // If more applicable subtypes are contained, skip.
2186 if (applicableSubtype != null
2187 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2188 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002189 if (!containsKeyboardSubtype
2190 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2191 containsKeyboardSubtype = true;
2192 }
2193 }
2194 }
satok3da92232011-01-11 22:46:30 +09002195 ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2196 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002197 if (!containsKeyboardSubtype) {
2198 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002199 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002200 if (lastResortKeyboardSubtype != null) {
2201 applicableSubtypes.add(lastResortKeyboardSubtype);
2202 }
2203 }
2204 return applicableSubtypes;
2205 }
2206
satok4e4569d2010-11-19 18:45:53 +09002207 /**
2208 * If there are no selected subtypes, tries finding the most applicable one according to the
2209 * given locale.
2210 * @param subtypes this function will search the most applicable subtype in subtypes
2211 * @param mode subtypes will be filtered by mode
2212 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002213 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2214 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002215 * @return the most applicable subtypeId
2216 */
satokdf31ae62011-01-15 06:19:44 +09002217 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2218 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002219 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002220 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002221 return null;
satok8fbb1e82010-11-02 23:15:58 +09002222 }
satok4e4569d2010-11-19 18:45:53 +09002223 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002224 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002225 }
satok8fbb1e82010-11-02 23:15:58 +09002226 final String language = locale.substring(0, 2);
2227 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002228 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002229 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002230 final int N = subtypes.size();
2231 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002232 InputMethodSubtype subtype = subtypes.get(i);
2233 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002234 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2235 // and all subtypes with all modes can be candidates.
2236 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002237 if (firstMatchedModeSubtype == null) {
2238 firstMatchedModeSubtype = subtype;
2239 }
satok9ef02832010-11-04 21:17:48 +09002240 if (locale.equals(subtypeLocale)) {
2241 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002242 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002243 break;
2244 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2245 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002246 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002247 partialMatchFound = true;
2248 }
satok8fbb1e82010-11-02 23:15:58 +09002249 }
2250 }
2251
satok7599a7f2010-12-22 13:45:23 +09002252 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2253 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002254 }
2255
satok8fbb1e82010-11-02 23:15:58 +09002256 // The first subtype applicable to the system locale will be defined as the most applicable
2257 // subtype.
2258 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002259 if (applicableSubtype != null) {
2260 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2261 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2262 }
satok8fbb1e82010-11-02 23:15:58 +09002263 }
satokcd7cd292010-11-20 15:46:23 +09002264 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002265 }
2266
satok4e4569d2010-11-19 18:45:53 +09002267 // If there are no selected shortcuts, tries finding the most applicable ones.
2268 private Pair<InputMethodInfo, InputMethodSubtype>
2269 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2270 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2271 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002272 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002273 boolean foundInSystemIME = false;
2274
2275 // Search applicable subtype for each InputMethodInfo
2276 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002277 final String imiId = imi.getId();
2278 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2279 continue;
2280 }
satokcd7cd292010-11-20 15:46:23 +09002281 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002282 final List<InputMethodSubtype> enabledSubtypes =
2283 getEnabledInputMethodSubtypeList(imi, true);
2284 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002285 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002286 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002287 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002288 }
satokdf31ae62011-01-15 06:19:44 +09002289 // 2. Search by the system locale from enabledSubtypes.
2290 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002291 if (subtype == null) {
2292 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002293 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002294 }
satok7599a7f2010-12-22 13:45:23 +09002295 // 4. Search by the current subtype's locale from all subtypes.
2296 if (subtype == null && mCurrentSubtype != null) {
2297 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002298 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002299 }
2300 // 5. Search by the system locale from all subtypes.
2301 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002302 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002303 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002304 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002305 }
satokcd7cd292010-11-20 15:46:23 +09002306 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002307 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002308 // The current input method is the most applicable IME.
2309 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002310 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002311 break;
satok7599a7f2010-12-22 13:45:23 +09002312 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002313 // The system input method is 2nd applicable IME.
2314 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002315 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002316 if ((imi.getServiceInfo().applicationInfo.flags
2317 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2318 foundInSystemIME = true;
2319 }
satok4e4569d2010-11-19 18:45:53 +09002320 }
2321 }
2322 }
2323 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002324 if (mostApplicableIMI != null) {
2325 Slog.w(TAG, "Most applicable shortcut input method was:"
2326 + mostApplicableIMI.getId());
2327 if (mostApplicableSubtype != null) {
2328 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2329 + "," + mostApplicableSubtype.getMode() + ","
2330 + mostApplicableSubtype.getLocale());
2331 }
2332 }
satok4e4569d2010-11-19 18:45:53 +09002333 }
satokcd7cd292010-11-20 15:46:23 +09002334 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002335 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002336 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002337 } else {
2338 return null;
2339 }
2340 }
2341
satokab751aa2010-09-14 19:17:36 +09002342 /**
2343 * @return Return the current subtype of this input method.
2344 */
satok42c5a162011-05-26 16:46:14 +09002345 @Override
satokab751aa2010-09-14 19:17:36 +09002346 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002347 boolean subtypeIsSelected = false;
2348 try {
2349 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2350 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2351 } catch (SettingNotFoundException e) {
2352 }
satok3ef8b292010-11-23 06:06:29 +09002353 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002354 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002355 String lastInputMethodId = Settings.Secure.getString(
2356 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002357 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2358 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002359 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2360 if (imi != null) {
2361 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002362 // the most applicable subtype from explicitly or implicitly enabled
2363 // subtypes.
2364 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2365 getEnabledInputMethodSubtypeList(imi, true);
2366 // If there is only one explicitly or implicitly enabled subtype,
2367 // just returns it.
2368 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2369 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2370 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2371 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2372 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2373 SUBTYPE_MODE_KEYBOARD, null, true);
2374 if (mCurrentSubtype == null) {
2375 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2376 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2377 true);
2378 }
2379 }
satok4e4569d2010-11-19 18:45:53 +09002380 }
satokcd7cd292010-11-20 15:46:23 +09002381 } else {
satok3ef8b292010-11-23 06:06:29 +09002382 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002383 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002384 }
satok8fbb1e82010-11-02 23:15:58 +09002385 }
satok3ef8b292010-11-23 06:06:29 +09002386 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002387 }
satokab751aa2010-09-14 19:17:36 +09002388 }
2389
satokf3db1af2010-11-23 13:34:33 +09002390 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2391 InputMethodSubtype subtype) {
2392 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2393 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2394 } else {
2395 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2396 subtypes.add(subtype);
2397 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2398 }
2399 }
2400
satok4e4569d2010-11-19 18:45:53 +09002401 // TODO: We should change the return type from List to List<Parcelable>
2402 public List getShortcutInputMethodsAndSubtypes() {
2403 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002404 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002405 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002406 // If there are no selected shortcut subtypes, the framework will try to find
2407 // the most applicable subtype from all subtypes whose mode is
2408 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002409 Pair<InputMethodInfo, InputMethodSubtype> info =
2410 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2411 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002412 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002413 ret.add(info.first);
2414 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002415 }
satok3da92232011-01-11 22:46:30 +09002416 return ret;
satokf3db1af2010-11-23 13:34:33 +09002417 }
satokf3db1af2010-11-23 13:34:33 +09002418 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2419 ret.add(imi);
2420 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2421 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002422 }
2423 }
satokf3db1af2010-11-23 13:34:33 +09002424 return ret;
satok4e4569d2010-11-19 18:45:53 +09002425 }
2426 }
2427
satok42c5a162011-05-26 16:46:14 +09002428 @Override
satokb66d2872010-11-10 01:04:04 +09002429 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2430 synchronized (mMethodMap) {
2431 if (subtype != null && mCurMethodId != null) {
2432 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2433 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2434 if (subtypeId != NOT_A_SUBTYPE_ID) {
2435 setInputMethodLocked(mCurMethodId, subtypeId);
2436 return true;
2437 }
2438 }
2439 return false;
2440 }
2441 }
2442
satokd87c2592010-09-29 11:52:06 +09002443 /**
2444 * Utility class for putting and getting settings for InputMethod
2445 * TODO: Move all putters and getters of settings to this class.
2446 */
2447 private static class InputMethodSettings {
2448 // The string for enabled input method is saved as follows:
2449 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2450 private static final char INPUT_METHOD_SEPARATER = ':';
2451 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002452 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002453 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2454
satok723a27e2010-11-11 14:58:11 +09002455 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002456 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2457
satokdf31ae62011-01-15 06:19:44 +09002458 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002459 private final ContentResolver mResolver;
2460 private final HashMap<String, InputMethodInfo> mMethodMap;
2461 private final ArrayList<InputMethodInfo> mMethodList;
2462
2463 private String mEnabledInputMethodsStrCache;
2464
2465 private static void buildEnabledInputMethodsSettingString(
2466 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2467 String id = pair.first;
2468 ArrayList<String> subtypes = pair.second;
2469 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002470 // Inputmethod and subtypes are saved in the settings as follows:
2471 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2472 for (String subtypeId: subtypes) {
2473 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002474 }
2475 }
2476
2477 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002478 Resources res, ContentResolver resolver,
2479 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2480 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002481 mResolver = resolver;
2482 mMethodMap = methodMap;
2483 mMethodList = methodList;
2484 }
2485
2486 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2487 return createEnabledInputMethodListLocked(
2488 getEnabledInputMethodsAndSubtypeListLocked());
2489 }
2490
satok7f35c8c2010-10-07 21:13:11 +09002491 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002492 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2493 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002494 getEnabledInputMethodsAndSubtypeListLocked());
2495 }
2496
satok67ddf9c2010-11-17 09:45:54 +09002497 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2498 InputMethodInfo imi) {
2499 List<Pair<String, ArrayList<String>>> imsList =
2500 getEnabledInputMethodsAndSubtypeListLocked();
2501 ArrayList<InputMethodSubtype> enabledSubtypes =
2502 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002503 if (imi != null) {
2504 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2505 InputMethodInfo info = mMethodMap.get(imsPair.first);
2506 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002507 final int subtypeCount = info.getSubtypeCount();
2508 for (int i = 0; i < subtypeCount; ++i) {
2509 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002510 for (String s: imsPair.second) {
2511 if (String.valueOf(ims.hashCode()).equals(s)) {
2512 enabledSubtypes.add(ims);
2513 }
satok67ddf9c2010-11-17 09:45:54 +09002514 }
2515 }
satok884ef9a2010-11-18 10:39:46 +09002516 break;
satok67ddf9c2010-11-17 09:45:54 +09002517 }
satok67ddf9c2010-11-17 09:45:54 +09002518 }
2519 }
2520 return enabledSubtypes;
2521 }
2522
satokd87c2592010-09-29 11:52:06 +09002523 // At the initial boot, the settings for input methods are not set,
2524 // so we need to enable IME in that case.
2525 public void enableAllIMEsIfThereIsNoEnabledIME() {
2526 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2527 StringBuilder sb = new StringBuilder();
2528 final int N = mMethodList.size();
2529 for (int i = 0; i < N; i++) {
2530 InputMethodInfo imi = mMethodList.get(i);
2531 Slog.i(TAG, "Adding: " + imi.getId());
2532 if (i > 0) sb.append(':');
2533 sb.append(imi.getId());
2534 }
2535 putEnabledInputMethodsStr(sb.toString());
2536 }
2537 }
2538
satokbb4aa062011-01-19 21:40:27 +09002539 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002540 ArrayList<Pair<String, ArrayList<String>>> imsList
2541 = new ArrayList<Pair<String, ArrayList<String>>>();
2542 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2543 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2544 return imsList;
2545 }
satok723a27e2010-11-11 14:58:11 +09002546 mInputMethodSplitter.setString(enabledInputMethodsStr);
2547 while (mInputMethodSplitter.hasNext()) {
2548 String nextImsStr = mInputMethodSplitter.next();
2549 mSubtypeSplitter.setString(nextImsStr);
2550 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002551 ArrayList<String> subtypeHashes = new ArrayList<String>();
2552 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002553 String imeId = mSubtypeSplitter.next();
2554 while (mSubtypeSplitter.hasNext()) {
2555 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002556 }
2557 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2558 }
2559 }
2560 return imsList;
2561 }
2562
2563 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2564 if (reloadInputMethodStr) {
2565 getEnabledInputMethodsStr();
2566 }
2567 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2568 // Add in the newly enabled input method.
2569 putEnabledInputMethodsStr(id);
2570 } else {
2571 putEnabledInputMethodsStr(
2572 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2573 }
2574 }
2575
2576 /**
2577 * Build and put a string of EnabledInputMethods with removing specified Id.
2578 * @return the specified id was removed or not.
2579 */
2580 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2581 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2582 boolean isRemoved = false;
2583 boolean needsAppendSeparator = false;
2584 for (Pair<String, ArrayList<String>> ims: imsList) {
2585 String curId = ims.first;
2586 if (curId.equals(id)) {
2587 // We are disabling this input method, and it is
2588 // currently enabled. Skip it to remove from the
2589 // new list.
2590 isRemoved = true;
2591 } else {
2592 if (needsAppendSeparator) {
2593 builder.append(INPUT_METHOD_SEPARATER);
2594 } else {
2595 needsAppendSeparator = true;
2596 }
2597 buildEnabledInputMethodsSettingString(builder, ims);
2598 }
2599 }
2600 if (isRemoved) {
2601 // Update the setting with the new list of input methods.
2602 putEnabledInputMethodsStr(builder.toString());
2603 }
2604 return isRemoved;
2605 }
2606
2607 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2608 List<Pair<String, ArrayList<String>>> imsList) {
2609 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2610 for (Pair<String, ArrayList<String>> ims: imsList) {
2611 InputMethodInfo info = mMethodMap.get(ims.first);
2612 if (info != null) {
2613 res.add(info);
2614 }
2615 }
2616 return res;
2617 }
2618
satok7f35c8c2010-10-07 21:13:11 +09002619 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002620 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002621 List<Pair<String, ArrayList<String>>> imsList) {
2622 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2623 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2624 for (Pair<String, ArrayList<String>> ims : imsList) {
2625 InputMethodInfo info = mMethodMap.get(ims.first);
2626 if (info != null) {
2627 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2628 }
2629 }
2630 return res;
2631 }
2632
satokd87c2592010-09-29 11:52:06 +09002633 private void putEnabledInputMethodsStr(String str) {
2634 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2635 mEnabledInputMethodsStrCache = str;
2636 }
2637
2638 private String getEnabledInputMethodsStr() {
2639 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2640 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002641 if (DEBUG) {
2642 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2643 }
satokd87c2592010-09-29 11:52:06 +09002644 return mEnabledInputMethodsStrCache;
2645 }
satok723a27e2010-11-11 14:58:11 +09002646
2647 private void saveSubtypeHistory(
2648 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2649 StringBuilder builder = new StringBuilder();
2650 boolean isImeAdded = false;
2651 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2652 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2653 newSubtypeId);
2654 isImeAdded = true;
2655 }
2656 for (Pair<String, String> ime: savedImes) {
2657 String imeId = ime.first;
2658 String subtypeId = ime.second;
2659 if (TextUtils.isEmpty(subtypeId)) {
2660 subtypeId = NOT_A_SUBTYPE_ID_STR;
2661 }
2662 if (isImeAdded) {
2663 builder.append(INPUT_METHOD_SEPARATER);
2664 } else {
2665 isImeAdded = true;
2666 }
2667 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2668 subtypeId);
2669 }
2670 // Remove the last INPUT_METHOD_SEPARATER
2671 putSubtypeHistoryStr(builder.toString());
2672 }
2673
2674 public void addSubtypeToHistory(String imeId, String subtypeId) {
2675 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2676 for (Pair<String, String> ime: subtypeHistory) {
2677 if (ime.first.equals(imeId)) {
2678 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002679 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002680 + ime.second);
2681 }
2682 // We should break here
2683 subtypeHistory.remove(ime);
2684 break;
2685 }
2686 }
satokbb4aa062011-01-19 21:40:27 +09002687 if (DEBUG) {
2688 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2689 }
satok723a27e2010-11-11 14:58:11 +09002690 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2691 }
2692
2693 private void putSubtypeHistoryStr(String str) {
2694 if (DEBUG) {
2695 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2696 }
2697 Settings.Secure.putString(
2698 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2699 }
2700
2701 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2702 // Gets the first one from the history
2703 return getLastSubtypeForInputMethodLockedInternal(null);
2704 }
2705
2706 public String getLastSubtypeForInputMethodLocked(String imeId) {
2707 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2708 if (ime != null) {
2709 return ime.second;
2710 } else {
2711 return null;
2712 }
2713 }
2714
2715 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2716 List<Pair<String, ArrayList<String>>> enabledImes =
2717 getEnabledInputMethodsAndSubtypeListLocked();
2718 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09002719 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09002720 final String imeInTheHistory = imeAndSubtype.first;
2721 // If imeId is empty, returns the first IME and subtype in the history
2722 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2723 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002724 final String subtypeHashCode =
2725 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2726 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002727 if (!TextUtils.isEmpty(subtypeHashCode)) {
2728 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002729 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002730 }
2731 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2732 }
2733 }
2734 }
2735 if (DEBUG) {
2736 Slog.d(TAG, "No enabled IME found in the history");
2737 }
2738 return null;
2739 }
2740
satokdf31ae62011-01-15 06:19:44 +09002741 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002742 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2743 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2744 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002745 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2746 if (explicitlyEnabledSubtypes.size() == 0) {
2747 // If there are no explicitly enabled subtypes, applicable subtypes are
2748 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002749 InputMethodInfo ime = mMethodMap.get(imeId);
2750 // If IME is enabled and no subtypes are enabled, applicable subtypes
2751 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002752 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002753 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002754 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002755 if (implicitlySelectedSubtypes != null) {
2756 final int N = implicitlySelectedSubtypes.size();
2757 for (int i = 0; i < N; ++i) {
2758 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2759 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2760 return subtypeHashCode;
2761 }
2762 }
2763 }
2764 }
2765 } else {
satokf6cafb62011-01-17 16:29:02 +09002766 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002767 if (s.equals(subtypeHashCode)) {
2768 // If both imeId and subtypeId are enabled, return subtypeId.
2769 return s;
2770 }
satok723a27e2010-11-11 14:58:11 +09002771 }
2772 }
2773 // If imeId was enabled but subtypeId was disabled.
2774 return NOT_A_SUBTYPE_ID_STR;
2775 }
2776 }
2777 // If both imeId and subtypeId are disabled, return null
2778 return null;
2779 }
2780
2781 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2782 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2783 final String subtypeHistoryStr = getSubtypeHistoryStr();
2784 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2785 return imsList;
2786 }
2787 mInputMethodSplitter.setString(subtypeHistoryStr);
2788 while (mInputMethodSplitter.hasNext()) {
2789 String nextImsStr = mInputMethodSplitter.next();
2790 mSubtypeSplitter.setString(nextImsStr);
2791 if (mSubtypeSplitter.hasNext()) {
2792 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2793 // The first element is ime id.
2794 String imeId = mSubtypeSplitter.next();
2795 while (mSubtypeSplitter.hasNext()) {
2796 subtypeId = mSubtypeSplitter.next();
2797 break;
2798 }
2799 imsList.add(new Pair<String, String>(imeId, subtypeId));
2800 }
2801 }
2802 return imsList;
2803 }
2804
2805 private String getSubtypeHistoryStr() {
2806 if (DEBUG) {
2807 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2808 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2809 }
2810 return Settings.Secure.getString(
2811 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2812 }
2813
2814 public void putSelectedInputMethod(String imeId) {
2815 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2816 }
2817
2818 public void putSelectedSubtype(int subtypeId) {
2819 Settings.Secure.putInt(
2820 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2821 }
satokd87c2592010-09-29 11:52:06 +09002822 }
2823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 @Override
2827 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2828 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2829 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2832 + Binder.getCallingPid()
2833 + ", uid=" + Binder.getCallingUid());
2834 return;
2835 }
2836
2837 IInputMethod method;
2838 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 synchronized (mMethodMap) {
2843 p.println("Current Input Method Manager state:");
2844 int N = mMethodList.size();
2845 p.println(" Input Methods:");
2846 for (int i=0; i<N; i++) {
2847 InputMethodInfo info = mMethodList.get(i);
2848 p.println(" InputMethod #" + i + ":");
2849 info.dump(p, " ");
2850 }
2851 p.println(" Clients:");
2852 for (ClientState ci : mClients.values()) {
2853 p.println(" Client " + ci + ":");
2854 p.println(" client=" + ci.client);
2855 p.println(" inputContext=" + ci.inputContext);
2856 p.println(" sessionRequested=" + ci.sessionRequested);
2857 p.println(" curSession=" + ci.curSession);
2858 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002859 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002861 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2862 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2864 + " mBoundToMethod=" + mBoundToMethod);
2865 p.println(" mCurToken=" + mCurToken);
2866 p.println(" mCurIntent=" + mCurIntent);
2867 method = mCurMethod;
2868 p.println(" mCurMethod=" + mCurMethod);
2869 p.println(" mEnabledSession=" + mEnabledSession);
2870 p.println(" mShowRequested=" + mShowRequested
2871 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2872 + " mShowForced=" + mShowForced
2873 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002874 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002876
Jeff Brownb88102f2010-09-08 11:49:43 -07002877 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 pw.flush();
2880 try {
2881 client.client.asBinder().dump(fd, args);
2882 } catch (RemoteException e) {
2883 p.println("Input method client dead: " + e);
2884 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002885 } else {
2886 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002888
Jeff Brownb88102f2010-09-08 11:49:43 -07002889 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 pw.flush();
2892 try {
2893 method.asBinder().dump(fd, args);
2894 } catch (RemoteException e) {
2895 p.println("Input method service dead: " + e);
2896 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002897 } else {
2898 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 }
2900 }
2901}