blob: fbde9d138ecf26b1fa0d1d0f980474f8de80dea6 [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;
satoke7c6998e2011-06-03 17:57:59 +090020import com.android.internal.os.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import com.android.internal.os.HandlerCaller;
satoke7c6998e2011-06-03 17:57:59 +090022import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import com.android.internal.view.IInputContext;
24import com.android.internal.view.IInputMethod;
25import com.android.internal.view.IInputMethodCallback;
26import com.android.internal.view.IInputMethodClient;
27import com.android.internal.view.IInputMethodManager;
28import com.android.internal.view.IInputMethodSession;
29import com.android.internal.view.InputBindResult;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080030import com.android.server.EventLogTags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
satoke7c6998e2011-06-03 17:57:59 +090032import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090034import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36import android.app.ActivityManagerNative;
37import android.app.AlertDialog;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070038import android.app.PendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.ComponentName;
40import android.content.ContentResolver;
41import android.content.Context;
42import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.content.DialogInterface.OnCancelListener;
44import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090045import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070047import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.content.pm.PackageManager;
49import android.content.pm.ResolveInfo;
50import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070051import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.res.Resources;
53import android.content.res.TypedArray;
54import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080055import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090057import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.os.Handler;
59import android.os.IBinder;
60import android.os.IInterface;
61import android.os.Message;
62import android.os.Parcel;
63import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080064import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.os.ServiceManager;
66import android.os.SystemClock;
67import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070068import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090069import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090071import android.text.style.SuggestionSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090073import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090074import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.util.PrintWriterPrinter;
76import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090077import android.util.Slog;
78import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.view.IWindowManager;
80import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090081import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.view.inputmethod.InputBinding;
83import android.view.inputmethod.InputMethod;
84import android.view.inputmethod.InputMethodInfo;
85import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090086import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
satoke7c6998e2011-06-03 17:57:59 +090088import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +090090import java.io.FileInputStream;
91import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import java.io.IOException;
93import java.io.PrintWriter;
94import java.util.ArrayList;
satoke7c6998e2011-06-03 17:57:59 +090095import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +090096import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +090098import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import java.util.List;
satoke7c6998e2011-06-03 17:57:59 +0900100import java.util.Set;
satok913a8922010-08-26 21:53:41 +0900101import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
103/**
104 * This class provides a system service that manages input methods.
105 */
106public class InputMethodManagerService extends IInputMethodManager.Stub
107 implements ServiceConnection, Handler.Callback {
108 static final boolean DEBUG = false;
109 static final String TAG = "InputManagerService";
110
111 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900112 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900113 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900114 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 static final int MSG_UNBIND_INPUT = 1000;
117 static final int MSG_BIND_INPUT = 1010;
118 static final int MSG_SHOW_SOFT_INPUT = 1020;
119 static final int MSG_HIDE_SOFT_INPUT = 1030;
120 static final int MSG_ATTACH_TOKEN = 1040;
121 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 static final int MSG_START_INPUT = 2000;
124 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 static final int MSG_UNBIND_METHOD = 3000;
127 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800130
satokf9f01002011-05-19 21:31:50 +0900131 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
132
satokab751aa2010-09-14 19:17:36 +0900133 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900134 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900135 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
136 private static final String SUBTYPE_MODE_VOICE = "voice";
137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800139 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900141 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700143 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 final IWindowManager mIWindowManager;
145 final HandlerCaller mCaller;
satoke7c6998e2011-06-03 17:57:59 +0900146 private final InputMethodFileManager mFileManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 // All known input methods. mMethodMap also serves as the global
151 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900152 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
153 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900154 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
155 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 class SessionState {
158 final ClientState client;
159 final IInputMethod method;
160 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 @Override
163 public String toString() {
164 return "SessionState{uid " + client.uid + " pid " + client.pid
165 + " method " + Integer.toHexString(
166 System.identityHashCode(method))
167 + " session " + Integer.toHexString(
168 System.identityHashCode(session))
169 + "}";
170 }
171
172 SessionState(ClientState _client, IInputMethod _method,
173 IInputMethodSession _session) {
174 client = _client;
175 method = _method;
176 session = _session;
177 }
178 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 class ClientState {
181 final IInputMethodClient client;
182 final IInputContext inputContext;
183 final int uid;
184 final int pid;
185 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 boolean sessionRequested;
188 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 @Override
191 public String toString() {
192 return "ClientState{" + Integer.toHexString(
193 System.identityHashCode(this)) + " uid " + uid
194 + " pid " + pid + "}";
195 }
196
197 ClientState(IInputMethodClient _client, IInputContext _inputContext,
198 int _uid, int _pid) {
199 client = _client;
200 inputContext = _inputContext;
201 uid = _uid;
202 pid = _pid;
203 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
204 }
205 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 final HashMap<IBinder, ClientState> mClients
208 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700211 * Set once the system is ready to run third party code.
212 */
213 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800214
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700215 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 * Id of the currently selected input method.
217 */
218 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 /**
221 * The current binding sequence number, incremented every time there is
222 * a new bind performed.
223 */
224 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 /**
227 * The client that is currently bound to an input method.
228 */
229 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700232 * The last window token that gained focus.
233 */
234 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800235
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700236 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 * The input context last provided by the current client.
238 */
239 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 /**
242 * The attributes last provided by the current client.
243 */
244 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 /**
247 * The input method ID of the input method service that we are currently
248 * connected to or in the process of connecting to.
249 */
250 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 /**
satokab751aa2010-09-14 19:17:36 +0900253 * The current subtype of the current input method.
254 */
255 private InputMethodSubtype mCurrentSubtype;
256
satok4e4569d2010-11-19 18:45:53 +0900257 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900258 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
259 mShortcutInputMethodsAndSubtypes =
260 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900261
262 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 * Set to true if our ServiceConnection is currently actively bound to
264 * a service (whether or not we have gotten its IBinder back yet).
265 */
266 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 /**
269 * Set if the client has asked for the input method to be shown.
270 */
271 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 /**
274 * Set if we were explicitly told to show the input method.
275 */
276 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
279 * Set if we were forced to be shown.
280 */
281 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 /**
284 * Set if we last told the input method to show itself.
285 */
286 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 /**
289 * The Intent used to connect to the current input method.
290 */
291 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 /**
294 * The token we have made for the currently active input method, to
295 * identify it in the future.
296 */
297 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * If non-null, this is the input method service we are currently connected
301 * to.
302 */
303 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 /**
306 * Time that we last initiated a bind to the input method, to determine
307 * if we should try to disconnect and reconnect to it.
308 */
309 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 /**
312 * Have we called mCurMethod.bindInput()?
313 */
314 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 /**
317 * Currently enabled session. Only touched by service thread, not
318 * protected by a lock.
319 */
320 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 /**
323 * True if the screen is on. The value is true initially.
324 */
325 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
Joe Onorato857fd9b2011-01-27 15:08:35 -0800327 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
328 int mImeWindowVis;
329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 AlertDialog.Builder mDialogBuilder;
331 AlertDialog mSwitchingDialog;
332 InputMethodInfo[] mIms;
333 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900334 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 class SettingsObserver extends ContentObserver {
337 SettingsObserver(Handler handler) {
338 super(handler);
339 ContentResolver resolver = mContext.getContentResolver();
340 resolver.registerContentObserver(Settings.Secure.getUriFor(
341 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900342 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900343 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
344 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900345 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 @Override public void onChange(boolean selfChange) {
349 synchronized (mMethodMap) {
350 updateFromSettingsLocked();
351 }
352 }
353 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
356 @Override
357 public void onReceive(Context context, Intent intent) {
358 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
359 mScreenOn = true;
360 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
361 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700362 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
363 hideInputMethodMenu();
364 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800366 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 }
368
369 // Inform the current client of the change in active status
370 try {
371 if (mCurClient != null && mCurClient.client != null) {
372 mCurClient.client.setActive(mScreenOn);
373 }
374 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800375 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 + mCurClient.pid + " uid " + mCurClient.uid);
377 }
378 }
379 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800380
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800381 class MyPackageMonitor extends PackageMonitor {
382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800384 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800386 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
388 final int N = mMethodList.size();
389 if (curInputMethodId != null) {
390 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800391 InputMethodInfo imi = mMethodList.get(i);
392 if (imi.getId().equals(curInputMethodId)) {
393 for (String pkg : packages) {
394 if (imi.getPackageName().equals(pkg)) {
395 if (!doit) {
396 return true;
397 }
satok723a27e2010-11-11 14:58:11 +0900398 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800399 chooseNewDefaultIMELocked();
400 return true;
401 }
402 }
403 }
404 }
405 }
406 }
407 return false;
408 }
409
410 @Override
411 public void onSomePackagesChanged() {
412 synchronized (mMethodMap) {
413 InputMethodInfo curIm = null;
414 String curInputMethodId = Settings.Secure.getString(mContext
415 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
416 final int N = mMethodList.size();
417 if (curInputMethodId != null) {
418 for (int i=0; i<N; i++) {
419 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900420 final String imiId = imi.getId();
421 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800422 curIm = imi;
423 }
satoke7c6998e2011-06-03 17:57:59 +0900424
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800425 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900426 if (isPackageModified(imi.getPackageName())) {
427 mFileManager.deleteAllInputMethodSubtypes(imiId);
428 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800429 if (change == PACKAGE_TEMPORARY_CHANGE
430 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800431 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800432 + imi.getComponent());
433 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 }
435 }
436 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800437
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800438 buildInputMethodListLocked(mMethodList, mMethodMap);
439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800441
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800442 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800443 int change = isPackageDisappearing(curIm.getPackageName());
444 if (change == PACKAGE_TEMPORARY_CHANGE
445 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800446 ServiceInfo si = null;
447 try {
448 si = mContext.getPackageManager().getServiceInfo(
449 curIm.getComponent(), 0);
450 } catch (PackageManager.NameNotFoundException ex) {
451 }
452 if (si == null) {
453 // Uh oh, current input method is no longer around!
454 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800455 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800456 mImeWindowVis = 0;
457 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
458 mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800459 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800460 changed = true;
461 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800462 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900463 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800464 }
465 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800466 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800467 }
satokab751aa2010-09-14 19:17:36 +0900468
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800469 if (curIm == null) {
470 // We currently don't have a default input method... is
471 // one now available?
472 changed = chooseNewDefaultIMELocked();
473 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800474
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800475 if (changed) {
476 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 }
478 }
479 }
480 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 class MethodCallback extends IInputMethodCallback.Stub {
483 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 MethodCallback(IInputMethod method) {
486 mMethod = method;
487 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800488
satoke7c6998e2011-06-03 17:57:59 +0900489 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 public void finishedEvent(int seq, boolean handled) throws RemoteException {
491 }
492
satoke7c6998e2011-06-03 17:57:59 +0900493 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 public void sessionCreated(IInputMethodSession session) throws RemoteException {
495 onSessionCreated(mMethod, session);
496 }
497 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800498
Joe Onorato089de882010-04-12 08:18:45 -0700499 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800501 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 mHandler = new Handler(this);
503 mIWindowManager = IWindowManager.Stub.asInterface(
504 ServiceManager.getService(Context.WINDOW_SERVICE));
505 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900506 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 public void executeMessage(Message msg) {
508 handleMessage(msg);
509 }
510 });
satoke7c6998e2011-06-03 17:57:59 +0900511 synchronized (mMethodMap) {
512 mFileManager = new InputMethodFileManager(mMethodMap);
513 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800514
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800515 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 IntentFilter screenOnOffFilt = new IntentFilter();
518 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
519 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700520 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800522
satok913a8922010-08-26 21:53:41 +0900523 mStatusBar = statusBar;
524 statusBar.setIconVisibility("ime", false);
525
satokd87c2592010-09-29 11:52:06 +0900526 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900527 mSettings = new InputMethodSettings(
528 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900530 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531
satokd87c2592010-09-29 11:52:06 +0900532 if (TextUtils.isEmpty(Settings.Secure.getString(
533 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900535 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
537 try {
satokd87c2592010-09-29 11:52:06 +0900538 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 imi.getPackageName(), 0).getResources();
540 if (res.getBoolean(imi.getIsDefaultResourceId())) {
541 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800542 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 }
544 } catch (PackageManager.NameNotFoundException ex) {
545 } catch (Resources.NotFoundException ex) {
546 }
547 }
548 }
satokd87c2592010-09-29 11:52:06 +0900549 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800551 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900554 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 }
556 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 mSettingsObserver = new SettingsObserver(mHandler);
559 updateFromSettingsLocked();
560 }
561
562 @Override
563 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
564 throws RemoteException {
565 try {
566 return super.onTransact(code, data, reply, flags);
567 } catch (RuntimeException e) {
568 // The input method manager only throws security exceptions, so let's
569 // log all others.
570 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800571 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 }
573 throw e;
574 }
575 }
576
577 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700578 synchronized (mMethodMap) {
579 if (!mSystemReady) {
580 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700581 try {
582 startInputInnerLocked();
583 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800584 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700585 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700586 }
587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800589
satoke7c6998e2011-06-03 17:57:59 +0900590 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 public List<InputMethodInfo> getInputMethodList() {
592 synchronized (mMethodMap) {
593 return new ArrayList<InputMethodInfo>(mMethodList);
594 }
595 }
596
satoke7c6998e2011-06-03 17:57:59 +0900597 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 public List<InputMethodInfo> getEnabledInputMethodList() {
599 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900600 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 }
602 }
603
satokbb4aa062011-01-19 21:40:27 +0900604 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
605 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
606 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
607 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
608 for (InputMethodInfo imi: getEnabledInputMethodList()) {
609 enabledInputMethodAndSubtypes.put(
610 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
611 }
612 return enabledInputMethodAndSubtypes;
613 }
614
615 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
616 boolean allowsImplicitlySelectedSubtypes) {
617 if (imi == null && mCurMethodId != null) {
618 imi = mMethodMap.get(mCurMethodId);
619 }
satok7265d9b2011-02-14 15:47:30 +0900620 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900621 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900622 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
623 enabledSubtypes = getApplicableSubtypesLocked(mRes, getSubtypes(imi));
satokbb4aa062011-01-19 21:40:27 +0900624 }
satok7265d9b2011-02-14 15:47:30 +0900625 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900626 }
627
satoke7c6998e2011-06-03 17:57:59 +0900628 @Override
satok16331c82010-12-20 23:48:46 +0900629 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
630 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900631 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900632 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900633 }
634 }
635
satoke7c6998e2011-06-03 17:57:59 +0900636 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 public void addClient(IInputMethodClient client,
638 IInputContext inputContext, int uid, int pid) {
639 synchronized (mMethodMap) {
640 mClients.put(client.asBinder(), new ClientState(client,
641 inputContext, uid, pid));
642 }
643 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800644
satoke7c6998e2011-06-03 17:57:59 +0900645 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 public void removeClient(IInputMethodClient client) {
647 synchronized (mMethodMap) {
648 mClients.remove(client.asBinder());
649 }
650 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 void executeOrSendMessage(IInterface target, Message msg) {
653 if (target.asBinder() instanceof Binder) {
654 mCaller.sendMessage(msg);
655 } else {
656 handleMessage(msg);
657 msg.recycle();
658 }
659 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800660
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700661 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800663 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 + mCurClient.client.asBinder());
665 if (mBoundToMethod) {
666 mBoundToMethod = false;
667 if (mCurMethod != null) {
668 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
669 MSG_UNBIND_INPUT, mCurMethod));
670 }
671 }
672 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
673 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
674 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 // Call setActive(false) on the old client
677 try {
678 mCurClient.client.setActive(false);
679 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800680 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 + mCurClient.pid + " uid " + mCurClient.uid);
682 }
683 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800684
The Android Open Source Project10592532009-03-18 17:39:46 -0700685 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 }
687 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 private int getImeShowFlags() {
690 int flags = 0;
691 if (mShowForced) {
692 flags |= InputMethod.SHOW_FORCED
693 | InputMethod.SHOW_EXPLICIT;
694 } else if (mShowExplicitlyRequested) {
695 flags |= InputMethod.SHOW_EXPLICIT;
696 }
697 return flags;
698 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 private int getAppShowFlags() {
701 int flags = 0;
702 if (mShowForced) {
703 flags |= InputMethodManager.SHOW_FORCED;
704 } else if (!mShowExplicitlyRequested) {
705 flags |= InputMethodManager.SHOW_IMPLICIT;
706 }
707 return flags;
708 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
711 if (!mBoundToMethod) {
712 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
713 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
714 mBoundToMethod = true;
715 }
716 final SessionState session = mCurClient.curSession;
717 if (initial) {
718 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
719 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
720 } else {
721 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
722 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
723 }
724 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800725 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800726 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 }
728 return needResult
729 ? new InputBindResult(session.session, mCurId, mCurSeq)
730 : null;
731 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 InputBindResult startInputLocked(IInputMethodClient client,
734 IInputContext inputContext, EditorInfo attribute,
735 boolean initial, boolean needResult) {
736 // If no method is currently selected, do nothing.
737 if (mCurMethodId == null) {
738 return mNoBinding;
739 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 ClientState cs = mClients.get(client.asBinder());
742 if (cs == null) {
743 throw new IllegalArgumentException("unknown client "
744 + client.asBinder());
745 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 try {
748 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
749 // Check with the window manager to make sure this client actually
750 // has a window with focus. If not, reject. This is thread safe
751 // because if the focus changes some time before or after, the
752 // next client receiving focus that has any interest in input will
753 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800754 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
756 return null;
757 }
758 } catch (RemoteException e) {
759 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 if (mCurClient != cs) {
762 // If the client is changing, we need to switch over to the new
763 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700764 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800765 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 + cs.client.asBinder());
767
768 // If the screen is on, inform the new client it is active
769 if (mScreenOn) {
770 try {
771 cs.client.setActive(mScreenOn);
772 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800773 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 + cs.pid + " uid " + cs.uid);
775 }
776 }
777 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 // Bump up the sequence for this client and attach it.
780 mCurSeq++;
781 if (mCurSeq <= 0) mCurSeq = 1;
782 mCurClient = cs;
783 mCurInputContext = inputContext;
784 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 // Check if the input method is changing.
787 if (mCurId != null && mCurId.equals(mCurMethodId)) {
788 if (cs.curSession != null) {
789 // Fast case: if we are already connected to the input method,
790 // then just return it.
791 return attachNewInputLocked(initial, needResult);
792 }
793 if (mHaveConnection) {
794 if (mCurMethod != null) {
795 if (!cs.sessionRequested) {
796 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800797 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
799 MSG_CREATE_SESSION, mCurMethod,
800 new MethodCallback(mCurMethod)));
801 }
802 // Return to client, and we will get back with it when
803 // we have had a session made for it.
804 return new InputBindResult(null, mCurId, mCurSeq);
805 } else if (SystemClock.uptimeMillis()
806 < (mLastBindTime+TIME_TO_RECONNECT)) {
807 // In this case we have connected to the service, but
808 // don't yet have its interface. If it hasn't been too
809 // long since we did the connection, we'll return to
810 // the client and wait to get the service interface so
811 // we can report back. If it has been too long, we want
812 // to fall through so we can try a disconnect/reconnect
813 // to see if we can get back in touch with the service.
814 return new InputBindResult(null, mCurId, mCurSeq);
815 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800816 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
817 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 }
819 }
820 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800821
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700822 return startInputInnerLocked();
823 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800824
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700825 InputBindResult startInputInnerLocked() {
826 if (mCurMethodId == null) {
827 return mNoBinding;
828 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800829
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700830 if (!mSystemReady) {
831 // If the system is not yet ready, we shouldn't be running third
832 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700833 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700834 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 InputMethodInfo info = mMethodMap.get(mCurMethodId);
837 if (info == null) {
838 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
839 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800840
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700841 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
844 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700845 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
846 com.android.internal.R.string.input_method_binding_label);
847 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
848 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
850 mLastBindTime = SystemClock.uptimeMillis();
851 mHaveConnection = true;
852 mCurId = info.getId();
853 mCurToken = new Binder();
854 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800855 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 mIWindowManager.addWindowToken(mCurToken,
857 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
858 } catch (RemoteException e) {
859 }
860 return new InputBindResult(null, mCurId, mCurSeq);
861 } else {
862 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800863 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 + mCurIntent);
865 }
866 return null;
867 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800868
satoke7c6998e2011-06-03 17:57:59 +0900869 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 public InputBindResult startInput(IInputMethodClient client,
871 IInputContext inputContext, EditorInfo attribute,
872 boolean initial, boolean needResult) {
873 synchronized (mMethodMap) {
874 final long ident = Binder.clearCallingIdentity();
875 try {
876 return startInputLocked(client, inputContext, attribute,
877 initial, needResult);
878 } finally {
879 Binder.restoreCallingIdentity(ident);
880 }
881 }
882 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800883
satoke7c6998e2011-06-03 17:57:59 +0900884 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 public void finishInput(IInputMethodClient client) {
886 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800887
satoke7c6998e2011-06-03 17:57:59 +0900888 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 public void onServiceConnected(ComponentName name, IBinder service) {
890 synchronized (mMethodMap) {
891 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
892 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700893 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800894 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700895 unbindCurrentMethodLocked(false);
896 return;
897 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800898 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700899 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
900 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800902 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700903 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700905 MSG_CREATE_SESSION, mCurMethod,
906 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 }
908 }
909 }
910 }
911
912 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
913 synchronized (mMethodMap) {
914 if (mCurMethod != null && method != null
915 && mCurMethod.asBinder() == method.asBinder()) {
916 if (mCurClient != null) {
917 mCurClient.curSession = new SessionState(mCurClient,
918 method, session);
919 mCurClient.sessionRequested = false;
920 InputBindResult res = attachNewInputLocked(true, true);
921 if (res.method != null) {
922 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
923 MSG_BIND_METHOD, mCurClient.client, res));
924 }
925 }
926 }
927 }
928 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800929
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700930 void unbindCurrentMethodLocked(boolean reportToClient) {
931 if (mHaveConnection) {
932 mContext.unbindService(this);
933 mHaveConnection = false;
934 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800935
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700936 if (mCurToken != null) {
937 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800938 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700939 mIWindowManager.removeWindowToken(mCurToken);
940 } catch (RemoteException e) {
941 }
942 mCurToken = null;
943 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800944
The Android Open Source Project10592532009-03-18 17:39:46 -0700945 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700946 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800947
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700948 if (reportToClient && mCurClient != null) {
949 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
950 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
951 }
952 }
953
Devin Taylor0c33ed22010-02-23 13:26:46 -0600954 private void finishSession(SessionState sessionState) {
955 if (sessionState != null && sessionState.session != null) {
956 try {
957 sessionState.session.finishSession();
958 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700959 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600960 }
961 }
962 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800963
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700964 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 if (mCurMethod != null) {
966 for (ClientState cs : mClients.values()) {
967 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600968 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 cs.curSession = null;
970 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600971
972 finishSession(mEnabledSession);
973 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 mCurMethod = null;
975 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700976 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800978
satoke7c6998e2011-06-03 17:57:59 +0900979 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 public void onServiceDisconnected(ComponentName name) {
981 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800982 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 + " mCurIntent=" + mCurIntent);
984 if (mCurMethod != null && mCurIntent != null
985 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700986 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 // We consider this to be a new bind attempt, since the system
988 // should now try to restart the service for us.
989 mLastBindTime = SystemClock.uptimeMillis();
990 mShowRequested = mInputShown;
991 mInputShown = false;
992 if (mCurClient != null) {
993 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
994 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
995 }
996 }
997 }
998 }
999
satokf9f01002011-05-19 21:31:50 +09001000 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001002 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 long ident = Binder.clearCallingIdentity();
1004 try {
1005 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001006 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 return;
1008 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 synchronized (mMethodMap) {
1011 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001012 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -07001013 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001015 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -07001016 mStatusBar.setIcon("ime", packageName, iconId, 0);
1017 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 }
1019 }
1020 } finally {
1021 Binder.restoreCallingIdentity(ident);
1022 }
1023 }
1024
satokf9f01002011-05-19 21:31:50 +09001025 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001026 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001027 int uid = Binder.getCallingUid();
1028 long ident = Binder.clearCallingIdentity();
1029 try {
1030 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001031 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001032 return;
1033 }
1034
1035 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001036 mImeWindowVis = vis;
1037 mBackDisposition = backDisposition;
1038 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
satok06487a52010-10-29 11:37:18 +09001039 }
1040 } finally {
1041 Binder.restoreCallingIdentity(ident);
1042 }
1043 }
1044
satoke7c6998e2011-06-03 17:57:59 +09001045 @Override
satokf9f01002011-05-19 21:31:50 +09001046 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1047 synchronized (mMethodMap) {
1048 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1049 for (int i = 0; i < spans.length; ++i) {
1050 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001051 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001052 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001053 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001054 }
1055 }
1056 }
1057 }
1058
satoke7c6998e2011-06-03 17:57:59 +09001059 @Override
satokf9f01002011-05-19 21:31:50 +09001060 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1061 synchronized (mMethodMap) {
1062 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1063 // TODO: Do not send the intent if the process of the targetImi is already dead.
1064 if (targetImi != null) {
1065 final String[] suggestions = span.getSuggestions();
1066 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001067 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001068 final Intent intent = new Intent();
1069 // Ensures that only a class in the original IME package will receive the
1070 // notification.
satok42c5a162011-05-26 16:46:14 +09001071 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001072 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1073 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1074 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1075 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1076 mContext.sendBroadcast(intent);
1077 return true;
1078 }
1079 }
1080 return false;
1081 }
1082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001084 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1085 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1086 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1087 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001089 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001090 // There is no input method selected, try to choose new applicable input method.
1091 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1092 id = Settings.Secure.getString(mContext.getContentResolver(),
1093 Settings.Secure.DEFAULT_INPUT_METHOD);
1094 }
1095 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 try {
satokab751aa2010-09-14 19:17:36 +09001097 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001099 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001100 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001101 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 }
satokf3db1af2010-11-23 13:34:33 +09001103 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001104 } else {
1105 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001106 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001107 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 }
1109 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001110
satokab751aa2010-09-14 19:17:36 +09001111 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 InputMethodInfo info = mMethodMap.get(id);
1113 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001114 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001118 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001119 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1120 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001121 }
1122 if (subtype != mCurrentSubtype) {
1123 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001124 if (subtype != null) {
1125 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1126 }
satokcd7cd292010-11-20 15:46:23 +09001127 if (mCurMethod != null) {
1128 try {
satoke40dea02011-01-30 01:14:02 +09001129 final Configuration conf = mRes.getConfiguration();
1130 final boolean haveHardKeyboard = conf.keyboard
1131 != Configuration.KEYBOARD_NOKEYS;
1132 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001133 && conf.hardKeyboardHidden
1134 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001135 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1136 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1137 : 0;
Joe Onorato857fd9b2011-01-27 15:08:35 -08001138 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1139 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001140 // If subtype is null, try to find the most applicable one from
1141 // getCurrentInputMethodSubtype.
1142 if (subtype == null) {
1143 subtype = getCurrentInputMethodSubtype();
1144 }
1145 mCurMethod.changeInputMethodSubtype(subtype);
1146 } catch (RemoteException e) {
1147 return;
satokab751aa2010-09-14 19:17:36 +09001148 }
1149 }
1150 }
1151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 return;
1153 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 final long ident = Binder.clearCallingIdentity();
1156 try {
satokab751aa2010-09-14 19:17:36 +09001157 // Set a subtype to this input method.
1158 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001159 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1160 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1161 // because mCurMethodId is stored as a history in
1162 // setSelectedInputMethodAndSubtypeLocked().
1163 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164
1165 if (ActivityManagerNative.isSystemReady()) {
1166 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001167 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 intent.putExtra("input_method_id", id);
1169 mContext.sendBroadcast(intent);
1170 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001171 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 } finally {
1173 Binder.restoreCallingIdentity(ident);
1174 }
1175 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001176
satok42c5a162011-05-26 16:46:14 +09001177 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001178 public boolean showSoftInput(IInputMethodClient client, int flags,
1179 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001180 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 long ident = Binder.clearCallingIdentity();
1182 try {
1183 synchronized (mMethodMap) {
1184 if (mCurClient == null || client == null
1185 || mCurClient.client.asBinder() != client.asBinder()) {
1186 try {
1187 // We need to check if this is the current client with
1188 // focus in the window manager, to allow this call to
1189 // be made before input is started in it.
1190 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001191 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001192 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 }
1194 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001195 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 }
1197 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001198
Joe Onorato8a9b2202010-02-26 18:56:32 -08001199 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001200 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
1202 } finally {
1203 Binder.restoreCallingIdentity(ident);
1204 }
1205 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001206
The Android Open Source Project4df24232009-03-05 14:34:35 -08001207 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 mShowRequested = true;
1209 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1210 mShowExplicitlyRequested = true;
1211 }
1212 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1213 mShowExplicitlyRequested = true;
1214 mShowForced = true;
1215 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001216
Dianne Hackborncc278702009-09-02 23:07:23 -07001217 if (!mSystemReady) {
1218 return false;
1219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001220
The Android Open Source Project4df24232009-03-05 14:34:35 -08001221 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001223 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1224 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1225 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001227 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 } else if (mHaveConnection && SystemClock.uptimeMillis()
1229 < (mLastBindTime+TIME_TO_RECONNECT)) {
1230 // The client has asked to have the input method shown, but
1231 // we have been sitting here too long with a connection to the
1232 // service and no interface received, so let's disconnect/connect
1233 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001234 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 SystemClock.uptimeMillis()-mLastBindTime,1);
1236 mContext.unbindService(this);
1237 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1238 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001239
The Android Open Source Project4df24232009-03-05 14:34:35 -08001240 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001242
satok42c5a162011-05-26 16:46:14 +09001243 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001244 public boolean hideSoftInput(IInputMethodClient client, int flags,
1245 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001246 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 long ident = Binder.clearCallingIdentity();
1248 try {
1249 synchronized (mMethodMap) {
1250 if (mCurClient == null || client == null
1251 || mCurClient.client.asBinder() != client.asBinder()) {
1252 try {
1253 // We need to check if this is the current client with
1254 // focus in the window manager, to allow this call to
1255 // be made before input is started in it.
1256 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001257 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1258 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001259 mImeWindowVis = 0;
1260 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1261 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001262 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 }
1264 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001265 mImeWindowVis = 0;
1266 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001267 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 }
1269 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001270
Joe Onorato8a9b2202010-02-26 18:56:32 -08001271 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001272 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 }
1274 } finally {
1275 Binder.restoreCallingIdentity(ident);
1276 }
1277 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001278
The Android Open Source Project4df24232009-03-05 14:34:35 -08001279 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1281 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001282 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001284 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 }
1286 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001287 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001289 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001291 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001293 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1294 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1295 res = true;
1296 } else {
1297 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 }
1299 mInputShown = false;
1300 mShowRequested = false;
1301 mShowExplicitlyRequested = false;
1302 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001303 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001305
satok42c5a162011-05-26 16:46:14 +09001306 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001307 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1309 boolean first, int windowFlags) {
1310 long ident = Binder.clearCallingIdentity();
1311 try {
1312 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001313 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 + " viewHasFocus=" + viewHasFocus
1315 + " isTextEditor=" + isTextEditor
1316 + " softInputMode=#" + Integer.toHexString(softInputMode)
1317 + " first=" + first + " flags=#"
1318 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 if (mCurClient == null || client == null
1321 || mCurClient.client.asBinder() != client.asBinder()) {
1322 try {
1323 // We need to check if this is the current client with
1324 // focus in the window manager, to allow this call to
1325 // be made before input is started in it.
1326 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001327 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 return;
1329 }
1330 } catch (RemoteException e) {
1331 }
1332 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001333
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001334 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001335 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001336 return;
1337 }
1338 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001339
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001340 // Should we auto-show the IME even if the caller has not
1341 // specified what should be done with it?
1342 // We only do this automatically if the window can resize
1343 // to accommodate the IME (so what the user sees will give
1344 // them good context without input information being obscured
1345 // by the IME) or if running on a large screen where there
1346 // is more room for the target window + IME.
1347 final boolean doAutoShow =
1348 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1349 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1350 || mRes.getConfiguration().isLayoutSizeAtLeast(
1351 Configuration.SCREENLAYOUT_SIZE_LARGE);
1352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1354 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001355 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1357 // There is no focus view, and this window will
1358 // be behind any soft input window, so hide the
1359 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001360 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001361 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001363 } else if (isTextEditor && doAutoShow && (softInputMode &
1364 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 // There is a focus view, and we are navigating forward
1366 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001367 // We only do this automatically if the window an resize
1368 // to accomodate the IME (so what the user sees will give
1369 // them good context without input information being obscured
1370 // by the IME) or if running on a large screen where there
1371 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001372 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001373 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 }
1375 break;
1376 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1377 // Do nothing.
1378 break;
1379 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1380 if ((softInputMode &
1381 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001382 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001383 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 }
1385 break;
1386 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001387 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001388 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 break;
1390 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1391 if ((softInputMode &
1392 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001393 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001394 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 }
1396 break;
1397 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001398 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001399 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 break;
1401 }
1402 }
1403 } finally {
1404 Binder.restoreCallingIdentity(ident);
1405 }
1406 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001407
satok42c5a162011-05-26 16:46:14 +09001408 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1410 synchronized (mMethodMap) {
1411 if (mCurClient == null || client == null
1412 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001413 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001414 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 }
1416
satok440aab52010-11-25 09:43:11 +09001417 // Always call subtype picker, because subtype picker is a superset of input method
1418 // picker.
satokab751aa2010-09-14 19:17:36 +09001419 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1420 }
1421 }
1422
satok42c5a162011-05-26 16:46:14 +09001423 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001425 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1426 }
1427
satok42c5a162011-05-26 16:46:14 +09001428 @Override
satok28203512010-11-24 11:06:49 +09001429 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1430 synchronized (mMethodMap) {
1431 if (subtype != null) {
1432 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1433 mMethodMap.get(id), subtype.hashCode()));
1434 } else {
1435 setInputMethod(token, id);
1436 }
1437 }
satokab751aa2010-09-14 19:17:36 +09001438 }
1439
satok42c5a162011-05-26 16:46:14 +09001440 @Override
satokb416a712010-11-25 20:42:14 +09001441 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001442 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001443 synchronized (mMethodMap) {
1444 if (mCurClient == null || client == null
1445 || mCurClient.client.asBinder() != client.asBinder()) {
1446 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1447 }
satok7fee71f2010-12-17 18:54:26 +09001448 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1449 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001450 }
1451 }
1452
satok4fc87d62011-05-20 16:13:43 +09001453 @Override
satok735cf382010-11-11 20:40:09 +09001454 public boolean switchToLastInputMethod(IBinder token) {
1455 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001456 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001457 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001458 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001459 lastImi = mMethodMap.get(lastIme.first);
1460 } else {
1461 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001462 }
satok4fc87d62011-05-20 16:13:43 +09001463 String targetLastImiId = null;
1464 int subtypeId = NOT_A_SUBTYPE_ID;
1465 if (lastIme != null && lastImi != null) {
1466 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1467 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1468 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1469 : mCurrentSubtype.hashCode();
1470 // If the last IME is the same as the current IME and the last subtype is not
1471 // defined, there is no need to switch to the last IME.
1472 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1473 targetLastImiId = lastIme.first;
1474 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1475 }
1476 }
1477
1478 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1479 // This is a safety net. If the currentSubtype can't be added to the history
1480 // and the framework couldn't find the last ime, we will make the last ime be
1481 // the most applicable enabled keyboard subtype of the system imes.
1482 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1483 if (enabled != null) {
1484 final int N = enabled.size();
1485 final String locale = mCurrentSubtype == null
1486 ? mRes.getConfiguration().locale.toString()
1487 : mCurrentSubtype.getLocale();
1488 for (int i = 0; i < N; ++i) {
1489 final InputMethodInfo imi = enabled.get(i);
1490 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1491 InputMethodSubtype keyboardSubtype =
1492 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1493 SUBTYPE_MODE_KEYBOARD, locale, true);
1494 if (keyboardSubtype != null) {
1495 targetLastImiId = imi.getId();
1496 subtypeId = getSubtypeIdFromHashCode(
1497 imi, keyboardSubtype.hashCode());
1498 if(keyboardSubtype.getLocale().equals(locale)) {
1499 break;
1500 }
1501 }
1502 }
1503 }
1504 }
1505 }
1506
1507 if (!TextUtils.isEmpty(targetLastImiId)) {
1508 if (DEBUG) {
1509 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1510 + ", from: " + mCurMethodId + ", " + subtypeId);
1511 }
1512 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1513 return true;
1514 } else {
1515 return false;
1516 }
satok735cf382010-11-11 20:40:09 +09001517 }
1518 }
1519
satoke7c6998e2011-06-03 17:57:59 +09001520 @Override
satok68f1b782011-04-11 14:26:04 +09001521 public InputMethodSubtype getLastInputMethodSubtype() {
1522 synchronized (mMethodMap) {
1523 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1524 // TODO: Handle the case of the last IME with no subtypes
1525 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1526 || TextUtils.isEmpty(lastIme.second)) return null;
1527 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1528 if (lastImi == null) return null;
1529 try {
1530 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1531 return lastImi.getSubtypeAt(getSubtypeIdFromHashCode(
1532 lastImi, lastSubtypeHash));
1533 } catch (NumberFormatException e) {
1534 return null;
1535 }
1536 }
1537 }
1538
satoke7c6998e2011-06-03 17:57:59 +09001539 @Override
1540 public boolean setAdditionalInputMethodSubtypes(IBinder token, InputMethodSubtype[] subtypes) {
1541 if (token == null || mCurToken != token) {
1542 return false;
1543 }
1544 if (subtypes == null || subtypes.length == 0) return false;
1545 synchronized (mMethodMap) {
1546 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1547 if (imi == null) return false;
1548 final int N = subtypes.length;
1549 mFileManager.addInputMethodSubtypes(mCurMethodId, subtypes);
1550 buildInputMethodListLocked(mMethodList, mMethodMap);
1551 return true;
1552 }
1553 }
1554
satok28203512010-11-24 11:06:49 +09001555 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 synchronized (mMethodMap) {
1557 if (token == null) {
1558 if (mContext.checkCallingOrSelfPermission(
1559 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1560 != PackageManager.PERMISSION_GRANTED) {
1561 throw new SecurityException(
1562 "Using null token requires permission "
1563 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1564 }
1565 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001566 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1567 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 return;
1569 }
1570
1571 long ident = Binder.clearCallingIdentity();
1572 try {
satokab751aa2010-09-14 19:17:36 +09001573 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 } finally {
1575 Binder.restoreCallingIdentity(ident);
1576 }
1577 }
1578 }
1579
satok42c5a162011-05-26 16:46:14 +09001580 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 public void hideMySoftInput(IBinder token, int flags) {
1582 synchronized (mMethodMap) {
1583 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001584 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1585 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 return;
1587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 long ident = Binder.clearCallingIdentity();
1589 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001590 hideCurrentInputLocked(flags, null);
1591 } finally {
1592 Binder.restoreCallingIdentity(ident);
1593 }
1594 }
1595 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001596
satok42c5a162011-05-26 16:46:14 +09001597 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001598 public void showMySoftInput(IBinder token, int flags) {
1599 synchronized (mMethodMap) {
1600 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001601 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1602 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001603 return;
1604 }
1605 long ident = Binder.clearCallingIdentity();
1606 try {
1607 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 } finally {
1609 Binder.restoreCallingIdentity(ident);
1610 }
1611 }
1612 }
1613
1614 void setEnabledSessionInMainThread(SessionState session) {
1615 if (mEnabledSession != session) {
1616 if (mEnabledSession != null) {
1617 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001618 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 mEnabledSession.method.setSessionEnabled(
1620 mEnabledSession.session, false);
1621 } catch (RemoteException e) {
1622 }
1623 }
1624 mEnabledSession = session;
1625 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001626 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 session.method.setSessionEnabled(
1628 session.session, true);
1629 } catch (RemoteException e) {
1630 }
1631 }
1632 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001633
satok42c5a162011-05-26 16:46:14 +09001634 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 public boolean handleMessage(Message msg) {
1636 HandlerCaller.SomeArgs args;
1637 switch (msg.what) {
1638 case MSG_SHOW_IM_PICKER:
1639 showInputMethodMenu();
1640 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001641
satokab751aa2010-09-14 19:17:36 +09001642 case MSG_SHOW_IM_SUBTYPE_PICKER:
1643 showInputMethodSubtypeMenu();
1644 return true;
1645
satok47a44912010-10-06 16:03:58 +09001646 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001647 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001648 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001649 return true;
1650
1651 case MSG_SHOW_IM_CONFIG:
1652 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001653 return true;
1654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 case MSG_UNBIND_INPUT:
1658 try {
1659 ((IInputMethod)msg.obj).unbindInput();
1660 } catch (RemoteException e) {
1661 // There is nothing interesting about the method dying.
1662 }
1663 return true;
1664 case MSG_BIND_INPUT:
1665 args = (HandlerCaller.SomeArgs)msg.obj;
1666 try {
1667 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1668 } catch (RemoteException e) {
1669 }
1670 return true;
1671 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001672 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001674 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1675 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 } catch (RemoteException e) {
1677 }
1678 return true;
1679 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001680 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001682 ((IInputMethod)args.arg1).hideSoftInput(0,
1683 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 } catch (RemoteException e) {
1685 }
1686 return true;
1687 case MSG_ATTACH_TOKEN:
1688 args = (HandlerCaller.SomeArgs)msg.obj;
1689 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001690 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1692 } catch (RemoteException e) {
1693 }
1694 return true;
1695 case MSG_CREATE_SESSION:
1696 args = (HandlerCaller.SomeArgs)msg.obj;
1697 try {
1698 ((IInputMethod)args.arg1).createSession(
1699 (IInputMethodCallback)args.arg2);
1700 } catch (RemoteException e) {
1701 }
1702 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 case MSG_START_INPUT:
1706 args = (HandlerCaller.SomeArgs)msg.obj;
1707 try {
1708 SessionState session = (SessionState)args.arg1;
1709 setEnabledSessionInMainThread(session);
1710 session.method.startInput((IInputContext)args.arg2,
1711 (EditorInfo)args.arg3);
1712 } catch (RemoteException e) {
1713 }
1714 return true;
1715 case MSG_RESTART_INPUT:
1716 args = (HandlerCaller.SomeArgs)msg.obj;
1717 try {
1718 SessionState session = (SessionState)args.arg1;
1719 setEnabledSessionInMainThread(session);
1720 session.method.restartInput((IInputContext)args.arg2,
1721 (EditorInfo)args.arg3);
1722 } catch (RemoteException e) {
1723 }
1724 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 case MSG_UNBIND_METHOD:
1729 try {
1730 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1731 } catch (RemoteException e) {
1732 // There is nothing interesting about the last client dying.
1733 }
1734 return true;
1735 case MSG_BIND_METHOD:
1736 args = (HandlerCaller.SomeArgs)msg.obj;
1737 try {
1738 ((IInputMethodClient)args.arg1).onBindMethod(
1739 (InputBindResult)args.arg2);
1740 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001741 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 }
1743 return true;
1744 }
1745 return false;
1746 }
1747
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001748 private boolean isSystemIme(InputMethodInfo inputMethod) {
1749 return (inputMethod.getServiceInfo().applicationInfo.flags
1750 & ApplicationInfo.FLAG_SYSTEM) != 0;
1751 }
1752
Ken Wakasa586f0512011-01-20 22:31:01 +09001753 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1754 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1755 final int subtypeCount = imi.getSubtypeCount();
1756 for (int i = 0; i < subtypeCount; ++i) {
1757 subtypes.add(imi.getSubtypeAt(i));
1758 }
1759 return subtypes;
1760 }
1761
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001762 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001763 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001764 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001765 // We'd prefer to fall back on a system IME, since that is safer.
1766 int i=enabled.size();
1767 while (i > 0) {
1768 i--;
1769 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1770 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1771 break;
1772 }
1773 }
satokab751aa2010-09-14 19:17:36 +09001774 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001775 if (DEBUG) {
1776 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1777 }
satok723a27e2010-11-11 14:58:11 +09001778 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001779 return true;
1780 }
1781
1782 return false;
1783 }
1784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1786 HashMap<String, InputMethodInfo> map) {
1787 list.clear();
1788 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001791 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001792 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1793 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1794 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1795 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796
1797 List<ResolveInfo> services = pm.queryIntentServices(
1798 new Intent(InputMethod.SERVICE_INTERFACE),
1799 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001800
satoke7c6998e2011-06-03 17:57:59 +09001801 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
1802 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 for (int i = 0; i < services.size(); ++i) {
1804 ResolveInfo ri = services.get(i);
1805 ServiceInfo si = ri.serviceInfo;
1806 ComponentName compName = new ComponentName(si.packageName, si.name);
1807 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1808 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001809 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 + ": it does not require the permission "
1811 + android.Manifest.permission.BIND_INPUT_METHOD);
1812 continue;
1813 }
1814
Joe Onorato8a9b2202010-02-26 18:56:32 -08001815 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816
1817 try {
satoke7c6998e2011-06-03 17:57:59 +09001818 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001820 final String id = p.getId();
1821 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822
Amith Yamasanie861ec12010-03-24 21:39:27 -07001823 // System IMEs are enabled by default, unless there's a hard keyboard
1824 // and the system IME was explicitly disabled
1825 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1826 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001827 }
1828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001830 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001834 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001836 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 }
1838 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001839
1840 String defaultIme = Settings.Secure.getString(mContext
1841 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001842 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001843 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001844 updateFromSettingsLocked();
1845 }
1846 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001850
satokab751aa2010-09-14 19:17:36 +09001851 private void showInputMethodMenu() {
1852 showInputMethodMenuInternal(false);
1853 }
1854
1855 private void showInputMethodSubtypeMenu() {
1856 showInputMethodMenuInternal(true);
1857 }
1858
satok217f5482010-12-15 05:19:19 +09001859 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001860 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001861 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001862 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1863 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001864 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001865 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001866 }
satok217f5482010-12-15 05:19:19 +09001867 mContext.startActivity(intent);
1868 }
1869
1870 private void showConfigureInputMethods() {
1871 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1872 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1873 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1874 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001875 mContext.startActivity(intent);
1876 }
1877
satokab751aa2010-09-14 19:17:36 +09001878 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001879 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 String lastInputMethodId = Settings.Secure.getString(context
1886 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001887 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001888 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001889
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001890 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09001891 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
1892 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001893 if (immis == null || immis.size() == 0) {
1894 return;
1895 }
1896
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001897 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898
Ken Wakasa761eb372011-03-04 19:06:18 +09001899 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
1900 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
1901 new Comparator<InputMethodInfo>() {
1902 @Override
1903 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
1904 if (imi2 == null) return 0;
1905 if (imi1 == null) return 1;
1906 if (pm == null) {
1907 return imi1.getId().compareTo(imi2.getId());
1908 }
1909 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
1910 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
1911 return imiId1.toString().compareTo(imiId2.toString());
1912 }
1913 });
satok913a8922010-08-26 21:53:41 +09001914
Ken Wakasa761eb372011-03-04 19:06:18 +09001915 sortedImmis.putAll(immis);
1916
1917 final ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>> imList =
1918 new ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>>();
1919
1920 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09001921 if (imi == null) continue;
1922 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09001923 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09001924 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
1925 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09001926 }
satokbb4aa062011-01-19 21:40:27 +09001927 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa81f6e3d2011-03-04 09:59:52 +09001928 final CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001929 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09001930 final int subtypeCount = imi.getSubtypeCount();
Ken Wakasa586f0512011-01-20 22:31:01 +09001931 for (int j = 0; j < subtypeCount; ++j) {
satokbb4aa062011-01-19 21:40:27 +09001932 InputMethodSubtype subtype = imi.getSubtypeAt(j);
satok9b415792011-05-30 12:37:52 +09001933 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))
1934 && !subtype.isAuxiliary()) {
Ken Wakasa761eb372011-03-04 19:06:18 +09001935 final CharSequence title;
satok7f35c8c2010-10-07 21:13:11 +09001936 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001937 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001938 if (nameResId != 0) {
satok4f313532011-06-01 16:13:45 +09001939 title = TextUtils.concat(subtype.getDisplayName(context,
1940 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
Ken Wakasa761eb372011-03-04 19:06:18 +09001941 (TextUtils.isEmpty(label) ? "" : " (" + label + ")"));
satok7f35c8c2010-10-07 21:13:11 +09001942 } else {
1943 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001944 // TODO: Use more friendly Title and UI
1945 title = label + "," + (mode == null ? "" : mode) + ","
1946 + (language == null ? "" : language);
1947 }
Ken Wakasa761eb372011-03-04 19:06:18 +09001948 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
1949 title, new Pair<InputMethodInfo, Integer>(imi, j)));
satokab751aa2010-09-14 19:17:36 +09001950 }
satokab751aa2010-09-14 19:17:36 +09001951 }
1952 } else {
Ken Wakasa761eb372011-03-04 19:06:18 +09001953 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
1954 label, new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID)));
satokab751aa2010-09-14 19:17:36 +09001955 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001956 }
satok913a8922010-08-26 21:53:41 +09001957
Ken Wakasa761eb372011-03-04 19:06:18 +09001958 final int N = imList.size();
1959 mItems = new CharSequence[N];
1960 for (int i = 0; i < N; ++i) {
1961 mItems[i] = imList.get(i).first;
1962 }
satokab751aa2010-09-14 19:17:36 +09001963 mIms = new InputMethodInfo[N];
1964 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001965 int checkedItem = 0;
1966 for (int i = 0; i < N; ++i) {
Ken Wakasa761eb372011-03-04 19:06:18 +09001967 Pair<InputMethodInfo, Integer> value = imList.get(i).second;
satokab751aa2010-09-14 19:17:36 +09001968 mIms[i] = value.first;
1969 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001970 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001971 int subtypeId = mSubtypeIds[i];
1972 if ((subtypeId == NOT_A_SUBTYPE_ID)
1973 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1974 || (subtypeId == lastInputMethodSubtypeId)) {
1975 checkedItem = i;
1976 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 }
satokab751aa2010-09-14 19:17:36 +09001979
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001980 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09001981 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001982 public void onClick(DialogInterface dialog, int which) {
1983 hideInputMethodMenu();
1984 }
1985 };
satokd87c2592010-09-29 11:52:06 +09001986
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001987 TypedArray a = context.obtainStyledAttributes(null,
1988 com.android.internal.R.styleable.DialogPreference,
1989 com.android.internal.R.attr.alertDialogStyle, 0);
1990 mDialogBuilder = new AlertDialog.Builder(context)
1991 .setTitle(com.android.internal.R.string.select_input_method)
1992 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09001993 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001994 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001996 }
1997 })
1998 .setIcon(a.getDrawable(
1999 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2000 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002001
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002002 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
2003 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002004 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002005 public void onClick(DialogInterface dialog, int which) {
2006 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002007 if (mIms == null || mIms.length <= which
2008 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002009 return;
2010 }
2011 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002012 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002013 hideInputMethodMenu();
2014 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002015 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002016 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002017 subtypeId = NOT_A_SUBTYPE_ID;
2018 }
2019 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002020 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002021 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002023 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024
satok7f35c8c2010-10-07 21:13:11 +09002025 if (showSubtypes) {
satok82beadf2010-12-27 19:03:06 +09002026 mDialogBuilder.setPositiveButton(
2027 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002028 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002029 @Override
satok7f35c8c2010-10-07 21:13:11 +09002030 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002031 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002032 }
2033 });
2034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002036 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 mSwitchingDialog.getWindow().setType(
2038 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002039 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 mSwitchingDialog.show();
2041 }
2042 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002045 synchronized (mMethodMap) {
2046 hideInputMethodMenuLocked();
2047 }
2048 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002049
The Android Open Source Project10592532009-03-18 17:39:46 -07002050 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002051 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052
The Android Open Source Project10592532009-03-18 17:39:46 -07002053 if (mSwitchingDialog != null) {
2054 mSwitchingDialog.dismiss();
2055 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002057
The Android Open Source Project10592532009-03-18 17:39:46 -07002058 mDialogBuilder = null;
2059 mItems = null;
2060 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002064
satok42c5a162011-05-26 16:46:14 +09002065 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 public boolean setInputMethodEnabled(String id, boolean enabled) {
2067 synchronized (mMethodMap) {
2068 if (mContext.checkCallingOrSelfPermission(
2069 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2070 != PackageManager.PERMISSION_GRANTED) {
2071 throw new SecurityException(
2072 "Requires permission "
2073 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2074 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 long ident = Binder.clearCallingIdentity();
2077 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002078 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 } finally {
2080 Binder.restoreCallingIdentity(ident);
2081 }
2082 }
2083 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002084
2085 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2086 // Make sure this is a valid input method.
2087 InputMethodInfo imm = mMethodMap.get(id);
2088 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002089 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002090 }
2091
satokd87c2592010-09-29 11:52:06 +09002092 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2093 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002094
satokd87c2592010-09-29 11:52:06 +09002095 if (enabled) {
2096 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2097 if (pair.first.equals(id)) {
2098 // We are enabling this input method, but it is already enabled.
2099 // Nothing to do. The previous state was enabled.
2100 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002101 }
2102 }
satokd87c2592010-09-29 11:52:06 +09002103 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2104 // Previous state was disabled.
2105 return false;
2106 } else {
2107 StringBuilder builder = new StringBuilder();
2108 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2109 builder, enabledInputMethodsList, id)) {
2110 // Disabled input method is currently selected, switch to another one.
2111 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2112 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002113 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2114 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2115 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002116 }
2117 // Previous state was enabled.
2118 return true;
2119 } else {
2120 // We are disabling the input method but it is already disabled.
2121 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002122 return false;
2123 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002124 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002125 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002126
satok57ffc002011-01-25 00:11:47 +09002127 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2128 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002129 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002130 }
2131
satok723a27e2010-11-11 14:58:11 +09002132 private void saveCurrentInputMethodAndSubtypeToHistory() {
2133 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2134 if (mCurrentSubtype != null) {
2135 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2136 }
satok57ffc002011-01-25 00:11:47 +09002137 if (canAddToLastInputMethod(mCurrentSubtype)) {
2138 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2139 }
satokab751aa2010-09-14 19:17:36 +09002140 }
2141
satok723a27e2010-11-11 14:58:11 +09002142 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2143 boolean setSubtypeOnly) {
2144 // Update the history of InputMethod and Subtype
2145 saveCurrentInputMethodAndSubtypeToHistory();
2146
2147 // Set Subtype here
2148 if (imi == null || subtypeId < 0) {
2149 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002150 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002151 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002152 if (subtypeId < imi.getSubtypeCount()) {
2153 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2154 mSettings.putSelectedSubtype(subtype.hashCode());
2155 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002156 } else {
2157 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2158 mCurrentSubtype = null;
2159 }
satokab751aa2010-09-14 19:17:36 +09002160 }
satok723a27e2010-11-11 14:58:11 +09002161
2162 if (!setSubtypeOnly) {
2163 // Set InputMethod here
2164 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2165 }
2166 }
2167
2168 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2169 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2170 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2171 // newDefaultIme is empty when there is no candidate for the selected IME.
2172 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2173 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2174 if (subtypeHashCode != null) {
2175 try {
2176 lastSubtypeId = getSubtypeIdFromHashCode(
2177 imi, Integer.valueOf(subtypeHashCode));
2178 } catch (NumberFormatException e) {
2179 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2180 }
2181 }
2182 }
2183 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002184 }
2185
2186 private int getSelectedInputMethodSubtypeId(String id) {
2187 InputMethodInfo imi = mMethodMap.get(id);
2188 if (imi == null) {
2189 return NOT_A_SUBTYPE_ID;
2190 }
satokab751aa2010-09-14 19:17:36 +09002191 int subtypeId;
2192 try {
2193 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2194 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2195 } catch (SettingNotFoundException e) {
2196 return NOT_A_SUBTYPE_ID;
2197 }
satok723a27e2010-11-11 14:58:11 +09002198 return getSubtypeIdFromHashCode(imi, subtypeId);
2199 }
2200
2201 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002202 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002203 final int subtypeCount = imi.getSubtypeCount();
2204 for (int i = 0; i < subtypeCount; ++i) {
2205 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002206 if (subtypeHashCode == ims.hashCode()) {
2207 return i;
2208 }
satokab751aa2010-09-14 19:17:36 +09002209 }
2210 }
2211 return NOT_A_SUBTYPE_ID;
2212 }
2213
satokdf31ae62011-01-15 06:19:44 +09002214 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2215 Resources res, List<InputMethodSubtype> subtypes) {
2216 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002217 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2218 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2219 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002220 final int N = subtypes.size();
2221 boolean containsKeyboardSubtype = false;
2222 for (int i = 0; i < N; ++i) {
2223 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002224 final String locale = subtype.getLocale();
2225 final String mode = subtype.getMode();
2226 // When system locale starts with subtype's locale, that subtype will be applicable
2227 // for system locale
2228 // For instance, it's clearly applicable for cases like system locale = en_US and
2229 // subtype = en, but it is not necessarily considered applicable for cases like system
2230 // locale = en and subtype = en_US.
2231 // We just call systemLocale.startsWith(locale) in this function because there is no
2232 // need to find applicable subtypes aggressively unlike
2233 // findLastResortApplicableSubtypeLocked.
2234 if (systemLocale.startsWith(locale)) {
2235 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2236 // If more applicable subtypes are contained, skip.
2237 if (applicableSubtype != null
2238 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2239 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002240 if (!containsKeyboardSubtype
2241 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2242 containsKeyboardSubtype = true;
2243 }
2244 }
2245 }
satok3da92232011-01-11 22:46:30 +09002246 ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2247 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002248 if (!containsKeyboardSubtype) {
2249 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002250 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002251 if (lastResortKeyboardSubtype != null) {
2252 applicableSubtypes.add(lastResortKeyboardSubtype);
2253 }
2254 }
2255 return applicableSubtypes;
2256 }
2257
satok4e4569d2010-11-19 18:45:53 +09002258 /**
2259 * If there are no selected subtypes, tries finding the most applicable one according to the
2260 * given locale.
2261 * @param subtypes this function will search the most applicable subtype in subtypes
2262 * @param mode subtypes will be filtered by mode
2263 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002264 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2265 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002266 * @return the most applicable subtypeId
2267 */
satokdf31ae62011-01-15 06:19:44 +09002268 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2269 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002270 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002271 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002272 return null;
satok8fbb1e82010-11-02 23:15:58 +09002273 }
satok4e4569d2010-11-19 18:45:53 +09002274 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002275 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002276 }
satok8fbb1e82010-11-02 23:15:58 +09002277 final String language = locale.substring(0, 2);
2278 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002279 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002280 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002281 final int N = subtypes.size();
2282 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002283 InputMethodSubtype subtype = subtypes.get(i);
2284 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002285 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2286 // and all subtypes with all modes can be candidates.
2287 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002288 if (firstMatchedModeSubtype == null) {
2289 firstMatchedModeSubtype = subtype;
2290 }
satok9ef02832010-11-04 21:17:48 +09002291 if (locale.equals(subtypeLocale)) {
2292 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002293 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002294 break;
2295 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2296 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002297 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002298 partialMatchFound = true;
2299 }
satok8fbb1e82010-11-02 23:15:58 +09002300 }
2301 }
2302
satok7599a7f2010-12-22 13:45:23 +09002303 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2304 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002305 }
2306
satok8fbb1e82010-11-02 23:15:58 +09002307 // The first subtype applicable to the system locale will be defined as the most applicable
2308 // subtype.
2309 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002310 if (applicableSubtype != null) {
2311 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2312 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2313 }
satok8fbb1e82010-11-02 23:15:58 +09002314 }
satokcd7cd292010-11-20 15:46:23 +09002315 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002316 }
2317
satok4e4569d2010-11-19 18:45:53 +09002318 // If there are no selected shortcuts, tries finding the most applicable ones.
2319 private Pair<InputMethodInfo, InputMethodSubtype>
2320 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2321 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2322 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002323 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002324 boolean foundInSystemIME = false;
2325
2326 // Search applicable subtype for each InputMethodInfo
2327 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002328 final String imiId = imi.getId();
2329 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2330 continue;
2331 }
satokcd7cd292010-11-20 15:46:23 +09002332 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002333 final List<InputMethodSubtype> enabledSubtypes =
2334 getEnabledInputMethodSubtypeList(imi, true);
2335 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002336 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002337 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002338 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002339 }
satokdf31ae62011-01-15 06:19:44 +09002340 // 2. Search by the system locale from enabledSubtypes.
2341 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002342 if (subtype == null) {
2343 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002344 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002345 }
satok7599a7f2010-12-22 13:45:23 +09002346 // 4. Search by the current subtype's locale from all subtypes.
2347 if (subtype == null && mCurrentSubtype != null) {
2348 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002349 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002350 }
2351 // 5. Search by the system locale from all subtypes.
2352 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002353 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002354 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002355 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002356 }
satokcd7cd292010-11-20 15:46:23 +09002357 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002358 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002359 // The current input method is the most applicable IME.
2360 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002361 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002362 break;
satok7599a7f2010-12-22 13:45:23 +09002363 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002364 // The system input method is 2nd applicable IME.
2365 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002366 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002367 if ((imi.getServiceInfo().applicationInfo.flags
2368 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2369 foundInSystemIME = true;
2370 }
satok4e4569d2010-11-19 18:45:53 +09002371 }
2372 }
2373 }
2374 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002375 if (mostApplicableIMI != null) {
2376 Slog.w(TAG, "Most applicable shortcut input method was:"
2377 + mostApplicableIMI.getId());
2378 if (mostApplicableSubtype != null) {
2379 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2380 + "," + mostApplicableSubtype.getMode() + ","
2381 + mostApplicableSubtype.getLocale());
2382 }
2383 }
satok4e4569d2010-11-19 18:45:53 +09002384 }
satokcd7cd292010-11-20 15:46:23 +09002385 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002386 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002387 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002388 } else {
2389 return null;
2390 }
2391 }
2392
satokab751aa2010-09-14 19:17:36 +09002393 /**
2394 * @return Return the current subtype of this input method.
2395 */
satok42c5a162011-05-26 16:46:14 +09002396 @Override
satokab751aa2010-09-14 19:17:36 +09002397 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002398 boolean subtypeIsSelected = false;
2399 try {
2400 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2401 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2402 } catch (SettingNotFoundException e) {
2403 }
satok3ef8b292010-11-23 06:06:29 +09002404 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002405 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002406 String lastInputMethodId = Settings.Secure.getString(
2407 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002408 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2409 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002410 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2411 if (imi != null) {
2412 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002413 // the most applicable subtype from explicitly or implicitly enabled
2414 // subtypes.
2415 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2416 getEnabledInputMethodSubtypeList(imi, true);
2417 // If there is only one explicitly or implicitly enabled subtype,
2418 // just returns it.
2419 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2420 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2421 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2422 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2423 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2424 SUBTYPE_MODE_KEYBOARD, null, true);
2425 if (mCurrentSubtype == null) {
2426 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2427 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2428 true);
2429 }
2430 }
satok4e4569d2010-11-19 18:45:53 +09002431 }
satokcd7cd292010-11-20 15:46:23 +09002432 } else {
satok3ef8b292010-11-23 06:06:29 +09002433 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002434 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002435 }
satok8fbb1e82010-11-02 23:15:58 +09002436 }
satok3ef8b292010-11-23 06:06:29 +09002437 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002438 }
satokab751aa2010-09-14 19:17:36 +09002439 }
2440
satokf3db1af2010-11-23 13:34:33 +09002441 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2442 InputMethodSubtype subtype) {
2443 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2444 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2445 } else {
2446 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2447 subtypes.add(subtype);
2448 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2449 }
2450 }
2451
satok4e4569d2010-11-19 18:45:53 +09002452 // TODO: We should change the return type from List to List<Parcelable>
satoke7c6998e2011-06-03 17:57:59 +09002453 @Override
satok4e4569d2010-11-19 18:45:53 +09002454 public List getShortcutInputMethodsAndSubtypes() {
2455 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002456 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002457 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002458 // If there are no selected shortcut subtypes, the framework will try to find
2459 // the most applicable subtype from all subtypes whose mode is
2460 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002461 Pair<InputMethodInfo, InputMethodSubtype> info =
2462 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2463 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002464 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002465 ret.add(info.first);
2466 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002467 }
satok3da92232011-01-11 22:46:30 +09002468 return ret;
satokf3db1af2010-11-23 13:34:33 +09002469 }
satokf3db1af2010-11-23 13:34:33 +09002470 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2471 ret.add(imi);
2472 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2473 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002474 }
2475 }
satokf3db1af2010-11-23 13:34:33 +09002476 return ret;
satok4e4569d2010-11-19 18:45:53 +09002477 }
2478 }
2479
satok42c5a162011-05-26 16:46:14 +09002480 @Override
satokb66d2872010-11-10 01:04:04 +09002481 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2482 synchronized (mMethodMap) {
2483 if (subtype != null && mCurMethodId != null) {
2484 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2485 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2486 if (subtypeId != NOT_A_SUBTYPE_ID) {
2487 setInputMethodLocked(mCurMethodId, subtypeId);
2488 return true;
2489 }
2490 }
2491 return false;
2492 }
2493 }
2494
satokd87c2592010-09-29 11:52:06 +09002495 /**
2496 * Utility class for putting and getting settings for InputMethod
2497 * TODO: Move all putters and getters of settings to this class.
2498 */
2499 private static class InputMethodSettings {
2500 // The string for enabled input method is saved as follows:
2501 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2502 private static final char INPUT_METHOD_SEPARATER = ':';
2503 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002504 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002505 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2506
satok723a27e2010-11-11 14:58:11 +09002507 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002508 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2509
satokdf31ae62011-01-15 06:19:44 +09002510 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002511 private final ContentResolver mResolver;
2512 private final HashMap<String, InputMethodInfo> mMethodMap;
2513 private final ArrayList<InputMethodInfo> mMethodList;
2514
2515 private String mEnabledInputMethodsStrCache;
2516
2517 private static void buildEnabledInputMethodsSettingString(
2518 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2519 String id = pair.first;
2520 ArrayList<String> subtypes = pair.second;
2521 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002522 // Inputmethod and subtypes are saved in the settings as follows:
2523 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2524 for (String subtypeId: subtypes) {
2525 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002526 }
2527 }
2528
2529 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002530 Resources res, ContentResolver resolver,
2531 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2532 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002533 mResolver = resolver;
2534 mMethodMap = methodMap;
2535 mMethodList = methodList;
2536 }
2537
2538 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2539 return createEnabledInputMethodListLocked(
2540 getEnabledInputMethodsAndSubtypeListLocked());
2541 }
2542
satok7f35c8c2010-10-07 21:13:11 +09002543 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002544 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2545 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002546 getEnabledInputMethodsAndSubtypeListLocked());
2547 }
2548
satok67ddf9c2010-11-17 09:45:54 +09002549 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2550 InputMethodInfo imi) {
2551 List<Pair<String, ArrayList<String>>> imsList =
2552 getEnabledInputMethodsAndSubtypeListLocked();
2553 ArrayList<InputMethodSubtype> enabledSubtypes =
2554 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002555 if (imi != null) {
2556 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2557 InputMethodInfo info = mMethodMap.get(imsPair.first);
2558 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002559 final int subtypeCount = info.getSubtypeCount();
2560 for (int i = 0; i < subtypeCount; ++i) {
2561 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002562 for (String s: imsPair.second) {
2563 if (String.valueOf(ims.hashCode()).equals(s)) {
2564 enabledSubtypes.add(ims);
2565 }
satok67ddf9c2010-11-17 09:45:54 +09002566 }
2567 }
satok884ef9a2010-11-18 10:39:46 +09002568 break;
satok67ddf9c2010-11-17 09:45:54 +09002569 }
satok67ddf9c2010-11-17 09:45:54 +09002570 }
2571 }
2572 return enabledSubtypes;
2573 }
2574
satokd87c2592010-09-29 11:52:06 +09002575 // At the initial boot, the settings for input methods are not set,
2576 // so we need to enable IME in that case.
2577 public void enableAllIMEsIfThereIsNoEnabledIME() {
2578 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2579 StringBuilder sb = new StringBuilder();
2580 final int N = mMethodList.size();
2581 for (int i = 0; i < N; i++) {
2582 InputMethodInfo imi = mMethodList.get(i);
2583 Slog.i(TAG, "Adding: " + imi.getId());
2584 if (i > 0) sb.append(':');
2585 sb.append(imi.getId());
2586 }
2587 putEnabledInputMethodsStr(sb.toString());
2588 }
2589 }
2590
satokbb4aa062011-01-19 21:40:27 +09002591 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002592 ArrayList<Pair<String, ArrayList<String>>> imsList
2593 = new ArrayList<Pair<String, ArrayList<String>>>();
2594 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2595 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2596 return imsList;
2597 }
satok723a27e2010-11-11 14:58:11 +09002598 mInputMethodSplitter.setString(enabledInputMethodsStr);
2599 while (mInputMethodSplitter.hasNext()) {
2600 String nextImsStr = mInputMethodSplitter.next();
2601 mSubtypeSplitter.setString(nextImsStr);
2602 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002603 ArrayList<String> subtypeHashes = new ArrayList<String>();
2604 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002605 String imeId = mSubtypeSplitter.next();
2606 while (mSubtypeSplitter.hasNext()) {
2607 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002608 }
2609 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2610 }
2611 }
2612 return imsList;
2613 }
2614
2615 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2616 if (reloadInputMethodStr) {
2617 getEnabledInputMethodsStr();
2618 }
2619 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2620 // Add in the newly enabled input method.
2621 putEnabledInputMethodsStr(id);
2622 } else {
2623 putEnabledInputMethodsStr(
2624 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2625 }
2626 }
2627
2628 /**
2629 * Build and put a string of EnabledInputMethods with removing specified Id.
2630 * @return the specified id was removed or not.
2631 */
2632 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2633 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2634 boolean isRemoved = false;
2635 boolean needsAppendSeparator = false;
2636 for (Pair<String, ArrayList<String>> ims: imsList) {
2637 String curId = ims.first;
2638 if (curId.equals(id)) {
2639 // We are disabling this input method, and it is
2640 // currently enabled. Skip it to remove from the
2641 // new list.
2642 isRemoved = true;
2643 } else {
2644 if (needsAppendSeparator) {
2645 builder.append(INPUT_METHOD_SEPARATER);
2646 } else {
2647 needsAppendSeparator = true;
2648 }
2649 buildEnabledInputMethodsSettingString(builder, ims);
2650 }
2651 }
2652 if (isRemoved) {
2653 // Update the setting with the new list of input methods.
2654 putEnabledInputMethodsStr(builder.toString());
2655 }
2656 return isRemoved;
2657 }
2658
2659 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2660 List<Pair<String, ArrayList<String>>> imsList) {
2661 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2662 for (Pair<String, ArrayList<String>> ims: imsList) {
2663 InputMethodInfo info = mMethodMap.get(ims.first);
2664 if (info != null) {
2665 res.add(info);
2666 }
2667 }
2668 return res;
2669 }
2670
satok7f35c8c2010-10-07 21:13:11 +09002671 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002672 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002673 List<Pair<String, ArrayList<String>>> imsList) {
2674 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2675 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2676 for (Pair<String, ArrayList<String>> ims : imsList) {
2677 InputMethodInfo info = mMethodMap.get(ims.first);
2678 if (info != null) {
2679 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2680 }
2681 }
2682 return res;
2683 }
2684
satokd87c2592010-09-29 11:52:06 +09002685 private void putEnabledInputMethodsStr(String str) {
2686 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2687 mEnabledInputMethodsStrCache = str;
2688 }
2689
2690 private String getEnabledInputMethodsStr() {
2691 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2692 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002693 if (DEBUG) {
2694 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2695 }
satokd87c2592010-09-29 11:52:06 +09002696 return mEnabledInputMethodsStrCache;
2697 }
satok723a27e2010-11-11 14:58:11 +09002698
2699 private void saveSubtypeHistory(
2700 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2701 StringBuilder builder = new StringBuilder();
2702 boolean isImeAdded = false;
2703 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2704 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2705 newSubtypeId);
2706 isImeAdded = true;
2707 }
2708 for (Pair<String, String> ime: savedImes) {
2709 String imeId = ime.first;
2710 String subtypeId = ime.second;
2711 if (TextUtils.isEmpty(subtypeId)) {
2712 subtypeId = NOT_A_SUBTYPE_ID_STR;
2713 }
2714 if (isImeAdded) {
2715 builder.append(INPUT_METHOD_SEPARATER);
2716 } else {
2717 isImeAdded = true;
2718 }
2719 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2720 subtypeId);
2721 }
2722 // Remove the last INPUT_METHOD_SEPARATER
2723 putSubtypeHistoryStr(builder.toString());
2724 }
2725
2726 public void addSubtypeToHistory(String imeId, String subtypeId) {
2727 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2728 for (Pair<String, String> ime: subtypeHistory) {
2729 if (ime.first.equals(imeId)) {
2730 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002731 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002732 + ime.second);
2733 }
2734 // We should break here
2735 subtypeHistory.remove(ime);
2736 break;
2737 }
2738 }
satokbb4aa062011-01-19 21:40:27 +09002739 if (DEBUG) {
2740 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2741 }
satok723a27e2010-11-11 14:58:11 +09002742 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2743 }
2744
2745 private void putSubtypeHistoryStr(String str) {
2746 if (DEBUG) {
2747 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2748 }
2749 Settings.Secure.putString(
2750 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2751 }
2752
2753 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2754 // Gets the first one from the history
2755 return getLastSubtypeForInputMethodLockedInternal(null);
2756 }
2757
2758 public String getLastSubtypeForInputMethodLocked(String imeId) {
2759 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2760 if (ime != null) {
2761 return ime.second;
2762 } else {
2763 return null;
2764 }
2765 }
2766
2767 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2768 List<Pair<String, ArrayList<String>>> enabledImes =
2769 getEnabledInputMethodsAndSubtypeListLocked();
2770 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09002771 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09002772 final String imeInTheHistory = imeAndSubtype.first;
2773 // If imeId is empty, returns the first IME and subtype in the history
2774 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2775 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002776 final String subtypeHashCode =
2777 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2778 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002779 if (!TextUtils.isEmpty(subtypeHashCode)) {
2780 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002781 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002782 }
2783 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2784 }
2785 }
2786 }
2787 if (DEBUG) {
2788 Slog.d(TAG, "No enabled IME found in the history");
2789 }
2790 return null;
2791 }
2792
satokdf31ae62011-01-15 06:19:44 +09002793 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002794 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2795 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2796 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002797 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2798 if (explicitlyEnabledSubtypes.size() == 0) {
2799 // If there are no explicitly enabled subtypes, applicable subtypes are
2800 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002801 InputMethodInfo ime = mMethodMap.get(imeId);
2802 // If IME is enabled and no subtypes are enabled, applicable subtypes
2803 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002804 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002805 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002806 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002807 if (implicitlySelectedSubtypes != null) {
2808 final int N = implicitlySelectedSubtypes.size();
2809 for (int i = 0; i < N; ++i) {
2810 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2811 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2812 return subtypeHashCode;
2813 }
2814 }
2815 }
2816 }
2817 } else {
satokf6cafb62011-01-17 16:29:02 +09002818 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002819 if (s.equals(subtypeHashCode)) {
2820 // If both imeId and subtypeId are enabled, return subtypeId.
2821 return s;
2822 }
satok723a27e2010-11-11 14:58:11 +09002823 }
2824 }
2825 // If imeId was enabled but subtypeId was disabled.
2826 return NOT_A_SUBTYPE_ID_STR;
2827 }
2828 }
2829 // If both imeId and subtypeId are disabled, return null
2830 return null;
2831 }
2832
2833 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2834 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2835 final String subtypeHistoryStr = getSubtypeHistoryStr();
2836 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2837 return imsList;
2838 }
2839 mInputMethodSplitter.setString(subtypeHistoryStr);
2840 while (mInputMethodSplitter.hasNext()) {
2841 String nextImsStr = mInputMethodSplitter.next();
2842 mSubtypeSplitter.setString(nextImsStr);
2843 if (mSubtypeSplitter.hasNext()) {
2844 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2845 // The first element is ime id.
2846 String imeId = mSubtypeSplitter.next();
2847 while (mSubtypeSplitter.hasNext()) {
2848 subtypeId = mSubtypeSplitter.next();
2849 break;
2850 }
2851 imsList.add(new Pair<String, String>(imeId, subtypeId));
2852 }
2853 }
2854 return imsList;
2855 }
2856
2857 private String getSubtypeHistoryStr() {
2858 if (DEBUG) {
2859 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2860 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2861 }
2862 return Settings.Secure.getString(
2863 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2864 }
2865
2866 public void putSelectedInputMethod(String imeId) {
2867 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2868 }
2869
2870 public void putSelectedSubtype(int subtypeId) {
2871 Settings.Secure.putInt(
2872 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2873 }
satokd87c2592010-09-29 11:52:06 +09002874 }
2875
satoke7c6998e2011-06-03 17:57:59 +09002876 private static class InputMethodFileManager {
2877 private static final String SYSTEM_PATH = "system";
2878 private static final String INPUT_METHOD_PATH = "inputmethod";
2879 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
2880 private static final String NODE_SUBTYPES = "subtypes";
2881 private static final String NODE_SUBTYPE = "subtype";
2882 private static final String NODE_IMI = "imi";
2883 private static final String ATTR_ID = "id";
2884 private static final String ATTR_LABEL = "label";
2885 private static final String ATTR_ICON = "icon";
2886 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
2887 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
2888 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
2889 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
2890 private final AtomicFile mAdditionalInputMethodSubtypeFile;
2891 private final HashMap<String, InputMethodInfo> mMethodMap;
2892 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
2893 new HashMap<String, List<InputMethodSubtype>>();
2894 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
2895 if (methodMap == null) {
2896 throw new NullPointerException("methodMap is null");
2897 }
2898 mMethodMap = methodMap;
2899 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
2900 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
2901 if (!inputMethodDir.mkdirs()) {
2902 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
2903 }
2904 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
2905 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
2906 if (!subtypeFile.exists()) {
2907 // If "subtypes.xml" doesn't exist, create a blank file.
2908 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
2909 methodMap);
2910 } else {
2911 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
2912 }
2913 }
2914
2915 private void deleteAllInputMethodSubtypes(String imiId) {
2916 synchronized (mMethodMap) {
2917 mSubtypesMap.remove(imiId);
2918 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
2919 mMethodMap);
2920 }
2921 }
2922
2923 public void addInputMethodSubtypes(
2924 String imiId, InputMethodSubtype[] additionalSubtypes) {
2925 synchronized (mMethodMap) {
2926 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2927 final int N = additionalSubtypes.length;
2928 for (int i = 0; i < N; ++i) {
2929 final InputMethodSubtype subtype = additionalSubtypes[i];
2930 if (!subtypes.contains(subtype)) {
2931 subtypes.add(subtype);
2932 }
2933 }
2934 mSubtypesMap.put(imiId, subtypes);
2935 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
2936 mMethodMap);
2937 }
2938 }
2939
2940 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
2941 synchronized (mMethodMap) {
2942 return mSubtypesMap;
2943 }
2944 }
2945
2946 private static void writeAdditionalInputMethodSubtypes(
2947 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
2948 HashMap<String, InputMethodInfo> methodMap) {
2949 // Safety net for the case that this function is called before methodMap is set.
2950 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
2951 FileOutputStream fos = null;
2952 try {
2953 fos = subtypesFile.startWrite();
2954 final XmlSerializer out = new FastXmlSerializer();
2955 out.setOutput(fos, "utf-8");
2956 out.startDocument(null, true);
2957 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
2958 out.startTag(null, NODE_SUBTYPES);
2959 for (String imiId : allSubtypes.keySet()) {
2960 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
2961 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
2962 continue;
2963 }
2964 out.startTag(null, NODE_IMI);
2965 out.attribute(null, ATTR_ID, imiId);
2966 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
2967 final int N = subtypesList.size();
2968 for (int i = 0; i < N; ++i) {
2969 final InputMethodSubtype subtype = subtypesList.get(i);
2970 out.startTag(null, NODE_SUBTYPE);
2971 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
2972 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
2973 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
2974 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
2975 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
2976 out.attribute(null, ATTR_IS_AUXILIARY,
2977 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
2978 out.endTag(null, NODE_SUBTYPE);
2979 }
2980 out.endTag(null, NODE_IMI);
2981 }
2982 out.endTag(null, NODE_SUBTYPES);
2983 out.endDocument();
2984 subtypesFile.finishWrite(fos);
2985 } catch (java.io.IOException e) {
2986 Slog.w(TAG, "Error writing subtypes", e);
2987 if (fos != null) {
2988 subtypesFile.failWrite(fos);
2989 }
2990 }
2991 }
2992
2993 private static void readAdditionalInputMethodSubtypes(
2994 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
2995 if (allSubtypes == null || subtypesFile == null) return;
2996 allSubtypes.clear();
2997 FileInputStream fis = null;
2998 try {
2999 fis = subtypesFile.openRead();
3000 final XmlPullParser parser = Xml.newPullParser();
3001 parser.setInput(fis, null);
3002 int type = parser.getEventType();
3003 // Skip parsing until START_TAG
3004 while ((type = parser.next()) != XmlPullParser.START_TAG
3005 && type != XmlPullParser.END_DOCUMENT) {}
3006 String firstNodeName = parser.getName();
3007 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3008 throw new XmlPullParserException("Xml doesn't start with subtypes");
3009 }
3010 final int depth =parser.getDepth();
3011 String currentImiId = null;
3012 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3013 while (((type = parser.next()) != XmlPullParser.END_TAG
3014 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3015 if (type != XmlPullParser.START_TAG)
3016 continue;
3017 final String nodeName = parser.getName();
3018 if (NODE_IMI.equals(nodeName)) {
3019 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3020 if (TextUtils.isEmpty(currentImiId)) {
3021 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3022 continue;
3023 }
3024 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3025 allSubtypes.put(currentImiId, tempSubtypesArray);
3026 } else if (NODE_SUBTYPE.equals(nodeName)) {
3027 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3028 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3029 continue;
3030 }
3031 final int icon = Integer.valueOf(
3032 parser.getAttributeValue(null, ATTR_ICON));
3033 final int label = Integer.valueOf(
3034 parser.getAttributeValue(null, ATTR_LABEL));
3035 final String imeSubtypeLocale =
3036 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3037 final String imeSubtypeMode =
3038 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3039 final String imeSubtypeExtraValue =
3040 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
3041 final boolean isAuxiliary =
3042 Boolean.valueOf(parser.getAttributeValue(null, ATTR_IS_AUXILIARY));
3043 final InputMethodSubtype subtype =
3044 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3045 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3046 tempSubtypesArray.add(subtype);
3047 }
3048 }
3049 } catch (XmlPullParserException e) {
3050 Slog.w(TAG, "Error reading subtypes: " + e);
3051 return;
3052 } catch (java.io.IOException e) {
3053 Slog.w(TAG, "Error reading subtypes: " + e);
3054 return;
3055 } catch (NumberFormatException e) {
3056 Slog.w(TAG, "Error reading subtypes: " + e);
3057 return;
3058 } finally {
3059 if (fis != null) {
3060 try {
3061 fis.close();
3062 } catch (java.io.IOException e1) {
3063 Slog.w(TAG, "Failed to close.");
3064 }
3065 }
3066 }
3067 }
3068 }
3069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 @Override
3073 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3074 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3075 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3078 + Binder.getCallingPid()
3079 + ", uid=" + Binder.getCallingUid());
3080 return;
3081 }
3082
3083 IInputMethod method;
3084 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 synchronized (mMethodMap) {
3089 p.println("Current Input Method Manager state:");
3090 int N = mMethodList.size();
3091 p.println(" Input Methods:");
3092 for (int i=0; i<N; i++) {
3093 InputMethodInfo info = mMethodList.get(i);
3094 p.println(" InputMethod #" + i + ":");
3095 info.dump(p, " ");
3096 }
3097 p.println(" Clients:");
3098 for (ClientState ci : mClients.values()) {
3099 p.println(" Client " + ci + ":");
3100 p.println(" client=" + ci.client);
3101 p.println(" inputContext=" + ci.inputContext);
3102 p.println(" sessionRequested=" + ci.sessionRequested);
3103 p.println(" curSession=" + ci.curSession);
3104 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003105 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003107 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3108 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3110 + " mBoundToMethod=" + mBoundToMethod);
3111 p.println(" mCurToken=" + mCurToken);
3112 p.println(" mCurIntent=" + mCurIntent);
3113 method = mCurMethod;
3114 p.println(" mCurMethod=" + mCurMethod);
3115 p.println(" mEnabledSession=" + mEnabledSession);
3116 p.println(" mShowRequested=" + mShowRequested
3117 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3118 + " mShowForced=" + mShowForced
3119 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003120 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003122
Jeff Brownb88102f2010-09-08 11:49:43 -07003123 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003124 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 pw.flush();
3126 try {
3127 client.client.asBinder().dump(fd, args);
3128 } catch (RemoteException e) {
3129 p.println("Input method client dead: " + e);
3130 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003131 } else {
3132 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003134
Jeff Brownb88102f2010-09-08 11:49:43 -07003135 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 pw.flush();
3138 try {
3139 method.asBinder().dump(fd, args);
3140 } catch (RemoteException e) {
3141 p.println("Input method service dead: " + e);
3142 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003143 } else {
3144 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 }
3146 }
3147}